Platform SDK: DirectX

Capturing Music

[Visual Basic]

This topic pertains only to applications written in C++. DirectX for Visual Basic does not support music capture.

[C++]

Capturing MIDI messages from a device such as a keyboard is very easy in DirectMusic.

Typically, you would create a port for the capture device and use its IDirectMusicPort::SetReadNotificationHandle method to cause an event to be signaled whenever messages are available to be read. In response to the event, call the IDirectMusicPort::Read method repeatedly to place pending events into a buffer until S_FALSE is returned. Each time Read is called, as many events are put into the buffer as are available, or as fit into the buffer. If at least one event was put into the buffer, S_OK is returned.

To retrieve events from the buffer, call the IDirectMusicBuffer::GetNextEvent method. Each call retrieves a single event until no more are available, at which point S_FALSE is returned.

The following code example illustrates this process:

/* Assume that hEvent was created with CreateEvent and
   given to the capture port pPort by a call to 
   SetReadNotificationHandle. Assume also that pBuffer was
   initialized by IDirectMusic::CreateMusicBuffer. */
 
REFERENCE_TIME rt;
DWORD          dwGroup;
DWORD          cb;
BYTE          *pb;
 
DWORD dw = WaitForMultipleObjects(1, hEvent, FALSE, INFINITE);
for (;;)
{
    hr = pPort->Read(pBuffer);
    if (hr == S_FALSE)
      {
          break;  // No more messages to read into the buffer
      }
    pBuffer->ResetReadPtr();
    for (;;)
    {
        hr = pBuffer->GetNextEvent(&rt, &dwGroup, &cb, &pb);
        if (hr == S_OK)
        {
            // pb points to the data structure for the message, and
            // you can do anything that you want with it.
        }
        else if (hr == S_FALSE)
        {
            break;  // No more messages in the buffer
        }
    }  // Done with the buffer
}  // Done reading pending events

If you do not want to intercept messages, but simply want to send them from one port to another, you can use the IDirectMusicThru interface.