|
1 | | -/*! p5.sound.js v0.14 2014-08-20 */ |
| 1 | +/*! p5.sound.js v0.14 2014-08-22 */ |
2 | 2 | /** |
3 | 3 | * p5.sound extends p5 with <a href="http://caniuse.com/audio-api" |
4 | 4 | * target="_blank">Web Audio</a> functionality including audio input, |
|
28 | 28 | * <b><a href="#/p5.Convolver">p5.Convolver:</a></b> Extends |
29 | 29 | * <a href="#/p5.Reverb">p5.Reverb</a> to simulate the sound of real |
30 | 30 | * 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 |
32 | 32 | * / save the .wav file. |
33 | 33 | * <br/><br/> |
34 | 34 | * p5.sound is on <a href="https://github.com/therewasaguy/p5.sound/">GitHub</a>. |
@@ -1892,14 +1892,8 @@ signal = function () { |
1892 | 1892 | p5.Signal = function (value) { |
1893 | 1893 | // scales the constant output to desired output |
1894 | 1894 | this.scalar = ac.createGain(); |
1895 | | - this.scalar.gain.maxValue = 10000; |
1896 | | - this.scalar.gain.minValue = -10000; |
1897 | 1895 | this.input = ac.createGain(); |
1898 | | - this.input.gain.maxValue = 10000; |
1899 | | - this.input.gain.minValue = -10000; |
1900 | 1896 | this.output = ac.createGain(); |
1901 | | - this.output.gain.maxValue = 10000; |
1902 | | - this.output.gain.minValue = -10000; |
1903 | 1897 | // the ratio of this value to the control signal |
1904 | 1898 | this._syncRatio = 1; |
1905 | 1899 | // connect the constant output to the scalar |
@@ -2103,8 +2097,6 @@ signal = function () { |
2103 | 2097 | p5.SignalMult = function (num, _input) { |
2104 | 2098 | var mult = new p5.Signal(); |
2105 | 2099 | mult.output = mult.input; |
2106 | | - mult.input.gain.maxValue = 10000; |
2107 | | - mult.input.gain.minValue = -10000; |
2108 | 2100 | mult.setValue = function (value) { |
2109 | 2101 | if (typeof value === 'number') { |
2110 | 2102 | this.input.gain.value = value; |
@@ -4755,7 +4747,7 @@ soundRecorder = function () { |
4755 | 4747 | * |
4756 | 4748 | * else if (state === 2) { |
4757 | 4749 | * soundFile.play(); // play the result! |
4758 | | - * saveSound(soundFile, 'mySound.wav'); |
| 4750 | + * save(soundFile, 'mySound.wav'); |
4759 | 4751 | * state++; |
4760 | 4752 | * } |
4761 | 4753 | * } |
@@ -4971,9 +4963,64 @@ soundRecorder = function () { |
4971 | 4963 | } |
4972 | 4964 | } |
4973 | 4965 | }(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); |
4974 | 5021 | var src_app; |
4975 | 5022 | src_app = function () { |
4976 | 5023 | 'use strict'; |
4977 | 5024 | var p5SOUND = sndcore; |
4978 | 5025 | 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