Index   Commented   Search   About

Using a Callback Function to Process Audio Driver Messages

You can also write your own callback function to process messages sent by the device driver. To use a callback function, specify the CALLBACK_FUNCTION flag in the dwFlags parameter and the address of the callback in the dwCallback parameter of the device-opening function.

Messages sent to a callback function are similar to messages sent to a window, except that they have two doubleword parameters instead of one unsigned-integer and one doubleword parameter.

Callback functions for the multimedia audio services are often called from another thread running asynchronously from the application's thread. It is therefore important to use critical sections to protect data shared between the callback routine and the rest of the application. For more information about critical sections, see Synchronization.

In 16-bit Windows, there is a limited set of APIs that an audio callback function may call. In 32-bit Windows, that strict limitation has been removed. However, it is strongly recommended that a 32-bit audio callback function restrain its calls to a similar set of functions. Here is the recommended list:

EnterCriticalSection
ReleaseSemaphore
LeaveCriticalSection
SetEvent
midiOutLongMsg
timeGetSystemTime
midiOutShortMsg
timeGetTime
OutputDebugString
timeKillEvent
PostMessage
timeSetEvent
PostThreadMessage

If your audio callback function does stray from this list, it should be careful not to call APIs that take a long time to complete. In particular, calling wave and midi APIs may result in a deadlock.

If an audio callback shares data with other code, a Critical Section or similar mutual exclusion mechanism should be used to protect the integrity of the data.

Use one of the following techniques to pass instance data from an application to a callback function residing in a dynamic link library:

  • Pass the instance data using the dwInstance parameter of the function that opens the device driver.

  • Pass the instance data using the dwUser member of the WAVEHDR and MIDIHDR structures that identify an audio data block being sent to a device driver.

If you need more than 32 bits of instance data, pass a pointer to a structure containing the additional information.

Related Links

Software for Delphi and C++ Builder developers
Software for Visual Studio .NET developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET
TMS Scripter Studio Pro components for Delphi/C++Builder

More Online Helps

Win32 Programmer's Reference (win32.hlp)
OLE Programmer's Reference (ole.hlp)
Microsoft Windows Pen API Programmer's Reference (penapi.hlp)
Microsoft Windows Sockets 2 Reference (sock2.hlp)
Microsoft Windows Telephony API (TAPI) Programmer's Reference (tapi.hlp)
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008 Free Tect Secrets ;) greatis just4fun network just4fun