80.2.3 Allocating and Preparing Audio Data Blocks

Some low-level audio functions require applications to allocate data blocks to pass to the device drivers for playback or recording purposes. Each of these functions uses a data structure (or header) to describe its data block. The following table identifies these functions and their associated header structures (the MMSYSTEM.H file defines the data structures for these headers):

Function Header Purpose

waveOutWrite WAVEHDR Waveform playback
waveInAddBuffer WAVEHDR Waveform recording
midiOutLongMsg MIDIHDR MIDI system-exclusive playback
midiInAddBuffer MIDIHDR MIDI system-exclusive recording

Before you use one of the functions listed above to pass a data block to a device driver, you must allocate memory for the data block according to the guidelines discussed in the following sections of this chapter.

80.2.3.1 Allocating Memory for Audio Data Blocks

Before preparing a data block, you must allocate memory for the data block and the header structure that describes the data block.

To allocate memory, use GlobalAlloc with the GMEM_MOVEABLE and GMEM_SHARE flags to get a handle to the memory block. Then, pass this handle to GlobalLock to get a pointer to the memory block.

To free a data block, use GlobalUnlock and GlobalFree.

80.2.3.2 Preparing Audio Data Blocks

Before you pass an audio data block to a device driver, you must prepare the data block by passing it to a . . . PrepareHeader function. When the device driver is finished with the data block and returns it, you must clean up this preparation by passing the data block to an . . . UnprepareHeader function before any allocated memory can be freed.

Windows provides the following functions for preparing and cleaning up audio data blocks:

Function Description

midiInPrepareHeader Prepares a MIDI input data block.
midiInUnprepareHeader Cleans up the preparation on a MIDI input data block.
midiOutPrepareHeader Prepares a MIDI output data block.
midiOutUnprepareHeader Cleans up the preparation on a MIDI output data block.
waveInPrepareHeader Prepares a waveform input data block.
waveInUnprepareHeader Cleans up the preparation on a waveform input data block.
waveOutPrepareHeader Prepares a waveform output data block.
waveOutUnprepareHeader Cleans up the preparation on a waveform output data block.