1.0.0 • Published 7 years ago

electrophone v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

ElectroPhone

JavaScript Library to build a modular synthesizer.

Based on WebAudio API and WebAudio Effects Libray tuna

npm version Build Status Coverage Status

Demo Application

https://civa86.github.io/electrophone

HTML5 Application to demonstrate the use of electrophone library.

Frameworks and Libraries:

NameVersionLink
React15.4.1https://facebook.github.io/react/
Redux3.6.0http://redux.js.org/
jQuery1.12.11https://jquery.com/
jquery-knob1.2.11http://anthonyterrien.com/knob/
Cytoscape.js2.5.5http://js.cytoscape.org/
Moment.js2.13.0https://momentjs.com/
ReduxForm6.5.0http://redux-form.com/6.5.0/
Bootstrap3.3.6http://getbootstrap.com/
Ionicons2.0.1http://ionicons.com/

Development Tools:

NameVersionLink
Babel6.18.0https://babeljs.io/
ESLint1.10.3http://eslint.org/
Less2.5.3http://lesscss.org/
webpack1.13.3https://webpack.github.io/
Mocha3.2.0https://mochajs.org/
Chai3.5.0http://chaijs.com/
ESDoc0.4.7https://esdoc.org/
isparta4.0.0https://github.com/douglasduteil/isparta

Library Code Documentation

Documentation Page

Code Documentation of the ElectroPhone class provided by the Library.

For a full development documentation:

  • Download / Clone the Repository
  • Enter in the electrophone project folder
  • Run lib::docs npm script
  • Run docs/index.html in a browser

Usage

Install npm module

npm install electrophone

Include Library

With ES5 via <script> tag:

<script src="<path_to_node_modules>/electrophone/dist/electrophone.min.js"/>

With ES2015 via Babel:

import ElectroPhone from 'electrophone'

Create an instance

// Get the browser AudioContext
var AudioCtx = window.AudioContext || window.webkitAudioContext;

// CREATE A ElectroPhone INSTANCE
var synth = new ElectroPhone(new AudioCtx(), {
        //Optional parameters
        spectrum: <true|false>,            // set true to manage data of playing sound
        updateSpectrum: <dataArray => {}>, // on receive data callback
        resetSpectrum: <() => {}>          // on stop data callback
    });

Create Modules

synth.create("moduleLabel", "moduleType", { ...properties });

Update Modules

synth.update("moduleLabel", { ...properties });

Destroy Modules

synth.destroy("moduleLabel");

Link modules

synth.link("sourceModuleLabel", "destinationModuleLabel");

Update Prebuilt Modules

// Master module
synth.master({ ...properties });

// ADSR module
synth.adsr({ ...properties });

Play and Stop sound

// Play a frequency (20 - 20000)
synth.play(440);

// Stop a frequency (20 - 20000)
synth.stop(440);

Get All Created Modules

synth.getModules();

Static Methods

// Get module properties by type.
ElectroPhone.getModuleProperties("moduleType");

// Describe all ElectroPhone modules with properties configuration.
ElectroPhone.describeModules("moduleType");

// Get frequency float value calculated from given note and octave.
ElectroPhone.getFrequency("A", 4);

// Get complete notes list.
ElectroPhone.getNotes();

Constants

The library expose two set of constants: CONST and TYPES

CONST

The set of string values to setup module properties.

