TIP Converting AMR to WAV and vice versa

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Contents

[edit] Introduction

Playing around with modern mobile phones you have probably got to the moment when you wanted to convert a favourite WAV (MP3, OGG Vorbis etc.) file to AMR or vice versa - AMR to WAV and Co.

Note: the scripts posted here are not rocket science: it might work out of the box or with a little tweaking from your side.

[edit] Requisites

Emerge sox, vorbis-tools and lame. Build the 3GPP reference converter files (compiled with these changes: makefile without -DETSI and add #include "sp_dec.h" to decoder.c).

[edit] Scripts

The script is tweaked taking into accound the usual defaults. You might still need some changes. It also corrects the part that was giving a headache in the original script: $FILE.$TEMP to $FILE.raw. The script uses decoder for converting the AMR to RAW, then sox for RAW to WAV and then oggenc or lame encodes the WAV to OGG Vorbis or MP3. Change the sox, oggenc and lame options so as to suit your needs. With this configuration, I didn't notice loss in quality with AMRs created by a Siemens S75.

[edit] Convert AMR to OGG Vorbis

File: /usr/local/bin/amrtoogg
#!/bin/bash

# By Aquarion - Aquarion@Aquarionics.com
# Do what you want with it, it's not rocket science.

##Change these:
#Wherever you put encode and decode when you compiled them:
CODEC=/usr/local/bin

#Temporary directory. Chances are you've already got this set
TEMP=/tmp

#Where do the final MP3s go?
FINAL=/home/user/media/siemens/ogg

for file in *.amr; do 
	FILE=`echo $file | sed -e "s/.amr//"`; 
	echo -n "$FILE [AMR] -> [RAW]"
	$CODEC/decoder $file $TEMP/$FILE.raw > log.std 2> log.err; 
	echo -n " -> [WAV] "
	sox -r 8000 -w -c 1 -s $TEMP/$FILE.raw -r 44100 \
		-w -c 1 $TEMP/$FILE.wav > log.std 2> log.err; 
	echo -n " -> [OGG] "
	oggenc $TEMP/$FILE.wav -q 5 -o $FINAL/$FILE.ogg --quiet \
		-t "$FILE" -a "Sanda" -l "Anul nou" -d `date +%Y`
	echo  " :-) "
	rm $TEMP/$FILE.wav;
	rm $TEMP/$FILE.raw;
done

[edit] Convert AMR to MP3

File: /usr/local/bin/amrtomp3
#!/bin/bash

# By Aquarion - Aquarion@Aquarionics.com
# Do what you want with it, it's not rocket science.

##Change these:
#Wherever you put encode and decode when you compiled them:
CODEC=/usr/local/bin

#Temporary directory. Chances are you've already got this set
TEMP=/tmp

#Where do the final MP3s go?
FINAL=/home/user/media/siemens/mp3

for file in *.amr; do 
	FILE=`echo $file | sed -e "s/.amr//"`; 
	echo -n "$FILE [AMR] -> [RAW]"
	$CODEC/decoder $file $TEMP/$FILE.raw > log.std 2> log.err; 
	echo -n " -> [WAV] "
	sox -r 8000 -w -c 1 -s $TEMP/$FILE.raw -r 44100 \
		-w -c 1 $TEMP/$FILE.wav > log.std 2> log.err; 
	echo -n " -> [MP3] "
	lame $TEMP/$FILE.wav $FINAL/$FILE.mp3 --preset standard --silent \
		--tt $FILE --ta Sanda --tl Anul\ Nou --ty `date +%Y`
	echo  " :-) "
	rm $TEMP/$FILE.wav;
	rm $TEMP/$FILE.raw;
done

[edit] Convert OGG, MP3 etc. to WAV

For converting OGG vorbis or MP3 to AMR look at the replies (search for ffmpeg; never tried it).

[edit] See also

TIP Converting Audio files from one format to another

[edit] External links

How_to_convert_AMR_files_to_MP3 - The original script

Personal tools