diff --git a/bower.json b/bower.json index dab1de1..853ed1e 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "BandJS", - "version": "1.0.0", + "version": "1.0.1", "main": "dist/band.min.js", "ignore": [ "**/.*", diff --git a/dist/band.js b/dist/band.js index 6188016..8416d01 100644 --- a/dist/band.js +++ b/dist/band.js @@ -719,28 +719,29 @@ function Player(conductor) { if ('up' !== direction && 'down' !== direction) { throw new Error('Direction must be either up or down.'); } + + var fadeDuration = 0.2; + faded = direction === 'down'; - var i = 100 * conductor.masterVolumeLevel, - fadeTimer = function() { - if (i > 0) { - i = i - 4; - i = i < 0 ? 0 : i; - var gain = 'up' === direction ? conductor.masterVolumeLevel * 100 - i : i; - conductor.masterVolume.gain.value = gain / 100; - requestAnimationFrame(fadeTimer); - } else { - if (typeof cb === 'function') { - cb.call(player); - } - if (resetVolume) { - faded = ! faded; - conductor.masterVolume.gain.value = conductor.masterVolumeLevel; - } - } - }; + if (direction === 'up') { + conductor.masterVolume.gain.linearRampToValueAtTime(0, conductor.audioContext.currentTime); + conductor.masterVolume.gain.linearRampToValueAtTime(conductor.masterVolumeLevel, conductor.audioContext.currentTime + fadeDuration); + } else { + conductor.masterVolume.gain.linearRampToValueAtTime(conductor.masterVolumeLevel, conductor.audioContext.currentTime); + conductor.masterVolume.gain.linearRampToValueAtTime(0, conductor.audioContext.currentTime + fadeDuration); + } - fadeTimer(); + setTimeout(function() { + if (typeof cb === 'function') { + cb.call(player); + } + + if (resetVolume) { + faded = ! faded; + conductor.masterVolume.gain.linearRampToValueAtTime(conductor.masterVolumeLevel, conductor.audioContext.currentTime); + } + }, fadeDuration * 1000); } /** @@ -835,7 +836,7 @@ function Player(conductor) { } } else { updateTotalPlayTime(); - requestAnimationFrame(totalPlayTimeCalculator); + setTimeout(totalPlayTimeCalculator, 1000 / 60); } } } diff --git a/dist/band.min.js b/dist/band.min.js index b092845..d4e6dd7 100644 --- a/dist/band.min.js +++ b/dist/band.min.js @@ -1,2 +1,2 @@ -/*! BandJS - v1.0.0 - 2014-07-14 */ -!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.BandJS=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g0&&e.destroy(),!a)throw new Error("JSON is required for this method to work.");if("undefined"==typeof a.instruments)throw new Error("You must define at least one instrument");if("undefined"==typeof a.notes)throw new Error("You must define notes for each instrument");"undefined"!=typeof a.timeSignature&&e.setTimeSignature(a.timeSignature[0],a.timeSignature[1]),"undefined"!=typeof a.tempo&&e.setTempo(a.tempo);var b={};for(var c in a.instruments)a.instruments.hasOwnProperty(c)&&(b[c]=e.createInstrument(a.instruments[c].name,a.instruments[c].pack));for(var d in a.notes)if(a.notes.hasOwnProperty(d))for(var f=-1;++fc&&(c=d.totalDuration)}e.totalDuration=c;var f=a("./player.js");return new f(e)},e.destroy=function(){e.audioContext=new g,e.instruments.length=0,e.masterVolume=e.audioContext.createGain(),e.masterVolume.connect(e.audioContext.destination)},e.setMasterVolume=function(a){a>1&&(a/=100),e.masterVolumeLevel=a,e.masterVolume.gain.value=a},e.getTotalSeconds=function(){return Math.round(e.totalDuration)},e.setTickerCallback=function(a){if("function"!=typeof a)throw new Error("Ticker must be a function.");e.onTickerCallback=a},e.setTimeSignature=function(a,b){if("undefined"==typeof h[b])throw new Error("The bottom time signature is not supported.");e.beatsPerBar=a,e.noteGetsBeat=h[b]},e.setTempo=function(a){e.tempo=60/a},e.setOnFinished=function(a){if("function"!=typeof a)throw new Error("onFinished callback must be a function.");e.onFinishedCallback=a},e.setMasterVolume(100),e.setTempo(120),e.setTimeSignature(4,4)}b.exports=c;var d={instrument:{},rhythm:{},tuning:{}};c.loadPack=function(a,b,c){if(-1===["tuning","rhythm","instrument"].indexOf(a))throw new Error(a=" is not a valid Pack Type.");if("undefined"!=typeof d[a][b])throw new Error("A(n) "+a+' pack with the name "'+b+'" has already been loaded.');d[a][b]=c}},{"./instrument.js":5,"./player.js":7,audiocontext:1}],3:[function(a,b){function c(a,b){function c(a){for(var c=2*b.sampleRate,d=b.createBuffer(1,c,b.sampleRate),e=d.getChannelData(0),f=0;c>f;f++)e[f]=2*Math.random()-1;var g=b.createBufferSource();return g.buffer=d,g.loop=!0,g.connect(a),g}function d(a){var c,d,e,f,g,h,i,j=2*b.sampleRate,k=b.createBuffer(1,j,b.sampleRate),l=k.getChannelData(0);c=d=e=f=g=h=i=0;for(var m=0;j>m;m++){var n=2*Math.random()-1;c=.99886*c+.0555179*n,d=.99332*d+.0750759*n,e=.969*e+.153852*n,f=.8665*f+.3104856*n,g=.55*g+.5329522*n,h=-.7616*h-.016898*n,l[m]=c+d+e+f+g+h+i+.5362*n,l[m]*=.11,i=.115926*n}var o=b.createBufferSource();return o.buffer=k,o.loop=!0,o.connect(a),o}function e(a){for(var c=2*b.sampleRate,d=b.createBuffer(1,c,b.sampleRate),e=d.getChannelData(0),f=0,g=0;c>g;g++){var h=2*Math.random()-1;e[g]=(f+.02*h)/1.02,f=e[g],e[g]*=3.5}var i=b.createBufferSource();return i.buffer=d,i.loop=!0,i.connect(a),i}var f=["white","pink","brown","brownian","red"];if(-1===f.indexOf(a))throw new Error(a+" is not a valid noise sound");return{createSound:function(b){switch(a){case"white":return c(b);case"pink":return d(b);case"brown":case"brownian":case"red":return e(b)}}}}b.exports=c},{}],4:[function(a,b){function c(a,b){var c={sine:0,square:1,sawtooth:2,triangle:3};if("undefined"==typeof c[a])throw new Error(a+" is not a valid Oscillator type");return{createSound:function(d,e){var f=b.createOscillator();return f.connect(d),f.type=c[a],f.frequency.value=e,f}}}b.exports=c},{}],5:[function(a,b){function c(a,b,c){function d(a){if("undefined"==typeof c.notes[a])throw new Error(a+" is not a correct rhythm.");return c.notes[a]*c.tempo/c.noteGetsBeat*10}function e(a){if(null==a||"object"!=typeof a)return a;var b=a.constructor();for(var c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b}if(a||(a="sine"),b||(b="oscillators"),"undefined"==typeof c.packs.instrument[b])throw new Error(b+" is not a currently loaded Instrument Pack.");var f=this,g=0,h=1;f.totalDuration=0,f.bufferPosition=0,f.instrument=c.packs.instrument[b](a,c.audioContext),f.sounds=[],f.setVolume=function(a){return a>1&&(a/=100),h=a,f},f.note=function(a,b,e){var g=d(a),i=e?0:.05*g;if(b){b=b.split(",");for(var j=-1;++jk))throw new Error(k+" is not a valid pitch.")}}return f.sounds.push({pitch:b,duration:g,articulationGap:i,tie:e,startTime:f.totalDuration,volumeLevel:h/4,stopTime:f.totalDuration+g-i}),f.totalDuration+=g,f},f.rest=function(a){var b=d(a);return f.sounds.push({pitch:!1,duration:b,articulationGap:0,startTime:f.totalDuration,stopTime:f.totalDuration+b}),f.totalDuration+=b,f},f.repeatStart=function(){return g=f.sounds.length,f},f.repeatFromBeginning=function(a){return g=0,f.repeat(a),f},f.repeat=function(a){a="undefined"==typeof a?1:a;for(var b=f.sounds.slice(g),c=0;a>c;c++)for(var d=-1;++d0){e-=4,e=0>e?0:e;var g="up"===b?100*a.masterVolumeLevel-e:e;a.masterVolume.gain.value=g/100,requestAnimationFrame(f)}else"function"==typeof c&&c.call(i),d&&(n=!n,a.masterVolume.gain.value=a.masterVolumeLevel)};f()}function d(b){b||(b=j);for(var c=[],d=-1;++dm)f++;else if(!1!==i){var o=a.audioContext.createGain();if(o.connect(a.masterVolume),o.gain.value=n,l>k&&(k=m-l),"undefined"==typeof i)c.push({startTime:l>k?m-l:k,stopTime:m,node:e.instrument.createSound(o),gain:o,volumeLevel:n});else for(var p=-1;++p0&&(e-=.001),f+=.001,d.gain.gain.setValueAtTime(0,e),d.gain.gain.linearRampToValueAtTime(d.volumeLevel,e+.001),d.gain.gain.setValueAtTime(d.volumeLevel,f-.001),d.gain.gain.linearRampToValueAtTime(0,f)),d.node.start(e),d.node.stop(f)}},j=function(){g=setTimeout(function(){if(i.playing&&!i.paused){var a=d();a.length>0&&(f(a),k=k.concat(a),j())}},5e3)};f(k),j(),n&&!i.muted&&c("up")},i.stop=function(d){i.playing=!1,m=0,"undefined"==typeof d&&(d=!0),d&&!i.muted?c("down",function(){l=0,b(),setTimeout(function(){a.onTickerCallback(m)},1)},!0):(l=0,b(),setTimeout(function(){a.onTickerCallback(m)},1))},i.pause=function(){i.paused=!0,f(),i.muted?b():c("down",function(){b()})},i.loop=function(a){i.looping=!!a},i.setTime=function(a){l=parseInt(a),b(),i.playing&&!i.paused&&i.play()},i.mute=function(a){i.muted=!0,c("down",a)},i.unmute=function(a){i.muted=!1,c("up",a)}}b.exports=c},{}],8:[function(a,b){b.exports={semibreve:1,dottedMinim:.75,minim:.5,dottedCrotchet:.375,tripletMinim:.33333334,crotchet:.25,dottedQuaver:.1875,tripletCrotchet:.166666667,quaver:.125,dottedSemiquaver:.09375,tripletQuaver:.083333333,semiquaver:.0625,tripletSemiquaver:.041666667,demisemiquaver:.03125}},{}],9:[function(a,b){b.exports={whole:1,dottedHalf:.75,half:.5,dottedQuarter:.375,tripletHalf:.33333334,quarter:.25,dottedEighth:.1875,tripletQuarter:.166666667,eighth:.125,dottedSixteenth:.09375,tripletEighth:.083333333,sixteenth:.0625,tripletSixteenth:.041666667,thirtySecond:.03125}},{}],10:[function(a,b){b.exports={C0:16.35,"C#0":17.32,Db0:17.32,D0:18.35,"D#0":19.45,Eb0:19.45,E0:20.6,F0:21.83,"F#0":23.12,Gb0:23.12,G0:24.5,"G#0":25.96,Ab0:25.96,A0:27.5,"A#0":29.14,Bb0:29.14,B0:30.87,C1:32.7,"C#1":34.65,Db1:34.65,D1:36.71,"D#1":38.89,Eb1:38.89,E1:41.2,F1:43.65,"F#1":46.25,Gb1:46.25,G1:49,"G#1":51.91,Ab1:51.91,A1:55,"A#1":58.27,Bb1:58.27,B1:61.74,C2:65.41,"C#2":69.3,Db2:69.3,D2:73.42,"D#2":77.78,Eb2:77.78,E2:82.41,F2:87.31,"F#2":92.5,Gb2:92.5,G2:98,"G#2":103.83,Ab2:103.83,A2:110,"A#2":116.54,Bb2:116.54,B2:123.47,C3:130.81,"C#3":138.59,Db3:138.59,D3:146.83,"D#3":155.56,Eb3:155.56,E3:164.81,F3:174.61,"F#3":185,Gb3:185,G3:196,"G#3":207.65,Ab3:207.65,A3:220,"A#3":233.08,Bb3:233.08,B3:246.94,C4:261.63,"C#4":277.18,Db4:277.18,D4:293.66,"D#4":311.13,Eb4:311.13,E4:329.63,F4:349.23,"F#4":369.99,Gb4:369.99,G4:392,"G#4":415.3,Ab4:415.3,A4:440,"A#4":466.16,Bb4:466.16,B4:493.88,C5:523.25,"C#5":554.37,Db5:554.37,D5:587.33,"D#5":622.25,Eb5:622.25,E5:659.26,F5:698.46,"F#5":739.99,Gb5:739.99,G5:783.99,"G#5":830.61,Ab5:830.61,A5:880,"A#5":932.33,Bb5:932.33,B5:987.77,C6:1046.5,"C#6":1108.73,Db6:1108.73,D6:1174.66,"D#6":1244.51,Eb6:1244.51,E6:1318.51,F6:1396.91,"F#6":1479.98,Gb6:1479.98,G6:1567.98,"G#6":1661.22,Ab6:1661.22,A6:1760,"A#6":1864.66,Bb6:1864.66,B6:1975.53,C7:2093,"C#7":2217.46,Db7:2217.46,D7:2349.32,"D#7":2489.02,Eb7:2489.02,E7:2637.02,F7:2793.83,"F#7":2959.96,Gb7:2959.96,G7:3135.96,"G#7":3322.44,Ab7:3322.44,A7:3520,"A#7":3729.31,Bb7:3729.31,B7:3951.07,C8:4186.01}},{}]},{},[6])(6)}); \ No newline at end of file +/*! BandJS - v1.0.1 - 2014-07-14 */ +!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.BandJS=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g0&&e.destroy(),!a)throw new Error("JSON is required for this method to work.");if("undefined"==typeof a.instruments)throw new Error("You must define at least one instrument");if("undefined"==typeof a.notes)throw new Error("You must define notes for each instrument");"undefined"!=typeof a.timeSignature&&e.setTimeSignature(a.timeSignature[0],a.timeSignature[1]),"undefined"!=typeof a.tempo&&e.setTempo(a.tempo);var b={};for(var c in a.instruments)a.instruments.hasOwnProperty(c)&&(b[c]=e.createInstrument(a.instruments[c].name,a.instruments[c].pack));for(var d in a.notes)if(a.notes.hasOwnProperty(d))for(var f=-1;++fc&&(c=d.totalDuration)}e.totalDuration=c;var f=a("./player.js");return new f(e)},e.destroy=function(){e.audioContext=new g,e.instruments.length=0,e.masterVolume=e.audioContext.createGain(),e.masterVolume.connect(e.audioContext.destination)},e.setMasterVolume=function(a){a>1&&(a/=100),e.masterVolumeLevel=a,e.masterVolume.gain.value=a},e.getTotalSeconds=function(){return Math.round(e.totalDuration)},e.setTickerCallback=function(a){if("function"!=typeof a)throw new Error("Ticker must be a function.");e.onTickerCallback=a},e.setTimeSignature=function(a,b){if("undefined"==typeof h[b])throw new Error("The bottom time signature is not supported.");e.beatsPerBar=a,e.noteGetsBeat=h[b]},e.setTempo=function(a){e.tempo=60/a},e.setOnFinished=function(a){if("function"!=typeof a)throw new Error("onFinished callback must be a function.");e.onFinishedCallback=a},e.setMasterVolume(100),e.setTempo(120),e.setTimeSignature(4,4)}b.exports=c;var d={instrument:{},rhythm:{},tuning:{}};c.loadPack=function(a,b,c){if(-1===["tuning","rhythm","instrument"].indexOf(a))throw new Error(a=" is not a valid Pack Type.");if("undefined"!=typeof d[a][b])throw new Error("A(n) "+a+' pack with the name "'+b+'" has already been loaded.');d[a][b]=c}},{"./instrument.js":5,"./player.js":7,audiocontext:1}],3:[function(a,b){function c(a,b){function c(a){for(var c=2*b.sampleRate,d=b.createBuffer(1,c,b.sampleRate),e=d.getChannelData(0),f=0;c>f;f++)e[f]=2*Math.random()-1;var g=b.createBufferSource();return g.buffer=d,g.loop=!0,g.connect(a),g}function d(a){var c,d,e,f,g,h,i,j=2*b.sampleRate,k=b.createBuffer(1,j,b.sampleRate),l=k.getChannelData(0);c=d=e=f=g=h=i=0;for(var m=0;j>m;m++){var n=2*Math.random()-1;c=.99886*c+.0555179*n,d=.99332*d+.0750759*n,e=.969*e+.153852*n,f=.8665*f+.3104856*n,g=.55*g+.5329522*n,h=-.7616*h-.016898*n,l[m]=c+d+e+f+g+h+i+.5362*n,l[m]*=.11,i=.115926*n}var o=b.createBufferSource();return o.buffer=k,o.loop=!0,o.connect(a),o}function e(a){for(var c=2*b.sampleRate,d=b.createBuffer(1,c,b.sampleRate),e=d.getChannelData(0),f=0,g=0;c>g;g++){var h=2*Math.random()-1;e[g]=(f+.02*h)/1.02,f=e[g],e[g]*=3.5}var i=b.createBufferSource();return i.buffer=d,i.loop=!0,i.connect(a),i}var f=["white","pink","brown","brownian","red"];if(-1===f.indexOf(a))throw new Error(a+" is not a valid noise sound");return{createSound:function(b){switch(a){case"white":return c(b);case"pink":return d(b);case"brown":case"brownian":case"red":return e(b)}}}}b.exports=c},{}],4:[function(a,b){function c(a,b){var c={sine:0,square:1,sawtooth:2,triangle:3};if("undefined"==typeof c[a])throw new Error(a+" is not a valid Oscillator type");return{createSound:function(d,e){var f=b.createOscillator();return f.connect(d),f.type=c[a],f.frequency.value=e,f}}}b.exports=c},{}],5:[function(a,b){function c(a,b,c){function d(a){if("undefined"==typeof c.notes[a])throw new Error(a+" is not a correct rhythm.");return c.notes[a]*c.tempo/c.noteGetsBeat*10}function e(a){if(null==a||"object"!=typeof a)return a;var b=a.constructor();for(var c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b}if(a||(a="sine"),b||(b="oscillators"),"undefined"==typeof c.packs.instrument[b])throw new Error(b+" is not a currently loaded Instrument Pack.");var f=this,g=0,h=1;f.totalDuration=0,f.bufferPosition=0,f.instrument=c.packs.instrument[b](a,c.audioContext),f.sounds=[],f.setVolume=function(a){return a>1&&(a/=100),h=a,f},f.note=function(a,b,e){var g=d(a),i=e?0:.05*g;if(b){b=b.split(",");for(var j=-1;++jk))throw new Error(k+" is not a valid pitch.")}}return f.sounds.push({pitch:b,duration:g,articulationGap:i,tie:e,startTime:f.totalDuration,volumeLevel:h/4,stopTime:f.totalDuration+g-i}),f.totalDuration+=g,f},f.rest=function(a){var b=d(a);return f.sounds.push({pitch:!1,duration:b,articulationGap:0,startTime:f.totalDuration,stopTime:f.totalDuration+b}),f.totalDuration+=b,f},f.repeatStart=function(){return g=f.sounds.length,f},f.repeatFromBeginning=function(a){return g=0,f.repeat(a),f},f.repeat=function(a){a="undefined"==typeof a?1:a;for(var b=f.sounds.slice(g),c=0;a>c;c++)for(var d=-1;++dm)f++;else if(!1!==i){var o=a.audioContext.createGain();if(o.connect(a.masterVolume),o.gain.value=n,l>k&&(k=m-l),"undefined"==typeof i)c.push({startTime:l>k?m-l:k,stopTime:m,node:e.instrument.createSound(o),gain:o,volumeLevel:n});else for(var p=-1;++p0&&(e-=.001),f+=.001,d.gain.gain.setValueAtTime(0,e),d.gain.gain.linearRampToValueAtTime(d.volumeLevel,e+.001),d.gain.gain.setValueAtTime(d.volumeLevel,f-.001),d.gain.gain.linearRampToValueAtTime(0,f)),d.node.start(e),d.node.stop(f)}},j=function(){g=setTimeout(function(){if(i.playing&&!i.paused){var a=d();a.length>0&&(f(a),k=k.concat(a),j())}},5e3)};f(k),j(),n&&!i.muted&&c("up")},i.stop=function(d){i.playing=!1,m=0,"undefined"==typeof d&&(d=!0),d&&!i.muted?c("down",function(){l=0,b(),setTimeout(function(){a.onTickerCallback(m)},1)},!0):(l=0,b(),setTimeout(function(){a.onTickerCallback(m)},1))},i.pause=function(){i.paused=!0,f(),i.muted?b():c("down",function(){b()})},i.loop=function(a){i.looping=!!a},i.setTime=function(a){l=parseInt(a),b(),i.playing&&!i.paused&&i.play()},i.mute=function(a){i.muted=!0,c("down",a)},i.unmute=function(a){i.muted=!1,c("up",a)}}b.exports=c},{}],8:[function(a,b){b.exports={semibreve:1,dottedMinim:.75,minim:.5,dottedCrotchet:.375,tripletMinim:.33333334,crotchet:.25,dottedQuaver:.1875,tripletCrotchet:.166666667,quaver:.125,dottedSemiquaver:.09375,tripletQuaver:.083333333,semiquaver:.0625,tripletSemiquaver:.041666667,demisemiquaver:.03125}},{}],9:[function(a,b){b.exports={whole:1,dottedHalf:.75,half:.5,dottedQuarter:.375,tripletHalf:.33333334,quarter:.25,dottedEighth:.1875,tripletQuarter:.166666667,eighth:.125,dottedSixteenth:.09375,tripletEighth:.083333333,sixteenth:.0625,tripletSixteenth:.041666667,thirtySecond:.03125}},{}],10:[function(a,b){b.exports={C0:16.35,"C#0":17.32,Db0:17.32,D0:18.35,"D#0":19.45,Eb0:19.45,E0:20.6,F0:21.83,"F#0":23.12,Gb0:23.12,G0:24.5,"G#0":25.96,Ab0:25.96,A0:27.5,"A#0":29.14,Bb0:29.14,B0:30.87,C1:32.7,"C#1":34.65,Db1:34.65,D1:36.71,"D#1":38.89,Eb1:38.89,E1:41.2,F1:43.65,"F#1":46.25,Gb1:46.25,G1:49,"G#1":51.91,Ab1:51.91,A1:55,"A#1":58.27,Bb1:58.27,B1:61.74,C2:65.41,"C#2":69.3,Db2:69.3,D2:73.42,"D#2":77.78,Eb2:77.78,E2:82.41,F2:87.31,"F#2":92.5,Gb2:92.5,G2:98,"G#2":103.83,Ab2:103.83,A2:110,"A#2":116.54,Bb2:116.54,B2:123.47,C3:130.81,"C#3":138.59,Db3:138.59,D3:146.83,"D#3":155.56,Eb3:155.56,E3:164.81,F3:174.61,"F#3":185,Gb3:185,G3:196,"G#3":207.65,Ab3:207.65,A3:220,"A#3":233.08,Bb3:233.08,B3:246.94,C4:261.63,"C#4":277.18,Db4:277.18,D4:293.66,"D#4":311.13,Eb4:311.13,E4:329.63,F4:349.23,"F#4":369.99,Gb4:369.99,G4:392,"G#4":415.3,Ab4:415.3,A4:440,"A#4":466.16,Bb4:466.16,B4:493.88,C5:523.25,"C#5":554.37,Db5:554.37,D5:587.33,"D#5":622.25,Eb5:622.25,E5:659.26,F5:698.46,"F#5":739.99,Gb5:739.99,G5:783.99,"G#5":830.61,Ab5:830.61,A5:880,"A#5":932.33,Bb5:932.33,B5:987.77,C6:1046.5,"C#6":1108.73,Db6:1108.73,D6:1174.66,"D#6":1244.51,Eb6:1244.51,E6:1318.51,F6:1396.91,"F#6":1479.98,Gb6:1479.98,G6:1567.98,"G#6":1661.22,Ab6:1661.22,A6:1760,"A#6":1864.66,Bb6:1864.66,B6:1975.53,C7:2093,"C#7":2217.46,Db7:2217.46,D7:2349.32,"D#7":2489.02,Eb7:2489.02,E7:2637.02,F7:2793.83,"F#7":2959.96,Gb7:2959.96,G7:3135.96,"G#7":3322.44,Ab7:3322.44,A7:3520,"A#7":3729.31,Bb7:3729.31,B7:3951.07,C8:4186.01}},{}]},{},[6])(6)}); \ No newline at end of file diff --git a/package.json b/package.json index 614f4a3..8f9be81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "BandJS", - "version": "1.0.0", + "version": "1.0.1", "description": "Band.js - Music Composer - An interface for the Web Audio API that supports rhythms, multiple instruments, repeating sections, and complex time signatures.", "main": "src/main.js", "repository": { diff --git a/src/player.js b/src/player.js index a841749..98c14ef 100644 --- a/src/player.js +++ b/src/player.js @@ -58,28 +58,29 @@ function Player(conductor) { if ('up' !== direction && 'down' !== direction) { throw new Error('Direction must be either up or down.'); } + + var fadeDuration = 0.2; + faded = direction === 'down'; - var i = 100 * conductor.masterVolumeLevel, - fadeTimer = function() { - if (i > 0) { - i = i - 4; - i = i < 0 ? 0 : i; - var gain = 'up' === direction ? conductor.masterVolumeLevel * 100 - i : i; - conductor.masterVolume.gain.value = gain / 100; - requestAnimationFrame(fadeTimer); - } else { - if (typeof cb === 'function') { - cb.call(player); - } - if (resetVolume) { - faded = ! faded; - conductor.masterVolume.gain.value = conductor.masterVolumeLevel; - } - } - }; + if (direction === 'up') { + conductor.masterVolume.gain.linearRampToValueAtTime(0, conductor.audioContext.currentTime); + conductor.masterVolume.gain.linearRampToValueAtTime(conductor.masterVolumeLevel, conductor.audioContext.currentTime + fadeDuration); + } else { + conductor.masterVolume.gain.linearRampToValueAtTime(conductor.masterVolumeLevel, conductor.audioContext.currentTime); + conductor.masterVolume.gain.linearRampToValueAtTime(0, conductor.audioContext.currentTime + fadeDuration); + } - fadeTimer(); + setTimeout(function() { + if (typeof cb === 'function') { + cb.call(player); + } + + if (resetVolume) { + faded = ! faded; + conductor.masterVolume.gain.linearRampToValueAtTime(conductor.masterVolumeLevel, conductor.audioContext.currentTime); + } + }, fadeDuration * 1000); } /** @@ -174,7 +175,7 @@ function Player(conductor) { } } else { updateTotalPlayTime(); - requestAnimationFrame(totalPlayTimeCalculator); + setTimeout(totalPlayTimeCalculator, 1000 / 60); } } }