Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a huge set of events in Mathematica that I need to react to, and it's not practical always checking for it, especially since events are randomly distributed in time.
So I wanted to add a simple sound to remind me to go check the event. I wanted to use Beep[] but I get no results, in fact, I didn't realize till now that Mathematica is making no sounds whatsoever.

I use Debian Squeeze Stable and have no trouble with any other application using sound.

This effects all recent versions (7, 8 and 9) of Mathematica and probably older versions depending on your system's sound set up.

share|improve this question

migrated from stackoverflow.com Jun 12 '11 at 2:31

This question came from our site for professional and enthusiast programmers.

    
This has been a problem in Mma ever since they added sound commands in version 6. I remember fixing it at the time, but have not bothered in the more recent versions. Try the documentation here. –  Simon Jun 11 '11 at 11:52
    
Should this question be migrated to superuser? –  Simon Jun 11 '11 at 11:56
    
Unfortunately I still get no sound. –  enedene Jun 11 '11 at 18:39
    
I'm the original poster on Stackoverflow. Thank you for telling me about this site, I didn't know this one existed. It's fantastic I could get good use of it. –  enedene Jun 12 '11 at 12:16
    
Also see: unix.stackexchange.com/q/15191/2842 –  Simon Jul 11 '11 at 6:55

1 Answer 1

up vote 9 down vote accepted

As far as I know, Mathematica has never really supported sound in Linux. The command Play was introduced in version 3, and the whole sound system overhauled in version 6. If you search comp.soft-sys.math.mathematica, you'll find questions going back all the way to version 3. In version 6, the command EmitSound was introduced. This now underlies most of the Mathematica sound generation, but I couldn't find a quick way to fix/hack it for linux, since EmitSound does some preprocessing (figuring out what types of objects its been given) before passing the sound to the frontend to evaluate.

The default sound driver/API used in most linux installs is ALSA (wiki). It became the default in 2002 in the linux kernel 2.6 and OSS was marked as "depreciated" (although OSS is still under active development). Many Linux distributions now use PulseAudio which sits on top of the underlying ALSA sound. (Anyone with a better understanding of Linux sound should feel free to edit this!)

Mathematica introduced sound in version 3 (1996) and thus used OSS for its *nix variants. This is still the case, despite OSS no longer being the default in almost any Linux variant. Knowing this gives us a possible solution: use an OSS emulation layer (see, e.g., http://wiki.debian.org/SoundFAQ).

The simplest is to use an userspace mode emulation and run Mathematica through aoss

aoss mathematica

or the PulseAudio equivalent

padsp mathematica

The problem with both of these is that it only works with MIDI sounds, not with sampled sounds. At least this is true on my Ubuntu 10.10 system and the linux system of Scott Kruger (of the WRI Technical Support team). A bug report has been filed on this issue.

Alternatively, you can load the OSS compatibility into your kernel

apt-get install alsa-oss
modprobe snd_pcm_oss
modprobe snd_mixer_oss

and you can add snd_pcm_oss and snd_mixer_oss to /etc/modules to load them at boottime. These modules are not readily available in Ubuntu 10.10 since they decided to remove the backwards compatibility. I can not test the above without recompiling my kernel...


Since the above OSS emulation is not currently working perfectly, here's a couple of quick work-arounds based on some of the discussions I've seen (e.g., a b c) and rewritten to use default ALSA commands.

For sampled sound, use aplay:

ALSASound[snd_, "WAV"] := Module[{playCmd = "aplay", soundFileName},
  soundFileName = "/dev/shm/" <> ToString[Unique["MmaSound"]] <> ".wav";
  playCmd = playCmd <> " " <> soundFileName;
  Export[soundFileName, snd, "WAV"];
  Run["(" <> playCmd <> ";" <> "/bin/rm -f " <> soundFileName <> ")&"];]

e.g., ALSASound[Play[Sin[1000 t^2], {t, 0, 1}], "WAV"].
You should also be able to Export straight onto the appropriate /dev/snd/ device... but I couldn't get that working.

For midi use aplaymidi. For this to work you need some sort of software/hardware midi synth installed. I have timidity running on port 128. (See here for help)

ALSASound[snd_, "MIDI"] := 
 Module[{playCmd = "aplaymidi", port = "128:0", soundFileName},
  soundFileName = "/dev/shm/" <> ToString[Unique["MmaSound"]] <> ".mid";
  playCmd = playCmd <> " -p " <> port <> " " <> soundFileName;
  Export[soundFileName, snd, "MIDI"];
  Run["(" <> playCmd <> ";" <> "/bin/rm -f " <> soundFileName <> ")&"];]

e.g., ALSASound[Sound[SoundNote /@ CharacterRange["A", "G"]], "MIDI"]

You can now wrap the appropriate ALSASound command around any sound object. You could also redefine Play to use ALSASound:

SetOptions[Play, DisplayFunction -> ((ALSASound[#, "WAV"]; #) &)];

This is essentially equivalent to setting $SoundDisplayFunction, as recommended by many of the discussions around the internet. This option setting will play the sound, then output the normal graphics.

Play[Sin[1000 t^2], {t, 0, 1}]

Play

but the start/stop buttons will not work, since they are based on EmitSound.

To make a simple Beep[] equivalent, try

ALSABeep[] := Play[Sin[5000 t], {t, 0, .1}, 
                   DisplayFunction -> (ALSASound[#, "WAV"]&)]

Finally, if you install the festival text-to-speech (or any other text-to-speech program), then you can replace the Mathematica Speak functionality, e.g.,

FestivalSpeak[str_String] := (Run["(echo \"" <> str <> "\" | esddsp festival --tts)&"];)
FestivalSpeak[expr_] := FestivalSpeak[SpokenString[expr]]
share|improve this answer
    
Simon, this answer did the trick, thank you very much. –  enedene Jun 12 '11 at 12:18
    
Btw, does anybody know if Mathematica plans to support sound on Linux in the future? I'd guess that they are specific software where percentage of people using it on Linux is much greater than for most other software. –  enedene Jun 12 '11 at 12:24
    
@enedene: I asked that question of WRI support today. Hopefully we'll get an answer in the next few days... –  Simon Jun 12 '11 at 12:27
    
Thank you Simon, you've been helpful. If you get an answer, please post it here or give a link. –  enedene Jun 12 '11 at 13:12
    
@enedene: I got a helpful reply from WRI support. See the edit! –  Simon Jun 16 '11 at 0:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.