Tuesday, January 04, 2011

Playing Midi Files with DirectMusic

Introduction
This article focuses on the use of CMidiMusic which allows an easy access to DirectX DirectMusic API. This class allows loading and playing general midi sequence files(.mid). The class is designed to perform midi playback in one segment and offers additional features like using any midi port installed in your system, 3D sound environment, sound effects, etc.

Direct Audio Specifications
The DirectX 8.0 Audio part offers improved integration between DirectSound and DirectMusic, including a great set of new features. Some of these are listed hereafter:
The last version of Direct Audio allows using hardware acceleration for sound synthesis.
With the new AudioPath model, the sound of a port does not go directly to a directsound buffer, instead, it goes to an audiopath which controls data flow from a performance to the final output. The audiopath allows controlling the 3D position of each sound and adding other effects.
The segments are modified independently and you can apply effects like pan, volume, individually.
It allows using DLS2 (Downloadable sound level 2 standard) which provides a great sound quality and unlimited use of instruments with the software synthesizer.
FX (Reverb, Chorus...) if it is available in the software synthesizer.
It overcomes the 16 midi channels limit, allowing the use of as many midi channels as the software is able to handle.
The playback can be controlled accurately in run time by selecting different sets of musical variations and changes in the chords progress.

3D Positioning.
Main Interfaces of DirectMusic
IDirectMusic8: This interface allows managing buffers and ports. There should only exist one instance of this interface per application.
IDirectMusicPerformance8: This is the most important interface in playback management. It is used to add and remove ports, play segments, notify event reception, control music parameters and obtain timing information.
IDirectMusicPort8: This interface provides access to DirectMusicPorts objects like MPU-401 or the software synthesizer.
IDirectMusicSegment8: This interface represents a segment, musical piece made up of multiple tracks. It can contain a midi file, a wave, a segment.
IDirectMusicLoader8: Its main function is to find and load the different objects. These objects are to be stored in a segment.
IDirectMusicSegmentState8: The (playback) engine creates a SegmentState object which allows analyzing the state of the segment currently playing.
IDirectMusicAudioPath8: The IDirectMusicAudioPath8 interface represents the stages of data flow from the data file to the primary buffer.

DirectMusic Architecture
Once a resource has been loaded in a segment, the performance dispatches the messages defined by a tool of an application, such tools are grouped in toolgraphs which process specific segment messages. A tool can modify a message and pass it on, delete it, or send a new message.
Finally, the messages are delivered to the output tool, which converts the data to MIDI format before passing it to the synthesizer. Channel-specific MIDI messages are directed to the appropriate channel group on the synthesizer. The synthesizer creates sound waves and streams them to a device called a sink, which manages the distribution of data through buses to DirectSound buffers.

Read more: Codeproject