Using Manipulate to vary the frequency of a sound produced by SoundNote[]

I have tried various ways to combine Manipulate and Sound/SoundNote that would allow me to vary the frequency of a sound while it is being produced. The addition of Dynamic didn't help, but did increase the variety of error messages. Is this just not possible?

Manipulate[EmitSound[Sound[SoundNote[n, 3]]], {n, 0, 10}]


My goal to listen to, or observe, the beat frequencies produced as a tone becomes closer to or further away from twice the frequency of a second, steady tone. My underlying issue is to develop insight into how original idea of an octave.

-
It is not possible to vary the frequency of a SoundNote[] continuously in Mathematica. The underlying technology, MIDI, does support this through sending "pitch bend" events (what the modwheel on many keyboards does), but Mathematica doesn't support this. I'm not sure if you can have a continuous variation if you use a wave output (not MIDI). –  Szabolcs Mar 10 '13 at 17:45
If I had to do this, I'd use a different program, not Mathematica. You could try PureData, which is free (but not trivial to set up). Perhaps you can even connect another program to Mathematica and let Mathematica control it. –  Szabolcs Mar 10 '13 at 19:21
You could also try ChucK –  Szabolcs Mar 10 '13 at 19:23
I'll try ChucK. I imagined Manipulate would do for sound what it does for color: Manipulate[Graphics[{RGBColor[r, 0, 0], Disk[]}], {r, 0, 1}]. I can imagine the underlying issues are not to same, but it doesn't seem that they would have to be. –  George Wolfe Mar 10 '13 at 19:29
I played a bit with ChucK many years ago. It's a command line program, and maybe it can be connected to Mathematica in a sufficiently convenient way. The link I gave you is to a ChucK IDE, which seems to be much easier to use. You can run programs concurrently and change parameters in real time using sliders. –  Szabolcs Mar 10 '13 at 19:40

The reason you are getting error messages is because you need to constrain the values of the SoundNote to integers. For instance:

 Manipulate[EmitSound[Sound[SoundNote[n, 3]]], {n, 1, 10, 1}]


works fine.

-
I see that it does work. Thanks. However, it doesn't do what I hoped to do, which was manipulate a continuous tone. The following doesn't work: Sound[Manipulate[Play[Sin[m 1000 t], {t, 0, 10}], {m, 5, 10, 1}]]. Could it be made to do so? –  George Wolfe Mar 10 '13 at 19:12
Trying beat frequency demo. –  George Wolfe Mar 10 '13 at 19:16

I think that varying a parameter of a sound continuously, while it is being played is not possible in Mathematica. A workaround is to use an external program. ChucK is a real-time audio programming language that can run many threads ("shreds") concurrently, and allows dynamically adding/removing shreds from the system. We can create shreds that vary parameters from within Mathematica.

Here's a working, continuously adjustable solution based on ChucK:

First, you need to download the chuck binary and put it some place accessible. I put it in ~/bin.

Then I wrote this chuck script, and put it in ~/chuck:

/* snd.ck */

public class MyState {
static int freq;
}

500 => MyState.freq;

SinOsc s => dac;

while (true) {
MyState.freq => s.freq;
50::ms => now;
}


Then I started a chuck host from a terminal using chuck --loop.

Finally, I did this in Mathematica:

(* this will start playing the sound by adding the above defined shred *)

SetDirectory["~/chuck"]
Run["~/bin/chuck + snd.ck"]

(* we'll use this script to adjust the static MyState.freq variable in chuck *)

Manipulate[
Export[script, IntegerString[s] <> " => MyState.freq;", "String"];
Run["~/bin/chuck + " <> script];
s,
{{s, 440}, 200, 1000, 1}
]


Kill chuck from the terminal to stop the sound. Of course there are better ways to do stop/start the sound, without killing chuck, but I was lazy. (You'd need to add/remove shreds that perform a specific task.) This is just a simple example showing how to control ChucK from within Mathematica, using sliders.

-

There is a nice Wolfram demonstration

that lets you listen to beat frequencies of sine waves. You will need to edit the .cdf in order to listen to any beat frequencies near the octave.

-
This is a good demo. Thanks. –  George Wolfe Mar 10 '13 at 20:08