[flocking] Possible bug in flocking.

Colin Clark colin at colinclark.org
Sat Mar 14 19:13:48 EDT 2015

Hi Derek,

The issue with your code is that you have created an infinite loop. If you look at the while statement inside makeHarmonics, the break-out condition is when the frequency of the last-generated harmonic is greater than or equal to the "maxFreq." But you've wrapped the unit generator in a biquad filter, which you've configured to always have a frequency of 500 Hz, and you're using this to compare with. So it never breaks out of the while loop.

While this isn't a bug in Flocking itself, it's definitely a sign that this "Additive Synthesis" demo that you have copy and pasted from isn't very effective. I originally wrote this example many years ago solely as a stress test for Flocking. It's a very poor example of additive synthesis, and is written in a way that is prone to errors like this. Here's a much simpler example that you might find helpful:

https://gist.github.com/colinbdclark/3a296e455798e78af464 <https://gist.github.com/colinbdclark/3a296e455798e78af464>

In terms of what you want to do more generally, it might help to describe the kind of sound you want first. There are lots of different synthesis strategies that you can use to create "decent-sounding tones," but the first step is to be able to describe the characteristics of the sound so you can pick the technique that works best. Additive synthesis is one way, but frequency modulation is another popular technique and can be very efficient. There's also granular synthesis which can produce some cool types of sounds, or you can trigger the playback of audio samples you've recorded. Lots of options!

In terms of learning more about these techniques, there are some good books and tutorials out there. Gordon Reid's Synths Secrets articles in Sound on Sound are quite interesting. Here are his tutorials on additive and FM synthesis:

http://www.soundonsound.com/sos/jun00/articles/synthsec.htm <http://www.soundonsound.com/sos/jun00/articles/synthsec.htm>
http://www.soundonsound.com/sos/apr00/articles/synthsecrets.htm <http://www.soundonsound.com/sos/apr00/articles/synthsecrets.htm>

I really like this book by Charles Dodge and Thomas Jerse, but it's out of print now. Looks like you might be able to get it used on Amazon or at the CU Boulder music library:

http://www.amazon.com/Computer-Music-Synthesis-Composition-Performance/dp/0028646827/ref=sr_1_4?ie=UTF8&qid=1426351002&sr=8-4&keywords=computer+music <http://www.amazon.com/Computer-Music-Synthesis-Composition-Performance/dp/0028646827/ref=sr_1_4?ie=UTF8&qid=1426351002&sr=8-4&keywords=computer+music>

http://encore.colorado.edu/iii/encore/record/C__Rb2780003__Scomputer%20music%20dodge%20jerse__Orightresult__U__X2?lang=eng&suite=cobalt <http://encore.colorado.edu/iii/encore/record/C__Rb2780003__Scomputer%20music%20dodge%20jerse__Orightresult__U__X2?lang=eng&suite=cobalt>

I hope this helps,


> On Mar 13, 2015, at 11:48 PM, derek riemer <Derek.Riemer at Colorado.EDU> wrote:
> I don't know why, but this code seems to cause browsers to hang and then kill the current script in the playground.
> What I was trying to do is make a decent sounding tone that would be easy to listen to with the sym we are doing. A pure sin wave works, but it isn't as nice on the ears as what types of sounds we have with instruments. Anyway, the code is below. 
> Thanks,
> Derek
>         // A bank of oscillators added together filtered with a band filter.
>         var harmonics = [1, 4, 5, 6, 7, 8],
>             ugenTypes = ["flock.ugen.saw", "flock.ugen.saw"],
>             fundamentalMultiplier = 440,
>             maxFreq = flock.enviro.shared.audioSettings.rates.audio / 4; // Highest harmonic shouldn't be more than a quarter of the Nyquist frequency.
>         var makeHarmonic = function (ugenTypes, fundamental, harmonic, octave, maxAmp) {
>             var freqScale = (harmonic * octave),
>                 ugen = flock.choose(ugenTypes);
>             return {
>                 ugen: "flock.ugen.filter.biquad.bp",
>                 freq:500,
>                 q: 3.0,
>                 source: {
>                     ugen: ugen,
>                     freq: fundamental * freqScale,
>                     mul: maxAmp / freqScale
>                 }
>             };
>         };
>         var makeHarmonics = function (fundamental) {
>             var sources = [],
>                 freqs = [];
>             $.each(harmonics, function (i, harmonic) {
>                 var freq = fundamental,
>                     octave = 1;
>                 while (freq <= maxFreq) {
>                     var ugenDef = makeHarmonic(ugenTypes, fundamental, harmonic, octave, 0.1);
>                     freq = ugenDef.freq;
>                     if (freq <= maxFreq && freqs.indexOf(freq) === -1) {
>                         freqs.push(freq);
>                         sources.push(ugenDef);
>                     }
>                     octave++;
>                 }
>             });
>             return sources;
>         };
>         var synth = flock.synth({
>             synthDef: {
>                 id: "adder",
>                 ugen: "flock.ugen.sum",
>                 sources: makeHarmonics(fundamentalMultiplier)
>             }
>         });
>         synth.enviro.asyncScheduler.repeat(0.5, function () {
>             var fundamental = (fundamentalMultiplier * Math.random()) + 60;
>             synth.input("adder.sources", makeHarmonics(fundamental));
>         });
> -- 
> Derek Riemer
> Department of Computer science Undergrad, proud CILA member, music lover, avid skier, and much more.
> "Not everything that is faced can be changed, but nothing can be changed until it is faced." - James Baldwin
> email me at derek.riemer at colorado.edu <mailto:derek.riemer at colorado.edu> 
> Phone: (303) 906-2194
> _______________________________________________
> flocking mailing list
> flocking at lists.idrc.ocad.ca
> http://lists.idrc.ocad.ca/cgi-bin/mailman/listinfo/flocking

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idrc.ocad.ca/pipermail/flocking/attachments/20150314/2c334256/attachment.html>

More information about the flocking mailing list