4
$\begingroup$

At the end of my code I'd like to put an imported (wav) sound. I would then play it with EmitSound[my_sound] when this cell will be evaluated. I can upload my wav file by specifying its location and of course EmitSound works fine:

zvok = Import[
    "F:\\Users\\disk_F\\Documents\\FAKS\\MEHanika_konstrukcij\\vaja3\\\
                New folder\\maribor_cutwav.wav"]

EmitSound[%]

But when I e-mail all this (both nb and wav file) the destination folder won't be the same, so the sound cannot be emitted.

What can I do?

(I've also tried simply to copy the graphics that is displayed when I import a sound. But then I don't know how to get a sound out of it automatically (with EmitSound)

My imported sound

If I press the play icon (triangular one), the sound is emitted, so there must be a way! Any suggestions are welcome !

$\endgroup$

2 Answers 2

7
$\begingroup$

In general, when you want to share a notebook and a data file, it is easier if you use reference the file from a well defined location that is either:

  • An absolute path that depends on the OS/user such as $HomeDirectory or $TemporaryDirectory.
  • A directory relative to the directory that the notebook is in (you can move up/down the tree starting from NotebookDirectory[]

For instance, with the second option, you could do something like:

Import[FileNameJoin[{NotebookDirectory[], "maribor_cutwav.wav"}]]

and as long as the recipient places the file in the same folder as the notebook, it will work.

An alternative (if you don't want to attach a WAV file) is to embed the data in the current notebook. I believe just having the Sound object in the notebook is sufficient (I haven't tried), but it might throw some "unsafe dynamic content" warnings. As a workaround, you could do this:

sound = First@Import["/path/to/file.wav"];
SetOptions[EvaluationNotebook[], TaggingRules -> {"SoundData" -> sound}]

followed by

Sound@CurrentValue[{TaggingRules, "SoundData"}]

to recreate the sound object. The first two lines can also be safely deleted before sending the notebook and the recipient should still be able to play it.

$\endgroup$
4
  • $\begingroup$ Thank you for suggestions. I've tried both. Creating a reference from directory where notebook lies, works fine. The other one unfortunatelly not. I still need to import .wav first. And of course this means defining the location in folder. $\endgroup$
    – MajaZupa
    Commented Nov 17, 2014 at 16:19
  • $\begingroup$ @MajaZupa You need to import it once on your end. After you evaluate the SetOptions line, you don't need it anymore. Please read the last line in the answer. $\endgroup$
    – rm -rf
    Commented Nov 17, 2014 at 16:32
  • $\begingroup$ +1 Really useful. I didn't know about TaggingRules $\endgroup$ Commented Dec 6, 2014 at 16:49
  • $\begingroup$ I did the second option exactly as you described and it works perfectly. After the file is loaded in Mathematica notebook I can (*comment*) the first two lines. Even if I hit Quit Kernel->local, it will still work when I "Evaluate notebook". Here is the example: (sound=First@Import["F:/Users/disk_F/Documents/FAKS/MEHanika_\ konstrukcij/vaja7in8/Komad.mp3"]; SetOptions[EvaluationNotebook[],TaggingRules[Rule]{"SoundData"[Rule]\ sound}]) Sound@CurrentValue[{TaggingRules, "SoundData"}]; EmitSound[%] $\endgroup$
    – MajaZupa
    Commented Dec 12, 2014 at 20:56
1
$\begingroup$

You comment: I've also tried simply to copy the graphics that is displayed when I import a sound. But then I don't know how to get a sound out of it automatically.

You can work with sound files by dragging and dropping, then extracting the desired parts from the sound objects. For example, you can type

snd = 

and then drag and drop the .wav file into the notebook. What you see is:

enter image description here

Now, to access the data within, you can look at the parts. If you type

snd[[1]]

you see a "SampledSoundObject". If you type

snd[[1,1]]

you see a pair of lists that are the stereo (left and right) channels of data. Using

snd[[1,1,1]]

gives the sampled data in the left channel and snd[[1,1,2]] gives sound data in the right channel. You can deal with these latter exactly as you normally would any list. For example,

ListPlot[snd[[1,1,1]],PlotRange->All]

plots it. You can rebuild the sound object directly by reversing the above procedure:

Sound[SampledSoundList[snd[[1,1,1]], 44100]]

After evaluation it looks and acts just like the graphic above. When you save the notebook, all of these steps are saved and there is no need to import the sound file the next time you (or someone else) opens it -- the sound data is embedded in the notebook. You can tell this is happening by looking at the size of the notebook: sound objects can be large, and the size of the notebook (in MB) will grow rapidly if you import a bunch of long sound objects.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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