Musical Melodies With Mathematica 6

Daniel G. Martinez

Science Applications International Corporation

dmartinez@sprintmail.com

V6's New Musical Capabilities

Compared to its previous versions, and in its own right, Mathematica 6 has wondrous faculties for the production of musical sounds.  Its sound functions and their many options are enormously versatile and can readily be used to play melodies of the user's own composition or derived from a musical score.

Scope Of Presentation

This presentation will demonstrate some techniques for using Mathematica 6 to play musical melodies.  The fundamental concept is that sounds are rendered from lists of notes whose pitch, duration, and "instrument" are specified by the user.  Techniques for elaborating musical melodies with harmony and rhythm also are presented.  Use of the Manipulate function enables users to set a melody's tempo, and musical instruments and song titles can be selected from setters.

Play a Single Note

Here the note middle 'a' is played for one second using SoundNote's instrument option "Violin".  Notice the vibrato, which is built into that particular option.  For comparison, listen to some of the other 127 instrument option that are available in Version 6.

Sound[SoundNote["A3",1,"Violin"]]

({{"Accordion", "Agogo", "AltoSax", "Applause"}, {"Atmosphere", "Bagpipe", "Bandoneon", "Banjo"}, {"BaritoneSax", "Bass", "BassAndLead", "Bassoon"}, {"Bird", "BlownBottle", "Bowed", "BrassSection"}, {"Breath", "Brightness", "BrightPiano", "Calliope"}, {"Celesta", "Cello", "Charang", "Chiff"}, {"Choir", "Clarinet", "Clavi", "Contrabass"}, {"Crystal", "DrawbarOrgan", "Dulcimer", "Echoes"}, {"ElectricBass", "ElectricGrandPiano", "ElectricGuitar", "ElectricPiano"}, {"ElectricPiano2", "EnglishHorn", "Fiddle", "Fifths"}, {"Flute", "FrenchHorn", "FretlessBass", "FretNoise"}, {"Glockenspiel", "Goblins", "Guitar", "GuitarDistorted"}, {"GuitarHarmonics", "GuitarMuted", "GuitarOverdriven", "Gunshot"}, {"Halo", "Harmonica", "Harp", "Harpsichord"}, {"Helicopter", "HonkyTonkPiano", "JazzGuitar", "Kalimba"}, {"Koto", "Marimba", "MelodicTom", "Metallic"}, {"MusicBox", "MutedTrumpet", "NewAge", "Oboe"}, {"Ocarina", "OrchestraHit", "Organ", "PanFlute"}, {"PercussiveOrgan", "Piano", "Piccolo", "PickedBass"}, {"PizzicatoStrings", "Polysynth", "Rain", "Recorder"}, {"ReedOrgan", "ReverseCymbal", "RockOrgan", "Sawtooth"}, {"SciFi", "Seashore", "Shakuhachi", "Shamisen"}, {"Shanai", "Sitar", "SlapBass", "SlapBass2"}, {"SopranoSax", "Soundtrack", "Square", "Steeldrums"}, {"SteelGuitar", "Strings", "Strings2", "Sweep"}, {"SynthBass", "SynthBass2", "SynthBrass", "SynthBrass2"}, {"SynthDrum", "SynthStrings", "SynthStrings2", "SynthVoice"}, {"Taiko", "Telephone", "TenorSax", "Timpani"}, {"Tinklebell", "TremoloStrings", "Trombone", "Trumpet"}, {"Tuba", "TubularBells", "Vibraphone", "Viola"}, {"Violin", "Voice", "VoiceAahs", "VoiceOohs"}, {"Warm", "Whistle", "Woodblock", "Xylophone"}})

Assignment of Notation Shorthand

ms={a0,bf0,b0,c0,cs0,d0,ef0,e0,f0,fs0,g0,af1,a1,bf1,b1,c1,cs1,d1,ef1,e1,f1,fs1,g1,gs1,a2,bf2,b2,c2}={"A2","Asharp2","B2","C3","Csharp3","D3","Dsharp3","E3","F3","Fsharp3","G3","Aflat3","A3","Asharp3","B3","C4","Csharp4","D4","Dsharp4","E4","F4","Fsharp4","G4","Gsharp4","A4","Asharp4","B4","C5"};  (*  user-defined notation shorthand, which is equivalent to built-in musical tones and semitones  *)

Scale Plot

