audiojs/audio

Change writing to use iteration.

jamen opened this issue · 4 comments

jamen commented

Iteration will be VERY helpful for writing multi-channel data.

For example, the use of generators would make it easy to write two dynamic channels at once:

const { PI, sin } = Math;

// Write sine waves
audio.write(function* (t, self) {
  // Channel 1 (first iteration)
  yield self.max * sin(2 * PI / 440 * t);

  // Channel 2 (second iteration)
  yield (self.max - 20) * sin(2 * PI / 1000 * 2);
});

With this, you could also use arrays to write static multi-channel data:

audio.write([12, 13]);

Then you could have single integers like 3 expand to [3, 3]

audio.write(3);
// Equivalent to
audio.write([3, 3]);
jamen commented

Notes to self:

  • Buffer#write is asynchronous so the Audio#write should have a callback somewhere for handling success/error.
  • This generator could technically be iterated asynchronously (for each channel), would be interesting and fast, also needs a callback (fitting the API).

Could you expand multiple integers for more than two channels in your last example?

jamen commented

Yeah, it doesn't literally "expand" I guess. It would use some system behind the scenes with audio.channels, and write the number howmany ever times over based on that.

jamen commented

I'm going to close this until I have a better idea of how I want to structure data things.