TooTallNate / node-wav

`Reader` and `Writer` streams for Microsoft WAVE audio files
MIT License
181 stars 37 forks source link

How can one pass actual data to the FileWriter before the file is written? #6

Closed j-k closed 8 years ago

j-k commented 10 years ago

How can one pass actual data to the FileWriter before the file is written? This is not explained in the API. I would like to put a one second 440 Hz sine wave (sample by sample) into a WAV file that is generated with Math.sin().

LinusU commented 8 years ago

The FileWriter is currently broken, I hope to get that fixed soon. But assuming it works, it could look something like this.

const output = new FileWriter('test.wav')

for (let i = 0; i < output.sampleRate; i++) {
  for (let c = 0; c < output.channels; c++) {
    const chunk = new Buffer(4)
    chunk.writeUInt16(Math.sin(i * X))
    output.write(chunk)
  }
}

output.end()
LinusU commented 8 years ago

To answer your question, write buffers to it with .write(_) and when you are done, call .end(). This is how all streams behave in Node.js.