0.3.3 • Published 4 years ago

g-sounds v0.3.3

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

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} = 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();
})();

Usage

gSounds.js expose following modules: Player,Note,Oscillator.

const {Player, Note, Oscillator} = require('g-sounds');

Player

The Player-Instance is used to handle audio-buffers and play with them.

const {Player} = require('g-sounds');
const p = new Player();

.loadBuffer( Buffer: audioBuffer ) : Promise

Load an audio-buffer. Resolve to decoded audio-buffer.

// buffer contains raw data obtained from `fs.readFile`
await p.loadBuffer(buffer);

.loadBufferFromNotes( Array|Object:Notes, Int:BPM ) : Promise

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() : Promise

Play loaded audio-buffer. Resolves to Player.

await p.play();

.saveFile( String: filePath ) : Promise

Save loaded audio-buffer to file path. (WAV-Files)

await p.saveFile('/path/to/save.wav')

.stop() : Promise

Stop played sound. Resolves to Player.

await p.stop();

Oscillator

The Oscillator is a class to handle easier with oscillator-waves.

constructor( Int:samples ): Oscillator

const {Oscillator} = require('g-sounds');
const o = new Oscillator();
const o1 = new Oscillator();

samples as default 44100.

create( Float:frequency, Int:samplingRate, Int:amplitude ) : Oscillator

Get Oscillator containing Wave-Data of a given frequency.

o.create(261.6); //C4
o1.create(440); //A4

add( Oscillator:otherOscillator ) : Oscillator

Add values of Oscillator to another one. Creating a new one.

const finalOsc = o.add(o1);

rawData() : Float32Array

Get raw data of created Wave to send directly to any wav-encoder.

const raw = finalOsc.rawData();

Note

The Note-Instance is helpful to get some frequency

const {Note} = require('g-sounds');
const n = new Note();

.getFrequency( String: note ) : Number|undefined

Get the frequency-value of a given note.

n.getFrequency('C4'); //261.6

.getNote( Float|Int: frequency) : String|undefined

Get the Note name of a given frequency.

n.getNote(261.61); // C4

or

n.getNote(261); // C4

.getOscillator( Float:frequency, Number:samplingRate, Int:samples, Int:amplitude ) : Oscillator

Get Oscillator containing Wave-Data of a given frequency.

n.getOscillator(261.6); // the sin-wave values for C4

Examples

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

0.3.2

4 years ago

0.3.1

4 years ago

0.3.3

4 years ago

0.3.0

4 years ago

0.1.0

4 years ago

0.2.0

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago