# Playing music locally or from streaming audio. Written by Karl Dahlke. ## MP3 files If you like music as much as I do, mpg123 is the greatest thing since sliced bread, especially when combined with the following alias. ``` alias music="mpg123 -q -C" ``` If you have purchased all 27 Mozart piano concertos by Murray Perahia, and if they are given filenames like Moz-pc01-3.mp3 (third movement of first concerto), then you can play them all with `music Moz-pc*`. Sit back and revel in the music, or just let it play in the background while you do your work. I like to let this run in console 11, with console 12 reserved for superuser functions (logged in as root). Simple keystrokes let you restart the song (in this case songs are movements of concertos), previous song, next song, pause, rewind, fast forward, etc. Beyond this, the author of mpg123 was kind enough to add a feature for me, wherein the music pauses when mpg123 receives signal 10 from another process. With this in mind, establish the following hot keys through your adapter or perhaps using a daemon like esekeyd: sysrq | amixer -q set Master 1- scroll | amixer -q set Master 1+ pause | killall -q -s10 mpg123 I can be in any console, in any application, and adjust the volume of my music, or pause it altogether if I have to concentrate on my work or answer the phone, using the three keys at the upper right. This replicates the layout of the Mac, without the little popping sounds as the volume changes. On some versions of linux, parallel instances of mpg123 can send music into the sound card at the same time. Like Data, in A Matter of Time, you can listen to four pieces of music simultaneously. Perhaps more interesting, you can use the pause key as an ab switch. Start playing a song in console 4, then pause. Switch to console 5 and start playing a song in console 5. Signal 10 now switches between the two songs. If your sound card is monogamous, and you are determined to play multiple songs at once, you can vector through play, like this. mpg123 -q -s foo.mp3 | play -t raw -b 16 -c 2 -e signed -q -r 44100 - mpg123 -q -s bar.mp3 | play -t raw -b 16 -c 2 -e signed -q -r 44100 - ... Edbrowse and mpg123 can play nicely together via the pb or pb.mp3 commands if this is in your .ebrc file. plugin { type = audio/mp3 desc = audio file in mp3 format suffix = mp3 content = audio/mpeg program = mpg123 -q -C %i } When edbrowse encounters a url that ends in .mp3, or has an http content type of mp3, it will play the audio passage, just like any other browser with plugin. ## Other audio files Of course, not all audio is in mp3. Here is another plugin descriptor that handles wav, ogg, etc. Edbrowse will play these files locally via the pb command, or access them from the internet as you browse through various websites. plugin { type = audio/basic desc = audio file in a wave format suffix = wav,voc,au,ogg content = audio/x-wav program = play -q %i } ## MP4 files This plugin extracts the audio from an mp4 video file. Others can be built for .webm etc. This assumes bash is processing the program script. plugin { type = video/mp4 desc = video file in mp4 format suffix = mp4 content = video/mpeg # Must be downloaded from the internet and put into a file down_url program = f4=%i; f3=${f4/%4/3}; ffmpeg -loglevel warning -i $f4 $f3; mpg123 -q -C $f3; rm $f3 } There's a problem with this plugin. On the raspberry pi, and perhaps other machines, system(3) uses sh, and ignores the SHELL environment variable. You can't invoke bash, and thus the inline substitution doesn't work. Here's a safer invokation, just like I use to do in 1982. program = f4=%i; f3=`echo $f4 | sed 's/\....$/.mp3/'`; ffmpeg -loglevel warning -i $f4 $f3; mpg123 -q -C $f3; rm $f3 There's another problem with this plugin. An mp4 url on the internet could be a file or a stream. If it's a stream then you can't download it into a temp file and play it from there. You have to pass it to a music player as a url. A more general approach passes mp4 to /usr/bin/mplayer. To listen to streaming audio, or even streaming video, install mplayer on your system. This is included in some distributions, but not in others. In particular, it is not part of fedora, unless you bring in [these repositories](http://www.fclose.com/1493/mplayer-on-fedora/), and then run yum install mplayer. The manual page for mplayer is quite long; I'm not going to describe its full functionality here. The simplest invocation is `mplayer -quiet foo.mp3`. Yes, you can use it just like mpg123, but it doesn't have as many interactive features, such as pause via signal 10 from another process, so you probably want to stick with mpg123 for local music. To listen to streams through edbrowse, put something like this in your .ebrc file. plugin { type = audio/x-pn-realaudio desc = streaming audio protocol = rtsp,pnm,sdp suffix = rm,ra,ram,mp4,ogg,pls content = audio/x-scpls,audio/mpeg,application/pls+xml program = /usr/bin/mplayer -quiet %i } mpv, a successor to mplayer, can be used as the program in the descriptor above. The arguments are the same. mpv has more options and capabilities. For example, with the following plugin descriptor, you can use it to play CUE sheets. plugin { type = audio/x-pn-realaudio desc = streaming audio protocol = rtsp,pnm,sdp,pls suffix = rm,ra,ram,ogg,mp3,mp4,m3u,m3u8,cue urlmatch = .youtube.com/watch? content = audio/x-scpls,audio/mpeg,application/pls+xml program = /usr/bin/mpv --really-quiet --load-unsafe-playlists %i } A monogamous sound card can cause all sorts of trouble for blind people using a software synthesizer. Your speech has to vector through play, and your music as well, and all your edbrowse plugins. Even if you have an external symthesizer, you might listen to music and browse the web at the same time. If you want to play a sound file or some instructions from a web page, the streams will collide. Even if you pause the music, it still holds the device open. As before, everything has to go through play, which multiplexes all the streams and sends them through one channel to the sound card. I don't even know how to send the output of mpv through play. Yeah, it's easier if you don't have to do this. Multiple programs can open the sound card simultaneously on the pi. ## Internet radio stations . Here is an assortment of [radio stations](http://apps.bmcginty.us/cgi-bin/radio.cgi). Select one, and you will reach a file with just one line, the url for that station. Type g for go, and listen to the music. Type q when finished. If you like the station then make it an alias, like this. ``` alias play70='mpv -quiet --load-unsafe-playlists "http://www.181.fm/tunein.pls?station=181-70s&style=mp3&description=Super%2070s"' ``` This station was selected from the aforementioned website, and it's very good. It has a great variety of music from the 70's, with very few commercials, though you will see some patterns if you listen for several days. Also stations from the 60's, 80's, classical, jazz, sports, even other languages, so enjoy. Here is another plugin that lets you jump right to the music from the station menu, instead of fetching the url and then typing g to go. plugin { type = audio/mp3 desc = audio mp3 reference by url suffix = m3u content = audio/mpegurl down_url program = mpg123 -q -C `cat %i` } Following the lead of the 70s station above, [181 FM](http://www.181.fm) has a wide array of stations. Click on one, unbrowse, look for station= in the html, and paste it into an alias, replacing space with %20. Here are a couple examples. ``` alias playhits='mpv -quiet --load-unsafe-playlists "http://www.181.fm/tunein.pls?station=181-greatoldies&style=mp3&description=Classic%20Hits%20181&jwp=1"' ``` ``` alias playxmas='mpv -quiet --load-unsafe-playlists "http://www.181.fm/tunein.pls?station=181-xmix&style=mp3&description=Christmas%20Mix&jwp=1"' ``` ## MIDI files This plugin uses VLC with one of its plugins to play MIDI files. plugin { type = audio/midi desc = audio file in a midi format suffix = mid content = audio/midi # Downloading the file is optional. down_url # Uses packages vlc and vlc-plugin-fluidsynth. program = vlc -I ncurses --play-and-exit %i > /dev/null 2>&1 } ## Playing music from a list of file names in a file There is a slick way to access all of your music, even if it is stored in many directories. find YourMusicDirectory/ | sort > songlist edbrowse songlist You now have in your buffer a sorted list of all your songs. Search for the one you want, by artist or title, then type g to play it. This isn't directory mode, but edbrowse will go to a file, or play a file, if that line contains the filename relative to your current directory. This is similar to a line that contains a url, g will take you to that web page. But remember, this is not directory mode. It's just a file. Deleting a line will not delete the file, and changing a line will not rename the file. So in some sense it is safer than directory mode.