audiojs / web-audio-api

Node.js implementation of Web audio API
MIT License
826 stars 67 forks source link

Error: the 2 AudioBuffers don't have the same sampleRate #59

Closed mreinstein closed 1 year ago

mreinstein commented 7 years ago

@sebpiq this module is freakin' amazing!

I think I may have stumbled onto a bug when looping an audio file. I load an mp3 file, and run it. It plays for a little while. When it's about to loop, I see this error:

/Users/michaelreinstein/wwwroot/whitenoise/node_modules/web-audio-api/build/AudioBuffer.js:46
      throw new Error('the 2 AudioBuffers don\'t have the same sampleRate')
      ^

Error: the 2 AudioBuffers don't have the same sampleRate
    at AudioBuffer.proto$0.set (/Users/michaelreinstein/wwwroot/whitenoise/node_modules/web-audio-api/build/AudioBuffer.js:46:13)
    at AudioBufferSourceNode.this$0._dsp (/Users/michaelreinstein/wwwroot/whitenoise/node_modules/web-audio-api/build/AudioBufferSourceNode.js:54:21)

when I modify line 54 of build/AudioBufferSourceNode.js from:

outBuffer = new AudioBuffer(this.buffer.numberOfChannels, blockSize, sampleRate)

to:

outBuffer = new AudioBuffer(this.buffer.numberOfChannels, blockSize, this.buffer.sampleRate)

The error goes away. I'm not sure why though! Is this a bug, or something specific to my particular use case?

sebpiq commented 7 years ago

Hi @mreinstein ! I am not maintaining this module anymore. I am trying to pass it on to a new maintainer that would be interested in it. But thanks for reporting this, and have fun playing with it!!!

dy commented 7 years ago

@sebpiq if you want - you can transfer node-web-audio-api to @audiojs - there are other mainteiners and related modules and we'd appreciate to do refreshing. Btw you are invited!)

G-Rath commented 6 years ago

I'm having the same issue, except I'm not playing my audio in a loop.

I've got a series of audio nodes 'buffered' in an array, where when one finishes, the next one plays. When the first node in the buffer finishes (or gets very close to finishing), I get this error.

I'll look into this more to try and find where exactly this is happening, but so far I've found it happens regardless of how many audio nodes get buffered.

G-Rath commented 6 years ago

@dfcreative I can confirm that the fix suggested by @mreinstein works.

Alternatively @mreinstein I found that if I provide AudioContext with the sample rate (myAudioContext.sampleRate = whateverMySampleRateIs;), it equally works fine.

I feel a change does need to happen in regards to this, because it seems to not match up with the web audio api: My audio played fine when using similar code in the browser, without me having to provide the sample rate to AudioContext.

Either way, my audio is working now.

BHznJNs commented 1 year ago

I have the same issue when I play some specific .mp3 audio, and I also wrote a npm package based on this and speaker. The error happened in the lib/AudioBuffer.js 31:23. Excuse me, do you have any solution now?

BHznJNs commented 1 year ago

Additional, the "specific .mp3 audio" all can be played but their tune will be lowered and this error will be thrown after the audio playing is ended.

BHznJNs commented 1 year ago

I think that I have known the solution now. After call the AudioContext.decodeAudioData, and get the target AudioBuffer, we can get the sampleRate of it and assign this value to the AudioContext instance. Thus, this error will not to be thrown.

BHznJNs commented 1 year ago

I think that I have known the solution now. After call the AudioContext.decodeAudioData, and get the target AudioBuffer, we can get the sampleRate of it and assign this value to the AudioContext instance. Thus, this error will not to be thrown.

I hope that could help. @mreinstein