CD Ripping in VB Part 1
Use the CDEx Ripping Library from within your own applications
This sample looks at ripping CDs from VB using the CD ripping library provided with CDEx. In addition to providing a VB compatible wrapper around the CD ripping functions, it provides a buffered WAV file writer which can be reused for other applications such as audio recorders and processors.
About the CDRip Library
The CDRip.DLL library is a core component of CDEx. CDEx is released under the GNU General Public Licence (GPL), which means that any component which uses this library must also be released under the GPL, unless you obtain agreement otherwise from the author. It goes without saying that if you want to use it, then you should be doing something more useful than simply duplicating part of/all of CDEx. The rather lame (small l) UI in the sample provided here is just a test harness to exercise the functions of the library. I encourage you to download CDEx and try it; it's a neat application.
CDRip.DLL is a Win32 DLL which means you should put it somewhere VB will find it; when using the IDE it is easiest to use the Windows\System directory, but you can put it in the same directory as the executable once deployed.
CDRip provides three main areas of functionality:
These functions are made available through a series of function calls from the DLL, but they're easier to use if wrapped into more specific classes. Therefore the implementation here provides the following VB wrapper code:
I'll cover these in turn.
This class is needed before you use the other functions. The Create method is used to initialise the CDRip library. The library persists settings into an INI file, and you can specify the location of this as a parameter. The CDDriveCount and CDDrive properties can then be used to obtain information about drives on the system.
This class provides the ability to configure how the ripper uses the drive, to play CDs and to obtain table of contents information.
In my experience, I've never needed to adjust the ripper parameters, but they can be adjusted using these methods. To be honest, I'm not certain what most of them do.
Playing CDs is accomplished through a handful of functions:
Other functions are as follows:
This class provides the table of contents for a drive, and also provides a convenient way to obtain the CDDB Id and CDDBQuery for the disc, which can be used with the code in the CD Track Listings to obtain album, artist and track name information.
This class provides information about a single track on the CD.
There are also various other methods to obtain the start time and length in various units and formats, which should be self-explanatory.
There are two ways of using the track ripper: either to extract a particular track based on its cTocEntry information (using CreateForTrack) or to extract a section of information from a cDrive (using CreateForSection. Note that the start and end sectors are specified in CD sectors, or 1/75 of a second.)
Once the create method has been called, use OpenRipper to initialise the ripper for reading. Once this has succeeded, you call Read repeatedly until it returns False; each read will populate the ripped data into the buffer which can be accessed through ReadBufferPtr and ReadBufferSize. Finally, when finished you call CloseRipper.
Writing a Wave File
A .WAV File is a fairly straightforward file format which uses an extensible file format based around the concept of "chunks" to define the contents. Chunks have headers which describe the type of the content (using a scheme called FourCC, in which the unsigned 32 bit dword created from four ASCII bytes is used as the tag), the size of the raw content, and then the raw content itself. In a .WAV file, you need at least three chunks:
The Windows multimedia IO functions make it easier to manage chunks, and will automatically keep track of the amount of content written to an individual chunk and append it to the header. For a .WAV file, you need to do the following (error handling omitted for clarity):
The code for this is wrapped up in the cWavWriter class. This simply has a OpenFile, WriteWavData and CloseFile methods.
This sample demonstrates how to use CDRip.DLL to copy tracks from CDs to WAV audio files. The next sample in this series looks at modularising the code and providing a plugin system which allows you to encode directly to other file formats; in particular MP3.