Summary: The article briefly introduces Microsoft® DirectX® and its application in VB multimedia programming. DirectX® is the hardware application interface (APIS) launched by Microsoft®, which includes DirectX® Graphics® and DirectX® Audio®, using it to easily implement two-dimensional, three-dimensional animation, audio data segment playback and other functions. DirectMusic® is one of the basic components of DirectX®, mainly used for playback of files such as MIDI, WAVE. This article focuses on its application in VB multimedia programming.
Keywords: DirectX®, VB, API, DirectMusic®, MIDI
First, DirectX introduction
Microsoft® DirectX® is a series of underlying program interfaces for game development and high quality multimedia applications. It includes support for two-dimensional, three-dimensional graphics, sound, music, and input, which can even support network communication in the application, such as online games that allow multiple users to participate. DirectX includes the following sections: DirectDraw®, Direct3D®, DirectMusic®, DirectSound®, DirectInput®, DirectPlay®.
DirectX APIS
DirectX Graphics
DirectX Audio
DirectShow
DirectDraw
Direct3D
DirectSound
DirectMusic
DirectInput
DirectPlay
DirectSetup
DirectDraw®
: An component of the DirectX API, it can directly output the contents of the display buffer, providing direct access to the display device and reserves compatibility on the Windows graphics device interface.
Direct3D®: Provides a 3D graphic programming interface.
DirectMusic®: Unlike DirectSound, it is not designed for digital audio capture and playback, but a message mechanism-based interface that converts digital music into sound samples through hardware or software synthesizers and playback. The use of DirectX in VB, MIDI music is also mainly used to use this set of programming interfaces.
DirectSound®: It provides high efficiency mixing, hardware acceleration, and direct access to sound devices.
DirectInput®: API for inputting devices such as mouse, keyboard, game controller, etc.).
DirectShow®: It is a streaming media constructor of the Microsoft Windows platform that uses DirectShow technology to make the application complete high quality audio, video capture, and playback.
DirectPlay®: Support for multi-user online games.
DirectSetup®: Install the API interface of the DirectX component.
Second, use DirectX® to implement the playback of the MIDI file in VB
To use DirectX in VB, first initialize the system environment as follows:
1. Start Microsoft Visual Basic 6.0, create a new standard EXE file (STANDARD EXE FILE);
2. Select the "Project" menu and select "References" in the menu;
3. After selecting "DirectX8 for Visual Basic Type Library" in the list box, click the "OK" button.
Through the above settings, DirectX8 APIs are successfully referenced in the Visual Basic programming environment, and now you can easily use the DirectX8 API function.
Three steps in MIDI back: initialization, play, and stop using DirectX program: initialization, play, and stop, release objects. That is, the DirectMusic object is initialized (including the creation of objects, data segment loading, and sound channel initialization) before calling and playing back MIDI files. The following functions can achieve this initialization process: 1.DirectMusicPerformanceCreate: Create a DirectMusicPerFormance8 object
Call: Object.directMusicPerFormanceCreate () As DirectMusicPerFormance8
Parameters: Object: Directx8 object
Return: DirectMusicPerformance8 object
Error: If the call is unsuccessful, Err.Number is set
2.DirectMusicLoaderCreate: Create a DirectMusiCloader8 object
Call: Object.directMusicLoadercreate () as DirectMusiCloader8
Parameters: Object: Directx8 object
Return: DirectMusicLoader8 object
Error: If the call is unsuccessful, Err.Number is set
3. Initaudio: Initialize the Performance object, let the sound can play normally, this function should be called first.
Call: Object.initaudio (HWND As Long, LFlags as const_dmus_audio, _
Audioparams as dmus_audioparams, _
[DirectSound As DirectSound8], _
[LDEFAUSIC_STANDARD_AUDIO_PATH, _
[LPCHANNELCOUNT As long])
Parameters: Object: DirectMusicPerFormance8 object
HWND: Create the window handle used by DirectSound, if 0, use the front desk window;
LFLAGS: Enumeration Type const_dmus_audio variable, used to specify the desired features. This structure includes:
DMUS_AUDIOF_3D: 3-D buffer
DMUS_AUDIOF_ALL: Combination of other various markers
DMUS_AUDIOF_BUFFERS: Multi Buffer
DMUS_AUDIOF_EAX: EAX Effect
DMUS_AUDIOF_ENVIRON: Environmental Model
DMUS_AUDIOF_STREAMING: Supports streaming media type
Audioparams: Structure Type DMUS_AUDIOPARAMS variable. By passing the specified parameters to the music synthesizer, you can also read the current parameter settings. This structural types include:
CLSIDDEFAULTSYNTH: Current Default Music Synthesizer
FinitNow: Boolean variables for designating whether the synthesis slot and synthesizer are established immediately
LFEATURES: Specifies possible channel logo
LsampleRate: Synthesis Tank and Synthesizer Frequency, range 11kHz to 96kHz, default 22khz
LvalidData: Specifies which parts of this structure contain valid data signs
LVoices: Sound Quantity, default is 64
DirectSound: As a DirectSound8 object that is default (this item is optional)
LPCHANNELCOUNT: Specify the number of Performance channels
Return Value: If the call is unsuccessful, Err.Number is set
After understanding the initialization process, the initializer can be written with VB. Before using the function, you should first create an object: DIM DX8 AS New DirectX8 'Defines DirectX8 objects.
DIM DXML As DirectMusicLoader8 'Defines DirectMusiCloader objects
DIM DXMP As DirectMusicPerFormance8 'Defines DirectMusicPerFormance objects
DIM DXMS as DirectMusicsEgment8 'Defines DirectMusicsEgment objects
DIM AUDPARAMS AS DMUS_AUDIOPARAMS 'Defines DMUS_AUDIOPARAMS Variables
The audio initialization function is as follows:
Private subinit_audio ()
Set DXMP = DX8.DirectMusicPerFormanceCreate 'Established DirectMusicPerFormance Objects
Set dxml = dx8.directMusicLoaderCreate 'Established DirectMusiCloader object
'Initializing the DirectMusicPerFormance object
Dxmp.initaudio me.hwnd, dmus_audiof_all, audparams, _
Nothing, DMUS_APATH_SHARED_STEREOPLUSREVERB, 16
DXMP.SETMASTERAUTODOWNLOAD TRUE
End Sub
At this point, the initialization process is completed, and the MIDI file can now be loaded and played back. To make playback and stop to use three functions:
1.LoadSegment: Load audio data segment file
Call: Object.LoadSegment (filename as string) as DirectMusicSegment8
Parameters: Object: DirectMusiCloader8 object
FILENAME: Specified file name
Returns: DirectMusicsEgment8 object
Error: If the call is unsuccessful, Err.Number is set
2.PlaySegmentex: playback data segment
Call: Object.PlaySegmentex (Source as unknown, _
Lflags as const_dmus_segf_flags, _
StartTime as long, _
[from as unknown], _
[Audiopath as unknown]) as DirectMusicsEgmentState8
Parameters: Object: DirectMusicPerFormance8 object
Source: DirectMusicsEgment8 object to be played back
LFLAGS: Sign for modifying method behavior
StartTime: Start playback
From: DirectMusicSegmentState8 or DirectMusicaudiopath8 object (optional)
Audiopath: Points to DirectMusicaudiopath8 (optional)
3.Stopex: Stop playback
Call: Object.Stopex (ObjectTostop as unknown, _
Lstoptime as long, _
Lflags as long
Parameters: Object: DirectMusicPerFormance8 object
ObjectTostop: DirectMusicSegment8, DirectMusicSegmentState8 or DirectMusicaudiopath8 object Lstoptime: Specify stop time
LFLAGS: Specifies the stop event flag
We can specify the MIDI file to be played back through a "Open" dialog box, the program code is as follows:
CommonDialog1.filter = "* .mid | * .mid"
CommonDialog1.showopen
If CommonDialog1.FileName <> "" "
Me.caption = comMmondialog1.filename
SET DXMS = DXML.LOADSEGMENT (Commondialog1.FileName) 'Points the file name points to the file name
IF Right $ (Commondialog.FileName, 4) = ".mid" Then
Dxms.setStandardMidiFile
END IF
END IF
Once the data segment file is successfully loaded, the sound playback and stop operation can be implemented with the following code.
DXMP.PLAYSEGMENTEX DXMS, DMUS_SEGF_DEFAULT, 0 'playback
DXMP.Stopex DXMS, 0, DMUS_SEGF_DEFAULT 'Stop
You must remember to release the object when you end the program!
IF not (dxms is nothing) THEN DXMP.STOPEX DXMS, 0, 0
SET DXMS = Nothing
SET DXML = Nothing
IF not (dxmp is nothing) THEN DXMP.CLOSEDOWN
SET DXMP = Nothing
Below is a MIDI player written in VB, most of which are described in detail above. Other methods of DirectMusicPerFormance8 objects are used in the program. This part is not a focus, only simple comments in the source code, interested friends can view information about DirectX SDK.
'Program debugging environment: Microsoft Windows 9x / Me, DirectX8.0, Microsoft VisualBasic 6.0
Common declarations
DIM DXML As DirectMusicLoader8 'Defines DirectMusiCloader objects
DIM DXMP As DirectMusicPerFormance8 'Defines DirectMusicPerFormance objects
DIM DXMS as DirectMusicsEgment8 'Defines DirectMusicsEgment objects
DIM AUDPARAMS AS DMUS_AUDIOPARAMS 'Defines DMUS_AUDIOPARAMS Variables
DIM DX8 AS New DirectX8 'Defines DirectX8 Objects
Private submmand1_click ()
Command1.enabled = false 'Settings button properties
Command2.enabled = TRUE
Command3.enabled = false
DXMP.PLAYSEGMENTEX DXMS, DMUS_SEGF_DEFAULT, 0 'Play Music
End Sub
Private sub fascist2_click ()
Command1.enabled = true 'Settings button Properties Command2.enabled = FALSE
Command3.enabled = TRUE
DXMP.Stopex DXMS, 0, DMUS_SEGF_DEFAULT 'Stop playing music
End Sub
Private submmand3_click ()
CommonDialog1.filter = "* .mid | * .mid | * .wav | * .wav | * .sgt | * .sgt" Settings Open File Filter
CommonDialog1.showopen
If CommonDialog1.FileName <> "" "
Me.caption = comMmondialog1.filename
SET DXMS = DXML.LOADSEGMENT (Commondialog1.FileName) 'Points the file name points to the file name
If Right $ (Commondialog1.FileName, 4) = "." If the file is the MIDI file, set the segment property to
Dxms.setstandardmidifile 'standard MIDI file
END IF
Command1.enabled = TRUE
Command3.enabled = false
END IF
End Sub
Private sub flow_load ()
Command1.enabled = false
Command2.enabled = false
Command3.enabled = TRUE
INIT_AUDIO 'initialized audio
End Sub
Private Sub Form_Unload (Cancel AS Integer)
Clear_audio
End Sub
Private sub slider1_scroll ()
DXMP.SETMASTERVOLUME SLIDER1.VALUE 'Sets the master volume
End Sub
Private sub slider2_scroll ()
DXMP.SETMASTERTEMPO SLIDER2.VALUE 'Sets the master playback speed
End Sub
Private subinit_audio ()
Set DXMP = DX8.DirectMusicPerFormanceCreate 'Established DirectMusicPerFormance Objects
Set dxml = dx8.directMusicLoaderCreate 'Established DirectMusiCloader object
'Initializing the DirectMusicPerFormance object
Dxmp.initaudio me.hwnd, dmus_audiof_all, audparams, _
Nothing, DMUS_APATH_SHARED_STEREOPLUSREVERB, 16
DXMP.SETMASTERAUTODOWNLOAD TRUE
End Sub
Private sub clear_audio ()
DXMP.RemovenotificationType DMUS_NOTIFY_ON_SEGMENT
IF not (dxms is nothing) THEN DXMP.STOPEX DXMS, 0, 0
SET DXMS = Nothing
SET DXML = Nothing
IF not (dxmp is nothing) Then DXMP.CLOSEDOWNSET DXMP = NOTHING
End Sub
Reference: Microsoft Developer NetWork (MSDN®)
Microsoft DirectX8® Software Developing Kit
(
SDK
)