CONSTDescription
MASTERthe Master module label
ADSRthe ADSR module label
NOISE_WHITEthe white Noise color value
NOISE_PINKthe pink Noise color value
NOISE_BROWNthe brown Noise color value
WAVE_SINEthe sine Wave type value
WAVE_SQUAREthe square Wave type value
WAVE_SAWTOOTHthe sawtooth Wave type value
WAVE_TRIANLGEthe triangle Wave type value
WAVE_CUSTOMthe custom Wave type value
FILTER_LOWPASSthe lowpass Filter type value
FILTER_HIGHPASSthe highpass Filter type value
FILTER_BANDPASSthe bandpass Filter type value
FILTER_LOWSHELFthe lowshelf Filter type value
FILTER_HIGHSHELFthe highshelf Filter type value
FILTER_PEAKINGthe peaking Filter type value
FILTER_NOTCHthe notch Filter type value
FILTER_ALLPASSthe allpass Filter type value
MODULATOR_TARGET_FREQthe frequency Modulator target value
MODULATOR_TARGET_DETUNEthe detune Modulator target value
ENVELOPE_TARGET_GAINthe gain Envelope target value
ENVELOPE_TARGET_FREQthe frequency Envelope target value
ENVELOPE_TARGET_DETUNEthe detune Envelope target value

TYPES

The set of string values to create modules.

TYPESUsage
OSCILLATORsynth.create('label', ElectroPhone.TYPES.OSCILLATOR, {})
NOISEsynth.create('label', ElectroPhone.TYPES.NOISE, {})
MODULATORsynth.create('label', ElectroPhone.TYPES.MODULATOR, {})
ENVELOPEsynth.create('label', ElectroPhone.TYPES.ENVELOPE, {})
PANsynth.create('label', ElectroPhone.TYPES.PAN, {})
FILTERsynth.create('label', ElectroPhone.TYPES.FILTER, {})
DELAYsynth.create('label', ElectroPhone.TYPES.DELAY, {})
PINGPONGDELAYsynth.create('label', ElectroPhone.TYPES.PINGPONGDELAY, {})
TREMOLOsynth.create('label', ElectroPhone.TYPES.TREMOLO, {})
OVERDRIVEsynth.create('label', ElectroPhone.TYPES.OVERDRIVE, {})
BITCRUSHERsynth.create('label', ElectroPhone.TYPES.BITCRUSHER, {})
MOOGFILTERsynth.create('label', ElectroPhone.TYPES.MOOGFILTER, {})

Full Example: Two Voices Synth

// Load library
import ElectroPhone from 'electrophone';

// Build the synth instance (See Usage Section)
const AudioCtx = window.AudioContext || window.webkitAudioContext;
const synth = new ElectroPhone(new AudioCtx());

// Create the first voice
synth.create(
    'Voice1',
    ElectroPhone.TYPES.OSCILLATOR,
    {
        wave: ElectroPhone.CONST.WAVE_SAWTOOTH,
        detune: 500,
        level: 50
    }
);

// Create the second voice
synth.create(
    'Voice2',
    ElectroPhone.TYPES.OSCILLATOR,
    {
        wave: ElectroPhone.CONST.WAVE_SAWTOOTH,
        detune: -500,
        level: 50
    }
);

//Link voices to Master
synth.link('Voice1', ElectroPhone.CONST.MASTER);
synth.link('Voice2', ElectroPhone.CONST.MASTER);

// Setup ADSR
synth.adsr({ attack: 0, decay: 1, sustain: 50, release: 25 });

// Set Master Volume to 80%
synth.master({ level: 80 });

// Get the A4 note frequency
const a4 = ElectroPhone.getFrequency("A", 4);

// Play the a4 note
synth.play(a4);

// Stop the a4 note after 1 second
setTimeout(() => synth.stop(a4), 1000);

Prebuilt Modules

Every ElectroPhone instance has 2 hardcoded modules: Master and ADSR.

This is the minimal configuration to let sound coming out from your sound card and control its behaviour during time.

Master

Main Output Gain.

PropertyTypeValuesDefault
levelInteger0 - 100100

ADSR

Envelope on the master gain. Describes final sound behavior during time.

PropertyTypeValuesDefault
attackInteger0 - 1000
decayInteger0 - 1001
sustainInteger0 - 100100
releaseInteger0 - 1005

Sound Modules

Sound Modules can generate sounds and play frequencies.

They can be linked to any Effect Module or directly to Master

Oscillator

Sound Wave Generator.

