Writing Audio CDs

Audio CD Creator Sample application

This sample demonstrates how to burn audio CDs using the .NET IMAPI Wrapper from WAV files. Sample code is provided in both VB and C#.

Writing Audio CDs

The IMAPI Wrapper exposes both data (Joliet) and audio (Redbook) CD burning facilities. The design of IMAPI means that most of the functions to create a CD are common; the only difference between data and audio CDs is how you construct the image of the CD to be burnt on disc.

If you have not already, then read the article Writing Data CDs first, as that describes how to configure the library, respond to the events and perform an asynchronous burn operation which are all used in this sample. Rather than go over them again here, I'll just cover the differences in the image construction.

The Redbook specification defines how an audio CD should be constructed. Audio CDs can have between 1 and 99 tracks, and each track consists of blocks of audio wave data, arranged as alternate L-R stereo signed 16-bit samples at 44.1kHz sampling frequency. The blocks themselves contain 2352 bytes of audio (or 1/75s) as well as error correction codes to allow CD players to recover from minor scratches. Or not, as seems to happen to most of mine. To construct an audio track using the IMAPI library, you need to have a source of audio data in this format. The easiest to use source of this is a Windows .WAV file at the right frequency. If you'd like to burn MP3 or MP4 files directly to disc, then take a look at the VB Classic Audio samples (these are currently being converted to .NET).

A Windows .WAV file uses the Resource Interchange File Format (RIFF) structure. This structure allows information to be tagged and stored in chunks. A WAV file requires at least three such tagged chunks: a RIFF header, defining the WAV file type; a "fmt" header defining the wave format and a "data" format containing the actual raw wave data, which is what is needed to write an audio CD. The easiest way to get hold of this information is to use the Windows Multimedia API. See the article Reading and Writing Audio Files for more information on how to do this.

Creating the CD

Once we have the ability to read audio data from a Stream, creating the audio CD is simple. Here's the code for creating the CD, in VB. Tracks are added in order using the AddFile method, then the CreateCD transfers the data for each file to the Redbook image, and finally burns the CD:

''' <summary>
''' Delegate representing the CreateCD method
''' </summary>
Public Delegate Sub CreateCDDelegate(ByVal simulate As Boolean, ByVal ejectWhenComplete As Boolean)

''' <summary>
''' Simple wrapper to create an Audio CD from a series of tracks.  By
''' wrapping the function in a class we can call the burn method on
''' a background thread using <c>BeginInvoke</c>.
''' </summary>
Public Class AudioCDCreator
    Private ReadOnly m_discMaster As DiscMaster
    Private ReadOnly m_redbookDiscMaster As RedbookDiscMaster
    Private ReadOnly m_files As ArrayList

    ''' <summary>
    ''' Constructor.  When this class is constructed redbook should
    ''' have been set as the active disc master format and a disc
    ''' recorder needs to have been made active.      
    ''' </summary>
    ''' <param name="discMaster">Disc master object</param>
    ''' <param name="redbookDiscMaster">Redbook Disc master object</param>
    Public Sub New( _
            ByVal discMasterParam As DiscMaster, _
            ByVal redbookDiscMasterParam As RedbookDiscMaster _
        m_discMaster = discMasterParam
        m_redbookDiscMaster = redbookDiscMasterParam
        m_files = New ArrayList()
    End Sub

    ''' <summary>
    ''' Adds a file to record.  Files should be added in track order.
    ''' </summary>
    ''' <param name="fileName">File name of the track</param>
    Public Sub AddFile(ByVal fileName As String)
    End Sub

    ''' <summary>
    ''' Creates an audio CD from the specified files
    ''' </summary>
    ''' <param name="simulate">Simulate CD burning</param>
    ''' <param name="ejectWhenComplete"><c>true</c> to eject the CD
    ''' tray when the burn is complete, <c>false</c> otherwise</param>
    Public Sub CreateCD(ByVal simulate As Boolean, ByVal ejectWhenComplete As Boolean)
        '// Ensure we don't have anything in the stage

        '// Stage the content
        Dim file As String
        For Each file In m_files
            Dim waveStream As WaveStreamReader = New WaveStreamReader(file)

        '// burn the disc
        m_discMaster.RecordDisc(simulate, ejectWhenComplete)

        ''' How easy was that?
    End Sub

End Class


This sample demonstrates how to create audio CDs using VB or C# Managed Code. Although the UI is rudimentary, it shouldn't be difficult to expand on to make a fully featured CD creator.