Skip to content

Commit

Permalink
test(sampling-master): test the sampling master
Browse files Browse the repository at this point in the history
With help from the "web-audio-test-api" project. Many thanks!
  • Loading branch information
dtinth committed Feb 19, 2015
1 parent 052d7cf commit 4ba3587
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"through2": "^0.6.3",
"url-loader": "^0.5.5",
"val-loader": "^0.5.0",
"web-audio-test-api": "^0.2.1",
"webpack": "^1.4.13",
"webpack-dev-server": "^1.6.6"
},
Expand Down
98 changes: 98 additions & 0 deletions spec/sampling-master/sampling-master_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

import SamplingMaster from 'bemuse/sampling-master'
import 'web-audio-test-api'

describe('SamplingMaster', function() {

let context
let master
before(() => { WebAudioTestAPI.use() })
beforeEach(() => {
context = new AudioContext()
master = new SamplingMaster(context)
})

describe('#unmute', function() {
it('unmutes the audio', function() {
let gain = context.createGain()
sinon.stub(context, 'createGain').returns(gain)
sinon.spy(gain, 'connect')
sinon.spy(gain, 'disconnect')
master.unmute()
void expect(context.createGain).to.have.been.called
void expect(gain.connect).to.have.been.called
void expect(gain.disconnect).to.have.been.called
})
})

describe('#sample', function() {
it('should coerce blob', function() {
return master.sample(new Blob([]))
})
it('should coerce array buffer', function() {
return master.sample(new ArrayBuffer(0))
})
it('should reject when decoding failed', function() {
context.DECODE_AUDIO_DATA_FAILED = true
return expect(master.sample(new ArrayBuffer(0))
.finally(() => context.DECODE_AUDIO_DATA_FAILED = false))
.to.be.rejected
})
describe('#play', function() {
let sample
let bufferSource
beforeEach(function() {
bufferSource = context.createBufferSource()
sinon.stub(context, 'createBufferSource').returns(bufferSource)
sinon.spy(bufferSource, 'start')
return master.sample(new Blob([])).then(s => sample = s)
})
it('should play a buffer source', function() {
sample.play()
void expect(context.createBufferSource).to.have.been.called
expect(bufferSource.start).to.have.been.calledWith(0)
})
it('should play a buffer source with delay', function() {
context.$processTo(1)
sample.play(20)
void expect(context.createBufferSource).to.have.been.called
expect(bufferSource.start).to.have.been.calledWith(21)
})
describe('#stop', function() {
it('should stop the buffer source', function() {
let instance = sample.play()
sinon.spy(bufferSource, 'stop')
instance.stop()
void expect(bufferSource.stop).to.have.been.called
})
})
})
})

describe('#destroy', function() {
let sample
beforeEach(function() {
return master.sample(new Blob([])).then(s => sample = s)
})
it('should stop all samples', function() {
let a = sample.play()
let b = sample.play()
let c = sample.play()
sinon.spy(a, 'stop')
sinon.spy(b, 'stop')
sinon.spy(c, 'stop')
master.destroy()
void expect(a.stop).to.have.been.called
void expect(b.stop).to.have.been.called
void expect(c.stop).to.have.been.called
})
it('can no longer create samples', function() {
master.destroy()
return expect(master.sample(new Blob([]))).to.be.rejected
})
})

after(() => { WebAudioTestAPI.unuse() })

})

2 changes: 1 addition & 1 deletion src/sampling-master/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class PlayInstance {
gain.connect(node || context.destination)
this._source = source
this._gain = gain
source.start(delay === 0 ? 0 : Math.max(0, context.currentTime + delay))
source.start(!delay ? 0 : Math.max(0, context.currentTime + delay))
setTimeout(() => this.stop(), (delay + buffer.duration + 0.01) * 1000)
this._master._startPlaying(this)
}
Expand Down

0 comments on commit 4ba3587

Please sign in to comment.