// Use this for initialization void Start() { SmfReader reader = new SmfReader(); reader.Read(File.OpenRead("mysong2.mid")); MidiMusic music = reader.Music; Directory.CreateDirectory("midi-out"); this.cutMusic(10000, 30000, music, "midi-out/1.mid"); reader.Read(File.OpenRead("midi-out/1.mid")); MidiMusic musicCut = reader.Music; Debug.Log("Original file length:" + GetTimeLengthInMinutes(music)); Debug.Log("Cut file length: " + GetTimeLengthInMinutes(musicCut)); }
/// <summary> /// Reads the given file with the given fileName. Expects a MIDI standard file. Otherwise an error will be trown. /// </summary> /// <param name="filePath">The path to the file.</param> public void readFile(string filePath) { FileStream fileStream = File.OpenRead(filePath); SmfReader reader = new SmfReader(); reader.Read(fileStream); this.music = reader.Music; fileStream.Close(); }
public static MidiMusic Load(string filename) { MidiMusic result = null; if (File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { SmfReader smf = new SmfReader(); smf.Read(fs); result = smf.Music; } } return(result); }
public static int Main(string [] args) { var apiProviderSpec = args.FirstOrDefault (a => a.StartsWith ("--provider:", StringComparison.Ordinal)); Type apiType = null; if (apiProviderSpec != null) { apiType = Type.GetType (apiProviderSpec.Substring ("--provider:".Length)); if (apiType == null) { ShowHelp (); Console.Error.WriteLine (); Console.Error.WriteLine (apiProviderSpec + " didn't work."); Console.Error.WriteLine (); return -1; } Console.Error.WriteLine ("Using MidiAccess '{0}'", apiType.AssemblyQualifiedName); } var api = apiProviderSpec != null ? (IMidiAccess) Activator.CreateInstance (apiType) : MidiAccessManager.Default; var output = api.Outputs.LastOrDefault (); var files = new List<string> (); bool diagnostic = false; foreach (var arg in args) { if (arg == apiProviderSpec) continue; if (arg == "--help") { ShowHelp (); return 0; } else if (arg == "--verbose") diagnostic = true; else if (arg.StartsWith ("--device:", StringComparison.Ordinal)) { output = api.Outputs.FirstOrDefault (o => o.Id == arg.Substring (9)); if (output == null) { ShowHelp (); Console.WriteLine (); Console.WriteLine ("Invalid MIDI output device ID."); Console.Error.WriteLine (); return -2; } } else files.Add (arg); } if (!files.Any ()) { ShowHelp (); return 0; } var wh = new ManualResetEvent (false); bool loop = true; foreach (var arg in files) { var parser = new SmfReader (); parser.Read (File.OpenRead (arg)); var player = new MidiPlayer (parser.Music, api.OpenOutputAsync (output.Id).Result); DateTimeOffset start = DateTimeOffset.Now; if (diagnostic) player.EventReceived += e => { string type = null; switch (e.EventType) { case SmfEvent.NoteOn: type = "NOn"; break; case SmfEvent.NoteOff: type = "NOff"; break; case SmfEvent.PAf: type = "PAf"; break; case SmfEvent.CC: type = "CC"; break; case SmfEvent.Program: type = "@"; break; case SmfEvent.CAf: type = "CAf"; break; case SmfEvent.Pitch: type = "P"; break; case SmfEvent.SysEx1: type = "SysEX"; break; case SmfEvent.SysEx2: type = "SysEX2"; break; case SmfEvent.Meta: type = "META"; break; } Console.WriteLine ("{0:06} {1:D02} {2} {3}", (DateTimeOffset.Now - start).TotalMilliseconds, e.Channel, type, e); }; player.Finished += delegate { loop = false; wh.Set (); }; new Task (() => { Console.WriteLine ("empty line to quit, P to pause and resume"); while (loop) { string line = Console.ReadLine (); if (line == "P") { if (player.State == PlayerState.Playing) player.PauseAsync (); else player.PlayAsync (); } else if (line == "") { loop = false; wh.Set (); player.Dispose (); break; } else Console.WriteLine ("what do you mean by '{0}' ?", line); } }).Start (); //player.StartLoop (); player.PlayAsync (); while (loop) { wh.WaitOne (); } player.PauseAsync (); } return 0; }
public static int Main(string [] args) { var apiProviderSpec = args.FirstOrDefault(a => a.StartsWith("--provider:", StringComparison.Ordinal)); Type apiType = null; if (apiProviderSpec != null) { apiType = Type.GetType(apiProviderSpec.Substring("--provider:".Length)); if (apiType == null) { ShowHelp(); Console.Error.WriteLine(); Console.Error.WriteLine(apiProviderSpec + " didn't work."); Console.Error.WriteLine(); return(-1); } Console.Error.WriteLine("Using MidiAccess '{0}'", apiType.AssemblyQualifiedName); } var api = apiProviderSpec != null ? (IMidiAccess)Activator.CreateInstance(apiType) : MidiAccessManager.Default; var output = api.Outputs.LastOrDefault(); var files = new List <string> (); bool diagnostic = false; foreach (var arg in args) { if (arg == apiProviderSpec) { continue; } if (arg == "--help") { ShowHelp(); return(0); } else if (arg == "--verbose") { diagnostic = true; } else if (arg.StartsWith("--device:", StringComparison.Ordinal)) { output = api.Outputs.FirstOrDefault(o => o.Id == arg.Substring(9)); if (output == null) { ShowHelp(); Console.WriteLine(); Console.WriteLine("Invalid MIDI output device ID."); Console.Error.WriteLine(); return(-2); } } else { files.Add(arg); } } if (!files.Any()) { ShowHelp(); return(0); } var wh = new ManualResetEvent(false); bool loop = true; foreach (var arg in files) { var parser = new SmfReader(); parser.Read(File.OpenRead(arg)); var player = new MidiPlayer(parser.Music, api.OpenOutputAsync(output.Id).Result); DateTimeOffset start = DateTimeOffset.Now; if (diagnostic) { player.EventReceived += e => { string type = null; switch (e.EventType) { case MidiEvent.NoteOn: type = "NOn"; break; case MidiEvent.NoteOff: type = "NOff"; break; case MidiEvent.PAf: type = "PAf"; break; case MidiEvent.CC: type = "CC"; break; case MidiEvent.Program: type = "@"; break; case MidiEvent.CAf: type = "CAf"; break; case MidiEvent.Pitch: type = "P"; break; case MidiEvent.SysEx1: type = "SysEX"; break; case MidiEvent.SysEx2: type = "SysEX2"; break; case MidiEvent.Meta: type = "META"; break; } Console.WriteLine("{0:06} {1:D02} {2} {3}", (DateTimeOffset.Now - start).TotalMilliseconds, e.Channel, type, e); } } ; player.Finished += delegate { loop = false; wh.Set(); }; new Task(() => { Console.WriteLine("empty line to quit, P to pause and resume"); while (loop) { string line = Console.ReadLine(); if (line == "P") { if (player.State == PlayerState.Playing) { player.PauseAsync(); } else { player.PlayAsync(); } } else if (line == "") { loop = false; wh.Set(); player.Dispose(); break; } else { Console.WriteLine("what do you mean by '{0}' ?", line); } } }).Start(); //player.StartLoop (); player.PlayAsync(); while (loop) { wh.WaitOne(); } player.PauseAsync(); } return(0); } }
static void Main(string[] args) { SmfReader SMFR = new SmfReader(); const int NOTEONSTART = 0x90; const int NOTEONEND = 0x9F; const int NOTEOFFSTART = 0x80; const int NOTEOFFEND = 0x8F; bool ExpectingNoteOff = false; bool ExpectingNoteOn = true; const double DefaultBPM = 120; const double DefaultTickToMicroSecond = 8.33333; // Read the file and display it line by line. string BaseDir = @"C:\temp\"; string File = "CAN_LONG"; string ExtOutput = ".txt"; string ExtInput = ".mid"; string Infile = BaseDir + File + ExtInput; System.IO.StreamWriter Outfile = new System.IO.StreamWriter(BaseDir + File + "_converted" + ExtOutput); List <Note> Chanson = new List <Note>(); List <string> Warnings = new List <string>(); using (FileStream FS = System.IO.File.OpenRead(Infile)) { SMFR.Read(FS); double TickToMicroSecond = DefaultTickToMicroSecond; if (SMFR.Music.DeltaTimeSpec != 0) { TickToMicroSecond = DefaultTickToMicroSecond * (DefaultBPM / (double)SMFR.Music.DeltaTimeSpec); } foreach (MidiMessage M in SMFR.Music.Tracks[1].Messages) { var E = M.Event; //NOTE ON if (M.Event.EventType >= NOTEONSTART && M.Event.EventType <= NOTEONEND) { //Start new note if (ExpectingNoteOn) { //Close previous note. if (Chanson.Count != 0) { Chanson[Chanson.Count - 1].SilenceAfter = M.DeltaTime * TickToMicroSecond; } Chanson.Add(new Note() { MidiNote = E.Msb, Freq = E.Frequency(), Duration = 0, SilenceAfter = 0 }); } //Close Previous note else { int PrevNote = Chanson.Count - 1; Warnings.Add("Unexpected note ON after note number: " + Chanson.Count + " MidiNote: " + Chanson[PrevNote].MidiNote); Chanson[PrevNote].Duration = M.DeltaTime * TickToMicroSecond; Chanson[PrevNote].SilenceAfter = 0; } ExpectingNoteOff = true; ExpectingNoteOn = false; } //NOTE OFF if (M.Event.EventType >= NOTEOFFSTART && M.Event.EventType <= NOTEOFFEND) { if (ExpectingNoteOff) { ExpectingNoteOff = false; ExpectingNoteOn = true; Chanson[Chanson.Count - 1].Duration = M.DeltaTime * TickToMicroSecond; } else { Warnings.Add("Unexpected note OFF after note number: " + Chanson.Count + " MidiNote: " + Chanson[Chanson.Count - 1].MidiNote); } } } } const int MaxDataCnt = 3; //OutputFormat: /* * float ChansonDEDU[3][18] = { * {196,196,196,247,247,261,261,349,247,196,196,294,247,196,330,294,247,261}, * {150,150,150,300,300,300,300,600,150,150,150,150,150,150,600,150,150,600}, * {100,200,100,200,200,200,200,200,100,200,100,200,100,400,400,200,100,200}, * }; */ int DataTripletCounter = Chanson.Count; Outfile.WriteLine(""); Outfile.WriteLine("const PROGMEM float " + File + "[" + MaxDataCnt + "][" + DataTripletCounter + "] = {"); Outfile.Write(" {"); for (int i = 0; i < DataTripletCounter; i++) { Outfile.Write(Math.Round(Chanson[i].Freq, 1)); if (i < DataTripletCounter - 1) { Outfile.Write(","); } } Outfile.Write("},\n"); Outfile.Write(" {"); for (int i = 0; i < DataTripletCounter; i++) { Outfile.Write(Math.Round(Chanson[i].Duration, 1)); if (i < DataTripletCounter - 1) { Outfile.Write(","); } } Outfile.Write("},\n"); Outfile.Write(" {"); for (int i = 0; i < DataTripletCounter; i++) { Outfile.Write(Math.Round(Chanson[i].SilenceAfter, 1)); if (i < DataTripletCounter - 1) { Outfile.Write(","); } } Outfile.Write("}\n"); Outfile.WriteLine(" };"); Outfile.WriteLine(""); Outfile.WriteLine(""); Outfile.WriteLine(""); Outfile.WriteLine(""); Outfile.WriteLine(""); Outfile.WriteLine(""); Outfile.WriteLine(""); Outfile.WriteLine("case XXXX:"); Outfile.WriteLine(" pf = (float*)" + File + ";"); Outfile.WriteLine(" NombreDeNotes = sizeof(" + File + "[0]) / sizeof(float);"); Outfile.WriteLine(" for (int i = 0; i < ParamChansons; i++)"); Outfile.WriteLine(" {"); Outfile.WriteLine(" for (int j = 0; j < NombreDeNotes; j++)"); Outfile.WriteLine(" {"); Outfile.WriteLine(" MaChanson[i][j] = pgm_read_float(pf+i*NombreDeNotes+j);"); Outfile.WriteLine(" }"); Outfile.WriteLine(" }"); Outfile.WriteLine(" ChansonFacteurRandomMin = XXXX;"); Outfile.WriteLine(" ChansonFacteurRandomMax = XXXX;"); Outfile.WriteLine(" return NombreDeNotes;"); Outfile.WriteLine("\n\n\n"); Outfile.WriteLine("ErrorLog count: " + Warnings.Count); foreach (string w in Warnings) { Outfile.WriteLine(w); } Outfile.Close(); System.Console.WriteLine("Export Complete"); // Suspend the screen. //System.Console.ReadLine(); }