ToneGenerator for node.js
This thing generates an array of numbers in waveforms. This waveform can be written into a wavefile as raw PCM data. It does not play the sounds. If you want to play sounds, make sure to read the examples on how to write wave files below.
var tone =// New Interface! More options!!var tonedata =// The old interface, still available for compatibilityvar tonedata =
Using the new interface
- freq frequency in hertz. defaults to 440
- lengthInSecs controls the length of the array output together with the samplerate defaults to 2.0
- volume controls max/min for the array values. If you intend to write 8-bit it should be less than or equal to tone.MAX_8, if 16 bit it should be less than or equal to tone.MAX_16. defaults to 30
- rate sample rate, number of samples per second. Together with lengthInSecs, this define the length of the output array (lengthInSeccs * rate). defaults to 44100
- shape controls the wave shape. Options are 'triangle', 'square', 'sine', 'saw'. You can also pass in a custom function, see the tests for an example of this. defaults to 'sine'
Using the old interface
The old interface takes four arguments: freq, lengthInSecs, volume, rate. volume and rate are optional, the default is shown above. Shape is not available in the old interface. If you want to specify rate, you have to specify volume!
The data is returned as a normal array, so you can do operations on it.
// An A-major chordvar tone1 =var tone2 =var tone3 =// "playing" one tone at the time// note that at this time, our sound is just an array// of gain values. By appending the raw PCM data for one after another,// we can play them in a sequencevar res = tone1// By adding values of the tones for each sample,// we play them simultaneously, as a chordforvar i = 0; i < tone1length; i++res
Volume on 8-bit and 16-bit PCM data
The meaning of the 'volume' value depends on whether you're creating 8-bit or 16-bit data. For 8-bit data, the max volume to avoid distortion is 128. For 16-bit data, the max volume is 32768. Those values are available as require('tonegenerator').MAX_8 and require('tonegenerator').MAX_16 respectively.
Writing 8-bit data to a Wave File
Before writing your PCM data to a file, you need to convert it to a buffer of UInt8 values. 8-bit wave data goes from 0-255, so we need to add 128 to each value:
var tone = ;// Use this package to write a header for the wave file//var header = ;var fs = ;var file = fsvar samples =file// Convert -128 -> 127 range into 0 -> 255var data = Uint8Arrayif Bufferfrom // Node 5+buffer = Bufferelsebuffer = datafilefile
Writing 16-bit data to a Wave file
16-bit data requires a little bit more work, since we need to take Endianess into account. Unlike 8-bit data, the volumes does not start at 0, but at -32768.
All the references to data length need to be doubled.
var tone = ;var header = ;var fs = ;var file = fsvar samples =filevar data = Int16Arrayvar size = datalength * 2 // 2 bytes per sampleif BufferallocUnsafe // Node 5+buffer = Bufferelsebuffer = sizedatafilefile
Writing stereo sounds
In stereo wave data, the sample for each channel comes right after each other.
The principle looks like
[sample0-1 sample0-2 sample1-1 sample1-2]. So we need to
first generate the data for each channel, then interleave them.
var tone = ;var header = ;var fs = ;var file = fs// A loud A for channel 1var channel1 =// A not so loud C for channel 2var channel2 =// create an array where the 2 channels are interleaved:var samples =for var i = 0; i < channel1length; i++samplessamplesfilevar data = Int16Arrayvar size = datalength * 2 // 2 bytes per sampleif BufferallocUnsafe // Node 5+buffer = Bufferelsebuffer = sizedatafilefile