Has anyone created a melody from a photo that I could study to create my own?
There are several ways to do this … this is just an example:
Take the image, convert the image to ImageData,“byte”, put the data in base 12 to have all the notes (in this case with all notes, the chromatic scale) and replace with the notes (in this case, in the 4th and 5th octaves).
im = Import["ExampleData/rose.gif"]
Length@Flatten@IntegerDigits[ImageData[im, "Byte"], 12]
![]()
It can be noticed that there is a lot of data (around 300000 notes) so I selected a part of the data that refers roughly in the middle where it is part of the rose)..
data = Flatten@IntegerDigits[ImageData[im, "Byte"], 12] /. {0 -> "C4",
1 -> "Db4", 2 -> "D4", 3 -> "Eb4", 4 -> "E4", 5 -> "F4",
6 -> "Gb4", 7 -> "G4", 8 -> "Ab5", 9 -> "A5", 10 -> "Bb5",
11 -> "B5"};
Sound[Take[
Table[SoundNote[data[[i]], .1], {i, 1, Length[data]}], {130800,
131700}]]
There are several instrument options, note interval etc … (here was made with the default instrument and each note .1 second duration). It is also possible to place multiple instruments simultaneously in the melody, overlapping parts of the sound, create simultaneous notes as chords .. the way I did here was in a raw and simple way.
Another example (with 3 instruments, 3 different parts of data, overlapping, major pentatonic scale):
im = Import["ExampleData/turtle.jpg"]
data = Flatten@IntegerDigits[ImageData[im, "Byte"], 5] /. {0 -> "C4",
1 -> "D4", 2 -> "E4", 3 -> "G4", 4 -> "A5"};
d1 = Sound[
Table[SoundNote[data[[i]], 0.3, "PanFlute",
SoundVolume -> 1/2], {i, 40000, 40500}], {0, 200}];
d2 = Sound[
Table[SoundNote[data[[i]], 0.3, "Harp"], {i, 41000, 41500}], {0,
300}];
d3 = Sound[
Table[SoundNote[data[[i]], 0.3, "Percussion"], {i, 42000,
42500}], {0, 200}];
Sound[{d1, d2, d3}]
There are also other ways to handle the data … for example, PixelValue, ImageValue .. and also can separate images into color channels to play each channel as a different instrument, different music bases, some channels using chords, etc.
im = Import["ExampleData/girlcloseup.jpg", ImageSize -> 100]
Eight tone Spanish scale; violin, guitar, sitar, voiceoohs, bass:
data1 = IntegerDigits[PixelValue[cs[[1]], {All, All}, "Byte"],
8] /. {0 -> "C3", 1 -> "Db3", 2 -> "Eb3", 3 -> "E3", 4 -> "F3",
5 -> "Gb3", 6 -> "Ab4", 7 -> "Bb4"}
data2 = Flatten@
IntegerDigits[PixelValue[cs[[2]], {All, All}, "Byte"], 8] /. {0 ->
"C4", 1 -> "Db4", 2 -> "Eb4", 3 -> "E4", 4 -> "F4", 5 -> "Gb4",
6 -> "Ab5", 7 -> "Bb5"}
data3 = IntegerDigits[PixelValue[cs[[3]], {All, All}, "Byte"],
8] /. {0 -> "C2", 1 -> "Db2", 2 -> "Eb2", 3 -> "E2", 4 -> "F2",
5 -> "Gb2", 6 -> "Ab3", 7 -> "Bb3"}
d1 = Sound[
Table[SoundNote[data1[[i]], 0.3,
RandomChoice[{"Guitar", "VoiceOohs", "Sitar"}],
SoundVolume -> 3/4], {i, 1000, 1200}], {0, 100}];
d2 = Sound[
Table[SoundNote[data2[[i]], 0.3, "Violin", SoundVolume -> 2/3], {i,
1000, 1200}], {0, 150}];
d3 = Sound[
Table[SoundNote[data3[[i]], 0.3, "Bass"], {i, 1000, 1200}], {0,
150}];
Sound[{d1, d2, d3}]
Many other ways to do this … other scales, another way to convert data, etc … you can be creative once you understand the process … so the way I showed it serves as a basis for a way to do this process … more information about the Sound, SoundNote, instrumentation, other options..: check the documentation.
Thank you very much. I was not sure how to add instruments.
Could you help me with another item? I would like to create a MIDI from the Sound and play it the MAC GarageBand.
Very well-presented and informative Claudio.
I was able to do what I wanted to do. Sorry to bother everyone.
I was not capable understanding how you got PixelValue[cs[[[1]] break down the pixels of the photo.
Hello Larry,
It is as follows:
“cs position 1” is the first exposed channel of the image in this example (image). “Byte” is the type of data you want to return, but there are other options.
The argument following the image controls the range of the data, in this first example only 1 pixel coordinate (x = 10, y = 10).
In the second example, pixel 10 (x coordinate, column) and pixels with pixel range 10 to 12 (y coordinate, rows).
In the third example a rectangle formed by pixels 10-12 in both rows and columns.
In the fourth example, all pixels in the column (all y) for position 10 (fixed x).
In the fifth example, a rectangle with all the pixels (y-axis) in columns 10 through 15 (x-axis).
In the sixth example, all the pixels of the image.
PixelValue[cs[[1]], {10, 10}, "Byte"]
PixelValue[cs[[1]], {10, 10 ;; 12}, "Byte"]
PixelValue[cs[[1]], {10 ;; 12, 10 ;; 12}, "Byte"]
PixelValue[cs[[1]], {10, All}, "Byte"]
PixelValue[cs[[1]], {10 ;; 15, All}, "Byte"]
PixelValue[cs[[1]], {All, All}, "Byte"]
I still don’t know how you did this.From your original suggestion I didn’t see how you got from the original photo to select cs[1] to get the pixels.
Do you mean this part?
im = Import["ExampleData/girlcloseup.jpg", ImageSize -> 100]
cs = ColorSeparate[im, "HSB"]
cs[[1]]
This way you will be splitting the photo into channels, on “HSB” channels, but there are other options too, such as: “RGB”, “RGBA”, “CMYK”, “XYZ” .. etc
Thank you very much , that was the part I was missing.
One more request. I would like to set up a list of all the available instruments and set up a series of notes using a method of selctng all the instruments identifying them and play all the instruments one at a time. Just using the simple Wolfram description, I couldn’t find any.
I don’t know if I fully understand what you want to do, but the list of “instruments” is detailed in the SoundNote/Details and Options (Documentation).
Below I have created a list of the instruments that are displayed there (in alphabetic order):
instruments = {"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"};
To listen to a sample of each instrument the following code can be used (C3 tone and 0.5 seconds):
ins = Map[Sound@SoundNote["C3", 0.5, #] &,
instruments]; Thread[{instruments, ins}]
There is also a list of percussion events (also listed in SoundNote) that can also be used regardless of the melody tone:
events = {"BassDrum", "BassDrum2", "BellTree", "Cabasa", "Castanets",
"ChineseCymbal", "Clap", "Claves", "Cowbell", "CrashCymbal",
"CrashCymbal2", "ElectricSnare", "GuiroLong", "GuiroShort",
"HighAgogo", "HighBongo", "HighCongaMute", "HighCongaOpen",
"HighFloorTom", "HighTimbale", "HighTom", "HighWoodblock",
"HiHatClosed", "HiHatOpen", "HiHatPedal", "JingleBell", "LowAgogo",
"LowBongo", "LowConga", "LowFloorTom", "LowTimbale", "LowTom",
"LowWoodblock", "Maracas", "MetronomeBell", "MetronomeClick",
"MidTom", "MidTom2", "MuteCuica", "MuteSurdo", "MuteTriangle",
"OpenCuica", "OpenSurdo", "OpenTriangle", "RideBell", "RideCymbal",
"RideCymbal2", "ScratchPull", "ScratchPush", "Shaker",
"SideStick", "Slap", "Snare", "SplashCymbal", "SquareClick",
"Sticks", "Tambourine", "Vibraslap", "WhistleLong",
"WhistleShort"};
To listen to samples of each percussion event, use the following code (note that the event is in place of the tone and there is no instrumentation or note interval option when it is an event):
eve = Map[Sound@SoundNote[#] &, events]; Thread[{events, eve}]
I hope this information was helpful to you.
After this , can you explain to me a little more what you want to do?
Thank you very much. That was a very good exposition. It gives mr a lot of information I can study up on.
This is what I wanted to do
Sound[Table[SoundNote[RandomInteger[12], 0.1, “Violin”], 20]]
only replace Violin with all the instruments , just hearing one note doesn’t; help me hear sound of the instrument,
For listening to instrument sound samples I recommend the command:
ResourceFunction[“MusicalScaleSample”]
..which can be used with the instruments list above this way, e.g.:
Map[{#, ResourceFunction["MusicalScaleSample"]["MajorPentatonic",
"Tone" -> "C3", "Instrument" -> #]} &, instruments]
There are 30 musical scale, and 60 tones available for this function, as well as all instruments described in SoundNote can be used. The interval of notes, volume, octaves, playing mode, etc. are also all options.
Visit this link for more details of this function:
https://resources.wolframcloud.com/FunctionRepository/resources/MusicalScaleSample
On the page of this function, there are also examples of how to assemble a melody, increase music sample time, overlap instruments, sequential instruments, etc.
There you can even download the notebook, where there are usage examples, options, and the source code of the function.
Thank you very much. That was exactly what I looking for. This language is amazing and the forums are amazing.
I managed to do a lot with one photo. i would like to create functions to generalize some items. I tried to see some of the examples but they copied to the system clipboard but i don’t know how to get to the clipboard . Any help would be appreciated.
Hello Larry, has the issue been resolved? … are you trying to copy documentation examples or function repository examples? Anyway, I believe the solution comes in the form of copy and paste. If you refer to the repository function, you have the option to download the notebook with all the examples. I believe you need to provide more details about the problem so that I can help you (if the problem persists).
I believe I will create a user function. I am studying it now by going thru the examples.






