npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

g-sounds

v0.3.3

Published

Generate/Compose & Play Sounds from scratch.

Downloads

34

Readme

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