Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added feature of manually setting "BUFFERSIZE" in amplitude analysis #419

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion examples/amplitude_analysis/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

var soundFile;
var amplitude;
var bufferSize = 2048; // a value which can be multiple of 2 , eg. 256 , 512 , 1024 , 2048 ..etc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this comment should be updated to read "power of 2"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya ofcourse !!


// description text
var description;
var p1;

var smoothing = .01;
var smoothSlider, smoothLabel;
var bufferDropDown, bufferLabel;

function preload() {
soundFile = loadSound(['../files/beat.mp3', '../files/beat.ogg']);
Expand All @@ -25,14 +27,24 @@ function setup() {
soundFile.loop();

// create a new p5.Amplitude. Optionally, give it a 'smoothing' value betw 0.0 and .999
amplitude = new p5.Amplitude(smoothing);
amplitude = new p5.Amplitude(smoothing, bufferSize);

// instruction text
description = 'Spacebar: pause/unpause the loop. <br>Press "N" to toggle Normalize';
p1 = createP(description);

smoothSlider = createSlider(0.0, 99.9, smoothing*100);
smoothLabel = createP('Smoothing: ' + smoothing);

bufferDropDown = createSelect();
bufferLabel = createP('BufferSize: ' + bufferSize);

bufferDropDown.option('256');
bufferDropDown.option('512');
bufferDropDown.option('1024');
bufferDropDown.option('2048');


}

function draw() {
Expand All @@ -56,6 +68,12 @@ function draw() {
smoothing = smoothSlider.value()/100;
smoothLabel.html('Smoothing: ' + smoothing);
amplitude.smooth(smoothing);

//change bufferSize
bufferSize = bufferDropDown.value();
bufferLabel.html('BufferSize :' + bufferSize);
amplitude.setBufferSize(parseInt(bufferSize));

}

// on key pressed...
Expand Down
25 changes: 23 additions & 2 deletions lib/p5.sound.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** [p5.sound] Version: 0.3.12 - 2020-01-06 */
/** [p5.sound] Version: 0.3.12 - 2020-03-09 */
/**
* <p>p5.sound extends p5 with <a href="http://caniuse.com/audio-api"
* target="_blank">Web Audio</a> functionality including audio input,
Expand Down Expand Up @@ -5228,7 +5228,8 @@ var __WEBPACK_AMD_DEFINE_RESULT__;


p5.Amplitude = function (smoothing) {
this.bufferSize = safeBufferSize(2048);
var bufferSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2048;
this.bufferSize = safeBufferSize(bufferSize);

this.audiocontext = p5sound.audiocontext;
this._workletNode = new AudioWorkletNode(this.audiocontext, processorNames.amplitudeProcessor, {
Expand Down Expand Up @@ -5466,6 +5467,26 @@ var __WEBPACK_AMD_DEFINE_RESULT__;
console.log('Error: smoothing must be between 0 and 1');
}
};
/**
* Smooth Amplitude analysis by averaging with the last analysis
* frame. Off by default.
*
* @method bufferSize
* @for p5.Amplitude
* @param {Number} set bufferSize to any value multiple of 2
*/


p5.Amplitude.prototype.setBufferSize = function (s) {
if (s % 2 == 0) {
this._workletNode.port.postMessage({
name: 'bufferSize',
bufferSize: s
});
} else {
console.log('Error: bufferSize must be multiple of 2');
}
};

p5.Amplitude.prototype.dispose = function () {
var index = p5sound.soundArray.indexOf(this);
Expand Down
2 changes: 1 addition & 1 deletion lib/p5.sound.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/p5.sound.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/p5.sound.min.js.map

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions src/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ define(function (require) {
*
* </code></div>
*/
p5.Amplitude = function(smoothing) {
p5.Amplitude = function(smoothing, bufferSize = 2048) {

// Set to 2048 for now. In future iterations, this should be inherited or parsed from p5sound's default
this.bufferSize = safeBufferSize(2048);
this.bufferSize = safeBufferSize(bufferSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment above this line should be deleted or updated. I'm curious if you considered the other options mentioned in the comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@therewasaguy
actually comment must be modified 😄
and
as far as inheritance part is concerned i think , i need to modify p5.sound pass a optional argument "bufferSize " like i did in here , but right now let's leave it and i will make it another pr !
what do you say ?


// set audio context
this.audiocontext = p5sound.audiocontext;
Expand Down Expand Up @@ -290,6 +290,21 @@ define(function (require) {
}
};

/**
* bufferSize of Amplitude
* @method setBufferSize
* @for p5.Amplitude
* @param {Number} set bufferSize to any value multiple of 2
*/

p5.Amplitude.prototype.setBufferSize = function(s) {
if (s%2==0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a good practice to use triple equals so that we check for type as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i have modified it in my local repo , i will be pushing it with your recommendations keeping in mind 😃

this._workletNode.port.postMessage({ name: 'bufferSize', bufferSize: s });
} else {
console.log('Error: bufferSize must be multiple of 2');
endurance21 marked this conversation as resolved.
Show resolved Hide resolved
}
};

p5.Amplitude.prototype.dispose = function() {
// remove reference from soundArray
var index = p5sound.soundArray.indexOf(this);
Expand All @@ -309,3 +324,5 @@ define(function (require) {
};

});