80.2.2 Opening and Closing Device Drivers

After getting the capabilities of an audio device, you must open the device before you can use it. Audio devices aren't guaranteed to be shareable, so a particular device might not be available when you request it. If this happens, you should notify the user and allow the user to try to open the device again. When you open an audio device, you should close the device as soon as you finish using it.

Windows provides the following functions to open and close different types of audio devices:

Function Description

midiInOpen Opens a specified MIDI input device for recording.
midiInClose Closes a specified MIDI input device.
midiOutOpen Opens a MIDI output device for playback.
midiOutClose Closes a specified MIDI output device.
waveInOpen Opens a waveform input device for recording.
waveInClose Closes a specified waveform input device.
waveOutOpen Opens a waveform output device for playback.
waveOutClose Closes a specified waveform output device.

These functions are discussed in detail later in this chapter.

80.2.2.1 About Device Handles

Each function that opens an audio device takes as parameters a device ID, a pointer to a memory location, and some parameters unique to each type of device. The memory location is filled with a device handle. Use this device handle to identify the open audio device when calling other audio functions.

The distinction between audio-device IDs and audio-device handles is subtle, but very important. Don't confuse the two in your application. The differences between device IDs and device handles are as follows:

Device IDs are determined implicitly from the number of devices present in a system, which is obtained by using the . . . GetNumDevs functions.

Device handles are returned when device drivers are opened by using the . . . Open functions.

The only functions that take device IDs as parameters are the . . . GetDevCaps, . . . Open, and . . . Volume functions. All other functions take device handles.

There are no functions for opening and closing auxiliary audio devices. Auxiliary audio devices don't need to be opened and closed like MIDI and waveform devices because there is no continuous data transfer associated with them. All auxiliary audio functions take device IDs to identify devices.