Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How to generate MP3 silence?

by tlm (Prior)
on Nov 27, 2007 at 03:53 UTC ( #653140=perlquestion: print w/replies, xml ) Need Help??

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.)

the lowliest monk

Replies are listed 'Best First'.
Re: How to generate MP3 silence?
by graff (Chancellor) on Nov 27, 2007 at 04:37 UTC
    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.

Re: How to generate MP3 silence?
by GrandFather (Saint) on Nov 27, 2007 at 05:44 UTC

    Obtain a copy of John Cage's 4'33" and splice bits of it as needed. It should go quite a long way if you are only using 0.1 seconds a time.

    Perl is environmentally friendly - it saves trees
Re: How to generate MP3 silence?
by marto (Cardinal) on Nov 27, 2007 at 11:12 UTC
    Further to graff's post, you may want to take a look at this example where someone wants to achieve something similar to your requirements.

    Hope this helps

Re: How to generate MP3 silence?
by Popcorn Dave (Abbot) on Nov 27, 2007 at 04:01 UTC
    This isn't a Perl solution, but I'm curious if you tried stitching the files together in Audacity? If so, did that work?

    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

      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.)

      the lowliest monk

        Well that does answer my question, and Graff's idea was along the lines I was thinking. If it worked in Audacity, then perhaps a wrapper for Audacity is the way to go.

        Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

        I would love to change the world, but they won't give me the source code

Re: How to generate MP3 silence?
by hawtin (Prior) on Nov 27, 2007 at 13:47 UTC

    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).

Re: How to generate MP3 silence?
by tlm (Prior) on Nov 27, 2007 at 21:35 UTC

    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!

    the lowliest monk

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://653140]
Approved by Popcorn Dave
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-01-25 02:19 GMT
Find Nodes?
    Voting Booth?