A few days ago I posted this random idea to Twitter
Random project idea: The Disintegrator - Load a track and slowly add more and more harmonic distortion until it is just white noise!
— AudioGrains (@AudioGrainsBlog) May 15, 2014
So naturally I went ahead and built a reasonable approximation using my new Web Audio API powers.
The Web Audio API has a built-in module for creating distortion modules called createWaveShaper
which takes two optional attributes- curve
and overSample
. A null value for curve
applies no distortion, whereas an array containing a distortion curve function (like this one – look at distortion curve equations you can see page 74 of this PDF for an equation and matlab implementation.
Creating the waveShaper
node is easily accomplished just by calling the createWaveShaper
method
VolumeSample.distNode = context.createWaveShaper();
This creates a waveShaper
node with a null value for curve
. We can add a distortion curve by calling it with the curve
attribute.
VolumeSample.distNode.curve = makeDistortionCurve(50);
(where makeDistortionCurve
is from the code linked above)
We can then use setInterval
to update the curve every 1000 milliseconds to increase the distortion.
setInterval(update,1000);
function update(){ VolumeSample.distNode.curve = makeDistortionCurve(updateIndex++); }
We then chain our nodes together to connect the source to the destination
source.connect(this.distNode); this.distNode.connect(this.gNode); this.gNode.connect(context.destination);
Try it out for yourself. Load any track (give it a second to load fully) and listen to it disintegrate. Now I wonder what Michael Gordon’s ‘Industry’ would sound like?
UPDATE: It sounds great!
I’m having a bizarre issue…when I try to load some longer classical pieces, what comes out is the first two seconds of “And Your Bird Can Sing” by the Beatles. I swear I am not making this up. I have no idea what it could be.
Oh actually that’s the default audio snippet that loads with the page to load something into the buffer. With longer tracks it takes a while to load the new buffer. I guess a ‘loading’ progress message would be good!
Distortion level might be better off as a percentage indicator. I have no idea how progressed a ‘distortion level’ of 200 is, but that’s where my song ended.
I’m hoping to change that pretty soon actually. Right now the distortion level is the variable of the equation that controls the shape of the distortion curve, but it would definitely be better to have the distortion vary proportional to the length of the song (so shorter songs would still hit the ‘full distortion’ level) so a percentage would be the correct way to reflect that!