public GamePlayer(InputDevice input, MidiFile midifile, MidiOptions midiOption) { inputDevice = input; processer = new MidiProcesser(midifile, midiOption); inputDevice.NoteOn += delegate(NoteOnMessage msg) { ComputeScore(msg.Pitch, processer.Position); }; inputDevice.NoteOff += delegate(NoteOffMessage msg) { }; inputDevice.ProgramChange += delegate(ProgramChangeMessage msg) { }; inputDevice.PitchBend += delegate(PitchBendMessage msg) { }; }
public LearningPlayer(InputDevice input, MidiFile midifile, MidiOptions midiOption) { processer = new MidiProcesser(midifile, midiOption); inputDevice = input; inputDevice.NoteOn += delegate(NoteOnMessage msg) { ProcessPitch(msg.Pitch); }; processer.NoteOn += delegate(Channel channel, Pitch pitch, int velocity) { waitPitch = pitch; processer.Stop(); }; }
public MidiPlayer(MidiFile midiFile, MidiOptions midiOptions) { processer = new MidiProcesser(midiFile, midiOptions); processer.NoteOn += delegate(Channel channel, Pitch pitch, int velocity) { if (outDevice != null) { outDevice.SendNoteOn(channel,pitch,velocity); } }; processer.NoteOff += delegate(Channel channel, Pitch pitch, int velocity) { if (outDevice != null) { outDevice.SendNoteOff(channel, pitch, velocity); } }; processer.ProgramChange += delegate(Channel channel, Instrument instrument) { if (outDevice != null) { outDevice.SendProgramChange(channel, instrument); } }; processer.ControlChange += delegate(Channel channel, MidiControl control, int value) { if (outDevice != null) { outDevice.SendControlChange(channel, control,value); } }; }
public void OutDeviceTest() { ReadOnlyCollection<OutputDevice> devices = OutputDevice.InstalledDevices; if (devices.Count == 0) { Console.WriteLine("No out put devices"); return; } foreach (OutputDevice device in devices) { Console.WriteLine(" OutputDevices Name:"+ device.Name); } MidiFile midiFile = new MidiFile("E:/13.mid"); MidiOptions options = new MidiOptions(midiFile); MidiPlayer player = new MidiPlayer(midiFile, options); foreach (OutputDevice device in devices) { if (device.IsMidiPort) { player.OutDevice = device; } } if (player.OutDevice == null) { player.OutDevice = devices[0]; } player.Start(); }
public void TestScore() { MidiFile midiFile = new MidiFile("E:/14.mid"); MidiOptions options = new MidiOptions(midiFile); ReadOnlyCollection<InputDevice> devices = InputDevice.InstalledDevices; if (devices.Count == 0) { Console.WriteLine("No input devices"); return; } ReadOnlyCollection<OutputDevice> outDevices = OutputDevice.InstalledDevices; if (outDevices.Count == 0) { Console.WriteLine("No out put devices"); return; } OutputDevice outDevice = outDevices[1]; if (outDevice != null && !outDevice.IsOpen) { outDevice.Open(); } foreach (InputDevice device in devices) { if (!device.IsOpen) { device.Open(); device.StartReceiving(new Clock(10)); } device.NoteOn += delegate(NoteOnMessage msg) { Console.WriteLine("NoteOnMessage => Pitch :"+msg.Pitch+"clock:"+msg.Time); }; device.NoteOff += delegate(NoteOffMessage msg) { Console.WriteLine("NoteOffMessage => Pitch :" + msg.Pitch); }; device.ProgramChange += delegate(ProgramChangeMessage msg) { Console.WriteLine("ProgramChangeMessage => Ins :" + msg.Instrument); }; device.PitchBend += delegate(PitchBendMessage msg) { Console.WriteLine("PitchBendMessage => Pitch :" + msg.Value); }; GamePlayer player = new GamePlayer(device, midiFile, options); player.StartPlayer(); player.PlayEvent += delegate(GamePlayer.State state, int score, MidiEvent[] events) { Console.WriteLine("state:" + state + " score:" + score); }; } }
/** Write this Midi file to the given filename. * If options is not null, apply those options to the midi events * before performing the write. * Return true if the file was saved successfully, else false. */ public bool ChangeSound(string destfile, MidiOptions options) { return Write(destfile, options); }
/** Apply the given sheet music options to the MidiNotes. * Return the midi tracks with the changes applied. */ public List<MidiTrack> ChangeMidiNotes(MidiOptions options) { List<MidiTrack> newtracks = new List<MidiTrack>(); for (int track = 0; track < tracks.Count; track++) { if (options.tracks[track]) { newtracks.Add(tracks[track].Clone() ); } } /* To make the sheet music look nicer, we round the start times * so that notes close together appear as a single chord. We * also extend the note durations, so that we have longer notes * and fewer rest symbols. */ TimeSignature time = timesig; if (options.time != null) { time = options.time; } MidiFile.RoundStartTimes(newtracks, options.combineInterval, timesig); MidiFile.RoundDurations(newtracks, time.Quarter); if (options.twoStaffs) { newtracks = MidiFile.CombineToTwoTracks(newtracks, timesig.Measure); } if (options.shifttime != 0) { MidiFile.ShiftTime(newtracks, options.shifttime); } if (options.transpose != 0) { MidiFile.Transpose(newtracks, options.transpose); } return newtracks; }
/* Apply the following sound options to the midi events: * - The tempo (the microseconds per pulse) * - The instruments per track * - The note number (transpose value) * - The tracks to include * Return the modified list of midi events. */ private List<MidiEvent>[] ApplyOptionsToEvents(MidiOptions options) { int i; if (trackPerChannel) { return ApplyOptionsPerChannel(options); } /* A midifile can contain tracks with notes and tracks without notes. * The options.tracks and options.instruments are for tracks with notes. * So the track numbers in 'options' may not match correctly if the * midi file has tracks without notes. Re-compute the instruments, and * tracks to keep. */ int num_tracks = events.Length; int[] instruments = new int[num_tracks]; bool[] keeptracks = new bool[num_tracks]; for (i = 0; i < num_tracks; i++) { instruments[i] = 0; keeptracks[i] = true; } for (int tracknum = 0; tracknum < tracks.Count; tracknum++) { MidiTrack track = tracks[tracknum]; int realtrack = track.Number; instruments[realtrack] = options.instruments[tracknum]; if (options.tracks[tracknum] == false || options.mute[tracknum] == true) { keeptracks[realtrack] = false; } } List<MidiEvent>[] newevents = CloneMidiEvents(events); /* Set the tempo at the beginning of each track */ for (int tracknum = 0; tracknum < newevents.Length; tracknum++) { MidiEvent mevent = CreateTempoEvent(options.tempo); newevents[tracknum].Insert(0, mevent); } /* Change the note number (transpose), instrument, and tempo */ for (int tracknum = 0; tracknum < newevents.Length; tracknum++) { foreach (MidiEvent mevent in newevents[tracknum]) { int num = mevent.Notenumber + options.transpose; if (num < 0) num = 0; if (num > 127) num = 127; mevent.Notenumber = (byte)num; if (!options.useDefaultInstruments) { mevent.Instrument = (byte)instruments[tracknum]; } mevent.Tempo = options.tempo; } } if (options.pauseTime != 0) { newevents = StartAtPauseTime(newevents, options.pauseTime); } /* Change the tracks to include */ int count = 0; for (int tracknum = 0; tracknum < keeptracks.Length; tracknum++) { if (keeptracks[tracknum]) { count++; } } List<MidiEvent>[] result = new List<MidiEvent>[count]; i = 0; for (int tracknum = 0; tracknum < keeptracks.Length; tracknum++) { if (keeptracks[tracknum]) { result[i] = newevents[tracknum]; i++; } } return result; }
/* Apply the following sound options to the midi events: * - The tempo (the microseconds per pulse) * - The instruments per track * - The note number (transpose value) * - The tracks to include * Return the modified list of midi events. * * This Midi file only has one actual track, but we've split that * into multiple fake tracks, one per channel, and displayed that * to the end-user. So changing the instrument, and tracks to * include, is implemented differently than ApplyOptionsToEvents(). * * - We change the instrument based on the channel, not the track. * - We include/exclude channels, not tracks. * - We exclude a channel by setting the note volume/velocity to 0. */ private List<MidiEvent>[] ApplyOptionsPerChannel(MidiOptions options) { /* Determine which channels to include/exclude. * Also, determine the instruments for each channel. */ int[] instruments = new int[16]; bool[] keepchannel = new bool[16]; for (int i = 0; i < 16; i++) { instruments[i] = 0; keepchannel[i] = true; } for (int tracknum = 0; tracknum < tracks.Count; tracknum++) { MidiTrack track = tracks[tracknum]; int channel = track.Notes[0].Channel; instruments[channel] = options.instruments[tracknum]; if (options.tracks[tracknum] == false || options.mute[tracknum] == true) { keepchannel[channel] = false; } } List<MidiEvent>[] newevents = CloneMidiEvents(events); /* Set the tempo at the beginning of each track */ for (int tracknum = 0; tracknum < newevents.Length; tracknum++) { MidiEvent mevent = CreateTempoEvent(options.tempo); newevents[tracknum].Insert(0, mevent); } /* Change the note number (transpose), instrument, and tempo */ for (int tracknum = 0; tracknum < newevents.Length; tracknum++) { foreach (MidiEvent mevent in newevents[tracknum]) { int num = mevent.Notenumber + options.transpose; if (num < 0) num = 0; if (num > 127) num = 127; mevent.Notenumber = (byte)num; if (!keepchannel[mevent.Channel]) { mevent.Velocity = 0; } if (!options.useDefaultInstruments) { mevent.Instrument = (byte)instruments[mevent.Channel]; } mevent.Tempo = options.tempo; } } if (options.pauseTime != 0) { newevents = StartAtPauseTime(newevents, options.pauseTime); } return newevents; }
/** Write this Midi file to the given stream. * If options is not null, apply those options to the midi events * before performing the write. * Return true if the file was saved successfully, else false. */ public bool Write(Stream stream, MidiOptions options) { List<MidiEvent>[] newevents = events; if (options != null) { newevents = ApplyOptionsToEvents(options); } return WriteEvents(stream, newevents, trackmode, quarternote); }
public bool Write(string destfile, MidiOptions options) { try { FileStream stream; stream = new FileStream(destfile, FileMode.Create); bool result = Write(stream, options); stream.Close(); return result; } catch (IOException e) { return false; } }