/// <summary>Transposes a MIDI sequence up/down the specified number of half-steps.</summary> /// <param name="sequence">The sequence to be transposed.</param> /// <param name="steps">The number of steps up(+) or down(-) to transpose the sequence.</param> /// <param name="includeDrums">Whether drum tracks should also be transposed.</param> /// <remarks>If the step value is too large or too small, notes may wrap.</remarks> public static void Transpose(MidiSequence sequence, int steps, bool includeDrums) { // If the sequence is null, throw an exception. if (sequence == null) { throw new ArgumentNullException("sequence"); } // Modify each track foreach (MidiTrack track in sequence) { // Modify each event foreach (MidiEvent ev in track.Events) { // If the event is not a voice MIDI event but the channel is the // drum channel and the user has chosen not to include drums in the // transposition (which makes sense), skip this event. NoteVoiceMidiEvent nvme = ev as NoteVoiceMidiEvent; if (nvme == null || (!includeDrums && nvme.Channel == (byte)SpecialChannels.Percussion)) { continue; } // If the event is a NoteOn, NoteOff, or Aftertouch, shift the note // according to the supplied number of steps. nvme.Note = (byte)((nvme.Note + steps) % 128); } } }
static MidiSequence CreateScaleSequence() { MidiSequence sequence = new MidiSequence(); MidiEventCollection events = sequence.Tracks.AddNewTrack().Events; string[] notes = new[] { "C5", "D5", "E5", "F5", "G5", "A5", "B5", "C6", "C6", "B5", "A5", "G5", "F5", "E5", "D5", "C5" }; events.AddRange(notes.SelectMany(note => NoteVoiceMidiEvent.Complete(null, 100, 0, note, 127, 100))); events.Add(new EndOfTrackMetaMidiEvent(null, notes.Length * 100)); return(sequence); }
public void Process(NoteVoiceMidiEvent n) { switch (n) { case OnNoteVoiceMidiEvent on: pressed.Add((on.Note)); break; case OffNoteVoiceMidiEvent off: pressed.Remove((off.Note)); break; } }
static void Main(string[] args) { double toneDistance = 1; double beatDistance = 1; double n; List <string> lines = new List <string>(); lines.Add("INSERT PUNCHHOLE"); string path; double note; double ticks; double time; if (args.Length == 0) { Console.WriteLine("Usage: WintergatanLaserMIDI <filename.mid> [Tone Distance (mm)] [Beat Distance (mm)]"); Console.WriteLine("\tfilename.mid = MIDI file for which to generate code"); Console.WriteLine("\tTone Distance = Defines the distance between every note-pitch."); Console.WriteLine("\tBeat Distance = Defines how far away the beats are from each other."); Console.WriteLine(); return; } if (!File.Exists(args[0])) { Console.WriteLine("Error: file {0} not found", args[0]); return; } path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + args[0] + ".scr"; path = new Uri(path).LocalPath; if (File.Exists(path)) { File.Delete(path); } if (args.Length == 2 && !string.IsNullOrEmpty(args[1])) { if (double.TryParse(args[1], out n)) { toneDistance = n; } else { Console.WriteLine("Tone Distance must be a number!"); return; } } if (args.Length == 3 && !string.IsNullOrEmpty(args[2])) { if (double.TryParse(args[2], out n)) { beatDistance = n; } else { Console.WriteLine("Beat Distance must be a number!"); return; } } try { MidiSequence sequence = MidiSequence.Open(File.OpenRead(args[0])); ticks = (double)sequence.Division; foreach (MidiTrack track in sequence) { track.Events.ConvertDeltasToTotals(); foreach (MidiEvent ev in track.Events) { if (ev.GetType().ToString() == "MidiSharp.Events.Voice.Note.OnNoteVoiceMidiEvent") { NoteVoiceMidiEvent nvme = ev as NoteVoiceMidiEvent; note = (double)nvme.Note; time = (double)ev.DeltaTime; Console.WriteLine(((time / ticks) * beatDistance) + "," + ((note - 60.0d) * toneDistance)); if (lines.Count >= 2) { lines.Add(" " + ((time / ticks) * beatDistance) + "," + ((note - 60.0d) * toneDistance) + ",0 1 1 0"); } else { lines.Add(((time / ticks) * beatDistance) + "," + ((note - 60.0d) * toneDistance) + ",0 1 1 0"); } } } } System.IO.File.WriteAllLines(path, lines); } catch (Exception exc) { Console.Error.WriteLine("Error: {0}", exc.Message); } }