public static string NormalizeTicksPerQuarterNote(string base64encodedMidiFile) { int standardTicksPerQuarterNote = 96; var midiFile = MidiFile.Read(base64encodedMidiFile); midiFile = ConvertDeltaTimeToAccumulatedTime(midiFile); var originalTicksPerQuarterNote = MidiProcessing.GetTicksPerBeatOfSong(base64encodedMidiFile); double conversionRate = standardTicksPerQuarterNote / (double)originalTicksPerQuarterNote; foreach (TrackChunk chunk in midiFile.Chunks) { foreach (var eventito in chunk.Events) { eventito.DeltaTime = (long)Math.Round(conversionRate * eventito.DeltaTime); } } midiFile = ConvertAccumulatedTimeToDeltaTime(midiFile); return(MidiProcessing.ConvertMidiFileToBase64encodedMidiFile(midiFile)); }
public static string GetMidiFromNotes(Song song, int songVersion) { var standardTicksPerQuarterNote = 96; var mf = new MidiFile(); mf.TimeDivision = new TicksPerQuarterNoteTimeDivision((short)standardTicksPerQuarterNote); var chunkitos = new Dictionary <GeneralMidi2Program, TrackChunk>(); TrackChunk percusionChunk = null; var instrumentsChannels = new Dictionary <GeneralMidi2Program, byte>(); foreach (var n in song.Versions[songVersion].Notes.OrderBy(n => n.StartSinceBeginningOfSongInTicks)) { if (n.IsPercussion) { if (percusionChunk == null) { percusionChunk = new TrackChunk(); } percusionChunk = AddEventsOfNote(percusionChunk, n, 9); } else { if (!chunkitos.Keys.Contains(n.Instrument)) { chunkitos[n.Instrument] = new TrackChunk(); } if (!(instrumentsChannels.Keys).ToList().Contains(n.Instrument)) { byte channel = GetFreeChannelForNote(song, instrumentsChannels, n, songVersion); instrumentsChannels[n.Instrument] = channel; chunkitos[n.Instrument] = AddProgramChangeEventToChunk(chunkitos[n.Instrument], channel, n.StartSinceBeginningOfSongInTicks, (byte)n.Instrument); } chunkitos[n.Instrument] = AddEventsOfNote(chunkitos[n.Instrument], n, instrumentsChannels[n.Instrument]); } } var channelIndependentChunkito = new TrackChunk(); channelIndependentChunkito = AddSetTempoEvents(channelIndependentChunkito, song.TempoChanges); var allChunks = chunkitos.Values.ToList(); if (percusionChunk != null) { allChunks.Add(percusionChunk); } if (channelIndependentChunkito.Events._events.Count > 0) { allChunks.Add(channelIndependentChunkito); } foreach (var chunky in allChunks) { chunky.Events._events = MidiProcessing.ConvertAccumulatedTimeToDeltaTime(chunky.Events.OrderBy(x => x.DeltaTime).ToList()); mf.Chunks.Add(chunky); } using (MemoryStream memStream = new MemoryStream(1000000)) { mf.Write(memStream); var bytes = memStream.ToArray(); return(Convert.ToBase64String(bytes)); } }