Message Boards Message Boards

Manipulate: musical notes in an Association

Posted 1 year ago

Hi,

Another discussion lead to being able to play tones via Manipulate, such as this:

Manipulate[
 AudioGenerator[
  {"Sin", freq}, length, SoundVolume -> volume],
  {freq, 150.0, 1000.0},
  {length, 0.7, 2.0},
  {volume, 0.01, 0.08}
 ]

I've added an association of some musical notes (sorry, this is long):

notes = <|"c2" -> 65.9, "c\[Sharp]2" -> 69.9, "d2" -> 73.9, 
  "d\[Sharp]2" -> 78.1, "e2" -> 82.7, "f2" -> 87.9, 
  "f\[Sharp]2" -> 92.7, "g2" -> 97.5, "g\[Sharp]2" -> 104.4, 
  "a2" -> 109.9, "a\[Sharp]2" -> 117.3, "b2" -> 124.4, "c3" -> 131.4, 
  "c\[Sharp]3" -> 139.4, "d3" -> 147.5, "d\[Sharp]3" -> 155.9, 
  "e3" -> 165.0, "f3" -> 173.3, "f\[Sharp]3" -> 184.1, "g3" -> 195.6, 
  "g\[Sharp]3" -> 207.8, "a3" -> 219.1, "a\[Sharp]3" -> 233.3, 
  "b3" -> 245.9, "c4" -> 260.5, "c\[Sharp]4" -> 277.5, "d4" -> 292.3, 
  "d\[Sharp]4" -> 311.2, "e4" -> 331.1, "f4" -> 347.5, 
  "f\[Sharp]4" -> 370.3, "g4" -> 390.2, "g\[Sharp]4" -> 415.1, 
  "a4" -> 439.9, "a\[Sharp]4" -> 467.2, "b4" -> 495.1, "d5" -> 589.7, 
  "d\[Sharp]5" -> 623.8, "e5" -> 660.2, "f5" -> 699.6, 
  "f\[Sharp]5" -> 741.6, "g5" -> 785.7, "g\[Sharp]5" -> 832.6, 
  "a5" -> 881.1, "a\[Sharp]5" -> 932.5, "b5" -> 991.6, "c6" -> 1046.3,
   "c\[Sharp]6" -> 1112.6, "d6" -> 1178.8, "d\[Sharp]6" -> 1249.0, 
  "e6" -> 1323.4, "f6" -> 1402.0, "f\[Sharp]6" -> 1484.4, 
  "g6" -> 1573.4, "g\[Sharp]6" -> 1666.1, "a6" -> 1765.1, 
  "a\[Sharp]6" -> 1870.7, "b6" -> 1981.2, "c7" -> 2100.1|>

What I'm hoping to do is have a slider which picks among the keys of "notes". Something like this, although it definitely is not correct:

Manipulate[
     AudioGenerator[
      {"Sin", note}, length, SoundVolume -> volume],
      {notes, "c2", "e2","d3"},
      {length, 0.7, 2.0},
      {volume, 0.01, 0.08}
     ]

That example is just three specific notes, and is wrong. I'm wondering how to use an association with Manipulate such that I can either manipulate among specific keys of the association, or through all keys automatically?

POSTED BY: Dan Stimits
4 Replies
Posted 1 year ago

Thank you. I'm still learning. I'm probably quite odd in that my background is C/C++, and I have trouble with "loosely typed" languages. It is nice to have people make suggestions since there isn't an exact signature to look up.

POSTED BY: Dan Stimits
Posted 1 year ago

The double square bracket notation works when the key is a string, but is not strictly needed. This will also work:

Manipulate[
  AudioGenerator[
    {"Sin",notes[note]},length,SoundVolume->volume
  ],
  {note,Keys[notes]},
  {length,0.7,2.0},
  {volume,0.01,0.08}
]
POSTED BY: Hans Milton
Posted 1 year ago

Thank you, that works! I missed the double square bracket notation.

POSTED BY: Dan Stimits
Posted 1 year ago

An example:

notes=<|"c6"->1046.3,"e6"->1323.4,"g6"->1573.4,"a6"->1765.1,"b6"->1981.2|>;

Manipulate[
  AudioGenerator[
    {"Sin",notes[[note]]},length,SoundVolume->volume
  ],
  {note,Keys[notes]},
  {length,0.7,2.0},
  {volume,0.01,0.08}
]
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract