Skip to content

Commit 0d4e933

Browse files
committed
Update audio-buffer
1 parent 34459bd commit 0d4e933

21 files changed

Lines changed: 72 additions & 61 deletions

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"dsp"
3232
],
3333
"dependencies": {
34-
"audio-buffer": "^6.0.1",
34+
"audio-buffer": "^7.3.0",
3535
"audio-decode": "^3.1.2",
3636
"audio-speaker": "^2.0.5",
3737
"automation-events": "^7.1.15",

src/ScriptProcessorNode.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BLOCK_SIZE } from './constants.js'
22
import AudioNode from './AudioNode.js'
33
import AudioBuffer from 'audio-buffer'
4+
import { from, concat, slice } from 'audio-buffer/util'
45

56

67
class ScriptProcessorNode extends AudioNode {
@@ -34,24 +35,22 @@ class ScriptProcessorNode extends AudioNode {
3435

3536
let inBlock = this._inputs[0]._tick()
3637
// Clone the input block since AudioInput reuses its internal mix buffer
37-
let cloned = new AudioBuffer(inBlock.numberOfChannels, inBlock.length, this.context.sampleRate)
38-
for (let c = 0; c < inBlock.numberOfChannels; c++)
39-
cloned.getChannelData(c).set(inBlock.getChannelData(c))
40-
inputBuffer = inputBuffer ? inputBuffer.concat(cloned) : cloned
38+
let cloned = from(inBlock)
39+
inputBuffer = inputBuffer ? concat(inputBuffer, cloned) : cloned
4140

4241
if (inputBuffer.length === this.bufferSize) {
4342
let event = this._processingEvent(inputBuffer)
4443
onaudioprocess(event)
4544
inputBuffer = null
46-
outputBuffer = outputBuffer ? outputBuffer.concat(event.outputBuffer) : event.outputBuffer
45+
outputBuffer = outputBuffer ? concat(outputBuffer, event.outputBuffer) : event.outputBuffer
4746
} else if (inputBuffer.length >= this.bufferSize) throw new Error('this shouldnt happen')
4847

4948
frameCount += BLOCK_SIZE
5049

5150
// Enforce 2*bufferSize latency: don't emit output until enough frames have passed
5251
if (frameCount > latency && outputBuffer && outputBuffer.length >= BLOCK_SIZE) {
53-
let returned = outputBuffer.slice(0, BLOCK_SIZE)
54-
outputBuffer = outputBuffer.length > BLOCK_SIZE ? outputBuffer.slice(BLOCK_SIZE) : null
52+
let returned = slice(outputBuffer, 0, BLOCK_SIZE)
53+
outputBuffer = outputBuffer.length > BLOCK_SIZE ? slice(outputBuffer, BLOCK_SIZE) : null
5554
return returned
5655
}
5756
return new AudioBuffer(this.numberOfOutputChannels, BLOCK_SIZE, this.context.sampleRate)

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import AudioBuffer from 'audio-buffer'
21
import decode from 'audio-decode'
2+
import { from as audioBufferFrom } from 'audio-buffer/util'
33

44
// polyfill Symbol.dispose
55
Symbol.dispose ||= Symbol('dispose')
@@ -9,7 +9,7 @@ Symbol.dispose ||= Symbol('dispose')
99
export async function decodeAudioData(buffer, done) {
1010
try {
1111
let { channelData, sampleRate } = await decode(buffer)
12-
let audioBuffer = AudioBuffer.fromArray(channelData, sampleRate)
12+
let audioBuffer = audioBufferFrom(channelData, { sampleRate })
1313
if (done) done(null, audioBuffer)
1414
return audioBuffer
1515
} catch (err) {

test/AnalyserNode.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'tst'
22
import { is, ok, throws, almost } from 'tst'
33
import AudioNode from '../src/AudioNode.js'
44
import AudioBuffer from 'audio-buffer'
5+
import { fill } from 'audio-buffer/util'
56
import AnalyserNode from '../src/AnalyserNode.js'
67
import { BLOCK_SIZE } from '../src/constants.js'
78

@@ -24,7 +25,7 @@ test('AnalyserNode > fftSize validation', () => {
2425
test.mute('AnalyserNode > passthrough', () => {
2526
let c = { sampleRate: SR, currentTime: 0 }
2627
let node = new AnalyserNode(c)
27-
wire(c, node, AudioBuffer.filledWithVal(0.5, 1, BLOCK_SIZE, SR))
28+
wire(c, node, fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.5))
2829
c.currentTime = 1; almost(node._tick().getChannelData(0)[0], 0.5, 1e-6, 'passthrough')
2930
})
3031

test/AudioBuffer.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'tst'
22
import { is, ok, throws, almost } from 'tst'
33
import AudioBuffer from 'audio-buffer'
4+
import { concat, slice, set, fill, from } from 'audio-buffer/util'
45

56
test('AudioBuffer > constructor', () => {
67
let buf = new AudioBuffer(3, 100, 44100)
@@ -32,7 +33,7 @@ test('AudioBuffer > getChannelData returns view (not copy)', () => {
3233
})
3334

3435
test.mute('AudioBuffer > filledWithVal', () => {
35-
let buf = AudioBuffer.filledWithVal(0.7, 2, 50, 44100)
36+
let buf = fill(new AudioBuffer(2, 50, 44100), 0.7)
3637
is(buf.numberOfChannels, 2)
3738
is(buf.length, 50)
3839
for (let i = 0; i < 50; i++) {
@@ -43,7 +44,7 @@ test.mute('AudioBuffer > filledWithVal', () => {
4344

4445
test('AudioBuffer > fromArray', () => {
4546
let arr = [new Float32Array([1, 2, 3]), new Float32Array([4, 5, 6])]
46-
let buf = AudioBuffer.fromArray(arr, 44100)
47+
let buf = from(arr, { sampleRate: 44100 })
4748
is(buf.numberOfChannels, 2)
4849
is(buf.length, 3)
4950
is(buf.getChannelData(0)[0], 1)
@@ -52,31 +53,31 @@ test('AudioBuffer > fromArray', () => {
5253

5354
test('AudioBuffer > slice', () => {
5455
let arr = [new Float32Array([1, 2, 3, 4, 5])]
55-
let buf = AudioBuffer.fromArray(arr, 44100)
56-
let sliced = buf.slice(1, 3)
56+
let buf = from(arr, { sampleRate: 44100 })
57+
let sliced = slice(buf, 1, 3)
5758
is(sliced.length, 2)
5859
is(sliced.getChannelData(0)[0], 2)
5960
is(sliced.getChannelData(0)[1], 3)
6061
})
6162

6263
test('AudioBuffer > concat', () => {
63-
let a = AudioBuffer.fromArray([new Float32Array([1, 2])], 44100)
64-
let b = AudioBuffer.fromArray([new Float32Array([3, 4])], 44100)
65-
let c = a.concat(b)
64+
let a = from([new Float32Array([1, 2])], { sampleRate: 44100 })
65+
let b = from([new Float32Array([3, 4])], { sampleRate: 44100 })
66+
let c = concat(a, b)
6667
is(c.length, 4)
6768
is(c.getChannelData(0)[2], 3)
6869
})
6970

7071
test('AudioBuffer > concat > rejects mismatched sampleRate', () => {
7172
let a = new AudioBuffer(1, 10, 44100)
7273
let b = new AudioBuffer(1, 10, 22050)
73-
throws(() => a.concat(b))
74+
throws(() => concat(a, b))
7475
})
7576

7677
test('AudioBuffer > set', () => {
7778
let a = new AudioBuffer(1, 10, 44100)
78-
let b = AudioBuffer.fromArray([new Float32Array([0.5, 0.6])], 44100)
79-
a.set(b, 3)
79+
let b = from([new Float32Array([0.5, 0.6])], { sampleRate: 44100 })
80+
set(a, b, 3)
8081
almost(a.getChannelData(0)[3], 0.5, 1e-6)
8182
almost(a.getChannelData(0)[4], 0.6, 1e-6)
8283
})

test/AudioWorklet.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { is, ok, almost, throws } from 'tst'
33
import AudioContext from '../src/AudioContext.js'
44
import { AudioWorkletNode, AudioWorkletProcessor } from '../src/AudioWorklet.js'
55
import AudioBuffer from 'audio-buffer'
6+
import { fill } from 'audio-buffer/util'
67
import AudioNode from '../src/AudioNode.js'
78
import { BLOCK_SIZE } from '../src/constants.js'
89

@@ -54,7 +55,7 @@ test('AudioWorklet > processes audio', async () => {
5455
let node = new AudioWorkletNode(ctx, 'half')
5556
let src = new AudioNode(ctx, 0, 1)
5657
src.connect(node)
57-
src._tick = () => AudioBuffer.filledWithVal(0.8, 1, BLOCK_SIZE, 44100)
58+
src._tick = () => fill(new AudioBuffer(1, BLOCK_SIZE, 44100), 0.8)
5859

5960
ctx._state = 'running'
6061
let buf = node._tick()
@@ -101,7 +102,7 @@ test('AudioWorklet > process returning false kills node, outputs silence', async
101102
let node = new AudioWorkletNode(ctx, 'oneshot')
102103
let src = new AudioNode(ctx, 0, 1)
103104
src.connect(node)
104-
src._tick = () => AudioBuffer.filledWithVal(0, 1, BLOCK_SIZE, 44100)
105+
src._tick = () => fill(new AudioBuffer(1, BLOCK_SIZE, 44100), 0)
105106

106107
ctx._state = 'running'
107108
node._tick() // calls=1
@@ -141,7 +142,7 @@ test('AudioWorklet > addModule with data URI', async () => {
141142
let node = new AudioWorkletNode(ctx, 'data-uri-proc')
142143
let src = new AudioNode(ctx, 0, 1)
143144
src.connect(node)
144-
src._tick = () => AudioBuffer.filledWithVal(0, 1, BLOCK_SIZE, 44100)
145+
src._tick = () => fill(new AudioBuffer(1, BLOCK_SIZE, 44100), 0)
145146

146147
ctx._state = 'running'
147148
let buf = node._tick()

test/BiquadFilterNode.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'tst'
22
import { is, ok, throws, almost } from 'tst'
33
import AudioNode from '../src/AudioNode.js'
44
import AudioBuffer from 'audio-buffer'
5+
import { fill } from 'audio-buffer/util'
56
import BiquadFilterNode from '../src/BiquadFilterNode.js'
67
import { BLOCK_SIZE } from '../src/constants.js'
78

@@ -22,7 +23,7 @@ test('BiquadFilterNode > type validation', () => {
2223
test.mute('BiquadFilterNode > lowpass passes DC', () => {
2324
let c = { sampleRate: SR, currentTime: 0 }
2425
let f = new BiquadFilterNode(c); f.type = 'lowpass'; f.frequency.value = 1000
25-
wire(c, f, AudioBuffer.filledWithVal(0.5, 1, BLOCK_SIZE, SR))
26+
wire(c, f, fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.5))
2627
for (let i = 0; i < 20; i++) { c.currentTime = i; f._tick() }
2728
c.currentTime = 20; let buf = f._tick()
2829
almost(buf.getChannelData(0)[BLOCK_SIZE - 1], 0.5, 0.05, 'DC passes')
@@ -31,7 +32,7 @@ test.mute('BiquadFilterNode > lowpass passes DC', () => {
3132
test.mute('BiquadFilterNode > highpass blocks DC', () => {
3233
let c = { sampleRate: SR, currentTime: 0 }
3334
let f = new BiquadFilterNode(c); f.type = 'highpass'; f.frequency.value = 1000
34-
wire(c, f, AudioBuffer.filledWithVal(0.5, 1, BLOCK_SIZE, SR))
35+
wire(c, f, fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.5))
3536
for (let i = 0; i < 20; i++) { c.currentTime = i; f._tick() }
3637
c.currentTime = 20; let buf = f._tick()
3738
almost(buf.getChannelData(0)[BLOCK_SIZE - 1], 0, 0.05, 'DC blocked')

test/ChannelNodes.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'tst'
22
import { is, ok, almost } from 'tst'
33
import AudioNode from '../src/AudioNode.js'
44
import AudioBuffer from 'audio-buffer'
5+
import { fill } from 'audio-buffer/util'
56
import ChannelSplitterNode from '../src/ChannelSplitterNode.js'
67
import ChannelMergerNode from '../src/ChannelMergerNode.js'
78
import { BLOCK_SIZE } from '../src/constants.js'
@@ -36,8 +37,8 @@ test.mute('ChannelMergerNode > merges mono to stereo', () => {
3637
let node = new ChannelMergerNode(c, { numberOfInputs: 2 })
3738
let s0 = new AudioNode(c, 0, 1), s1 = new AudioNode(c, 0, 1)
3839
s0.connect(node, 0, 0); s1.connect(node, 0, 1)
39-
s0._tick = () => AudioBuffer.filledWithVal(0.2, 1, BLOCK_SIZE, SR)
40-
s1._tick = () => AudioBuffer.filledWithVal(0.8, 1, BLOCK_SIZE, SR)
40+
s0._tick = () => fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.2)
41+
s1._tick = () => fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.8)
4142
c.currentTime = 1; let buf = node._tick()
4243
is(buf.numberOfChannels, 2)
4344
almost(buf.getChannelData(0)[0], 0.2, 1e-6, 'ch0'); almost(buf.getChannelData(1)[0], 0.8, 1e-6, 'ch1')

test/ConvolverNode.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'tst'
22
import { ok, almost } from 'tst'
33
import AudioNode from '../src/AudioNode.js'
44
import AudioBuffer from 'audio-buffer'
5+
import { fill } from 'audio-buffer/util'
56
import ConvolverNode from '../src/ConvolverNode.js'
67
import { BLOCK_SIZE } from '../src/constants.js'
78

@@ -11,7 +12,7 @@ let wire = (c, node, buf) => { let s = new AudioNode(c, 0, 1); s.connect(node);
1112
test('ConvolverNode > passthrough when no buffer', () => {
1213
let c = { sampleRate: SR, currentTime: 0 }
1314
let node = new ConvolverNode(c)
14-
wire(c, node, AudioBuffer.filledWithVal(0.5, 1, BLOCK_SIZE, SR))
15+
wire(c, node, fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.5))
1516
c.currentTime = 1; almost(node._tick().getChannelData(0)[0], 0.5, 1e-6)
1617
})
1718

@@ -21,7 +22,7 @@ test.mute('ConvolverNode > unit impulse IR = passthrough', () => {
2122
node.normalize = false
2223
let ir = new AudioBuffer(1, 1, SR); ir.getChannelData(0)[0] = 1
2324
node.buffer = ir
24-
wire(c, node, AudioBuffer.filledWithVal(0.7, 1, BLOCK_SIZE, SR))
25+
wire(c, node, fill(new AudioBuffer(1, BLOCK_SIZE, SR), 0.7))
2526
c.currentTime = 1; almost(node._tick().getChannelData(0)[0], 0.7, 0.01, 'passthrough')
2627
})
2728

0 commit comments

Comments
 (0)