&& Linux Sound and Audio ------------------------: This book tries to contain succinct recipes for using and editing audio with the Linux operating system. The main emphasis is on using the command line (bash shell). Topics may include: speech recognition, speech synthesis, sound file splitting and joining, sound effects, music synthesis ... == some tools .. waon - converts waves to notes .. timidity - converts notes to waves To select a device for recording, use set followed by the name of the some difference PLAYERS AND JUKEBOXES and another == music players .. amarok - .. rhythmbox - .. gmusicbrowser .. CONFIGURING THE AUDIO SYSTEM * test speaker channels >> speaker-test -D plug:surround51 -c 6 -l 1 -t wav * Alias for quick command-line volume set (works also remotely via >> alias setvol='aumix -v' * try to fix a ubuntu broken sound server >> sudo killall -9 pulseaudio; pulseaudio >/dev/null 2>&1 & * Disable annoying sound emanations from the PC speaker >> sudo rmmod pcspkr * reload the alsa sound system when it stops playing >> sudo alsa force-reload STREAMING AUDIO FILES The process of 'streaming' an audio file means to make the audio file available to users one little chunk at a time. The advantage of this is that the user can start listening to the sound file before the entire file has downloaded to her computer. * stream an mp3 file 'song.mp3' using the netcat tool >> nc -l -p 2000 < song.mp3 * save the tuesday 14.00pm sbs language program to a file >> mplayer http://media.sbs.com.au/ondemand/audio/Tuesday_ONDemand_SBS_RADIO3_14_00.flv -dumpstream -dumpfile sbs.dump * recode the raw audio obtained above as a wav file 'audiodump.wav' >> mplayer -ao pcm sbs.dump PLAYING SOUND FILES Before you begin playing sound, make sure you've set the master and PCM volume levels with the mixer PLAYING ONE SOUND FILE .... * play audio but dont show video from mp4 >> mplayer -vo null video.mp4 >> mplayer -novideo video.mp4 # the same * play the file 'pentastar.aiff' >> play pentastar.aiff * play the sound file 'new.wav' starting at 20 seconds in the file >> play new.wav trim 20 * play a random '.wav' file somewhere on the computer >> play "$(locate '*.wav' | shuf | head -1)" * play a file by copying to /dev/dsp but doesnt work for me >> sox $(locate '*.wav'|shuf|head -1) -t ossdsp > /dev/dsp * select a 'wav' sound file in the current folder to play >> PS3="Enter a number: "; select f in *.wav;do play $f; break; done * play the MP3 file 'september-wind.mp3' >> mpg321 september-wind.mp3 * play 'in.wav' removing all silence at the beginning >> play in.wav silence 1 0 2% == command line audio file players .. play - the sox player .. mpg321 - for mp3s .. mpg123 - same .. afplay - the osx player ? .. playsound - can play mp3s as well .. lame - .. PLAYING REMOTE FILES .... * play the MP3 stream at the url >> mpg321 http://example.net/broadcast/live.mp3 * play the MP3 stream from a url with a 2MB audio buffer >> mpg321 -b 2048 http://example.net/broadcast/live.mp3 PLAYLISTS OR MULTIPLE SOUND FILES .... * play a random audio file whilst applying a bass boosting effect >> play "$(locate '*.ogg' | shuf | head -1)" bass +3 * play the first three wav files found >> play $(locate '*.wav' | head -3) * play the first three mp3 files in the 'eu' folder tree >> mpg321 $(locate '*/eu/*.mp3' | head -3) * play each 'wav' sound and then sleep for 1 second >> locate '*.wav' | shuf | xargs -I{} echo "play {};sleep 1" | bash * Store a playlist of mp3s on variable and play with mpg123 >> PLAYLIST=$(ls -1) ; mpg123 -C $PLAYLIST RANDOM ORDER PLAYING .... * play in random order '.wav' sound files >> locate '*.wav' | shuf | xargs -I{} play "{}" * play mp3s in the current folder in a random order >> mpg321 -z *.mp3 * play an infinite random loop of mp3s in the current folder >> mpg321 -Z *.mp3 * play all mp3s in a random order and pause for a user key press >> for i in $(locate -i "*.mp3" |shuf); do mpg321 $i; read r; done * play a random list, pause and play again if 'a' is pressed >> for i in $(locate -i "*.mp3" |shuf);{ r=a; while [ "$r" == "a" ]; do mpg321 $i; read -p'>' r; done; unset r; } * play each sound in random order and then sleep for 1 second >> locate '*.wav' | shuf | xargs -I{} bash -c "play {};sleep 2" >> locate '*.wav' | shuf | xargs -I{} bash -c "play \"{}\";sleep 2" * another way to achieve the same thing >> locate '*.wav' | shuf | xargs -I{} echo "play {};sleep 1" | bash In order to kill the above process one needs to press several times. * play in random order '.wav' and '.mp3' sound files >> locate '*.wav' '*.mp3' | shuf | xargs -I{} playsound "{}" * play in a loop random '.wav' sound files from the 'ja' folder tree >> locate '*/ja/*.wav' | shuf | xargs -I{} play "{}" * play in a loop random '.mp3' sound files from the 'eu' folder tree >> locate '*/eu/*.mp3' | xargs -I{} mpg321 -z "{}" >> locate '*/eu/*.mp3' | shuf | xargs -I{} mpg321 "{}" * create and use a function to play a random loop of sound in a folder >> so(){ locate "*/$1/*.mp3" | shuf | xargs -I{} mpg321 "{}"; }; so eu * randomly play a mp3 file >> mpg123 "`locate -r '\.mp3$'| awk '{a[NR]=$0}END{print a['"$RANDOM"' % NR]}'`" * present a menu of mp3 files with 'indo' in the name and play one >> IFS=$'\n';select f in $(locate '*indo*mp3'); do mpg321 $f; done * the same as above but slower (because of -r regular expressions) >> IFS=$'\n';select f in $(locate -r '.*indo.*.mp3$'); do mpg321 $f; done The 'IFS=' trick above allows the command to handle sound files with spaces in their names. * a bash function to find and play sound with a particular name/path >> so(){ select f in $(locate "*$1*mp3"); do mpg321 $f; done;}; so eu * a bash function to choose and play a sound file ------------------------------------------------- function indo { echo 'searching for sound files ...'; IFS=$'\n'; select f in $(locate -r '.*indo.*.mp3$'); do mpg321 $f; done } ,,, Buffering audio may be useful for when the system is running many processes or otherwise has a lot of activity, * play the MIDI file 'copa-cabana.mid' >> playmidi copa-cabana.mid * play the MIDI file 'copa-cabana.mid' on a non-MIDI sound card >> playmidi -f copa-cabana.mid while [ "$r" != "q" ] ;do read -p'>' r; done; unset r INTERNET RADIO == internet radio tools .. get-iplayer - bbc iplayer radio stations .. mimms - down load mms:// streams .. * play random music from station 'blip.fm' >> mpg123 `curl -s http://blip.fm/all | sed -e 's#"#\n#g' | grep mp3$ | xargs` * Listen to BBC Radio from the command line. >> bbcradio() { local s;echo "Select a station:";select s in 1 1x 2 3 4 5 6 7 "Asian Network an" "Nations & Local lcl";do break;done;s=($s);mplayer -playlist "http://www.bbc.co.uk/radio/listen/live/r"${s[@]: -1}".asx";} * listen to an internet radio station with 'mplayer' >> mplayer mms://server:port/path * plays music from somafm >> read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls * Play music from youtube without download >> wget -q -O - `youtube-dl -b -g $url`| ffmpeg -i - -f mp3 -vn -acodec libmp3lame -| mpg123 - PLAY LISTS == playlist formats .. .m3u .. XMLPlaylist .. * add 10 random unrated songs to xmms2 playlist >> xmms2 mlib search NOT +rating | grep -r '^[0-9]' | sed -r 's/^([0-9]+).*/\1/' | sort -R | head | xargs -L 1 xmms2 addid * Generate a playlist of all the files in the directory, newer first >> find . -type f -print0 | xargs -r0 stat -c %Y\ %n | sort -rn | gawk '{sub(/.\//,"",$2); print $2}' > /tmp/playlist.m3u EDITING SOUND FILES == graphical sound editing tools .. audacity - the most popular graphical sound editor .. sweep - .. rosegarden - another graphical editor .. jokosher - another sound editor == command line sound editing .. sox - perhaps the best tool .. cutmp3 - a command line interactive editing tool for mp3 files only .. quelcom - command line editing of wav and mp3 audio .. * open the sound file 'mixdown.wav' in the program 'snd' >> snd mixdown.wav SOX PIPE LINES * pipe the result of one sox command to another sox command >> play "|sox -n -p synth 2" "|sox -n -p synth 2 tremolo 10" stat MIXING Mixing is the process of overlaying 2 or more sounds to create a new one. * mix together two audio files and save a 'mixed.flac' >> sox -m music.mp3 voice.wav mixed.flac SPLITTING SOUND FILES Splitting sound files refers to dividing a single sound file up into several smaller files each containing a time interval of the original file. == tools .. mp3splt - splits mp3/ogg files on silence .. oggsplt - the same as 'mp3splt' .. sox - sox can split of silence .. * copy the first 40 seconds of 'old.wav' in the sound file 'new.wav' >> sox old.wav new.wav trim 0 40 * Split lossless audio (ape, flac, wav, wv) by cue file >> cuebreakpoints | shnsplit -o