Package Exports
- soundrive
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (soundrive) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Soundrive
Because oscillators in JavaScript shouldn't be that hard.
Preface
So what's wrong with OscillatorNode? It's a great API and all but, if you want to unit test your audio filter with an OscillatorNode, you're gonna have a baaaaaaaad time.
The need for Soundrive came from wanting an oscillator that can smoothly sweep between frequencies while still being easy to use and test both on the browser and server-side.
It's currently in the alpha stage, so some things might not work well or at all. Right now, it only generates sine waves because that's what I most need it for at the moment. In the future, I want to add more patterns as well as modulators. I just want to get this up so I can continue my other work as soon as possible, so it's very incomplete. But feel free to play around with it!
Installation
npm install --save soundrive
Or to install from HEAD:
npm install --save Ravenstine/soundrive
Use
Example use:
const Soundrive = require('soundrive');
var oscillator = new Soundrive.Oscillator({frequency: 12345});
var frameSize = 4096
var frame = new Float32Array(frameSize)
var i = 0;
while (i < frameSize) {
frame[i] = oscillator.oscillate();
i++;
}
This would fill up your frame with samples for a 12345hz sine wave.
To write to a file on your file system:
const fs = require('fs');
var buffer = new Buffer(4096 * 4);
var i = 0;
while (i < frameSize) {
buffer.writeFloatLE(frame[i], i*4);
i++;
}
fs.writeFile("./sine.raw", buffer, 'binary', function(err){
if(err){
console.log(err);
}
});This creates a file with raw sample data that you can open up in Audacity through File > Import.
The following creates a sweep(i.e. chirp) between two frequencies:
var oscillator = new Soundrive.Oscillator({
frequency: 1234,
sweepSize: 0.5
});
var frameSize = 44100 // one second if that's the sample rate
var frame = new Float32Array(frameSize)
var i = 0;
oscillator.changeFrequency(2345);
while (i < frameSize) {
frame[i] = oscillator.oscillate();
i++;
}The resulting frame of samples will be 1 second with a 0.5 second transition between 1234hz and 2345hz.
Pipe
You can "pipe" oscillators together to produce multiple tones.
var oscillator1 = new Soundrive.Oscillator({frequency: 770});
var oscillator2 = new Soundrive.Oscillator({frequency: 852});
var frameSize = 4096
var frame = new Float32Array(frameSize)
var i = 0;
while (i < frameSize) {
frame[i] = oscillator1.pipe(oscillator2);
i++;
}
The mechanism of piping is subject to change, but this is a fundamental feature that I want to have implemented.
Development
The source is written in CoffeeScript and can be compile to dist by running gulp coffee-src.