Streampunk / naudiodon

Node.js stream bindings for PortAudio
Apache License 2.0
295 stars 82 forks source link

Trying to understand number of processed buffers per second #61

Open polyclick opened 3 years ago

polyclick commented 3 years ago

Hi

I'm really trying to understand why the following happens. Any insight would be super useful. 🤪

I have set the following settings from an audio-in readable stream:

sampleRate: 44100
sampleFormat: 16   // 2 bytes per sample per channel
channelCount: 12   // sound card with 12 inputs (6x Left/Right)
highwaterMark = 12 * 2 * 512 // (12 open channels) * (2 bytes per frame) * (512 frames per buffer) = 12288 bytes

I'd think that the buffer-callback gets called +- 86 times per second: 44100 samples per second / 512 samples = 86.1328125

Yet, when I manually count the amount of buffer-callbacks, I get 172, which is exactly the double. Here's the code how I count the buffer-callback:

let prevTimestamp = 0
let processedBuffersInSecond = 0
this.stream.on(`data`, (buffer) => {
  processedBuffersInSecond++

  if(buffer.timestamp - prevTimestamp > 1) {
    console.log(`Buffers processed in 1 sec: ${processedBuffersInSecond}, ${this.sampleRate} / ${this.framesPerBuffer} = ${this.sampleRate / this.framesPerBuffer}`)
    processedBuffersInSecond = 0
    prevTimestamp = buffer.timestamp
  }
})
# Output
Buffers processed in 1 sec: 172, 44100 / 512 = 86.1328125
Buffers processed in 1 sec: 172, 44100 / 512 = 86.1328125
Buffers processed in 1 sec: 172, 44100 / 512 = 86.1328125
Buffers processed in 1 sec: 173, 44100 / 512 = 86.1328125
Buffers processed in 1 sec: 172, 44100 / 512 = 86.1328125
Buffers processed in 1 sec: 172, 44100 / 512 = 86.1328125
Buffers processed in 1 sec: 172, 44100 / 512 = 86.1328125

Why is this? Am I missing something?