voice recorder (2): using SoX to record mp3's on the fly


FBnil

They'll own everything and be miserable.
Joined
Dec 14, 2012
Messages
5,116
Location
Yurp
Get sox for the OpenPandora here: http://repo.openpandora.org/?page=detail&app=sox

If you install the pnd and run it once, then /mnt/utmp/sox/bin/ is created. You can reuse that directory until you reboot again (do not close the terminal that it opens, or copy bin include and lib to your directory).

A few different binaries are found there, let's focus on sox for now.

First, we open a terminal and we export two important variables the program needs:


        export LD_LIBRARY_PATH=/mnt/utmp/sox/lib
        export PATH="/mnt/utmp/sox/bin":$PATH

Then, we can run this command:


sox -q -t oss -c 1 /dev/adsp recording.mp3
It will write to recording.mp3. Use Control+C to cancel recording.

These settings will create a 48000 32kb mp3 file. Crisp, yet smaller than wav

Sox has many other tricks up its sleeve, for example:


sox -q -t oss -v 18.0 -c 1 /dev/adsp recording.mp3 trim 0 00:05 tempo 2
It will be quiet, use oss, amplify the sound 18 times, 1 channel (mono), read from adsp internal microphone with compression model 0 and write to recording.mp3 for 5 seconds only, and with twice the normal speed.

You can type "sox --help" or check this webpage for more effects: http://linux.about.com/od/commands/l/blcmdl1_sox.htm

I've written a small memo recorder you can use as a starting point, it is attached. If you rightclick on the desktop you can create a launcher for it.

In order to not write to the nand (bad!) create a symbolic link called "audio" in your homefolder to a directory on your card, like so:


ln -s /media/mycard/mydirectory ~/audio
For more recording related topics, see also:

Pandora Wiki: http://pandorawiki.org/Audio_system#Recording

Voice recording with arecord: http://boards.openpandora.org/index.php/topic/4471-voice-recorder/
 
Last edited by a moderator:
Code:
NOW=`date +"%Y-%m-%d_%H%M"`
PIDFILE=/mnt/utmp/sox/active.pid

if [ -f "$PIDFILE" ];then
	# stop recording
	pid=`cat "$PIDFILE"`
	# kill process
 	msg=`kill $pid`
	notify-send --icon=gtk-media-record.png -t 1000 "done" "Stopped recording $pid $msg"
	rm "$PIDFILE" 
else
	# start recording
	
	# write to symlink to a card
	if [ -d ~/audio ];then cd ~/audio;fi

	# run sox once (and keep it open) to  have this dir, copy the files to your card for permanent files
	if [ -f /mnt/utmp/sox/bin/sox ]; then
		export LD_LIBRARY_PATH=/mnt/utmp/sox/lib
		export PATH="/mnt/utmp/sox/bin":$PATH
		notify-send --icon=gtk-media-record.png -t 2000 "Recording MP3" "$NOW.mp3"
		sox -q -t oss -v 18.0 -c 1 /dev/adsp -C0 $NOW.mp3 &
		# get Process ID to kill it later
		echo $! > "$PIDFILE"
	else
		# fallback to WAV (always available on openpandora)
		notify-send --icon=gtk-media-record.png -t 2000 "Recording WAV" "$NOW.wav"
		arecord -f cd -r 11025 -D hw:0,1 > $NOW.wav &
		echo $! > "$PIDFILE"
	fi
fi
 
Last edited by a moderator:
The voice-recorder has evolved, and I've decided to share what I use and wrap it into an installer PND http://repo.openpandora.org/?page=detail&app=memorecorder-installer-201402

It now has led lights control (c source included) and has been made more robust.

The installer takes care of asking the right questions and guides you (hopefully) through the whole process.

Defaults to ogg (but you can change the ogg in the configuration file to record in mp3)

plans for the future:

* Add record level control (so you never record silence because you forgot to set the volume)

* Dimming recording light (sox can write to stdout the volume of the audio it is processing, these can be changed to a fading record light, handy to know that you are TOO close to the mic)

* starting with shift pressed will grab an alternative file, enabling, for example, low quality, to record reaaaly long things.

---

About the process:

I like these programs that you symlink with another name, and they change functionality. So op_leds if renamed or symlinked as op_backlight can change backlight.

Of course, the symlink uses very little space (rounded up to a block), so I save very little.

I had to get the grand-child pid (to be able to kill the rec binary which is started by the rec.sh). So you fetch your child with $! and then pgrep -P that to get the grand-child pid. Nice.

now if only op_lid worked... still tinkering on that one.
 
Last edited by a moderator:
Why don't you just use, "killall", to stop the recording process?  Normally, there shouldn't be multiple programs trying to record simultaneously.
 
True, but those were like the first versions. Hoever because this is for fun (you have to be a coder to understand, for most of the people this is worse than self-flagellation), I try to mould the code and forge it in special ways, walk new paths and find new uses for code. And yes, sometimes this means overcomplicating things. Who knows, maybe it will come in handy in the future (the pgrep command I'd never used before). But my main motivation was more robustness. Sometimes you have multiple installations of Websphere, with the default webserver name. Now, you can check the path (which is unique), or you can go use $? with pgrep, or check if the lockfile is still locked and get your pid there... the way of programming then becomes engrained and I believe this helps you grow as a coder/programmer.

Like this code, that has a nice 'ring' to it, with a promise that there maybe is a bill2 variable somewhere...


bill=`pidof -s rec`
kill $bill

I could go for a more haiku approach and try to minimize the code.

Thanks for trying it out.
 
Last edited by a moderator:
Back
Top