# How to emit sound of specified frequency for a specified amount of time?

Sorry but I am confused by several sound/play commands in Mathematica. What I want to do is to emit a succession of sounds according to a succession of frequencies and durations. Specifying timbres would be even nicer. What is the best way to do it?

You can use Play to produce a fixed-duration sound of arbitrary frequency. For example to play, to get a 2600 Hz tone for 1 second you could use:

Play[Sin[2600 2 π t], {t, 0, 1}]


To play a sequence of fixed-duration sounds , you can use Sound on a list of Play functions as shown in the first Scope example here.

For those of a certain age, here is a sequence that might sound familiar and helpful.

Sound[{Play[Sin[350 2 π t] + Sin[440 2 π t], {t, 0, 1}],
Play[Sin[770 2 π t] + Sin[1336 2 π t], {t, 0, 0.2}],
SoundNote[None, 0.1],
Play[Sin[770 2 π t] + Sin[1336 2 π t], {t, 0, 0.2}],
SoundNote[None, 0.1],
Play[Sin[770 2 π t] + Sin[1336 2 π t], {t, 0, 0.2}],
SoundNote[None, 0.1],
Play[Sin[697 2 π t] + Sin[1209 2 π t], {t, 0, 0.2}],
SoundNote[None, 0.1],
Play[Sin[697 2 π t] + Sin[1336 2 π t], {t, 0, 0.2}],
SoundNote[None, 0.1],
Play[Sin[697 2 π t] + Sin[1209 2 π t], {t, 0, 0.2}],
SoundNote[None, 0.1],
Play[Sin[697 2 π t] + Sin[1336 2 π t], {t, 0, 0.2}]}]


Edited to add 100 ms silence breaks between sounds with SoundNote[None, 0.1].

For regular use, I would define my own functions using Map to simplify. For example:

freqSound[freqs_, duration_] := Play[Sum[Sin[2 \[Pi] t f], {f, Flatten[{freqs}]}], {t, 0, duration}]
freqSequence[listFreqDur___] := Sound[Map[freqSound[#[[1]], #[[2]]] &, {listFreqDur}]]


These can be used to reproduce the sounds above like this (where I am using a 1 Hz tone as quick shorthand for an inaudible silence):

freqSound[{350, 440}, 1]
freqSequence[{{350, 440}, 1}, {{770, 1336}, 0.2}, {1, 0.1}, {{770, 1336}, 0.2}, {1, 0.1}, {{770, 1336}, 0.2}, {1, 0.1}, {{697, 1209}, 0.2}, {1, 0.1}, {{697, 1336}, 0.2}, {1, 0.1}, {{697, 1209}, 0.2}, {1, 0.1}, {{697, 1336}, 0.2}]

• Sorry it sounds quite strange on my computer, I cannot recognize any melody, what is it supposed to be? Does it depend on hardware? Apr 24 '16 at 21:41
• Aha now I've got it :D Apr 24 '16 at 22:37
• Looks like I should accept that. Seems however strange that there is no simpler command to do that Apr 26 '16 at 10:53
• @მამუკაჯიბლაძე, I would just construct my own helper functions using Map based on the specific sounds and input syntax I would want. I edited to add such examples above. Hope this helps. Apr 26 '16 at 12:48

Working on my previous comment; this:

Sound[{SoundNote["D", 1, "Violin"], SoundNote["E", 1, "Violin"],
SoundNote["F", 1, "Violin"], SoundNote["G", 1, "Violin"],
SoundNote["A", 1, "Violin"], SoundNote["B", 1, "Violin"]}]


plays a succession of 6 notes for 1 second each. Violin specifies the instrument; there are a lot of different styles in the Details and Option in the documentation.

• Unfortunately SoundNote has fixed frequencies, you cannot move away from halftone multiples; what I need is to have arbitrary frequencies Apr 24 '16 at 19:09