mirror of
https://github.com/Tonejs/Tone.js
synced 2025-01-13 20:39:06 +00:00
NoiseSynth
This commit is contained in:
parent
c67493478d
commit
ab28ad9a99
3 changed files with 177 additions and 2 deletions
|
@ -22,6 +22,7 @@ Synths:
|
|||
* Setters on PluckSynth and PulseOscillator
|
||||
* new PWMOscillator
|
||||
* OmniOscillator which combines PWMOscillator, Oscillator, and PulseOscillator into one
|
||||
* NoiseSynth
|
||||
|
||||
|
||||
### r2 - Getting Physical
|
||||
|
|
143
Tone/instrument/NoiseSynth.js
Normal file
143
Tone/instrument/NoiseSynth.js
Normal file
|
@ -0,0 +1,143 @@
|
|||
define(["Tone/core/Tone", "Tone/component/AmplitudeEnvelope", "Tone/component/ScaledEnvelope",
|
||||
"Tone/source/Noise", "Tone/signal/Signal", "Tone/component/Filter", "Tone/instrument/Instrument"],
|
||||
function(Tone){
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* @class the NoiseSynth is a single oscillator, monophonic synthesizer
|
||||
* with a filter, and two envelopes (on the filter and the amplitude)
|
||||
*
|
||||
* @constructor
|
||||
* @extends {Tone.Instrument}
|
||||
* @param {Object} options the options available for the synth
|
||||
* see defaults below
|
||||
*/
|
||||
Tone.NoiseSynth = function(options){
|
||||
|
||||
//get the defaults
|
||||
options = this.defaultArg(options, Tone.NoiseSynth.defaults);
|
||||
Tone.Instrument.call(this);
|
||||
|
||||
/**
|
||||
* the noise source
|
||||
* @type {Tone.Noise}
|
||||
*/
|
||||
this.noise = new Tone.Noise();
|
||||
|
||||
/**
|
||||
* the filter
|
||||
* @type {Tone.Filter}
|
||||
*/
|
||||
this.filter = new Tone.Filter(options.filter);
|
||||
|
||||
/**
|
||||
* the filter envelope
|
||||
* @type {Tone.Envelope}
|
||||
*/
|
||||
this.filterEnvelope = new Tone.ScaledEnvelope(options.filterEnvelope);
|
||||
|
||||
/**
|
||||
* the amplitude envelope
|
||||
* @type {Tone.Envelope}
|
||||
*/
|
||||
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
|
||||
|
||||
//connect the noise to the output
|
||||
this.chain(this.noise, this.filter, this.envelope, this.output);
|
||||
//start the noise
|
||||
this.noise.start();
|
||||
//connect the filter envelope
|
||||
this.filterEnvelope.connect(this.filter.frequency);
|
||||
};
|
||||
|
||||
Tone.extend(Tone.NoiseSynth, Tone.Instrument);
|
||||
|
||||
/**
|
||||
* @const
|
||||
* @static
|
||||
* @type {Object}
|
||||
*/
|
||||
Tone.NoiseSynth.defaults = {
|
||||
"noise" : {
|
||||
"type" : "white"
|
||||
},
|
||||
"filter" : {
|
||||
"Q" : 6,
|
||||
"type" : "highpass",
|
||||
"rolloff" : -24
|
||||
},
|
||||
"envelope" : {
|
||||
"attack" : 0.005,
|
||||
"decay" : 0.1,
|
||||
"sustain" : 0.0,
|
||||
},
|
||||
"filterEnvelope" : {
|
||||
"attack" : 0.06,
|
||||
"decay" : 0.2,
|
||||
"sustain" : 0,
|
||||
"release" : 2,
|
||||
"min" : 20,
|
||||
"max" : 4000,
|
||||
"exponent" : 2
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* start the attack portion of the envelope
|
||||
* @param {Tone.Time=} [time=now] the time the attack should start
|
||||
* @param {number=} velocity the velocity of the note (0-1)
|
||||
*/
|
||||
Tone.NoiseSynth.prototype.triggerAttack = function(time, velocity){
|
||||
//the envelopes
|
||||
this.envelope.triggerAttack(time, velocity);
|
||||
this.filterEnvelope.triggerAttack(time);
|
||||
};
|
||||
|
||||
/**
|
||||
* start the release portion of the envelope
|
||||
* @param {Tone.Time=} [time=now] the time the release should start
|
||||
*/
|
||||
Tone.NoiseSynth.prototype.triggerRelease = function(time){
|
||||
this.envelope.triggerRelease(time);
|
||||
this.filterEnvelope.triggerRelease(time);
|
||||
};
|
||||
|
||||
/**
|
||||
* set the oscillator type
|
||||
* @param {string} oscType the type of oscillator
|
||||
*/
|
||||
Tone.NoiseSynth.prototype.setNoiseType = function(type){
|
||||
this.noise.setType(type);
|
||||
};
|
||||
|
||||
/**
|
||||
* set the members at once
|
||||
* @param {Object} params all of the parameters as an object.
|
||||
* params for envelope and filterEnvelope
|
||||
* should be nested objects.
|
||||
*/
|
||||
Tone.NoiseSynth.prototype.set = function(params){
|
||||
if (!this.isUndef(params.noise)) this.noise.set(params.noise);
|
||||
if (!this.isUndef(params.filterEnvelope)) this.filterEnvelope.set(params.filterEnvelope);
|
||||
if (!this.isUndef(params.envelope)) this.envelope.set(params.envelope);
|
||||
if (!this.isUndef(params.filter)) this.filter.set(params.filter);
|
||||
};
|
||||
|
||||
/**
|
||||
* clean up
|
||||
*/
|
||||
Tone.NoiseSynth.prototype.dispose = function(){
|
||||
Tone.Instrument.prototype.dispose.call(this);
|
||||
this.noise.dispose();
|
||||
this.noise = null;
|
||||
this.envelope.dispose();
|
||||
this.envelope = null;
|
||||
this.filterEnvelope.dispose();
|
||||
this.filterEnvelope = null;
|
||||
this.filter.dispose();
|
||||
this.filter = null;
|
||||
};
|
||||
|
||||
return Tone.NoiseSynth;
|
||||
});
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
define(["tests/Core", "chai", "Tone/instrument/DuoSynth", "Tone/instrument/MonoSynth", "Tone/instrument/FMSynth",
|
||||
"Tone/instrument/PolySynth", "Tone/instrument/Sampler", "Tone/instrument/MultiSampler",
|
||||
"tests/Common", "Tone/instrument/Instrument", "Tone/instrument/PluckSynth", "Tone/instrument/AMSynth"],
|
||||
"tests/Common", "Tone/instrument/Instrument", "Tone/instrument/PluckSynth", "Tone/instrument/AMSynth",
|
||||
"Tone/instrument/NoiseSynth"],
|
||||
function(Tone, chai, DuoSynth, MonoSynth, FMSynth, PolySynth, Sampler, MultiSampler, Test, Instrument,
|
||||
PluckSynth, AMSynth){
|
||||
PluckSynth, AMSynth, NoiseSynth){
|
||||
|
||||
var expect = chai.expect;
|
||||
|
||||
|
@ -243,4 +244,34 @@ function(Tone, chai, DuoSynth, MonoSynth, FMSynth, PolySynth, Sampler, MultiSamp
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Tone.NoiseSynth", function(){
|
||||
it("can be created and disposed", function(){
|
||||
var noiseSynth = new NoiseSynth();
|
||||
noiseSynth.dispose();
|
||||
Test.wasDisposed(noiseSynth);
|
||||
});
|
||||
|
||||
it("extends Instrument", function(){
|
||||
extendsInstrument(NoiseSynth);
|
||||
});
|
||||
|
||||
it("handles output connections", function(){
|
||||
var noiseSynth = new NoiseSynth();
|
||||
Test.acceptsOutput(noiseSynth);
|
||||
noiseSynth.dispose();
|
||||
});
|
||||
|
||||
it("outputs a sound", function(done){
|
||||
var noiseSynth;
|
||||
Test.outputsAudio(function(dest){
|
||||
noiseSynth = new NoiseSynth();
|
||||
noiseSynth.connect(dest);
|
||||
noiseSynth.triggerAttack();
|
||||
}, function(){
|
||||
noiseSynth.dispose();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue