C # + MIDI simulates the performance of various instruments

Source: Internet
Author: User

Some time ago, I was on the rise.ProgramCode.

I even learned a little about basic music theory, figured out what the twelve average law is, what the semi-sound, the whole sound, and so on, and finally made such a thing, such:


Its function is to input a "simplified music" to play beautiful music.

It is a pity that the "continuous sound" method cannot be implemented.

There are rules in the program:

    1. Take 01234567 as the basic four-minute note. Each note is separated by a comma, for example, 1, 2, 4, and so on. 0 indicates the break.
    2. Enter "+-#!" before the note -#!" The plus sign indicates that the sound is increased by eight degrees, while the minus sign is lowered by eight degrees. The well number is raised by half and the exclamation point is lowered by half.
    3. After the basic note, add a slash "/" to indicate that the value is halved, and the half is 8 minutes. The double slash is 16 minutes.
    4. You can add a hyphen "-" (minus sign) on the basis of the above. A minus sign is used to prolong the time value by one time, which is the same as that in the notation.
    5. You can also add the note "." On the basis of the above, which serves the same purpose as the notation (dual points are allowed ).

======================================

Windows API functions used in the program

View code

 ///    <Summary>  
/// Reset MIDI output
/// </Summary>
/// <Param name = "handle"> </param>
/// <Returns> </returns>
[Dllimport ( " Winmm. dll " )]
Protected Static Extern Int Midioutreset ( Int Handle );

/// <Summary>
/// Send information to the output port
/// </Summary>
/// <Param name = "handle"> </param>
/// <Param name = "message"> </param>
/// <Returns> </returns>
[Dllimport ( " Winmm. dll " )]
Protected Static Extern Int Midioutshortmsg ( Int Handle, Int Message );
/// <Summary>
/// Enable the Midi output device
/// </Summary>
/// <Param name = "handle"> </param>
/// <Param name = "DeviceID"> </param>
/// <Param name = "proc"> </param>
/// <Param name = "instance"> </param>
/// <Param name = "Flags"> </param>
/// <Returns> </returns>
[Dllimport ( " Winmm. dll " )]
Protected Static Extern Int Midioutopen ( Ref Int Handle, Int DeviceID,
Midioutproc proc, Int Instance, Int Flags );

/// <Summary>
/// Close
/// </Summary>
/// <Param name = "handle"> </param>
/// <Returns> </returns>
[Dllimport ( " Winmm. dll " )]
Protected Static Extern Int Midioutclose ( Int Handle );

The core is to send information to Midi. For details about the instruction format, refer to Baidu encyclopedia.

After the device is turned on, the information can be sent. The above midioutshortmsg function is used. The following is the encapsulated analog keyboard press or lift function.

Private     Void  Send (  Int  Istatus,  Int  Ichannel,  Int  Idata1,  Int  Idata2)
{
Midioutshortmsg (hndle, istatus | Ichannel | (Idata1 < 8 ) | (Idata2 < 16 ));
}
/// <Summary>
/// Press the keyboard. The default value is the first channel.
/// </Summary>
/// <Param name = "idata1"> </param>
/// <Param name = "idata2"> </param>
Public Void Note_on ( Int Idata1, Int Idata2)
{
Note_on ( 0 , Idata1, idata2 );
}
Public Void Note_on ( Int Ichannel, Int Idata1, Int Idata2)
{
Send ( 0x90 , Ichannel, idata1, idata2 );
}
Public Void Note_off ( Int Idata1, Int Idata2)
{
Note_off ( 0 , Idata1, idata2 );
}
Public Void Note_off ( Int Ichannel, Int Idata1, Int Idata2)
{
Send ( 0x80 , Ichannel, idata1, idata2 );
}

Note: 0x90 indicates that 9 represents the keyboard Press (start voice), 0 represents the first channel, a total of 16 channels, that is, 16 instruments can be played at the same time, icannel is the channel number, idata1 is the symbol code, 0 is the lowest, 127 is the highest, C is 60 in the center of the piano keyboard, idata2 is the volume or intensity (I do not quite understand, you can use 100 for a disk ). This information is carried out in the send function.AndAfter calculation and shift operations, obtain information such as 0x92 48 96, indicating that Channel 3 starts playing C2

0x80 indicates that the keyboard is raised, and the function is to stop the voice. Generally, we use 0x90 + the volume is 0 to mute it (for specific reasons, forget, it seems that such a command can effectively reduce the storage length of the MID file)

For more commands, see Baidu or Gu Ge. Download the program and source code.

Simplified music player download

Source code download (vs2010)

In addition, this program references the API declaration code of a Project C # MIDI toolkit in a codeproject. This project is a keyboard and piano program. The running interface is as follows:

Some piano keyboards written in VB are also found on a website, which is of great reference value.

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.