# How to play microtonal music using Mathematica?

I am trying to model microtonal music using Play. I have only partial success because it takes such a long time to make some sound. Could you please have a look at my stackoverflow.com question? The link is

http://stackoverflow.com/questions/14849184/how-to-play-microtonal-music-using-mathematica-or-other-tools

=========== Original question linked above ===============

Do you have any suggestions how to write and play microtonal music?

My solution:

I tried to use Play and Piecewise but it takes a LONG time to take a sound!

As you know there are many notes other than Do or Re or Mi of western music. So maybe I can not use SoundNote for ancient music. Each note is represented as a fraction. Using middle Do as a base (i.e., base = 260.741 Hz); the next notes are represented by re = 9/8 base, mi = 5/4 base, fa = 4/3 base, di = 3/2 base, etc. For example, to play fa for 2 seconds with SoundVolume = 1/3, evaluate

Play[1/3 Sin[ fa 2 Pi x], {x,0,2}].


I model each such instance with a list of the form {time, note, soundvolume}, for example {2, fa, 1/3}. Consider a very short example of such a sequence of notes

voice1= {{1, 9/8, 1}, {1, 5/4, 2/3}, {1/2, 4/3, 1/3}, {1/2, 9/8, 1/3}, {1, 4/3, 1},
{1, 3/2, 2/3}, {1, 4/3, 1/3}}


How can voice1 be easily played using Play?

I tried using Piecewise, but with that I have to wait for a LONG TIME before I get any sound result. (Maybe the reason is the use Piecewise in Play, but I am not sure.)

Please, try the following example to see how slow you get the sound result for voice1.

Play[
Piecewise[{
{1, 0. < x <= 1},
{2/3, 1 < x <= 2},
{1/3, 2 < x <= 5/2 || 5/2 < x <= 3},
{1, 3 < x <= 4},
{2/3, 4 < x <= 5},
{1/3, 5 < x <= 6},
{0, True}}]
Sin[ base 2 Pi
Piecewise[{
{9/8, 0. < x <= 1},
{5/4, 1 < x <= 2},
{4/3, 2 < x <= 5/2},
{9/8, 5/2 < x <= 3},
{4/3, 3 < x <= 4},
{3/2, 4 < x <= 5},
{4/3, 5 < x <= 6},
{0, True}
}]],
{x,0, 6}]

-
It would be better to post the full question here. As it stands, this is likely to be closed as "not a real question". – Simon Woods Feb 13 '13 at 8:50
I second @SimonWoods request and went as far as to copy here your original question. – Vitaliy Kaurov Feb 13 '13 at 10:20
I, however, object strongly to the question being posted on two different sites within a very short period. While I prefer Mathematica questions were posted here, you barely gave the user on stackoverflow time to answer it there. – rcollyer Feb 13 '13 at 14:44

Here's mine:

-first create a scale by dividing an octave to some intervals (12 gives you the usual good temperament):

microscale[divisions_Integer, baseFreq_: 260.741] :=
Module[{interval},
interval = 2 π /divisions;
Play[#, {t, 0, .4}] & /@ Table[Sin[(2 π + i) baseFreq t], {i, 0, 2 π, interval}]
];


which you can use like so: EmitSound/@microscale[12].

-Now succumb to the temptation to create many more microtonal masterpieces like cormullion's and make a little piano to play the scale:

createPiano[divisions_Integer, baseFreq_: 260.741] :=
Module[{scale},
scale = microscale[divisions,  baseFreq];
GraphicsGrid[
{Table[With[{i = i},
Button[Graphics[Rectangle[{0 , 0}, {1 , 2}]], EmitSound[scale[[i]]]]],
{i, 1, divisions }]},
Frame -> All]]


which you can use like so createPiano[17].

-
+1 for the piano keyboard, not the music... but you don't have to restrict yourself to just a single row :) – cormullion Feb 13 '13 at 12:04
:) thanks - I agree, there is so much temptation to take this over the top! The Fokker arrangement being the most tempting, but sadly there's only so many hours in a day – gpap Feb 13 '13 at 12:24

Here's an idea:

rationalNote[{p_, d_}] :=
EmitSound[
Play[Sin[4 Pi 260.741 *p t] + Sin[2 Pi 260.741 *p t] , {t, 0, d}]]

ancientMelody = Partition[
Riffle[
RandomSample[FindDivisions[{0.6, 1.4}, 15]],
Table[RandomChoice[{0.25, .5, 0.75}], {n, 15}]],
2]

rationalNote /@ ancientMelody


It's enough to make Pythagoras turn in his grave.

-

You should define time duration not as a single number, but as an interval from initial time to final. The Mathematica will know in what sequence to play the sounds. Here is how to restructure your data and play them. Original data (format: {{duration, frequency, amplitude},...} ):

voice = {{1, 9/8, 1}, {1, 5/4, 2/3}, {1/2, 4/3, 1/3}, {1/2, 9/8,
1/3}, {1, 4/3, 1}, {1, 3/2, 2/3}, {1, 4/3, 1/3}};


Restructured data (format: {{frequency, amplitude, t_initial, t_final},...} ):

dur = Flatten /@ Thread[{voice[[All, 1 ;; 2]],
Partition[{0}~Join~Accumulate[voice[[All, 1]]], 2, 1]}];


Define function that plays a single interval:

f[a_, f_, t1_, t2_] := Play[a Sin[f 2 Pi 260.741 t], {t, t1, t2}]


Play it all:

Sound[f @@@ dur]


If you really want to change the SoundVolume then you should keep Sin amplitude constant and instead define function as:

g[a_, f_, t1_, t2_] := Sound[Play[Sin[f 2 Pi 260.741 t], {t, t1, t2}], SoundVolume -> a];

Sound[g @@@ dur]


You can clearly see and hear decrease in the amplitude now.

-
Thank you for answer! How will you change your code for the case of two different voices of the same total duration? – kornaros Feb 13 '13 at 10:53
@kornaros Played simultaneously? If you can add 2nd voice data and detailed explanation to the question - I maybe can help you more. – Vitaliy Kaurov Feb 13 '13 at 11:03
In music theory this is called polyphony ( two or more simultaneous lines of independent melody, as opposed to music with just one voice monophony). For example, please take voice2= {{2, 9/8, 2/3}, {2, 9/8, 1/3}, {1, 4/3, 1}, {1, 3/2, 1/3}}; with the same duration. Maybe this is just an addition of the Sin of these two voices but I dont know how to achieve this addition! – kornaros Feb 13 '13 at 19:33