protected override void WriteContent(MidiWriter writer, WritingSettings settings) { writer.WriteVlqNumber(A); writer.WriteVlqNumber(B?.Length ?? 0); writer.WriteString(B); writer.WriteByte(C); }
/// <summary> /// Ask user where to save the midi & generate midi /// </summary> private void Generate() { try { using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = "*.mid|*.mid"; if (sfd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { cancelGenerating = false; List <Note> notes = GenerateNotes(); if (notes != null && notes.Count > 0) { MidiWriter.Write(sfd.FileName, boardSettings.Instrument, notes); lblStatus.Text = "Midi saved to " + sfd.FileName; } } } } catch (Exception ex) { MessageBox.Show("Could not save file, error: " + ex.GetType().FullName + " - " + ex.Message, "Could not save file"); } }
internal OutputDevice(int id) : base(id) { _midiWriter = new MidiWriter(_memoryStream); SetDeviceInformation(); }
public void Write(MidiEvent midiEvent, MidiWriter writer, WritingSettings settings, bool writeStatusByte) { if (writeStatusByte) { var statusByte = GetStatusByte(midiEvent); writer.WriteByte(statusByte); } midiEvent.Write(writer, settings); }
public void WriteMidi(string filename, int ticksPerPixel, int ppq, int startOffset, bool useColorEvents) { int tracks = (Palette.Colors.Count + 15 - ((Palette.Colors.Count + 15) % 16)) / 16; MidiWriter writer = new MidiWriter(new BufferedStream(File.Open(filename, FileMode.Create))); writer.Init(); writer.WriteFormat(1); writer.WritePPQ((ushort)ppq); writer.WriteNtrks((ushort)tracks); //writer.InitTrack(); //writer.Write(new TimeSignatureEvent(0, 4, 2, 24, 8)); //writer.Write(new TempoEvent(0, 500000)); //writer.EndTrack(); for (int i = 0; i < tracks; i++) { writer.InitTrack(); if (useColorEvents) { for (byte j = 0; j < 16; j++) { if (i * 16 + j < Palette.Colors.Count) { var c = Palette.Colors[i * 16 + j]; writer.Write(new ColorEvent(0, j, c.R, c.G, c.B, c.A)); } } } uint o = (uint)startOffset; foreach (MIDIEvent e in EventBuffers[i]) { var _e = e.Clone(); _e.DeltaTime *= (uint)ticksPerPixel; _e.DeltaTime += o; o = 0; writer.Write(_e); } writer.EndTrack(); } writer.Close(); }
/// <summary> /// Writes content of a MIDI meta event. /// </summary> /// <param name="writer">Writer to write the content with.</param> /// <param name="settings">Settings according to which the event's content must be written.</param> protected override void WriteContent(MidiWriter writer, WritingSettings settings) { }
internal override sealed void Write(MidiWriter writer, WritingSettings settings) { }
/// <summary> /// Writes content of a MIDI meta event. /// </summary> /// <param name="writer">Writer to write the content with.</param> /// <param name="settings">Settings according to which the event's content must be written.</param> protected abstract void WriteContent(MidiWriter writer, WritingSettings settings);
/// <summary> /// Writes content of a MIDI event. /// </summary> /// <param name="writer">Writer to write the content with.</param> /// <param name="settings">Settings according to which the event's content must be written.</param> internal sealed override void Write(MidiWriter writer, WritingSettings settings) { WriteContent(writer, settings); }
static void Main(string[] args) { string filepath; if (args.Length > 0) { filepath = args[0]; } else { Console.WriteLine("Select MIDI"); var open = new OpenFileDialog(); open.Filter = "Midi files (*.mid)|*.mid"; if ((bool)open.ShowDialog()) { filepath = open.FileName; } else { return; } } string output; if (args.Length == 1) { var ext = Path.GetExtension(filepath); output = filepath.Substring(filepath.Length - ext.Length, ext.Length) + "_OR.mid"; } else if (args.Length > 2) { output = args[1]; } else { Console.WriteLine("Save file..."); var save = new SaveFileDialog(); save.Filter = "Midi files (*.mid)|*.mid"; if ((bool)save.ShowDialog()) { output = save.FileName; } else { return; } } MidiFile file; ParallelStream tempOutput; Stream trueOutput; try { file = new MidiFile(() => new BufferedStream(File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read), 4096 * 4)); } catch (IOException) { Console.WriteLine("Could not open input file\nPress any key to exit..."); Console.ReadKey(); return; } try { var s = File.Open(output + ".tmp", FileMode.Create); tempOutput = new ParallelStream(s, 4096 * 16); } catch (IOException) { Console.WriteLine("Could not open temporary output file\nPress any key to exit..."); Console.ReadKey(); return; } try { trueOutput = File.Open(output, FileMode.Create); } catch (IOException) { Console.WriteLine("Could not open true output file\nPress any key to exit..."); Console.ReadKey(); return; } Console.WriteLine("Processing the midi..."); BlockingCollection <MIDIEvent>[] cacheEvents = new BlockingCollection <MIDIEvent> [file.TrackCount]; AsyncNoteParse[] inputNotes = new AsyncNoteParse[file.TrackCount]; AsyncNoteWriter[] outputNotes = new AsyncNoteWriter[file.TrackCount]; Note[] nextNotes = new Note[file.TrackCount]; Note[] topNotes = new Note[128]; Queue <Note>[] fullNotes = new Queue <Note> [file.TrackCount]; for (int i = 0; i < file.TrackCount; i++) { Console.WriteLine("Loading Tracks " + (i + 1) + "/" + file.TrackCount); cacheEvents[i] = new BlockingCollection <MIDIEvent>(); var reader = file.GetAsyncBufferedTrack(i, 10000); inputNotes[i] = new AsyncNoteParse(reader, cacheEvents[i], 128); inputNotes[i].Init(); outputNotes[i] = new AsyncNoteWriter(tempOutput.GetStream(i), cacheEvents[i], inputNotes[i], 128); fullNotes[i] = new Queue <Note>(); } for (int i = 0; i < file.TrackCount; i++) { try { nextNotes[i] = inputNotes[i].Take(); } catch { } } bool allEnded = false; bool addedNotes = false; long nc = 0; long nc2 = 0; Stopwatch sw = new Stopwatch(); sw.Start(); Task writeTask = null; for (ulong tick = 0; !allEnded;) { for (int i = 0; i < topNotes.Length; i++) { topNotes[i] = null; } allEnded = true; addedNotes = false; ulong nextSmallest = 0; bool first = true; for (int i = 0; i < file.TrackCount; i++) { while (nextNotes[i] != null && nextNotes[i].Start == tick) { addedNotes = true; var n = nextNotes[i]; if (writeTask != null) { if (!writeTask.IsCompleted) { //Stopwatch d = new Stopwatch(); //d.Start(); writeTask.GetAwaiter().GetResult(); //Console.WriteLine(d.ElapsedTicks); } } if (topNotes[n.Key] == null) { topNotes[n.Key] = n; fullNotes[i].Enqueue(n); } else { if (topNotes[n.Key].End < n.End) { topNotes[n.Key] = n; fullNotes[i].Enqueue(n); } else { if (topNotes[n.Key].Velocity < n.Velocity) { topNotes[n.Key].Velocity = n.Velocity; } } } try { nextNotes[i] = inputNotes[i].Take(); nc++; } catch { nextNotes[i] = null; } } if (nextNotes[i] != null) { allEnded = false; if (first || nextNotes[i].Start < nextSmallest) { first = false; nextSmallest = nextNotes[i].Start; } } } if (!allEnded) { tick = nextSmallest; } if (addedNotes) { writeTask = Task.Run(() => { Parallel.For(0, file.TrackCount, i => { var fn = fullNotes[i]; while (fn.Count != 0) { outputNotes[i].Write(fn.Dequeue()); //fn.Dequeue(); nc2++; } if (nextNotes[i] == null && !outputNotes[i].Finalised) { try { outputNotes[i].Finalise(); } catch { } } }); }); } if (sw.ElapsedMilliseconds > 1000) { Console.WriteLine("Processed: " + nc.ToString("#,##0") + "\tKept: " + nc2.ToString("#,##0")); sw.Reset(); sw.Start(); } } Console.WriteLine("Processed: " + nc.ToString("#,##0") + "\tKept: " + nc2.ToString("#,##0")); Console.WriteLine("Waiting for all writers to finish..."); writeTask.GetAwaiter().GetResult(); for (int i = 0; i < file.TrackCount; i++) { if (!outputNotes[i].Finalised) { try { outputNotes[i].Finalise(); } catch { } } } for (int i = 0; i < file.TrackCount; i++) { outputNotes[i].Join(); } for (int i = 0; i < file.TrackCount; i++) { outputNotes[i].Close(); } Console.WriteLine("Writing final midi"); var writer = new MidiWriter(trueOutput); writer.Init(); writer.WriteNtrks((ushort)file.TrackCount); writer.WritePPQ(file.PPQ); writer.WriteFormat(file.Format); for (int i = 0; i < file.TrackCount; i++) { Console.WriteLine("Copying Track " + i + "/" + file.TrackCount); writer.InitTrack(); var r = tempOutput.GetStream(i, true); r.CopyTo(trueOutput); r.Close(); writer.EndTrack(); } writer.Close(); tempOutput.CloseAllStreams(); tempOutput.Dispose(); try { File.Delete(output + ".tmp"); } catch { Console.WriteLine("Couldn't delete temporary file"); } Console.WriteLine("Complete!\nPress any key to exit..."); Console.ReadKey(); }
/// <summary> /// Writes content of a MIDI meta event. /// </summary> /// <param name="writer">Writer to write the content with.</param> /// <param name="settings">Settings according to which the event's content must be written.</param> protected override void WriteContent(MidiWriter writer, WritingSettings settings) { writer.WriteByte(Port); }
/// <summary> /// Writes content of a MIDI meta event. /// </summary> /// <param name="writer">Writer to write the content with.</param> /// <param name="settings">Settings according to which the event's content must be written.</param> protected override void WriteContent(MidiWriter writer, WritingSettings settings) { writer.WriteWord(Number); }
protected override void WriteContent(MidiWriter writer, WritingSettings settings) { throw new System.NotImplementedException(); }
public void GenerateSong() { MovementInfo info = new MovementInfo() { //Chord = Chord.chords.GetRandomElement(), Chord = Chord.chords[0], //Root = Note.GetNoteIndex(String.Format("{0}{1}", keys.GetRandomElement(), Octives.GetRandomElement())), Root = Note.GetNoteIndex(String.Format("{0}{1}", 'G', '4')), //Tempo = Tempo.Tempos.GetRandomElement(), Tempo = Tempo.Tempos[127], //TimeSignature = TimeSignature.TimeSignatures.GetRandomElement(), TimeSignature = TimeSignature.TimeSignatures[3], Volume = 120 }; Track t = new Track(Instruments.Electric_Piano_2, new List <Note>()); Track t2 = new Track(Instruments.Fiddle, new List <Note>()); Track m = new Track(Instruments.Steel_Drums, new List <Note>()); Track m2 = new Track(Instruments.Banjo, new List <Note>()); Track x = new Track(Instruments.Church_Organ, new List <Note>()); Bar chordBar = Bar.GenerateBar(info, Bar.BarType.Chord); Bar chordBar2 = Bar.GenerateBar(info, Bar.BarType.Chord2); Bar PercussInstrument1Bar = Bar.GenerateBar(info, Bar.BarType.PercussInstrument1); Bar PercussInstrument2Bar = Bar.GenerateBar(info, Bar.BarType.PercussInst2); Bar GenerateBassBar = Bar.GenerateBar(info, Bar.BarType.BassLine); // //info.Volume = 0; t.AppendBar(chordBar); //info.Volume = 30; t2.AppendBar(chordBar2); //info.Volume = 45; m.AppendBar(PercussInstrument1Bar); m2.AppendBar(PercussInstrument2Bar); //info.Volume = 120; x.AppendBar(GenerateBassBar); // m2.AppendBar(Bar.GenerateBar(info, Bar.BarType.Melody)); /* Left for later reference, WIP * //switching chord to D * info.Root = Note.GetNoteIndex(String.Format("{0}{1}", 'D', '4')); * chordBar = Bar.GenerateBar(info, Bar.BarType.Chord); * for (int i = 0; i < 1; i++) * { * t.AppendBar(chordBar); * } * * //Switching chord to Em * info.Root = Note.GetNoteIndex(String.Format("{0}{1}", 'E', '4')); * //Minor Chord * info.Chord = Chord.chords[1]; * chordBar = Bar.GenerateBar(info, Bar.BarType.Chord); * for (int i = 0; i < 1; i++) * { * t.AppendBar(chordBar); * } * * //Switching chord to C * info.Root = Note.GetNoteIndex(String.Format("{0}{1}", 'C', '4')); * //Major Chord * info.Chord = Chord.chords[0]; * chordBar = Bar.GenerateBar(info, Bar.BarType.Chord); * for (int i = 0; i < 1; i++) * { * t.AppendBar(chordBar); * } */ List <Track> tracks = new List <Track>() { t, t2, m, m2, x }; foreach (Track tt in tracks) { //Console.WriteLine("Track len {0}", tt.TrackLength); //Form1.programInfo.Text("test"); sb.AppendLine("Track length" + tt.TrackLength); } String fName = @"test1.midi"; MidiWriter.Write(fName, tracks); try { Midi.SaveMidiFile(fName); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Please create a folder on your C:/ drive called 'TestFolder' which the program will write the MIDI files to."); System.Windows.Forms.Application.Exit(); } Midi.PlayMidiFile(fName); }
protected override void WriteContent(MidiWriter writer, WritingSettings settings) { writer.WriteVlqNumber(A); }
public static void PlayScale() { List <String> NotesInScale = new List <string>() { "C4", "D4", "E4", "F4", "G4", "A5", "B5", "C5" }; //Time measured in milliseconds since start of song int currTime = 0; //Create a place to store the notes List <Note> notes = new List <Note>(); //Play the scale ascending foreach (String s in NotesInScale) { //TimeDown: When to press the key, measured in milliseconds since beginning of song //midiIndex: Index of key to press. Mapped //durationMS: numer of miliseconds to hold the key down for //volume :Forcefulness to play the key. 0=Silent -> 127=Max volume Note n = new Note(currTime, Note.GetNoteIndex(s), 100, 64); notes.Add(n); currTime += 100; } //Play the scale descending NotesInScale.Reverse(); foreach (String s in NotesInScale) { notes.Add(new Note(currTime, Note.GetNoteIndex(s), 100, 64)); currTime += 100; } //Play a chord foreach (int offSet in Chord.chords[0].offsets) { int indx = (Note.GetNoteIndex("C4") + offSet); notes.Add(new Note(currTime, (byte)indx, 200, 64)); } //Console.WriteLine(); currTime += 200; //Play another chord foreach (int offSet in Chord.chords[4].offsets) { int indx = (Note.GetNoteIndex("C4") + offSet); notes.Add(new Note(currTime, (byte)indx, 200, 64)); } //Console.WriteLine(); currTime += 200; //Play the first chord again foreach (int offSet in Chord.chords[0].offsets) { int indx = (Note.GetNoteIndex("C4") + offSet); notes.Add(new Note(currTime, (byte)indx, 400, 64)); } //Console.WriteLine(); currTime += 400; //Create a track List <Track> tracks = new List <Track>() { new Track(Instruments.Rock_Organ, notes) }; //Write the midifile with the selected tracks String fName = @"test1.midi"; MidiWriter.Write(fName, tracks); //Start playing the midifile [The Midi Object is not thread safe. Ensure that all Midi functions are called from the same thread. only becomes an issue with multi-threading or GUIs] Midi.PlayMidiFile(fName); //Wait for it to complete while (Midi.Status() != "stopped") { System.Threading.Thread.Sleep(10); } //Stop playing the midi file. Midi.StopMidiFile(); }
static void Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new ColourForm()); var midiout = new StreamWriter(@"E:\test.mid"); var midiin = new StreamReader(@"E:\Midi\Pi.mid"); MidiWriter writer = new MidiWriter(midiout.BaseStream); writer.Init(); var filter = new TrackFilter(); MidiFileInfo info = MidiFileInfo.Parse(midiin.BaseStream); writer.WriteDivision(info.Division); writer.WriteNtrks((ushort)Math.Min(info.TrackCount, 65535)); writer.WriteFormat(info.Format); for (int i = 0; i < info.TrackCount; i++) { Console.WriteLine("Priocessing track: " + i); byte[] trackbytes = new byte[info.Tracks[i].Length]; midiin.BaseStream.Position = info.Tracks[i].Start; midiin.BaseStream.Read(trackbytes, 0, (int)info.Tracks[i].Length); writer.InitTrack(); long prevtime = 0; double hue = i * 60; int d = 3; filter.MidiEventFilter = (byte[] dtimeb, int dtime, byte[] data, long time) => { byte[] e = new byte[] { 0xFF, 0x0A, 0x08, 0x00, 0x0F, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFF }; //byte[] e = new byte[] { 0xFF, 0x0A, 0x0C, 0x00, 0x0F, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF }; if (time - prevtime > d) { List <byte> o = new List <byte>(); long start = time - dtime; while (prevtime + d < time) { prevtime += d; int delta = (int)(prevtime - start); int r, g, b; HsvToRgb(hue, 1, 1, out r, out g, out b); hue += 1; hue = hue % 360; e[7] = (byte)r; e[8] = (byte)g; e[9] = (byte)b; //HsvToRgb(hue + 60, 1, 1, out r, out g, out b); //hue += 1; //hue = hue % 360; //e[11] = (byte)r; //e[12] = (byte)g; //e[13] = (byte)b; o.AddRange(filter.MakeVariableLen(delta)); o.AddRange(e); start += delta; } o.AddRange(filter.MakeVariableLen((int)(time - start))); o.AddRange(data); return(o.ToArray()); } return(dtimeb.Concat(data).ToArray()); }; var newtrackbytes = filter.FilterTrack(new MemoryByteReader(trackbytes)); writer.Write(newtrackbytes); writer.EndTrack(); } writer.Close(); }