ms1={"a0","bf0","b0","c0","cs0","d0","ef0","e0","f0","fs0","g0","af1","a1","bf1","b1","c1","cs1","d1","ef1","e1","f1","fs1","g1","gs1","a2","bf2","b2","c2"};  (*  user-defined notation shorthand put into string form for plotting only  *)
pt[tx_,xy_]:=Graphics[Text[ToString[tx],{xy[[1]],xy[[2]]},Background->GrayLevel[0.99]]]
λ[y_]:=Graphics[Line[{{0,y},{40,y}}]]
pos={8,11,15,18,21};  (*  relative vertical positions of horizontal lines  *)
p0=Map[λ,pos];
p1=Table[{pt[(Subscript[ms1, [[j]]]),{ j,j}]},{j,Length[ms1]}];
p2=Table[{pt[(Subscript[ms, [[j]]]),{ j+10,j}]},{j,Length[ms1]}];
Show[p0,p1,p2]

Play Songs Using Manipulate

Russia={   (*  list of notes and their relative durations for part of the melody of the John Barry song From Russia With Love  *)
{e0,0.3},{a1,0.3},{c1,0.65},{a1,0.2},{f1,0.65},{e1,0.4},{ef1,0.4},{e1,0.4},{b1,0.65},
{None,0.2},
{e0,0.3},{a1,0.3},{c1,0.65},{a1,0.2},{g1,0.65},{fs1,0.4},{f1,0.4},{e1,0.4},{gs1,0.65},
{None,0.2},
{e1,0.25},{a2,0.4},{e1,0.35},{c1,0.35},{a1,0.35},
{None,0.2},
{g1,0.3},{g1,0.4},{f1,0.3},{d1,0.65},
{None,0.2},
{b1,0.3},{f1,0.3},{e1,0.5},{e1,0.3},{gs1,0.3},{f1,0.5},{e1,0.3},{a2,1}
};

Phantom={  (*  melody line of All I Ask Of You, Andrew Lloyd Webber, from Phantom Of The Opera using standard notation  *)
{"D4",0.5},{"C4",0.5},{"D4",0.5},{"E4",0.5},{"E4",0.5},{"G3",0.7},{None,0.3},
{"E4",0.5},{"D4",0.5},{"C4",0.5},{"D4",0.5},{"E4",0.5},{"G4",0.7},{None,0.3},
{"A4",0.5},{"E4",1},{"G4",0.35},{"F4",0.35},{"E4",0.35},{"G4",0.5},{"D4",1},{None,0.2},
{"E4",0.5},{"F4",0.5},{"G4",0.5},{"F4",0.5},{"E4",0.5},{"G4",0.5},{"D4",1},{None,0.2},

{"D4",0.5},{"C4",0.5},{"D4",0.5},{"E4",0.5},{"E4",0.5},{"G3",1},{None,0.3},
{"E4",0.5},{"D4",0.5},{"C4",0.5},{"D4",0.5},{"E4",0.5},{"G4",0.7},{None,0.3},
{"A4",0.5},{"E4",1},{"G4",0.35},{"F4",0.35},{"E4",0.35},{"G4",0.5},{"D4",1},{None,0.2},
{"E4",0.5},{"F4",0.5},{"G4",0.5},{"F4",0.5},{"E4",0.5},{"G4",0.5},{"D4",1},{None,0.4},

{"D5",1},{"D5",1},{"D5",0.4},{"C5",0.5},{"E4",0.5},{"G4",0.5},{"A4",1},{"A4",1},{"A4",0.4},{"B3",1},{None,0.2},
{"C5",1},{"B4",0.5},{"A4",0.5},{"G4",0.5},{"C4",0.5},{"E4",0.5},{"G4",0.25},{"A4",0.25},{"G4",1},{None,0.2},

{"G4",0.15},{"Gsharp4",0.15},{"A4",0.15},{"Asharp4",0.15},{"B4",0.15},{"C5",0.15},{"Csharp5",0.15},

{"D5",1},{"D5",1},{"D5",0.4},{"C5",0.5},{"E4",0.5},{"G4",0.5},{"A4",1},{"A4",1},{"A4",0.4},{"B3",1},{None,0.2},
{"E4",0.25},{"F4",0.25},{"G4",1},{"G4",0.5},{"G4",0.5},{"F4",0.5},{"C5",0.5},{"A4",0.5},{"G4",1.3},{None,0.2},
{"D4",0.5},{"E4",0.55},{"G4",0.6},{"E4",0.65},{"D4",0.7},{"C4",2}
};

