tlm has asked for the
wisdom of the Perl Monks concerning the following question:

Dear Monks,

My home coding project du jour is to make a collection of MP3 "sound flashcards" for foreign language learning drills. The "basic flashcard" consists of a bit of speech signal (the "challenge"), followed by a silence, followed by some more speech signal (the "answer").

As my starting material are a few hundred "challenge" MP3 files and the matching "answer" MP3 files. My mission is to generate individual MP3 files for each challenge-answer pair. I.e. I need to stitch together a challenge file and its corresponding answer file, and stick some silence in between.

My initial idea was to generate a small "spacer" silence file using Audacity (I generated 0.1 seconds of silence and exported it as MP3), and to use MPEG::Audio::Frame to stitch together the challenge file, an variable number of copies of the spacer file, as needed, and finally the answer file.

Well, no cigar. The resulting stitched up file plays OK in Audacity, but bombs in iTunes. For some reason, iTunes aborts the playback of the file in the middle of the silence between the challenge and the answer.

I observed a lot more weirdness with this than I want to bore you with. Suffice it to say that, despite what Audacity says, I have reasons to suspect that the silence I'm inserting between the challenge and the answer is not all that quiet.

So I'm back to square 1. Can someone suggest a better way to do all this? And more specifically, a better way generate some "MP3 silence" to use as a sound spacer? (FWIW, I'm using OS X Tiger.)

Stitching the files in Audacity works, if by this one means using Audacity to open the files, copying-and-pasting the waveforms side-by-side, and exporting the resulting waveform as MP3. (Of course, this solution is impractical if one has hundreds of such stitchings to do.)

You should probably check out SoX -- it's a command-line tool that is easy to install on osx (except that I think you may need to rename a file in the distro, from "INSTALL" to something different, e.g. "INSTALL.txt", so that "make install" will do the right thing). I haven't used it on mp3 data, but I think the tool supports that... If not, just do everything with wav or raw files, then convert to mp3 afterwards.

The command-line syntax for sox is pretty strange, so making up a perl wrapper script to do what you want on a list of files will be very handy (just trying to keep the thread "on topic" as a perl question...)

UPDATE: In case you seriously want to pursue a "pure perl" solution, my first inclination would be to convert your mp3 files of "challenge" and "answer" utterances into uncompressed raw pcm streams, join them together with a suitable number of 16-bit zeros to make up the appropriate length of silence for the given sample rate and channel count, then convert back to mp3. (If the files are stereo and 22.05 KHz, you need 4410 16-bit zeros to create 0.1 seconds of silence.) Have fun with that.

Another approach would be to do all your audio stuff in an
easier format (WAV for example) and have your perl script
encode it as mp3 as the final step.

When I bring music into my collection I convert all input files to WAV, do all my adjustments (volume normalisation etc) using the WAV format files and then call "lame" at the end to create an mp3 that works
everywhere (well really the final step is tagging my
created mp3, but you get the idea).

Thank you all for your suggestions. I ended up following the gist of graff's advice. I used AIFF (it turned out to be a more convenient choice than raw or WAV in this case) and I used brother Joost's Audio::SndFile to read in the AIFF files and add the silences as packed zeroes. (Granted, this does not produce a pregnant Cagean silence, fraught with meaning and anticipation, but for language flashcards it'll do...)

It all worked like a charm. Mind you, graff's post is the sum total of my education on these matters. (PCM??? Phase-controlled magnetron? Partido Comunista Mexicano? Please call me? ...). I have no idea of what I'm doing... (If someone happens to know of a particularly good "sound-for-cretins"-type book, let me know.)

Now I'm off to learn about LAME and what I can do with it... (Thanks to hawtin for the tip.) Cheers!

When putting a smiley right before a closing parenthesis, do you:

Use two parentheses: (Like this: :) )
Use one parenthesis: (Like this: :)
Reverse direction of the smiley: (Like this: (: )
Use angle/square brackets instead of parentheses
Use C-style commenting to set the smiley off from the closing parenthesis
Make the smiley a dunce: (:>
I disapprove of emoticons
Other