2017年5月4日木曜日

C#でMIDI その2

 C#でMIDIの続きです。

 前回は音を鳴らすだけでした。今回はWindowsっぽくフォームにしてみます。

 今回のフォームにはボタンが1つだけです。ユーザーインターフェースは非常に大事ですが、ソフトの機能が決まらないとユーザーインターフェースも決められません。
 実のところ、このプログラムの最終仕様はまだ決まっていません。というわけで、今のところ、ユーザーインターフェースも適当です。ご勘弁。


using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

class Program
{
[STAThread]

static void Main()
{
Application.Run( new FormMidi());
}
}

class FormMidi : Form
{
/*--------------------------------------------------*/
[DllImport( "Winmm.dll")]
extern static uint midiOutGetNumDevs();

[DllImport( "Winmm.dll")]
extern static uint midiOutOpen( ref long lphmo, uint uDeviceID, uint dwCallback, uint dwCallbackInstance, uint dwFlags);

[DllImport( "Winmm.dll")]
extern static uint midiOutClose( long hmo);

[DllImport( "Winmm.dll")]
extern static uint midiOutShortMsg( long hmo, uint dwMsg);

[DllImport( "Winmm.dll")]
extern static uint midiOutReset( long hmo);

[DllImport( "Kernel32.dll")]
extern static void Sleep( uint dwMilliseconds);

private const uint MMSYSERR_NOERROR = 0;

private const uint MMSYSERR_BADDEVICEID = 2;
private const uint MMSYSERR_ALLOCATED = 4;
private const uint MMSYSERR_NOMEM = 7;
private const uint MMSYSERR_INVALPARAM = 11;
private const uint MMSYSERR_NODEVICE = 68;

private const uint MMSYSERR_INVALHANDLE = 5;
private const uint MIDIERR_STILLPLAYING = 65;

private const uint MIDI_MAPPER = 0xffffffff;

/*--------------------------------------------------*/

long hMidi;

Button btn;

/*--------------------------------------------------*/
/* FormMidi */
/*--------------------------------------------------*/
public FormMidi()
{
this.ClientSize = new Size( 240, 120);

this.Text = "Midi";
this.Load += new EventHandler( this.FormMidi_Load);
this.Closed += new EventHandler( this.FormMidi_Closed);

this.btn = new Button();
this.btn.SetBounds( 40, 40, 160, 40);
    this.btn.Font = new Font( "Arial", 12);
this.btn.Text = "START";
this.btn.Click += new EventHandler( this.btn_Click);
this.Controls.Add( this.btn);
}

/*--------------------------------------------------*/
/* FormMidi_Load */
/*--------------------------------------------------*/
private void FormMidi_Load( object sender, EventArgs e)
{
Console.WriteLine( "FormMidi_Load");
}

/*--------------------------------------------------*/
/* FormMidi_Closed */
/*--------------------------------------------------*/
private void FormMidi_Closed( object sender, EventArgs e)
{
Console.WriteLine( "FormMidi_Closed");
}

/*--------------------------------------------------*/
/* Click */
/*--------------------------------------------------*/
private void btn_Click( object sender, EventArgs e)
{
uint msg;
byte[] keys = new byte[8];

if( midiOutOpen( ref this.hMidi, MIDI_MAPPER, 0, 0, 0) != MMSYSERR_NOERROR)
{
MessageBox.Show( "midiOutOpen error");
return;
}

keys[0] = (byte) 60;
keys[1] = (byte) 62;
keys[2] = (byte) 64;
keys[3] = (byte) 65;
keys[4] = (byte) 67;
keys[5] = (byte) 69;
keys[6] = (byte) 71;
keys[7] = (byte) 72;

for( int i = 0; i < keys.Length; i++)
{
Console.WriteLine( "note on " + "\t" + keys[i]);
msg = (uint) ( ( 0x7f << 16) + ( keys[i] << 8) + 0x90);
midiOutShortMsg( this.hMidi, msg);

Sleep( 500);

Console.WriteLine( "note off" + "\t" + keys[i]);
msg = (uint) ( ( 0x7f << 16) + ( keys[i] << 8) + 0x80);
midiOutShortMsg( this.hMidi, msg);
}

midiOutClose( this.hMidi);
}
}

0 件のコメント:

コメントを投稿