gSounds.js is a NodeJS-Library to simplify creation of audio files directly from scratch.
It can be very helpful for CLI-Apps with sounds, generate Sound automatically, quick conversions of audio-files, AI-Tasks, etc...
Play the first two bars of Mozart's "Kleine Nachtmusik"
const {Player} = require('g-sounds');
//[frequency,duration]
const NOTES = [[783.99, 0.5], [0, 0.25], [587.33, 0.25], [783.99, 0.5], [0, 0.25], [587.33, 0.25], [783.99, 0.25], [587.33, 0.25], [783.99, 0.25], [987.77, 0.25], [1174.7, 0.25]];
const p = new Player();
(async () => {
console.log('Loading Notes from array...');
await p.loadBufferFromNotes(NOTES);
console.log('loaded...playing...');
await p.play();
})();
gSounds.js expose following modules: Player
,Note
,Oscillator
.
const {Player, Note, Oscillator} = require('g-sounds');
The Player-Instance is used to handle audio-buffers and play with them.
const {Player} = require('g-sounds');
const p = new Player();
Load an audio-buffer. Resolve to decoded audio-buffer.
// buffer contains raw data obtained from `fs.readFile`
await p.loadBuffer(buffer);
Load an audio-buffer from given Array of Tuples.
//load a A4 in 60BPM
await p.loadBufferFromNotes([[440,1]], 60);
Use an Object instead to generate an audio with multiple channels
const channel = [[440,1]]; //load a A4
const channel2 = [[261,1]]; //load a C4
await p.loadBufferFromNotes({channel,channel2});
Play loaded audio-buffer. Resolves to Player
.
await p.play();
Save loaded audio-buffer to file path. (WAV-Files)
await p.saveFile('/path/to/save.wav')
Stop played sound. Resolves to Player
.
await p.stop();
The Oscillator is a class to handle easier with oscillator-waves.
const {Oscillator} = require('g-sounds');
const o = new Oscillator();
const o1 = new Oscillator();
samples
as default 44100
.
Get Oscillator containing Wave-Data of a given frequency.
o.create(261.6); //C4
o1.create(440); //A4
Add values of Oscillator to another one. Creating a new one.
const finalOsc = o.add(o1);
Get raw data of created Wave to send directly to any wav-encoder.
const raw = finalOsc.rawData();
The Note-Instance is helpful to get some frequency
const {Note} = require('g-sounds');
const n = new Note();
Get the frequency-value of a given note.
n.getFrequency('C4'); //261.6
Get the Note name of a given frequency.
n.getNote(261.61); // C4
or
n.getNote(261); // C4
Get Oscillator containing Wave-Data of a given frequency.
n.getOscillator(261.6); // the sin-wave values for C4
Saving a wav-file created from scratch
const {Player} = require('g-sounds');
const NOTES = [[440,1]]; //simple A
const p = new Player();
(async () => {
console.log('Loading Notes from array...');
await p.loadBufferFromNotes(NOTES);
await p.saveFile('test.wav');
console.log('File was written!');
process.exit(0);
})();
Print the notes of "Kleine Nachtmusik" from frequencies values.
const {Note}= require('g-sounds');
//first two bars of Mozart's "Kleine Nachtmusik"
const NOTES = [[783.99, 0.5], [0, 0.25], [587.33, 0.25], [783.99, 0.5], [0, 0.25], [587.33, 0.25], [783.99, 0.25], [587.33, 0.25], [783.99, 0.25], [987.77, 0.25], [1174.7, 0.25]];
const n = new Note();
for (const [frequency, duration] of NOTES) {
const noteName = n.getNote(frequency);
console.log(frequency, '=>', noteName);
}
// 783.99 '=>' 'G5'
// 0 '=>' 'pause'
// 587.33 '=>' 'D5'
// 783.99 '=>' 'G5'
// 0 '=>' 'pause'
// 587.33 '=>' 'D5'
// 783.99 '=>' 'G5'
// 587.33 '=>' 'D5'
// 783.99 '=>' 'G5'
// 987.77 '=>' 'B5'
// 1174.7 '=>' 'D6'
More examples here