Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

After waiting for days for my license I now got the opportunity to install Wolfram Mathematica 8 at home. However, neither the Play nor the Speak functions are doing anything as it seems that Mathematica cannot produce any sound output due to unknown reasons. Might anyone have an idea how I can fix this?

share|improve this question
    
Does the Mathematica documentation say anything about sound? If there's a configuration somewhere where pulseaudio is a possible setting, select it. –  Gilles Jun 17 '11 at 18:49
    
@Gilles: I found a "solution" here however apt doesn't know the arts package. Any idea how to install it? –  Christian Ivicevic Jun 17 '11 at 19:06
    
Mathematica tries to use OSS, while most linux dists use ALSA. See No sound in Mathematica 8.0.1 under Linux –  Simon Jul 8 '11 at 13:12
add comment

2 Answers

The problem is that that version of Mathematica using OSS and that your Ubuntu using ALSA.

Add the following 'sound.m' script to ~/.Mathematica/Kernel:

(* ::Package:: *)

(*
  * Set up a $SoundDisplayFunction for the
  * Linux version of Mathematica and potentially other unixes, too.
  *)

Begin["System`Private`"]

Unprotect[$SoundDisplayFunction]
Clear[$SoundDisplayFunction]

$SoundDisplayFunction :=
     Module[{playCmd,soundFileName},
            Export[$SoundDisplay, #1];
         (* is there a way to get the sample rate, etc. from the audio 
stream? *)
         playCmd = "/usr/bin/play";
         soundFileName = "/tmp/" <> ToString[Unique["sound"]] <> ".wav";
         playCmd = playCmd <> " " <> soundFileName;

         Export[soundFileName, #1, "WAV"];
         Run[playCmd];
         Run["/bin/rm -f " <> soundFileName];
        ] &

Protect[$SoundDisplayFunction]

End[];

(********************CODE ENDS***************************)

After creating 'sound.m', add the following line
to ~/.Mathematica/Kernel/init.m :

Get["sound.m"];

Restart Mathematica to reload the kernel.

share|improve this answer
    
I get a "Set::wrsym: Symbol $SoundDisplayFunction is Protected." error when starting Mathematica 6.0.2.1. Also, I assume everything after "CODE ENDS" should not be in sound.m, right? thanks –  Geremia Jan 11 at 2:11
    
Also, when playing sounds in Mathematica, I get this error (although I hear the sound): «Export::infer: "Cannot infer format of file \!(\"stdout\")."» thanks –  Geremia Jan 11 at 2:20
    
Also, is there a similar fix as this for CDF Player? –  Geremia Jan 11 at 2:26
    
Also, this solution doesn't seem to work with other functions like FrequencyModulation. –  Geremia Jan 11 at 2:39
add comment

Ubuntu uses the PulseAudio sound system. It comes with the paplay command to play a sound file. The approach you found on the Ubuntu wiki should work, but you need to use paplay (or some other equivalent program) rather than the now obsolete aRts.

Here's some code adapted from the Ubuntu wiki. Add it to your ~/.Mathematica/Kernel/init.m or to a file that it includes. In addition to running paplay rather than artsplay, I've changed it to pipe the data rather than store it in a temporary file. Untested.

Begin["System`Private`"]
Unprotect[$SoundDisplayFunction]
Clear[$SoundDisplayFunction]
$SoundDisplayFunction =
    Module[{stream},
      stream = OpenWrite["!pacat", BinaryFormat -> True];
      BinaryWrite[stream, ExportString[#1, "WAV"]];
      Close[stream];
    ] &
Protect[$SoundDisplayFunction]
End[];

See also No sound in Mathematica 8.0.1 under Linux, which reports that a similar approach worked in Mathematica 8 (with ALSA rather than PulseAudio).

share|improve this answer
1  
+1; Although to get it working I had to replace OpenWrite["!paplay"] with OpenWrite["!pacat",BinaryFormat -> True]. Also, you get the normal graphics output if you append a #1 to the end of your $SoundDisplayFunction. Finally, I've updated my answer to further discuss OSS (which Mathematica wants to use) versus ALSA/PA (that Ubuntu uses). –  Simon Jul 8 '11 at 13:07
add comment

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.