gSounds.js
!!BETA!!
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...
Quick Start
Play the first two bars of Mozart's "Kleine Nachtmusik"
const Player = ; //[frequency,duration]const NOTES = 78399 05 0 025 58733 025 78399 05 0 025 58733 025 78399 025 58733 025 78399 025 98777 025 11747 025; const p = ;async { console; await p; console; await p;};
Usage
gSounds.js expose following modules: Player
,Note
,Oscillator
.
const Player Note Oscillator = ;
Player
The Player-Instance is used to handle audio-buffers and play with them.
const Player = ;const p = ;
.loadBuffer( Buffer: audioBuffer ) : Promise
Load an audio-buffer. Resolve to decoded audio-buffer.
// buffer contains raw data obtained from `fs.readFile`await p;
.loadBufferFromNotes( Array|Object:Notes, Int:BPM ) : Promise
Load an audio-buffer from given Array of Tuples.
//load a A4 in 60BPMawait p;
Use an Object instead to generate an audio with multiple channels
const channel = 4401; //load a A4const channel2 = 2611; //load a C4await p;
.play() : Promise
Play loaded audio-buffer. Resolves to Player
.
await p;
.saveFile( String: filePath ) : Promise
Save loaded audio-buffer to file path. (WAV-Files)
await p
.stop() : Promise
Stop played sound. Resolves to Player
.
await p;
Oscillator
The Oscillator is a class to handle easier with oscillator-waves.
constructor( Int:samples ): Oscillator
const Oscillator = ;const o = ;const o1 = ;
samples
as default 44100
.
create( Float:frequency, Int:samplingRate, Int:amplitude ) : Oscillator
Get Oscillator containing Wave-Data of a given frequency.
o; //C4o1; //A4
add( Oscillator:otherOscillator ) : Oscillator
Add values of Oscillator to another one. Creating a new one.
const finalOsc = o;
rawData() : Float32Array
Get raw data of created Wave to send directly to any wav-encoder.
const raw = finalOsc;
Note
The Note-Instance is helpful to get some frequency
const Note = ;const n = ;
.getFrequency( String: note ) : Number|undefined
Get the frequency-value of a given note.
n; //261.6
.getNote( Float|Int: frequency) : String|undefined
Get the Note name of a given frequency.
n; // C4
or
n; // C4
.getOscillator( Float:frequency, Number:samplingRate, Int:samples, Int:amplitude ) : Oscillator
Get Oscillator containing Wave-Data of a given frequency.
n; // the sin-wave values for C4
Examples
Saving a wav-file created from scratch
const Player = ; const NOTES = 4401; //simple A const p = ;async { console; await p; await p; console; process;};
Print the notes of "Kleine Nachtmusik" from frequencies values.
const Note= ; //first two bars of Mozart's "Kleine Nachtmusik"const NOTES = 78399 05 0 025 58733 025 78399 05 0 025 58733 025 78399 025 58733 025 78399 025 98777 025 11747 025; const n = ; for const frequency duration of NOTES const noteName = n; console; // 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