C # WinForm uses the digital interface of the instrument,

Source: Internet
Author: User

C # WinForm uses the digital interface of the instrument,

To continue, C # has been implemented before calling Windows API to implement the pop-up dialog box function. The main code for using the User32.dll file is as follows:

[DllImport ("User32.dll")]
Public static extern int MessageBox (int h, string m, string c, int type );
For more information, see: http://hovertree.com/h/bjaf/psjdasa2.htm

The effect is as follows:


I also practiced calling winmm. dll to play the wav audio file. The main code is as follows:
[DllImport ("winmm. dll")]
Private static extern int sndPlaySoundA (byte [] lpszSoundName, int uFlags );

Http://hovertree.com/h/bjaf/jeg0ytf5.htm for details

:


Next, let's continue to call other APIs to call the midi playback function of winmm. dll.

Musical instruments Digital Interface (MIDI) was proposed in early 1980s to solve the communication problem between electroacoustic instruments. MIDI is the most widely used music standard format in the editing industry. It can be called a music score that can be understood by a computer ". It uses the digital control signals of the Notes to record music. A complete MIDI music is only dozens of KB and can contain dozens of music tracks. Almost all modern music is produced and synthesized using MIDI and audio libraries. MIDI transmits instructions such as notes and control parameters instead of sound signals. It indicates what the MIDI device is going to do, how it is going to do, such as which notes to play and how much volume it is. They are uniformly represented as MIDI messages ). Asynchronous serial communication is used for transmission. The standard communication baud rate is 31.25 × (1 ± 0. 01) KBaud.

Therefore, you can call the API to specify the playback instruction in the format for playing midi. Next, we will test and implement the midi function.

First, open the API of the midi output device:
MMRESULT midiOutOpen (
LPHMIDIOUT lphmo,
UINT uDeviceID,
DWORD_PTR dwCallback,
DWORD_PTR dwCallbackInstance,
DWORD dwFlags
);
See http://hovertree.com/h/bjaf/tug59e9l.htm for details.

Call this API in C:
[DllImport ("winmm. dll")]
Private static extern UInt32 midiOutOpen (out UInt32 lphMidiOut, uint uDeviceID, UInt32 dwCallback, UInt32 dwInstance, UInt32 dwFlags );

Here, lphMidiOut is the handle of the device and will be used for playing back the audio.

Then, send the Playback command API to the device:
MMRESULT midiOutShortMsg (
HMIDIOUT hmo,
DWORD dwMsg
);
For more information, see: http://hovertree.com/h/bjaf/4ext7m8l.htm

Call in C:
[DllImport ("winmm. dll")]
Private static extern UInt32 midiOutShortMsg (UInt32 hMidiOut, UInt32 dwMsg );

Here, hMidiOut is the lphMidiOut of the midiOutOpen method.
Next, encapsulate these Apis.

After the Midi output device is enabled, the sound can be played. Add a status in the encapsulated HewenqiMidi class to indicate whether the device is enabled. The code is private bool _ isOpened, therefore, the device has a handle, that is, the lphMidiOut mentioned above. Therefore, a device handle field is added in the class. The handle type is 32-bit unsigned integer UNIT. For details, see uint _ deviceHandle; it is used to store the handle lphMidiOut obtained by midiOutOpen. These two fields can be encapsulated as attributes and read-only attributes.

Next, the main code is to enable the device:
Uint h_Device;
Uint h_r = midiOutOpen (out h_Device, 0, 0, 0 );
When h_r and 0, it indicates that the opening is successful, otherwise the opening is not successful, the specific return value can be referred to: http://hovertree.com/h/bjaf/tug59e9l.htm

Method for playing sound:
Public uint semi play (uint msg)
{
If (_ isOpened)
Return midiOutShortMsg (_ deviceHandle, msg );
Else
Return 621;
}
First, check whether the device is enabled. When enabled, the sound is played. If 0 is returned, the result is successful. Reference: http://hovertree.com/h/bjaf/37iovb7c.htm

The msg parameter indicates the sound to be played. This parameter can contain the volume and sound level information. For details, see:
Http://hovertree.com/h/bjaf/4ext7m8l.htm
The following method is constructed based on this parameter:

/// <Summary>
/// Play the sound
/// </Summary>
/// <Param name = "key"> pitch (tone) </param>
/// <Param name = "volume"> volume </param>
/// <Param name = "chenel"> channel </param>
/// <Returns> </returns>
Public uint volume play (uint key, uint volume, uint chenel)
{
Return volume play (144 + key * 256 + volume * 65536 + chenel );
}


This allows you to easily adjust the playback volume and sound height. Of course, you can set the tone, which will be introduced later.

How can I disable the MIDI device after it is enabled? The API is also called:
MidiOutClose (
HMidiOut: HMIDIOUT {Device handle}
)
Details: http://hovertree.com/h/bjaf/gmn82vlo.htm

Call in C:
[DllImport ("winmm. dll")]
Private static extern UInt32 midiOutClose (UInt32 hMidiOut );
0 indicates success

Then, use the HewenqiMidi class to create a WinForm and test the playback function. As shown in:

Demo: http://hovertree.com/h/bjaf/xy6rbg5m.htm

Source Code published to this URL: http://hovertree.net/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.