private static void MakeSilent(string midifile) { var mf = new MidiFile(midifile, false); MidiEventCollection collection = new MidiEventCollection(mf.FileFormat, mf.DeltaTicksPerQuarterNote); for (int n = 0; n < mf.Tracks; n++) { collection.AddTrack(); foreach (var midiEvent in mf.Events[n]) { if (midiEvent.CommandCode == MidiCommandCode.NoteOn) { var ne = (NoteEvent)midiEvent; ne.Velocity = 1; collection.AddEvent((MidiEvent)ne, n); } else { collection.AddEvent(midiEvent, n); } } } MidiFile.Export(output_dir + "\\" + midifile, collection); Console.WriteLine("Created " + output_dir + "\\" + midifile); }
public static void Main(string[] args) { MidiEventCollection eventCollection = new MidiEventCollection(1, 30); eventCollection.AddEvent(new NoteOnEvent(15, 1, MidiNote.C1, 100, 15), 1); //Note on eventCollection.AddEvent(new NoteOnEvent(15 + 100, 1, MidiNote.C1, 0, 0), 1); //Note off eventCollection.PrepareForExport(); MidiFile.Export("test.mid", eventCollection); }
public static MidiEventCollection ToMidiEventCollection(this List<Quote> quotes, int track) { var events = quotes.ToMidiEvents(); var result = new MidiEventCollection(MIDI_FILE_TYPE, DELTA_TICKS_PER_QUARTER_NOTE); events.ForEach(n => result.AddEvent(n, track)); return result; }
internal static MidiEventCollection<MidiInEvent> GetMidiBuffer(this MidiInPort port, uint nframes) { MidiEventCollection<MidiInEvent> eventCollection = new MidiEventCollection<MidiInEvent> (port); foreach (MidiInEvent midiEvent in port.GetMidiEvents(nframes)) { eventCollection.AddEvent (midiEvent); } return eventCollection; }
public void TestType1ToType0() { MidiEventCollection collection = new MidiEventCollection(1, 120); collection.AddEvent(new TextEvent("Test", MetaEventType.TextEvent, 0), 0); collection.AddEvent(new NoteOnEvent(0, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(15, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(30, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(0, 10, 60, 100, 15), 10); collection.AddEvent(new NoteOnEvent(15, 10, 60, 100, 15), 10); collection.AddEvent(new NoteOnEvent(30, 10, 60, 100, 15), 10); Assert.AreEqual(collection.Tracks, 11); collection.MidiFileType = 0; collection.PrepareForExport(); Assert.AreEqual(collection.Tracks, 1); IList<MidiEvent> track0 = collection.GetTrackEvents(0); Assert.AreEqual(track0.Count, 8); Assert.IsTrue(MidiEvent.IsEndTrack(track0[track0.Count - 1])); }
internal static MidiEventCollection <MidiInEvent> GetMidiBuffer(this MidiInPort port, uint nframes) { MidiEventCollection <MidiInEvent> eventCollection = new MidiEventCollection <MidiInEvent> (port); foreach (MidiInEvent midiEvent in port.GetMidiEvents(nframes)) { eventCollection.AddEvent(midiEvent); } return(eventCollection); }
public void CanCloneForSameTrack() { var collection = new MidiEventCollection(0, 120); collection.AddEvent(new NoteOnEvent(0, 1, 30, 100, 15), 0); var clone = (NoteOnEvent)collection[0][0].Clone(); clone.AbsoluteTime += 15; clone.NoteNumber++; collection.AddEvent(clone, 0); collection.PrepareForExport(); Assert.That(collection[0][0].AbsoluteTime, Is.EqualTo(0)); Assert.That(collection[0][1].AbsoluteTime, Is.EqualTo(15)); Assert.That(((NoteOnEvent)collection[0][0]).NoteNumber, Is.EqualTo(30)); Assert.That(((NoteOnEvent)collection[0][1]).NoteNumber, Is.EqualTo(31)); }
public void Add(params Note[] notes) { int longestNoteType = 0; foreach (Note note in notes) { if ((int)note.Type > longestNoteType) { longestNoteType = (int)note.Type; } foreach (MidiNote tone in note.Tones) { events.AddEvent(new NoteOnEvent(currentTime, 1, tone, note.Velocity, (int)(note.Type)), 1); //Note on events.AddEvent(new NoteOnEvent(currentTime + (int)note.Type, 1, tone, 0, 0), 1); //Note off } } currentTime += longestNoteType; }
private MemoryStream ExportTempoEvents() { MidiEventCollection events = new MidiEventCollection(0, 480); foreach (var tempo in tempoData) { events.AddEvent(new TempoEvent((int)tempo.microsecondsPerQuarterNote, (long)tempo.tick), 0); } events.PrepareForExport(); return(Utility.ExportMidiToStream(events)); }
public NoteBuilder(int bpm) { double microsecondsPerQuarternote = 60000000 / bpm; double oneTickFrequency = microsecondsPerQuarternote / 1000000 / pulsesPerQuarter; int microSecondsPerQuarterNode = (int)(pulsesPerQuarter * oneTickFrequency * 1000000.0); events = new MidiEventCollection(1, pulsesPerQuarter); events.AddEvent(new TempoEvent(microSecondsPerQuarterNode, 0), 0); currentTime = 0; }
public void SaveToFile(string fileName, List <NoteOnEvent> NoteOnList) { const int MidiFileType = 0; const int BeatsPerMinute = 60; const int TicksPerQuarterNote = 120; const int TrackNumber = 0; const int ChannelNumber = 1; long absoluteTime = 0; var collection = new MidiEventCollection(MidiFileType, TicksPerQuarterNote); collection.AddEvent(new TextEvent("Note Stream", MetaEventType.TextEvent, absoluteTime), TrackNumber); ++absoluteTime; collection.AddEvent(new TempoEvent(CalculateMicrosecondsPerQuaterNote(BeatsPerMinute), absoluteTime), TrackNumber); // var patchParser = new PatchParser(); int patchNumber = 25; collection.AddEvent(new PatchChangeEvent(0, ChannelNumber, patchNumber), TrackNumber); // const int NoteVelocity = 100; // const int NoteDuration = 3 * TicksPerQuarterNote / 4; // i need this to reflect the actual time const long SpaceBetweenNotes = TicksPerQuarterNote; // currently irrelevant if (NoteOnList != null) { foreach (var note in NoteOnList) { collection.AddEvent(new NoteOnEvent(note.AbsoluteTime, ChannelNumber, note.NoteNumber, note.Velocity, note.NoteLength), TrackNumber); collection.AddEvent(new NoteEvent(note.AbsoluteTime + note.NoteLength, ChannelNumber, MidiCommandCode.NoteOff, note.NoteNumber, 0), TrackNumber); absoluteTime += SpaceBetweenNotes; } collection.PrepareForExport(); MidiFile.Export(fileName, collection); } }
public void SaveToFile(string fileName, IEnumerable <Pitch> allNotes) { const int MidiFileType = 0; const int BeatsPerMinute = 60; const int TicksPerQuarterNote = 120; const int TrackNumber = 0; const int ChannelNumber = 1; long absoluteTime = 0; var collection = new MidiEventCollection(MidiFileType, TicksPerQuarterNote); collection.AddEvent(new TextEvent("Note Stream", MetaEventType.TextEvent, absoluteTime), TrackNumber); ++absoluteTime; collection.AddEvent(new TempoEvent(CalculateMicrosecondsPerQuaterNote(BeatsPerMinute), absoluteTime), TrackNumber); var patchParser = new PatchParser(); int patchNumber = patchParser.Patch("steel"); collection.AddEvent(new PatchChangeEvent(0, ChannelNumber, patchNumber), TrackNumber); const int NoteVelocity = 100; const int NoteDuration = 3 * TicksPerQuarterNote / 4; const long SpaceBetweenNotes = TicksPerQuarterNote; foreach (var note in allNotes) { collection.AddEvent(new NoteOnEvent(absoluteTime, ChannelNumber, note.MidiValue, NoteVelocity, NoteDuration), TrackNumber); collection.AddEvent(new NoteEvent(absoluteTime + NoteDuration, ChannelNumber, MidiCommandCode.NoteOff, note.MidiValue, 0), TrackNumber); absoluteTime += SpaceBetweenNotes; } collection.PrepareForExport(); MidiFile.Export(fileName, collection); }
public static void SaveToFile(string fileName, int[] bankNumbers, int[] patchNumbers, int[] noteNumbers, double[] durations, int[] velocities) { const int MidiFileType = 0; const int MicrosecondsPerQuaterNote = 1000000; const int TicksPerQuarterNote = 120; const int TrackNumber = 0; const long Time = 0; var collection = new MidiEventCollection(MidiFileType, TicksPerQuarterNote); collection.AddEvent(new TextEvent("Note Stream", MetaEventType.TextEvent, Time), TrackNumber); collection.AddEvent(new TempoEvent(MicrosecondsPerQuaterNote, Time), TrackNumber); var channels = new List <Tuple <int, int> >(); int notesAdded = 0; for (int i = 0; i < noteNumbers.Length; i++) { var channelIndex = FindChannel(channels, bankNumbers[i], patchNumbers[i]); if (channelIndex == -1) { channels.Add(new Tuple <int, int>(bankNumbers[i], patchNumbers[i])); channelIndex = channels.Count; collection.AddEvent(new ControlChangeEvent(Time, channelIndex, MidiController.BankSelect, bankNumbers[i] >> 8 << 8), TrackNumber); collection.AddEvent(new ControlChangeEvent(Time, channelIndex, MidiController.BankSelectLsb, (byte)bankNumbers[i]), TrackNumber); collection.AddEvent(new PatchChangeEvent(Time, channelIndex, patchNumbers[i]), TrackNumber); } var tickDuration = (int)(durations[i] * 1000 / MicrosecondsPerQuaterNote * TicksPerQuarterNote); collection.AddEvent(new NoteOnEvent(Time, channelIndex, noteNumbers[i], velocities[i], tickDuration), TrackNumber); collection.AddEvent(new NoteEvent(Time + tickDuration, channelIndex, MidiCommandCode.NoteOff, noteNumbers[i], 0), TrackNumber); notesAdded++; } if (notesAdded == 0) { return; } collection.PrepareForExport(); MidiFile.Export(fileName, collection); }
public void TestType0() { MidiEventCollection collection = new MidiEventCollection(0, 120); collection.AddEvent(new TextEvent("Test", MetaEventType.TextEvent, 0), 0); collection.AddEvent(new NoteOnEvent(0, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(15, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(30, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(0, 10, 60, 100, 15), 10); collection.AddEvent(new NoteOnEvent(15, 10, 60, 100, 15), 10); collection.AddEvent(new NoteOnEvent(30, 10, 60, 100, 15), 10); Assert.AreEqual(collection.Tracks, 1); collection.PrepareForExport(); Assert.AreEqual(collection.Tracks, 1); IList <MidiEvent> track0 = collection.GetTrackEvents(0); Assert.AreEqual(track0.Count, 8); Assert.IsTrue(MidiEvent.IsEndTrack(track0[track0.Count - 1])); }
public void TestType0ToType1() { MidiEventCollection collection = new MidiEventCollection(0, 120); collection.AddEvent(new TextEvent("Test", MetaEventType.TextEvent, 0), 0); collection.AddEvent(new NoteOnEvent(0, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(15, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(30, 1, 30, 100, 15), 1); collection.AddEvent(new NoteOnEvent(0, 10, 60, 100, 15), 10); collection.AddEvent(new NoteOnEvent(15, 10, 60, 100, 15), 10); collection.AddEvent(new NoteOnEvent(30, 10, 60, 100, 15), 10); Assert.AreEqual(collection.Tracks, 1); collection.MidiFileType = 1; collection.PrepareForExport(); Assert.AreEqual(3, collection.Tracks, "Wrong number of tracks"); IList <MidiEvent> track0 = collection.GetTrackEvents(0); Assert.AreEqual(track0.Count, 2); Assert.AreEqual(collection.GetTrackEvents(1).Count, 4); Assert.AreEqual(collection.GetTrackEvents(2).Count, 4); Assert.IsTrue(MidiEvent.IsEndTrack(track0[track0.Count - 1])); }
private void c_Click(object sender, EventArgs e) { _midiOut.Send(MidiMessage.StartNote(60, 127, 1).RawData); Thread.Sleep(100); _midiOut.Send(MidiMessage.StopNote(60, 127, 1).RawData); if (_isRecord) { _coll.AddEvent(new NoteEvent((DateTime.Now.Ticks - startTime) / 90000, 1, MidiCommandCode.NoteOn, 60, 127), 1); _coll.AddEvent(new NoteEvent((DateTime.Now.Ticks - startTime) / 90000, 1, MidiCommandCode.NoteOff, 60, 0), 1); i++; } }
public static void ExportToAudicaFile(AudicaFile audicaFile, bool autoSave) { if (!File.Exists(audicaFile.filepath)) { Debug.Log("Save file is gone... :("); return; } Encoding encoding = Encoding.GetEncoding(437); string targetPath = audicaFile.filepath; string autoSavePath = ""; using (var archive = ZipArchive.Open(audicaFile.filepath)) { HandleCache.CheckCacheFolderValid(); HandleCache.CheckSaveFolderValid(); bool expert = false, advanced = false, standard = false, easy = false, modifiers = false; //Write the cues files to disk so we can add them to the audica file. if (audicaFile.diffs.expert.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/expert-new.cues", CuesToJson(audicaFile.diffs.expert)); expert = true; } if (audicaFile.diffs.advanced.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/advanced-new.cues", CuesToJson(audicaFile.diffs.advanced)); advanced = true; } if (audicaFile.diffs.moderate.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/moderate-new.cues", CuesToJson(audicaFile.diffs.moderate)); standard = true; } if (audicaFile.diffs.beginner.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/beginner-new.cues", CuesToJson(audicaFile.diffs.beginner)); easy = true; } audicaFile.modifiers = new ModifierList(); audicaFile.modifiers.modifiers = ModifierHandler.Instance.MapToDTO(); if (audicaFile.modifiers.modifiers.Count > 0) { File.WriteAllText($"{Application.dataPath}/.cache/modifiers-new.json", ModifiersToJson2(audicaFile.modifiers)); modifiers = true; } File.WriteAllText($"{Application.dataPath}/.cache/{audicaFile.desc.moggSong}", audicaFile.mainMoggSong.ExportToText()); File.WriteAllText($"{Application.dataPath}/.cache/song-new.desc", Newtonsoft.Json.JsonConvert.SerializeObject(audicaFile.desc, Formatting.Indented)); var workFolder = Path.Combine(Application.streamingAssetsPath, "Ogg2Audica"); MidiFile songMidi = new MidiFile(Path.Combine(workFolder, "songtemplate.mid")); MidiEventCollection events = new MidiEventCollection(0, (int)Constants.PulsesPerQuarterNote); foreach (var tempo in audicaFile.desc.tempoList) { events.AddEvent(new TempoEvent((int)tempo.microsecondsPerQuarterNote, (long)tempo.time.tick), 0); events.AddEvent(new TimeSignatureEvent((long)tempo.time.tick, (int)tempo.timeSignature.Numerator, (int)TimeSignature.GetMIDIDenominator(tempo.timeSignature.Denominator), 0, 8), 0); } events.PrepareForExport(); MidiFile.Export(Path.Combine(workFolder, $"{Application.dataPath}/.cache/song.mid"), events); //Remove any files we'll be replacing foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.ToString() == "expert.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "song.desc") { archive.RemoveEntry(entry); } else if (entry.ToString() == audicaFile.desc.moggSong) { archive.RemoveEntry(entry); } else if (entry.ToString() == "song.mid") { archive.RemoveEntry(entry); } else if (entry.ToString() == "song.png") { archive.RemoveEntry(entry); } else if (entry.ToString() == "advanced.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "moderate.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "beginner.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "modifiers.json") { archive.RemoveEntry(entry); } } if (expert) { archive.AddEntry("expert.cues", $"{Application.dataPath}/.cache/expert-new.cues"); } if (advanced) { archive.AddEntry("advanced.cues", $"{Application.dataPath}/.cache/advanced-new.cues"); } if (standard) { archive.AddEntry("moderate.cues", $"{Application.dataPath}/.cache/moderate-new.cues"); } if (easy) { archive.AddEntry("beginner.cues", $"{Application.dataPath}/.cache/beginner-new.cues"); } if (modifiers) { archive.AddEntry("modifiers.json", $"{Application.dataPath}/.cache/modifiers-new.json"); } if (autoSave) { int pos = audicaFile.filepath.LastIndexOf(@"\") + 1; string fileName = audicaFile.filepath.Substring(pos, audicaFile.filepath.Length - pos); string shortName = fileName.Substring(0, fileName.LastIndexOf(@".")); shortName = shortName.Replace(" ", ""); targetPath = $"{Application.dataPath}/autosaves/{shortName}/"; autoSavePath = targetPath; targetPath += DateTime.Now.ToString("MM-dd_h-mm-ss_"); targetPath += fileName; if (!Directory.Exists($"{Application.dataPath}/autosaves/")) { Directory.CreateDirectory($"{Application.dataPath}/autosaves/"); } if (!Directory.Exists($"{Application.dataPath}/autosaves/{shortName}/")) { Directory.CreateDirectory($"{Application.dataPath}/autosaves/{shortName}/"); } } archive.AddEntry($"{audicaFile.desc.moggSong}", $"{Application.dataPath}/.cache/{audicaFile.desc.moggSong}"); archive.AddEntry("song.desc", $"{Application.dataPath}/.cache/song-new.desc"); archive.AddEntry("song.mid", $"{Application.dataPath}/.cache/song.mid"); if (File.Exists($"{Application.dataPath}/.cache/song.png")) { archive.AddEntry("song.png", $"{Application.dataPath}/.cache/song.png"); } archive.SaveTo(audicaFile.filepath + ".temp", SharpCompress.Common.CompressionType.None); archive.Dispose(); } File.Delete($"{Application.dataPath}/.cache/{audicaFile.desc.moggSong}"); if (!autoSave) { File.Delete(audicaFile.filepath); } File.Move(audicaFile.filepath + ".temp", targetPath); if (autoSave) { NRSettings.autosavePath = autoSavePath; } Debug.Log("Export finished."); }
public static void ExportToAudicaFile(AudicaFile audicaFile) { if (!File.Exists(audicaFile.filepath)) { Debug.Log("Save file is gone... :("); return; } Encoding encoding = Encoding.GetEncoding(437); using (var archive = ZipArchive.Open(audicaFile.filepath)) { HandleCache.CheckCacheFolderValid(); HandleCache.CheckSaveFolderValid(); bool expert = false, advanced = false, standard = false, easy = false; //Write the cues files to disk so we can add them to the audica file. if (audicaFile.diffs.expert.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/expert-new.cues", CuesToJson(audicaFile.diffs.expert)); expert = true; } if (audicaFile.diffs.advanced.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/advanced-new.cues", CuesToJson(audicaFile.diffs.advanced)); advanced = true; } if (audicaFile.diffs.moderate.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/moderate-new.cues", CuesToJson(audicaFile.diffs.moderate)); standard = true; } if (audicaFile.diffs.beginner.cues != null) { File.WriteAllText($"{Application.dataPath}/.cache/beginner-new.cues", CuesToJson(audicaFile.diffs.beginner)); easy = true; } File.WriteAllText($"{Application.dataPath}/.cache/song-new.desc", JsonUtility.ToJson(audicaFile.desc)); var workFolder = Path.Combine(Application.streamingAssetsPath, "Ogg2Audica"); MidiFile songMidi = new MidiFile(Path.Combine(workFolder, "songtemplate.mid")); MidiEventCollection events = new MidiEventCollection(0, (int)Constants.PulsesPerQuarterNote); foreach (var tempo in audicaFile.desc.tempoList) { events.AddEvent(new TempoEvent((int)tempo.microsecondsPerQuarterNote, (long)tempo.time.tick), 0); events.AddEvent(new TimeSignatureEvent((long)tempo.time.tick, (int)tempo.timeSignature.Numerator, (int)TimeSignature.GetMIDIDenominator(tempo.timeSignature.Denominator), 0, 8), 0); } events.PrepareForExport(); MidiFile.Export(Path.Combine(workFolder, $"{Application.dataPath}/.cache/song.mid"), events); //Remove any files we'll be replacing foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.ToString() == "expert.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "song.desc") { archive.RemoveEntry(entry); } else if (entry.ToString() == "song.mid") { archive.RemoveEntry(entry); } else if (entry.ToString() == "advanced.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "moderate.cues") { archive.RemoveEntry(entry); } else if (entry.ToString() == "beginner.cues") { archive.RemoveEntry(entry); } } if (expert) { archive.AddEntry("expert.cues", $"{Application.dataPath}/.cache/expert-new.cues"); } if (advanced) { archive.AddEntry("advanced.cues", $"{Application.dataPath}/.cache/advanced-new.cues"); } if (standard) { archive.AddEntry("moderate.cues", $"{Application.dataPath}/.cache/moderate-new.cues"); } if (easy) { archive.AddEntry("beginner.cues", $"{Application.dataPath}/.cache/beginner-new.cues"); } archive.AddEntry("song.desc", $"{Application.dataPath}/.cache/song-new.desc"); archive.AddEntry("song.mid", $"{Application.dataPath}/.cache/song.mid"); archive.SaveTo(audicaFile.filepath + ".temp", SharpCompress.Common.CompressionType.None); archive.Dispose(); } File.Delete(audicaFile.filepath); File.Move(audicaFile.filepath + ".temp", audicaFile.filepath); Debug.Log("Export finished."); }