Skip to content

Commit

Permalink
v0.26
Browse files Browse the repository at this point in the history
Gesture audio player
  • Loading branch information
HongshuoFan committed Nov 6, 2023
1 parent f7115c1 commit 5a29b7e
Show file tree
Hide file tree
Showing 4 changed files with 3,282 additions and 0 deletions.
155 changes: 155 additions & 0 deletions source/code/MG_AudioPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
inlets = 2;
outlets = 1;

var players = new Array();
var num_instance = 0;
var gesture_labels = [];
var result_obj,labels,umenu_info;

var mc_output,mapping_dict;

var box_w = 300;
var box_h = 50;
var num_col = 3;
var shift_x = 10;
var shift_y = 50;

function bang()
{
result_obj = this.patcher.getnamed("result");
labels = this.patcher.getnamed("labels");
umenu_info = this.patcher.getnamed("umenu_info");
fileLoader = this.patcher.getnamed("fileLoader");

mc_output = this.patcher.getnamed("mc_output");
mapping_dict = this.patcher.getnamed("mapping_dict");
// Run garbage collection to clear out any old object listeners
// before registering new ones.
// gc();
// var obj;

// // Assign listeners to all of the numboxes

// obj = this.patcher.getnamed("i_numBox");
// new MaxobjListener(obj, instance_numBox_Callback);
// outlet(0, bang);

}

function anything() {

var a = arrayfromargs(messagename, arguments);
gesture_labels = a;

}

function msg_int(value) {
remove_all();

num_instance = value;

for (var i = 0; i < num_instance; i++) {
add_Splyer(i);


}


}

// function instance_numBox_Callback(data){
// num_instance = data.value;
// msg_int(num_instance);
// }
// instance_numBox_Callback.local = 1;

function add_Splyer(script_index){
var a = new Array();
var script_name = "splayer" + script_index;
a[0] = 0;
a[1] = 0;
a[2] = "bpatcher";
a[3] = "bp_sPlayer.maxpat"
a[4] = gesture_labels[script_index]
a[5] = gesture_labels[script_index]
var splayer = this.patcher.newdefault(a);
//add splayer to array
players.push(splayer);

//size and location
allocate_box(splayer, script_index);
// location(splayer, 100, 100);
// sizebox(splayer, box_w, box_h);
//set bp_sPlayer attributes
splayer.varname = script_name;
splayer.setboxattr("presentation", "1");

//splayer.setboxattr("args", gesture_labels[script_index]);

//connect input to splayer
this.patcher.connect(result_obj,0,splayer,0);
this.patcher.connect(labels,0,splayer,1);
this.patcher.connect(umenu_info,0,splayer,2);
this.patcher.connect(fileLoader,0,splayer,3);

//connect splayer to mc_output and mapping_dict
this.patcher.connect(splayer,0,mc_output,0);
this.patcher.connect(splayer,1,mapping_dict,0);

outlet(0, gesture_labels[script_index]);
this.patcher.disconnect(labels,0,splayer,1);
}

function location(splayer,x,y)
{
vx = x;
vy = y;
if (splayer) {
var width,height;
var r = new Array();

width = splayer.rect[2] - splayer.rect[0];
height = splayer.rect[3] - splayer.rect[1];
r[0] = x;
r[1] = y;
r[2] = x+width;
r[3] = y+height;

splayer.rect = r;
}
}



function sizebox(splayer, width,height)
{
if (splayer) {
var r = new Array();

r[0] = splayer.rect[0];
r[1] = splayer.rect[1];
r[2] = splayer.rect[0]+width;
r[3] = splayer.rect[1]+height;

splayer.rect = r;
}
}

function remove_all() {

if(players.length > 0){
for (var i = 0; i < players.length; i++) {
this.patcher.remove(players[i]);
}
}

}

function allocate_box(box, index){
var x,y;
x = shift_x + (index % num_col) * (box_w );
y = shift_y + Math.floor(index / num_col) * (box_h );
location(box, x, y);
sizebox(box, box_w, box_h);

}
106 changes: 106 additions & 0 deletions source/code/stft_featureExatraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

var refbuf, result_buf;
var sample_rate = 44100;
var number_of_bins = 3;

if (jsarguments.length>1)
number_of_bins = jsarguments[1];
sample_rate = jsarguments[2];

function buffer(source)
{
refbuf = Buffer(source);
var number_of_frames = refbuf.framecount();
var fftSize = refbuf.channelcount();
// post("number of frames: " + number_of_frames);
// post("fftSize: " + fftSize);
// post('\n');
var result_size = number_of_frames*number_of_bins;
result_buf.send("sizeinsamps", result_size);

for (var i = 0; i < number_of_frames; i++) {
var c_FrameSTFT = getFrameSTFT(i, fftSize);
var selectedBinsIndexs = getTopIndices(c_FrameSTFT, number_of_bins);
var selectedBinsFreq = convertBinIndexs2Frequency(selectedBinsIndexs, fftSize, sample_rate);
writeResult(i*number_of_bins, selectedBinsFreq);
}
// post("result size: " + result_size);
// var i = 1;
// var c_FrameSTFT = getFrameSTFT(i, fftSize);

// var selectedBinsIndexs = getTopIndices(c_FrameSTFT, number_of_bins);

// post("selectedBinsIndexs: " + selectedBinsIndexs);
// post('\n');

// var selectedBinsFreq = convertBinIndexs2Frequency(selectedBinsIndexs, fftSize, sample_rate);
// post("selectedBinsFreq: " + selectedBinsFreq);
// post('\n');
// writeResult(i*number_of_bins, selectedBinsFreq);

outlet(0, "bang");
}

function getFrameSTFT(frame, fftSize) {
// return a list containing the STFT of the frame
//create a list to store the STFT
if (refbuf!=null){
var stft = [];
for (var i = 0; i < fftSize; i++) {
magnitudes = refbuf.peek(i,frame,1)
stft.push(magnitudes);

}
return stft;
}

}

function convertBinIndexs2Frequency(BinIndexs, fftSize, SR) {
// convert the bin index to the frequency
var frequency_rasolution = SR / fftSize;
var freqs = [];
for(var i = 0; i < BinIndexs.length; i++){
var freq = frequency_rasolution * BinIndexs[i];
// freq /= sample_rate/10;
freqs.push(freq);
}

return freqs;
}

function getTopIndices(arr, N) {
var indices = [];
var indexList = [];

for (var i = 0; i < arr.length; i++) {
indexList.push(i);
}

for (var i = 0; i < N; i++) {
//find the max and its index
var max = Math.max.apply(null, arr);
var index = arr.indexOf(max);

//get the index and store it
var out_index = indexList[index];
indices.push(out_index);
//remove the current max and its index
arr.splice(index, 1);
indexList.splice(index, 1);
}


return indices;
}

function result_buffer(source){
result_buf = Buffer(source);
result_buf.send("clear");
}

function writeResult(Startframe, arr){
for (var i = 0; i < arr.length; i++) {
result_buf.poke(1, Startframe+i, arr[i]);
}
}
Loading

0 comments on commit 5a29b7e

Please sign in to comment.