Skip to content

Commit afceb48

Browse files
committed
updating p5.sound
1 parent 263772a commit afceb48

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

lib/addons/p5.sound.js

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.sound.js v0.14 2014-08-20 */
1+
/*! p5.sound.js v0.14 2014-08-22 */
22
/**
33
* p5.sound extends p5 with <a href="http://caniuse.com/audio-api"
44
* target="_blank">Web Audio</a> functionality including audio input,
@@ -28,7 +28,7 @@
2828
* <b><a href="#/p5.Convolver">p5.Convolver:</a></b> Extends
2929
* <a href="#/p5.Reverb">p5.Reverb</a> to simulate the sound of real
3030
* physical spaces through convolution.<br/>
31-
* <a href="#/p5.SoundRecorder">p5.SoundRecorder</a>: Record sound for playback
31+
* <b><a href="#/p5.SoundRecorder">p5.SoundRecorder</a></b>: Record sound for playback
3232
* / save the .wav file.
3333
* <br/><br/>
3434
* p5.sound is on <a href="https://github.com/therewasaguy/p5.sound/">GitHub</a>.
@@ -1892,14 +1892,8 @@ signal = function () {
18921892
p5.Signal = function (value) {
18931893
// scales the constant output to desired output
18941894
this.scalar = ac.createGain();
1895-
this.scalar.gain.maxValue = 10000;
1896-
this.scalar.gain.minValue = -10000;
18971895
this.input = ac.createGain();
1898-
this.input.gain.maxValue = 10000;
1899-
this.input.gain.minValue = -10000;
19001896
this.output = ac.createGain();
1901-
this.output.gain.maxValue = 10000;
1902-
this.output.gain.minValue = -10000;
19031897
// the ratio of this value to the control signal
19041898
this._syncRatio = 1;
19051899
// connect the constant output to the scalar
@@ -2103,8 +2097,6 @@ signal = function () {
21032097
p5.SignalMult = function (num, _input) {
21042098
var mult = new p5.Signal();
21052099
mult.output = mult.input;
2106-
mult.input.gain.maxValue = 10000;
2107-
mult.input.gain.minValue = -10000;
21082100
mult.setValue = function (value) {
21092101
if (typeof value === 'number') {
21102102
this.input.gain.value = value;
@@ -4755,7 +4747,7 @@ soundRecorder = function () {
47554747
*
47564748
* else if (state === 2) {
47574749
* soundFile.play(); // play the result!
4758-
* saveSound(soundFile, 'mySound.wav');
4750+
* save(soundFile, 'mySound.wav');
47594751
* state++;
47604752
* }
47614753
* }
@@ -4971,9 +4963,64 @@ soundRecorder = function () {
49714963
}
49724964
}
49734965
}(sndcore, master);
4966+
var metro;
4967+
metro = function () {
4968+
'use strict';
4969+
var p5sound = master;
4970+
var ac = p5sound.audiocontext;
4971+
var upTick = false;
4972+
var tatums = 4;
4973+
// lowest possible division of a beat
4974+
// Oscillator + Script Processor to keep time.
4975+
// inspired by Tone.js library's Transport (MIT license, Yotam Mann)
4976+
// https://github.com/TONEnoTONE/Tone.js/blob/master/Tone/core/Transport.js
4977+
p5.Metro = function () {
4978+
this.metroTicks = 0;
4979+
/**
4980+
* watch this.oscillator for timing ticks
4981+
*/
4982+
this._jsNode = p5sound.audiocontext.createScriptProcessor(this.bufferSize, 1, 1);
4983+
this._jsNode.onaudioprocess = this._processBuffer.bind(this);
4984+
this._jsNode.connect(p5.soundOut._silentNode);
4985+
this.oscillator = ac.createOscillator();
4986+
this.oscillator.type = 'square';
4987+
this.oscillator.connect(this._jsNode);
4988+
this.oscillator.start();
4989+
this.oscillator.frequency.value = 1;
4990+
this.lastTick = 0;
4991+
};
4992+
p5.Metro.prototype._processBuffer = function (event) {
4993+
var now = ac.currentTime;
4994+
var incomingBuffer = event.inputBuffer.getChannelData(0);
4995+
var bufferSize = this._jsNode.bufferSize;
4996+
for (var i = 0; i < bufferSize; i++) {
4997+
var sample = incomingBuffer[i];
4998+
if (sample > 0 && !upTick) {
4999+
upTick = true;
5000+
this._processTick(now + i / ac.sampleRate);
5001+
} else if (sample < 0 && upTick) {
5002+
upTick = false;
5003+
}
5004+
}
5005+
};
5006+
p5.Metro.prototype._processTick = function (tickTime) {
5007+
this.metroTicks += 1;
5008+
console.log('interval: ' + (tickTime - this.lastTick));
5009+
this.lastTick = tickTime;
5010+
};
5011+
p5.Metro.prototype.setBPM = function (bpm, rampTime) {
5012+
// var tatumFreq = this.secondsToFrequency(this.notationToSeconds(tatum.toString() + "n", bpm, transportTimeSignature));
5013+
var freq = bpm / 60 / 2 * tatums;
5014+
var ramp = rampTime || 0;
5015+
this.oscillator.frequency.linearRampToValueAtTime(freq, ramp);
5016+
};
5017+
p5.Metro.prototype.getBPM = function (tempo) {
5018+
return this.oscillator.frequency.value * 60 * 2;
5019+
};
5020+
}(master);
49745021
var src_app;
49755022
src_app = function () {
49765023
'use strict';
49775024
var p5SOUND = sndcore;
49785025
return p5SOUND;
4979-
}(sndcore, master, helpers, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, looper, soundRecorder);
5026+
}(sndcore, master, helpers, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, looper, soundRecorder, metro);

0 commit comments

Comments
 (0)