Linux Online
[ Register ]

[ Applications ]
[ Documentation ]
[ Distributions ]
[ Download Info ]
[ General Info ]
[ Book Store ]

Advertisement

[ Courses ]
[ News ]
[ People ]
[ Hardware ]
[ Vendors ]
[ Projects ]
[ Events ]
[ User Groups ]
[ User Area ]

Linux Kernel Development

[ About Us ]
[ Home Page ]
[ Advertise ]

Lesson: Using SoX

Using SoX with Ogg Vorbis

One of the most important projects going on in the Open Source world is the Ogg Vorbis codecs for sound compression. Created by the Ziph.org Foundation, Ogg format aims at replacing the proprietary MP3 format. In many comparisons, Ogg is superior to MP3 in resulting sound quality.

Recording streaming music

Warning

First, I'd like to add a kind of disclaimer. There are radio stations on the Internet that stream in Ogg format. Using the tools we'll talk about in this section, I have managed to capture these streams and save the music to my hard disk. Saving music to your hard disk without purchasing it is kind of a controversial topic, so I have decided to leave out mention of specific stations that do this. I'll limit myself to describe the general process and you can then try these experiments at home on real subjects. Also, the greater your bandwidth, the better the sound and vice-versa. So, if you have a dial-up connection, you're not likely to capture anything that's worth saving, unless you like listening to AM radio on a car's dashboard radio speaker.

First, if you don't have the tools necessary to play and create files in Ogg format, you're going to need those. Major distributions, like RedHat/Fedora , are now including these programs in their standard install. Just in case yours doesn't, you'll need to get the following.

  • libvorbis

  • libvorbis-devel

  • vorbis-tools

  • libogg

  • libogg-devel

To simply play an Ogg stream, all you have to do is type the following in a terminal or Xterm:

ogg123 http://somestation.com/theirstream.ogg

And you should now have their music nicely flowing into your speakers. 'ogg123' is a program for listening to Ogg music via the command line. It takes its name, I assume, from the once ubiquitous 'mpg123' command line MP3 player. Most of the features are the same.

Now, if you want to capture it, we need to complicate this a little more.

ogg123 -d oss -d wav -f mystream.wav http://somestation.com/theirstream.ogg

What this does is capture the stream in *.wav format in a file called 'mystream.wav'. You, of course, can name the file what you'd like. The -d oss option is so you can listen to it at the same time.

There are obvious drawbacks to this system. One is that *.wav format consumes an large amount of hard disk space. There are a couple of ways around that, and I'll talk about them shortly. The other drawback is that you're probably going to get a lot of junk at the beginning and end of the song (DJs talking, commercials, etc). Fortunately, we already know how to use SoX's 'trim' features to get rid of that. The only other drawback that I see is that, in my case, when I'm working and I hear a song I'd like, by the time I have the song capture going, I may have cut off a bit at the beginning. If there's not too much cut off, the song is probably worth saving. However, we can make it sound a little better with a fade in, which is another effect that SoX can do. To get a fade-in effect at the beginning of the file, you can do this:

sox cutoff_file.wav improved_file.wav fade t 3 0 0

The syntax here is a bit confusing at first. First, put the file you want to work on and after the file you want to create. After the effect 'fade', you need to choose a fading style. I have chosen 't' which is a linear style - just a straight fade-in. There are other styles like logarithmic and parabola. Consult 'man sox' for the letter codes for these. The first value is the fade-in length. I have chosen to fade-in for 3 seconds. The next is the point I want to start the fade. Here, 0 corresponds to 0 seconds - ie. the beginning. The last value is the fade-out length. Seeing as we're not fading out in this example, the value is zero. On the other hand, if we wanted to get a fade-out effect, you could do something like this:

sox cutoff_file.wav improved_file.wav fade t 0 242 3

In this example, I've taken a 245 second long file and produced a three second fade out at the end. That is, the file starts fading at 242 seconds until the end.

Now, let's go back to the little problem we have with space and size. As I mentioned, *.wav files are huge and it's probably a good idea to actually convert them back to Ogg format again. Here's one way to do that:

oggenc -b 192 your_file.wav

This is the basic way to put a file in Ogg format at a bitrate of 192, which will give you nice sound quality. If you want to add a little bit of information to your Ogg file, I suggest the following options:

-a "Artist_Name" -t "Song_title" -l "Album" your_file.wav -n "%a_%t.ogg"

Now, there are probably some people reading this who are thinking: 'Hmmm. Ogg to wav and back to Ogg? There's got to be a simpler way.'. And the fact is, there is a simpler way. With the latest version of SoX, you can capture an Ogg stream and convert it more or less directly to a local Ogg file. SoX now converts files to Ogg format.

Note

You need the latest version of SoX, which, at the time of this writing is 12.17.4

To reproduce the stream and save it directly to an Ogg file, you can do the following:

ogg123 http://someradio.wav/stream.ogg -d wav -f - | sox -t wav -r 44100 -s -w -c 2 - mystream.ogg

If you notice, we've slightly modified our above example to pipe the wav file directly to SoX. No wav file is actually created. SoX takes it as input and creates another Ogg file with it. For this to work well, we need to specify more options with SoX. First, the -t specifies the type of sample SoX is getting, which is wav. Secondly, we specify the rate with -r. Then we need to get a little more specific about the samplings of what SoX is receiving. The -s option means 'signed linear', -w is for 16-bit sound, which is pretty standard and -c is for channels. Here we've chosen 2 for stereo sound. The only thing I should point out is that Ogg encoding via SoX is limited to a 128 bitrate at this time, though this is quite acceptable.



Comments: feedback (at) linux.org
Advertising: banners (at) linux.org
Copyright Linux Online Inc.
Compilation ©1994-2008 Linux Online, Inc.
All rights reserved.