Manipulate[Sound[Table[{SoundNote[ToExpression[song][[j,1]],ToExpression[song][[j,2]]×tempo,instrument]},{j,Length[ToExpression[song]]}]],
{{tempo,1,"Tempo"},0.5,1.5},
{{song,"Russia","Song"},{"Russia","Phantom"}},
{{instrument,"Goblins","Instrument"},{"Cello","Goblins","Metallic","Warm"}}
]

Combine Three Lists For Melody, Harmony, and Rhythm

JazzSuite2 ( melody list )

JazzSuite2={  (*   Dmitri Shostakovich ( 1906 - 78 ) *)
{None,2.8},{c1,0.4},{g0,0.5},{fs0,0.2},{e0,0.5},{None,0.2},
{e0,0.3},{fs0,0.3},{g0,0.3},{e0,0.3},{g0,0.3},{b1,0.5},{c1,0.3},{b1,0.5},{a1,0.5},{None,0.2},
{a1,0.5},{fs0,0.5},{e0,0.2},{ef0,0.5},{None,0.2},
{ef0,0.3},{fs0,0.3},{a1,0.3},{fs0,0.3},{a1,0.3},{b1,0.5},{c1,0.3},{bf1,0.5},{b1,0.5},{None,0.2},

{g1,0.5},{fs1,0.5},{e1,0.3},{d1,0.5},{c1,0.3},{a1,0.6},{None,0.2},
{fs1,0.5},{e1,0.5},{d1,0.3},{d1,0.6},{None,0.2},

{g0,0.3},{a1,0.3},{b1,0.3},{b1,0.2},{a1,0.2},{b1,0.2},{c1,0.2},{a1,0.3},{a1,0.2},{g0,0.2},{a1,0.2},{b1,0.2},{g0,0.5},{None,0.2},
{g0,0.3},{a1,0.3},{b1,0.3},{b1,0.2},{a1,0.2},{b1,0.2},{c1,0.2},{a1,0.3},{a1,0.2},{g0,0.2},{a1,0.2},{b1,0.2},{g0,0.5},{None,0.4},

{d1,0.2},{None,0.1},{g1,0.3}

};

Experiment In Joining Melody, Harmony, and Rhythm

js2=Prepend[Accumulate[JazzSuite2[[All,2]]],0];  (*  accumulated Jazz Suite 2 melody timing  *)

jsr={   (*  Jazz Suite 2 rhythm line, whose note onset times have been entered in accumulated form [ illustrative only: not actual rhythm ]  *)
{b0,0},{a0,0.7},{b0,1.4},{a0,2.1},{b0,2.8},{a0,3.9},{b0,5.2},{a0,6.1},{b0,6.9},{a0,7.4},{b0,8.1},{a0,9.3},{b0,10.3},{a0,11.2},{b0,12},{a0,12.8}
};

jsh={   (*  Jazz Suite 2 harmony line  *)
{None,13.5},{c1,0.5},{b1,0.5},{a1,0.3},{g0,0.5},{f0,0.3},{d0,0.6},{None,0.2},{b1,0.5},{a1,0.5},{g0,0.3},{g0,0.6}
};

jh2=Prepend[Accumulate[jsh[[All,2]]],0];  (*  accumulated Jazz Suite 2 harmony timing  *)

jm[x_]:=SoundNote[(Subscript[x, [[1]]]),(Subscript[x, [[2]]]),"Oboe"]  (*  melody mapping function  *)

jr[x_]:=SoundNote[(Subscript[x, [[1]]]),(Subscript[x, [[2]]]),"Bassoon",SoundVolume->1/4]  (*  rhythm mapping function  *)

jh[x_]:=SoundNote[(Subscript[x, [[1]]]),(Subscript[x, [[2]]]),"Flute",SoundVolume->1/3]  (*  harmony mapping function  *)

Manipulate[Sound[Join[
Map[jm,Table[{(Subscript[JazzSuite2, [[j,1]]]),{((Subscript[js2, [[j]]])/tp),(((Subscript[js2, [[j]]])+(Subscript[JazzSuite2, [[j,2]]]))/tp)}},{j,Length[JazzSuite2]}]],
Map[jr,Table[{(Subscript[jsr, [[j,1]]]),{((Subscript[jsr, [[j,2]]])/tp),(((Subscript[jsr, [[j,2]]])+0.3)/tp)}},{j,Length[jsr]}]],
Map[jh,Table[{(Subscript[jsh, [[j,1]]]),{((Subscript[jh2, [[j]]])/tp),(((Subscript[jh2, [[j]]])+(Subscript[jsh, [[j,2]]]))/tp)}},{j,Length[jsh]}]]
]],{{tp,1.4,"tempo"},2,0.8}]