Pizzicato aims to simplify the way you create and manipulate sounds via the Web Audio API. Take a look at the demo site here.
## Create a sound
To create a new sound, use the ```Pizzicato.Sound``` constructor, which takes an object with the sound's ```description``` as argument and a callback that will be executed when the sound is ready to be used. If an error occurs, the callback will be called with the error as a parameter.
```javascript
var sound = new Pizzicato.Sound(Object description, [Function callback]);
```
For example:
```javascript
var click = new Pizzicato.Sound({ source: 'wave' }, function(error) {
if (!error)
console.log('Sound ready to use!');
});
```
Typically, the description
object contains a string source
and an object options
. The options
object varies depending on the source of the sound being created.
For example, this objects describes a sine waveform with a frequency of 440:
{
source: 'wave',
options: {
type: 'sine',
frequency: 440
}
}
Sounds can be created from a variety of sources.
### Sounds from a wave ([example](https://alemangui.github.io/pizzicato/#sound-from-waveform))
To create a sound from an oscillator with a certain waveform, use the ```source: wave``` in your description. Additionally, the following optional parameters are possible inside the ```options``` object:
* ```type``` _(Optional; ```sine```, ```square```, ```triangle``` or ```sawtooth```, defaults to ```sine```)_: Specifies the type of waveform.
* ```frequency``` _(Optional; defaults to 440)_: Indicates the frequency of the wave (i.e., a 440 value will yield an A note).
* ```volume``` _(Optional; min: 0, max: 1, defaults to 1)_: Loudness of the sound.
* ```release``` _(Optional; defaults to 0.4)_: Value in seconds that indicates the fade-out time when the sound is stopped.
* ```attack``` _(Optional; defaults to 0.4)_: Value in seconds that indicates the fade-in time when the sound is played.
* ```detached``` _(Optional; defaults to false)_: If true, the sound will not be connected to the context's destination, and thus, will not be audible.
var sound = new Pizzicato.Sound({
source: 'wave',
options: { type: 'sawtooth', frequency: 440 }
});
Creating a Pizzicato Sound with an empty constructor will create a sound with a sine wave and a frequency of 440.
var sound = new Pizzicato.Sound();
### Sounds from a file ([example](https://alemangui.github.io/pizzicato/#sound-from-file))
In order to load a sound from a file, include the ```source: file``` in your description. Additionally, the following parameters are possible inside the ```options``` object:
* ```path``` _(Mandatory; string or array of strings)_: Specifies the path of the sound file. It is also possible to have an array of paths to fallback on. Pizzicato will attempt to load the paths in order, passing on to the next one in case of failure.
* ```loop``` _(Optional; boolean, defaults to false)_: If set, the file will start playing again after the end.
* ```volume``` _(Optional; min: 0, max: 1, defaults to 1)_: Loudness of the sound.
* ```release``` _(Optional; defaults to 0)_: Value in seconds that indicates the fade-out time once the sound is stopped.
* ```attack``` _(Optional; defaults to 0.4)_: Value in seconds that indicates the fade-in time when the sound is played.
* ```detached``` _(Optional; defaults to false)_: If true, the sound will not be connected to the context's destination, and thus, will not be audible.
var sound = new Pizzicato.Sound({
source: 'file',
options: { path: './audio/sound.wav' }
}, function() {
console.log('sound file loaded!');
});
It is possible to pass several paths to fallback in case of error:
var sound = new Pizzicato.Sound({
source: 'file',
options: { path: ['./audio/sound-special-format.wav', './audio/sound.wav'] }
}, function() {
console.log('sound file loaded!');
});
Alternatively, you can also simply pass a string to the constructor with the path of the sound file.
var sound = new Pizzicato.Sound('./audio/sound.wav', function() {...});
### Compressor ([example](https://alemangui.github.io/pizzicato/#compressor))
A compressor allows reducing the range between the loudest and the quietest parts of a sound. This is done by boosting the quiet segments and attenuating the loud ones.
The following options are available when creating a compressor effect:
threshold
(min: -100, max: 0, defaults to -24): The decibel value above which the compression will start taking effect.knee
(min: 0, max: 40, defaults to 30): A value representing the range above the threshold where the curve smoothly transitions to the "ratio" portion.attack
(min: 0, max: 1, defaults to 0.003): How soon the compressor starts to compress the dynamics after the threshold is exceeded. Short values will result in a fast response to sudden, loud sounds, but will make the changes in volume more obvious to listeners.release
(min: 0, max: 1, defaults to 0.025): How soon the compressor starts to release the volume level back to normal after the level drops below the threshold.ratio
(min: 1, max: 20, defaults to 12): The amount of compression applied to the audio once it passes the threshold level. The higher the Ratio the more the loud parts of the audio will be compressed.mix
(min: 0, max: 1, defaults to 0.5): Volume balance between the original audio and the effected output.
Example:
var compressor = new Pizzicato.Effects.Delay({
threshold: -20,
knee: 22,
attack: 0.05,
release: 0.05,
ratio: 18
});
sound.addEffect(compressor);
sound.play();
### Reverb ([example](https://alemangui.github.io/pizzicato/#reverb))
The reverb effect is similar to the convolver effect in that it allows the sound to be heard with a certain ressonance or repercussion. This simulates a particular physical environment in which the sound could be played (e.g., an auditorium, a concert hall, etc).
Unlike the convolver effect, the reverb can be adjusted programatically without the need for any external elements.
time
(min: 0.0001, max: 10, defaults to 0.01): Duration of impulse, in seconds.decay
(min: 0, max: 10, defaults to 0.01): The rate for the reflections of sound to fade away.reverse
(boolean): Whether or not to reverse the impulse shape.mix
(min: 0, max: 1, defaults to 0.5): Volume balance between the original audio and the effected output.
Example:
var reverb = new Pizzicato.Effects.Reverb({
time: 1,
decay: 0.8,
reverse: true,
mix: 0.5
});
sound.addEffect(reverb);
sound.play();