How to play a MIDI file in C# ?

C

cartooncat

I wrote following code:

using System.Runtime.InteropServices;
[DllImport("winmm.dll")]
public static extern int mciSendString(string lpstrCommand, string
lpstrReturnString,int uReturnLength,
int dwCallback);
[DllImport("winmm.dll")]
public static extern int mciGetErrorString(int errCode,string errMsg,uint
buflen);

private void btnPlay_Click(object sender, System.EventArgs e)
{
int r1,r2;
string s1,s2;
string Playingfile;
s1="";
s2="";

Playingfile=listBox1.SelectedItem.ToString();
r1=mciSendString("OPEN "+Playingfile+" TYPE Sequencer wait","",0,0);
r2=mciSendString("PLAY "+Playingfile,"",0,0);
mciGetErrorString(r1,s1,128);
mciGetErrorString(r2,s2,128);
MessageBox.Show("s1="+s1 +" s2="+s2+" r1="+r1.ToString()+"
r2="+r2.ToString());
}

The MessageBox shows s1 s2 are empty string, while r1=0 and r2=337.
I don't know what the return value 337 mean.
I just want to play a MIDI file in C#. any better way?
Waiting for reply. Thanks.
 
R

Rog

I recomend you to use The old DirectX library 8.1 for Visual Basic.
You Can use it easy in c#.
In the new Managed directX 9 for managed Code is the Midiplayback
included but the functionality is limited and the playback produce
sometimes Runtime Errors.
I recomend the DirectX 8.1 for VB.

Here an example of my wrapper class:


using DxVBLib;
using System;
using System.Threading;

namespace HTCDemo
{
/// <summary>
/// The AudioX7 class uses the DirectX 7 for VB to playback the
MIDI-Files.
/// </summary>
/// <remarks>
/// <br></br>
/// <p>Roger Barras</p>
/// <p>ver: 1.1 |
/// date: 27/11/2003 |
/// rev: 10/12/2003</p>
/// </remarks>
public class AudioX7
{
private static int port = -1;
public static int Port
{
get{return port;}
set{port = value;}
}

private DxVBLib.DirectX7 dx;
private DxVBLib.DirectMusicLoader loader;
private DxVBLib.DirectMusicPerformance performance;
private DxVBLib.DirectMusicPerformance performance2;
private DxVBLib.DirectMusicSegmentState segmentState;
private DxVBLib.DirectMusicSegment segment;
private bool fIsPaused = false;
private double dTempo;
private double seconds = 0;
private long startTime = 0;
private long mtTime = 0;
private double mtLength;
private long offset = 0;
private bool playing = false;
public double CurrentPosition
{
get{return this.GetCurrentPosition();}
set{this.SetCurrentPosition(value);}
}
public double Duration
{
get{return this.GetDuration();}
}

public AudioX7()
{
dx = new DxVBLib.DirectX7();
loader = dx.DirectMusicLoaderCreate();
performance = dx.DirectMusicPerformanceCreate();

performance.Init(null, 0);
performance.SetPort(Port,16);
performance.SetMasterAutoDownload(true);

//Creating a Perf2 so that we can get all the segment information
without having to play the segment
performance2 = dx.DirectMusicPerformanceCreate();
performance2.Init(null, 0);
performance2.SetPort(Port,16);
performance2.GetMasterAutoDownload();
}

public AudioX7(string path, string filename):this()
{
Open(path, filename);
}

public void Open(string path, string filename)
{
int zero = 0;
loader.SetSearchDirectory(path);
segment = loader.LoadSegment(filename);
segment.SetStandardMidiFile();

// Play the segment just long enough to get the info
mtTime = performance2.GetMusicTime();
performance2.PlaySegment(segment, 0, (int)mtTime + 2000);

//GetTempo
dTempo = performance2.GetTempo((int)mtTime + 2000, ref zero);

// Get Length
mtLength = (((segment.GetLength() / 768) * 60) / dTempo);

}

public void Play()
{
if (segment == null) return;
if (fIsPaused)
{
offset = mtTime - startTime + offset + 1;
segment.SetStartPoint((int)offset);
segmentState = performance.PlaySegment(segment, 0, 0);
playing = true;
Thread.Sleep(90);
}
else
{
offset = 0;
if (performance.IsPlaying(segment, segmentState) == true)
performance.Stop(segment, segmentState, 0,0);
segment.SetStartPoint(0);
segmentState = performance.PlaySegment(segment, 0, 0);
playing = true;
Thread.Sleep(90);
}
fIsPaused = false;
}

public void Stop()
{
if (segment == null) return;
fIsPaused = false;
if (performance != null)
{
performance.Stop(segment, segmentState, 0, 0);
playing = false;
}
seconds = 0;
}

public void Pause()
{
//Console.WriteLine(performance.GetMusicTime());
//Console.WriteLine(performance.GetClockTime());
if (segment == null) return;
if (this.Playing())
{
fIsPaused = true;
mtTime = performance.GetMusicTime();
startTime = segmentState.GetStartTime();
performance.Stop(segment, null, 0, 0);
playing = false;
}
else
{
fIsPaused = false;
offset = mtTime - startTime + offset + 1;
segment.SetStartPoint((int)offset);
segmentState = performance.PlaySegment(segment, 0, 0);
playing = true;
Thread.Sleep(90);
}
}

public bool Playing()
{
//return (performance.IsPlaying(null, segmentState));
return playing;
}

public bool Paused()
{
return fIsPaused;
}

public void Close()
{
performance.CloseDown();
}

public double GetDuration()
{
return mtLength;
}

private double GetCurrentPosition()
{
if (performance.IsPlaying(null, segmentState))
{
// calculate the seconds
seconds = ((((performance.GetMusicTime() -
(segmentState.GetStartTime() - offset))/768) * 60) / dTempo);
}
return seconds;
}

private void SetCurrentPosition(double position)
{
offset = (long)((position*dTempo/60)*768 -
performance.GetMusicTime() + segmentState.GetStartTime())+1;
if (performance.IsPlaying(null, segmentState))
{
mtTime = performance.GetMusicTime();
startTime = segmentState.GetStartTime();
performance.Stop(segment, null, 0, 0);
playing = false;
fIsPaused = true;
this.Play();
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top