#86 Patch for MP2 write support using Twolame

Paul Kelly

Currently sox supports MP2 for reading (using libmad) but not for writing. This can be quite confusing as it doesn't complain when given an output file with an .mp2 extension, but silently writes an MP3 file instead.

The attached patch is an attempt to add MP2 write support to sox using the Twolame library (LGPL). I considered adding MP2 as a separate format handler, but seeing so much of the code is shared with MP3 (including all the decoding code) it seemed better to modify the MP3 format handler to also support MP2 writing.

A few points that might be important:

1. I tried to follow all the logic for supporting dynamic loading of the library and to do it the same way as done for liblame and libmad. I haven't been in a position to thoroughly test this on all platforms, but it works fine on Linux as far as I can see.

2. My code decides to write an MP2 rather than an MP3 file if the character '2' occurs in the filetype. This works for me but I'm not sure if it's robust in all circumstances. However the worst outcome could be that it silently writes an MP3 file, which is the same as the current behaviour, so perhaps not a huge problem.

3. Related to his, if a user attempts to write an MP2 file and Twolame support hasn't been compiled in, sox will now refuse and issue an error message rather than continuing and silently writing an MP3 file. This is arguably an improvement on the current behaviour. But of course it could be changed back if required.

4. The desired bitrate is set from the compression factor in the same way as for MP3 output. The Twolame API seems to indicate that it supports variable bitrate, but there are lots of reports of this not working and no players being able to play the files, so I thought it better to disable variable bitrate support to avoid reports of broken files.

5. Twolame does not have a variable quality parameter, so the fractional part of the compression factor is ignored (for MP3 this is used to set the quality). It does have two quality-related functions, twolame_get_quick_mode() and twolame_set_quick_count(), but defining a mapping between a numeric quality parameter and an appropriate combination of these functions just seemed like too much of a subjective judgement to attempt, so I just leave Twolame to use its default quality.

6. Since MP2/MP3 audio has a finite range of samplerates that it supports, it surprised me that they weren't specified when the format handler was initialised. So I added this in. This is obviously not necessary for the MP2 support and could be removed if not desired.

I hope this patch can be considered for inclusion into the sox source, and I'm happy to respond to comments/feedback and prepare a revised version if necessary.

Best regards

Paul Kelly


  • Paul Kelly

    Paul Kelly - 2012-01-09

    Updated patch to (a) fix bug whereby HAVE_TWOLAME was being checked before before it had been defined, and (b) patch against current version of src/mp3.c.

  • Ulrich Klauer

    Ulrich Klauer - 2013-02-16
    • status: open --> closed-accepted

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks