Re: Programming Fail
- To: mathgroup at smc.vnet.net
- Subject: [mg132026] Re: Programming Fail
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 19 Nov 2013 02:56:55 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
On 11/17/13 at 6:22 PM, deanrosenthal at gmail.com (Dean Rosenthal) wrote: >If you do this >pitchMap = {1 -> {"C4", "D4"}, 2 -> "D4", 3 -> "E4", 4 -> "F4", 5 -> >"G4"}; >Sound[Thread[SoundNote[Flatten[IntegerPartitions[5]] /. pitchMap]]] >You get a music player and can play the result. >If you do this: >A = NestList[ >StringReplace[#, {"0" -> "2", "2" -> "12", "1" -> "23", "3" -> "45", >"4" -> "56"}] &, "0", 7] >pitchMap = {0 -> "B4", 1 -> "C4", 2 -> "D4", 3 -> "E4", 4 -> "F4", 5 >-> "G4", >6 -> "A4"}; >Sound[Thread[SoundNote[Flatten[A]] /. pitchMap]]] >You get nothing, just output. See what I did there? I substituted, >because that's what you do if you don't know how to program! On the >face of things, I can't see why it wouldn't work, but I've wasted at >least an hour going through documentation and Googling and trying >different approaches. In both cases pitchMap is a list of rules that replace an integer with a string representing a note. In the example that works, you are working with a list of integers. That is Flatten[IntegerPartitions[5]] returns a flat list of integers. But you have defined A above to be a list of strings. You need to either convert A to a list of integers or you need to change pitchMap to work on strings rather than integers. You could convert A to a list of integers as follows b=ToExpression[Characters[StringJoin@@A]]; the StringJoin@@ part converts A to one long string. Characters creates a list of single character strings from the long string and finally, ToExpression converts the characters to integers. Now with b you can do Thread[SoundNote[b/.pitchMap]] to get the desired result. The alternative would be to redefine pitchMap to work on strings rather than integers, i.e., pitchMap = {"0" -> "B4", "1" -> "C4", "2" -> "D4", "3" -> "E4", "4" -> "F4", "5" ->"G4", "6" -> "A4"}; and convert A to a list of character strings: b=Characters[StringJoin@@A]; then obtain the sound with Thread[SoundNote[b/.pitchMap]]