Skip to content

Commit

Permalink
Merge pull request #380 from oshoham/amplitude-getlevel-bugfix
Browse files Browse the repository at this point in the history
Bugfixes for p5.Amplitude and p5.Soundfile for browsers without AudioWorklet support
  • Loading branch information
oshoham authored Aug 21, 2019
2 parents 0dacdc7 + b32e460 commit 4d3a383
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 152 deletions.
22 changes: 17 additions & 5 deletions examples/pause_soundfile/sketch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// ====================
// DEMO: pause sound when the user presses a key, resume on release
// ====================
'use strict';

var soundFile;
var audioContextStarted = false;

function preload() {
// create a SoundFile
Expand All @@ -14,16 +16,26 @@ function setup() {
createCanvas(400, 400);
background(0, 255, 0);

soundFile.loop();
userStartAudio().then(function() {
soundFile.loop();
audioContextStarted = true;
});

createP('Press any key to pause. Resume when the key is released')
}

function keyTyped() {
soundFile.pause();
background(255, 0, 0);
if (!audioContextStarted) {
return;
}
soundFile.pause();
background(255, 0, 0);
}

function keyReleased() {
soundFile.play();
background(0, 255, 0);
if (!audioContextStarted) {
return;
}
soundFile.play();
background(0, 255, 0);
}
218 changes: 108 additions & 110 deletions lib/p5.sound.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 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.

8 changes: 5 additions & 3 deletions src/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ define(function (require) {
this.audiocontext = p5sound.audiocontext;
this._workletNode = new AudioWorkletNode(this.audiocontext, processorNames.amplitudeProcessor, {
outputChannelCount: [1],
parameterData: { smoothing: smoothing || 0 },
processorOptions: { normalize: false }
processorOptions: {
normalize: false,
smoothing: smoothing || 0
}
});

this._workletNode.port.onmessage = function(event) {
Expand Down Expand Up @@ -256,7 +258,7 @@ define(function (require) {
*/
p5.Amplitude.prototype.smooth = function(s) {
if (s >= 0 && s < 1) {
this._workletNode.parameters.get('smoothing').value = s;
this._workletNode.port.postMessage({ name: 'smoothing', smoothing: s });
} else {
console.log('Error: smoothing must be between 0 and 1');
}
Expand Down
19 changes: 5 additions & 14 deletions src/audioWorklet/amplitudeProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@
const processorNames = preval.require('./processorNames');

class AmplitudeProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: 'smoothing',
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: 'k-rate'
}
];
}

constructor(options) {
super();

const processorOptions = options.processorOptions || {};
this.smoothing = processorOptions.smoothing || 0;
this.normalize = processorOptions.normalize || false;

this.stereoVol = [0, 0];
Expand All @@ -29,15 +18,17 @@ class AmplitudeProcessor extends AudioWorkletProcessor {
const data = event.data;
if (data.name === 'toggleNormalize') {
this.normalize = data.normalize;
} else if (data.name === 'smoothing') {
this.smoothing = Math.max(0, Math.min(1, data.smoothing));
}
};
}

// TO DO make this stereo / dependent on # of audio channels
process(inputs, outputs, parameters) {
process(inputs, outputs) {
const input = inputs[0];
const output = outputs[0];
const smoothing = parameters.smoothing;
const smoothing = this.smoothing;

for (let channel = 0; channel < input.length; ++channel) {
const inputBuffer = input[channel];
Expand Down
27 changes: 10 additions & 17 deletions src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ define(function (require) {
this._whileLoading = function() {};
}

this._onAudioProcess = _onAudioProcess.bind(this);
this._clearOnEnd = _clearOnEnd.bind(this);
};

Expand Down Expand Up @@ -351,7 +350,6 @@ define(function (require) {
return;
}

var self = this;
var now = p5sound.audiocontext.currentTime;
var cueStart, cueEnd;
var time = startTime || 0;
Expand All @@ -371,7 +369,6 @@ define(function (require) {

// TO DO: if already playing, create array of buffers for easy stop()
if (this.buffer) {

// reset the pause time (if it was paused)
this._pauseTime = 0;

Expand Down Expand Up @@ -440,6 +437,7 @@ define(function (require) {
this._counterNode.loopStart = cueStart;
this._counterNode.loopEnd = cueEnd;
}

};


Expand Down Expand Up @@ -539,11 +537,12 @@ define(function (require) {
var pTime = time + now;

if (this.isPlaying() && this.buffer && this.bufferSourceNode) {
this._paused = true;
this._playing = false;

this.pauseTime = this.currentTime();
this.bufferSourceNode.stop(pTime);
this._counterNode.stop(pTime);
this._paused = true;
this._playing = false;

this._pauseTime = this.currentTime();
// TO DO: make sure play() still starts from orig start position
Expand Down Expand Up @@ -1245,14 +1244,18 @@ define(function (require) {
delete self._workletNode;
}
self._workletNode = new AudioWorkletNode(ac, processorNames.soundFileProcessor);
self._workletNode.port.onmessage = function(event) {
self._workletNode.port.onmessage = event => {
if (event.data.name === 'position') {
// event.data.position should only be 0 when paused
if (event.data.position === 0) {
return;
}
this._lastPos = event.data.position;

// do any callbacks that have been scheduled
this._onTimeUpdate(self._lastPos);
}
}.bind(self);
};

// create counter buffer of the same length as self.buffer
cNode.buffer = _createCounterBuffer( self.buffer );
Expand Down Expand Up @@ -1753,16 +1756,6 @@ define(function (require) {
return new Blob([dataView], { type: 'audio/wav' });
};

// event handler to keep track of current position
function _onAudioProcess(processEvent) {
var inputBuffer = processEvent.inputBuffer.getChannelData(0);

this._lastPos = inputBuffer[inputBuffer.length - 1] || 0;

// do any callbacks that have been scheduled
this._onTimeUpdate(self._lastPos);
}

// event handler to remove references to the bufferSourceNode when it is done playing
function _clearOnEnd(e) {
const thisBufferSourceNode = e.target;
Expand Down

0 comments on commit 4d3a383

Please sign in to comment.