mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 08:38:00 +00:00
Merge branch 'master' of https://github.com/TONEnoTONE/Tone.js
Conflicts: Tone/source/Player.js
This commit is contained in:
commit
410814505c
7 changed files with 101 additions and 21 deletions
|
@ -1,11 +1,13 @@
|
|||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// AUDIO PLAYER
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
define(["Tone/core/Tone"], function(Tone){
|
||||
|
||||
/**
|
||||
* Audio Player
|
||||
*
|
||||
* Audio file player with start, loop, stop.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {Tone.Source}
|
||||
* @param {string} url
|
||||
*/
|
||||
Tone.Player = function(url){
|
||||
//extend Unit
|
||||
Tone.call(this);
|
||||
|
@ -22,7 +24,11 @@ define(["Tone/core/Tone"], function(Tone){
|
|||
|
||||
/**
|
||||
* makes an xhr reqest for the selected url
|
||||
* @param {function(Tone.Player)=} callback invoked when the sample is loaded
|
||||
* Load the audio file as an audio buffer.
|
||||
* Decodes the audio asynchronously and invokes
|
||||
* the callback once the audio buffer loads.
|
||||
*
|
||||
* @param {function(Tone.Player)} callback
|
||||
*/
|
||||
Tone.Player.prototype.load = function(callback){
|
||||
if (!this.buffer){
|
||||
|
@ -61,18 +67,26 @@ define(["Tone/core/Tone"], function(Tone){
|
|||
startTime = this.defaultArg(startTime, this.now());
|
||||
offset = this.defaultArg(offset, 0);
|
||||
duration = this.defaultArg(duration, this.buffer.duration - offset);
|
||||
volume = this.defaultArg(volume, 1);
|
||||
//make the source
|
||||
this.source = this.context.createBufferSource();
|
||||
this.source.buffer = this.buffer;
|
||||
this.source.loop = false;
|
||||
this.source.start(this.toSeconds(startTime), this.toSeconds(offset), this.toSeconds(duration));
|
||||
this.source.onended = this._onended.bind(this);
|
||||
this.chain(this.source, gain, this.output);
|
||||
this.chain(this.source, this.output);
|
||||
}
|
||||
};
|
||||
|
||||
//play the buffer from start to finish at a time
|
||||
/**
|
||||
* Loop the buffer from start to finish at a time
|
||||
*
|
||||
* @param {Tone.Time} startTime
|
||||
* @param {Tone.Time} loopStart
|
||||
* @param {Tone.Time} loopEnd
|
||||
* @param {Tone.Time} offset
|
||||
* @param {Tone.Time} duration
|
||||
* @param {Tone.Time} volume
|
||||
*/
|
||||
Tone.Player.prototype.loop = function(startTime, loopStart, loopEnd, offset, duration, volume){
|
||||
if (this.buffer){
|
||||
//default args
|
||||
|
@ -90,7 +104,8 @@ define(["Tone/core/Tone"], function(Tone){
|
|||
};
|
||||
|
||||
/**
|
||||
* stop the playback of the sample
|
||||
* Stop playback.
|
||||
*
|
||||
* @param {Tone.Time} stopTime
|
||||
*/
|
||||
Tone.Player.prototype.stop = function(stopTime){
|
||||
|
|
|
@ -7,8 +7,15 @@
|
|||
</head>
|
||||
<body>
|
||||
<input type='range'>
|
||||
<form action="" id="noise">
|
||||
Noise Type:
|
||||
<input type="radio" name="noise" value="brown" checked>brown</input>
|
||||
<input type="radio" name="noise" value="pink">pink</input>
|
||||
<input type="radio" name="noise" value="white">white</input>
|
||||
</form>
|
||||
<script type="text/javascript">
|
||||
|
||||
|
||||
//extend effect
|
||||
var NoiseEffect = function(){
|
||||
//call the super class
|
||||
|
@ -16,9 +23,13 @@
|
|||
|
||||
//components
|
||||
this.noise = new Tone.Noise("brown");
|
||||
this.crush = new Tone.BitCrusher(5);
|
||||
|
||||
// make the noise quieter
|
||||
this.noise.setVolume(.1);
|
||||
//connections
|
||||
this.connectEffect(this.noise);
|
||||
this.connectEffect(this.crush);
|
||||
}
|
||||
//extend effect
|
||||
Tone.extend(NoiseEffect, Tone.Effect);
|
||||
|
@ -27,14 +38,29 @@
|
|||
var noiseEffect = new NoiseEffect();
|
||||
noiseEffect.toMaster();
|
||||
|
||||
|
||||
// play a sound and connect it to the effect?
|
||||
var player = new Tone.Player("../audio/casio/A1.mp3");
|
||||
player.load(doLoop);
|
||||
|
||||
function doLoop() {
|
||||
player.loop(0., .1, .31);
|
||||
player.connect(noiseEffect);
|
||||
}
|
||||
//INTERFACE//
|
||||
|
||||
var range = document.querySelector("input");
|
||||
|
||||
range.onchange = function(){
|
||||
var val = range.value;
|
||||
range.oninput = function(e){
|
||||
var val = e.target.value;
|
||||
noiseEffect.setDry(val / 50 - 1);
|
||||
}
|
||||
|
||||
var noiseType = document.getElementById("noise");
|
||||
noiseType.onchange = function(e){
|
||||
noiseEffect.noise.setType(e.target.value);
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -47,11 +47,17 @@
|
|||
|
||||
var range = document.querySelector("input");
|
||||
|
||||
range.onchange = function(){
|
||||
var val = range.value;
|
||||
range.oninput = function(e){
|
||||
var val = e.target.value;
|
||||
pan.setPan(val/100, "+.1");
|
||||
}
|
||||
|
||||
var mouseX, mouseY;
|
||||
window.onmousemove = function(e) {
|
||||
mouseX = e.x;
|
||||
mouseY = e.y;
|
||||
};
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -12,7 +12,7 @@ require.config({
|
|||
}
|
||||
});
|
||||
|
||||
require(["tests/Timing", "tests/Signal", "tests/Math", "tests/Transport"], function(){
|
||||
require(["tests/Timing", "tests/Signal", "tests/Math", "tests/Transport", "tests/Sources"], function(){
|
||||
if (window.mochaPhantomJS) {
|
||||
mochaPhantomJS.run();
|
||||
} else {
|
||||
|
|
BIN
test/testAudio/kick.mp3
Normal file
BIN
test/testAudio/kick.mp3
Normal file
Binary file not shown.
33
test/tests/Sources.js
Normal file
33
test/tests/Sources.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
define(["chai", "Tone/source/Player"], function(chai, Player){
|
||||
var expect = chai.expect;
|
||||
|
||||
describe("Tone.Player", function(){
|
||||
this.timeout(100);
|
||||
var player = new Player("./testAudio/kick.mp3");
|
||||
|
||||
it("loads a file", function(done){
|
||||
player.load(function(){
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("returns correct file duration", function(){
|
||||
expect(player.getDuration()).to.equal(0.2361678034067154);
|
||||
});
|
||||
|
||||
it("plays a file", function(){
|
||||
var ended = false;
|
||||
player._onended = function(){
|
||||
ended = true;
|
||||
};
|
||||
expect(ended).to.equal(false);
|
||||
player.start();
|
||||
setTimeout(function(){
|
||||
expect(ended).to.equal(true);
|
||||
}, 0.5);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
Loading…
Reference in a new issue