PropertyTypeValuesDefault
levelInteger0 - 100100
detuneInteger-1200 - 12000
waveStringsine, square, sawtooth, triangle, customsine
linkStringmaster, "any module label"-

Noise

Noise Generator.

PropertyTypeValuesDefault
levelInteger0 - 100100
detuneInteger-1200 - 12000
colorStringwhite, brown, pinkwhite
linkStringmaster, "any module label"-

Control Modules

Control Modules can change the behavior of a specific module property or modify the final sound.

Modulator

An Oscillator that produces modulation on another module property.

It can be linked to any module with freq or detune property.

PropertyTypeValuesDefault
levelInteger0 - 100100
freqInteger1 - 1005
waveStringsine, square, sawtooth, triangle, customsine
targetStringfrequency, detunefrequency
linkStringmaster, "any module label"-

Envelope

A module that can describe property changes during time.

It can be linked to any module with level property (gain target).

PropertyTypeValuesDefault
levelInteger0 - 100100
attackInteger0 - 1000
decayInteger0 - 1001
sustainInteger0 - 100100
releaseInteger0 - 1005
targetStringfrequency, detune, gaingain
linkStringmaster, "any module label"-

Pan

A module that routes sound between left and right channel.

It can be placed between a Sound Module and its destination to setup stereo routing.

PropertyTypeValuesDefault
levelInteger0 - 100100
panFloat-1 - 10
linkStringmaster, "any module label"-

Effect Modules

Effect Modules can change the nature of a sound.

They can be linked to other Effect Modules to create an effect chain and finally to Master.

Filter

A module that filters frequencies with different algorithms.

PropertyTypeValuesDefault
levelInteger0 - 100100
freqInteger20 - 20000440
qInteger0 - 10010
filterGainInteger-40 - 400
filterTypeStringlowpass, highpass, bandpass, lowshelf, highshelf, peaking, notch, allpasslowpass
bypassFlag0 / 10
linkStringmaster, "any module label"-

Delay

A module that plays a sound back after a period of time.

PropertyTypeValuesDefault
levelInteger0 - 100100
dryFloat0 - 11
wetFloat0 - 10.5
feedbackFloat0 - 0.90.4
cutoffInteger20 - 20000440
delayTimeInteger20 - 1000100
bypassFlag0 / 10
linkStringmaster, "any module label"-

PingPongDelay

A different kind of Delay.

PropertyTypeValuesDefault
levelInteger0 - 100100
wetFloat0 - 10.5
feedbackFloat0 - 10.3
cutoffInteger20 - 20000440
delayTimeLeftInteger1 - 10000200
delayTimeRightInteger1 - 10000400
bypassFlag0 / 10
linkStringmaster, "any module label"-

Tremolo

A trembling sound effect.

PropertyTypeValuesDefault
levelInteger0 - 100100
intensityFloat0 - 10.3
rateFloat0 - 115
stereoPhaseInteger0 - 1800
bypassFlag0 / 10
linkStringmaster, "any module label"-

Overdrive

A module that alters the sound increasing its gain with distortion.

PropertyTypeValuesDefault
levelInteger0 - 100100
outputGainFloat0 - 11
driveFloat0 - 11
curveAmountFloat0 - 10.7
algorithmIndexInteger0 - 50
bypassFlag0 / 10
linkStringmaster, "any module label"-

Bitcrusher

A module that produces a distortion by the reduction of the resolution or bandwidth of digital audio data.

PropertyTypeValuesDefault
levelInteger0 - 100100
bitsInteger1 - 164
normfreqFloat0 - 10.1
bufferSizeInteger256 - 163844096
bypassFlag0 / 10
linkStringmaster, "any module label"-

Moogfilter

A Moog inspired filter

PropertyTypeValuesDefault
levelInteger0 - 100100
cutoffFloat0 - 10.1
resonanceFloat0 - 43.5
bufferSizeInteger256 - 163844096
bypassFlag0 / 10
linkStringmaster, "any module label"-