private static void HandleConnection(PlayerIOClient.Connection con) { con.OnMessage += new MessageReceivedEventHandler(onMessage); con.Send("init"); con.Send("init2"); int dots = 0; while (WorldKey == "") { Console.Write("Connecting"); for (int i = 0; i < dots; ++i) { Console.Write("."); } Console.Write("\r"); System.Threading.Thread.Sleep(250); dots = (dots + 1) % 4; } Console.Write("\n"); Console.WriteLine("Connected to room"); Midi m = new Midi(); while (true) { Console.Write("Command: "); string cmd = Console.ReadLine(); string[] cmds = cmd.Split(' '); switch (cmds [0]) { case "midi": try{ for (int i = 0; i < 16; ++i) { chans[i] = true; } m = new Midi(); Console.WriteLine("Successfully loaded."); if (!m.Read(cmd.Substring(5))) { Console.WriteLine("Failed to load file. Reason:"); Console.WriteLine(m.LastError); } } catch (Exception e) { Console.WriteLine(e.Message); } break; case "write": Console.WriteLine("Writing to world (This will take a while...)\nPress q to abort."); m.Rewind(); Write(con, m); Console.WriteLine("Done!"); break; case "quit": case "exit": case "close": throw new Exception("Exiting..."); case "strip": foreach (string s in cmds) { try { chans [Convert.ToInt16(s)] = false; } catch (Exception e) { } } break; case "unstrip": foreach (string s in cmds) { try { chans [Convert.ToInt16(s)] = true; } catch (Exception e) { } } break; case "help": Console.WriteLine("Available commands are:\n" + " midi [path to midi file] - Load a midi file\n" + " write - Save the midi file to the level\n" + " strip [0-15] - Remove a channel from the midi file (9 is drums)\n" + " unstrip [0-15] - Add a channel back to the midi\n" + " help - Display this message.\n"); break; default: Console.WriteLine("Unknown command. Try `help`."); break; } } }
public static void Write(PlayerIOClient.Connection con, Midi m) { //If calling from multiple threads, ensure the existing one aborts. if (stop == 2) { stop = 1; while (stop == 1) { System.Threading.Thread.Sleep(100); } } stop = 2; con.Send("clear"); System.Threading.Thread.Sleep(100); long x = 1; long y = 2; long offset = 0; int distance = WorldHeight - 6; int id = 1; long xprev = 1; long tempo = 60000000 / 120; //Default microseconds per beat (120 bpm) long tempobase = 0; // long tempooffset = 0; // while (stop == 2) { if (Console.KeyAvailable) { if (Console.ReadKey().KeyChar == 'q') { break; } } Midi.Event e = m.NextEvent(); if (!e.Valid) { break; } long timestamp = (e.Time - tempobase) * tempo / m.TicksPerQuarter / (1000000L / 85L) + tempooffset; if (e.Status == 0xFF) { //On meta event, I only really care about tempo here. if (e.Arg1 == 0x51 && e.Arg2 == 3) { tempooffset = timestamp; tempobase = e.Time; tempo = (e.Data [0] << 16) + (e.Data[1] << 8) + e.Data[2]; } } if ((e.Status >> 4) == 9) { int note = e.Arg1; for (int i = 3; i < e.Arg2; i += 805) //If the note volume is 3 or less, don't emit a note. //If the note volume is 88 or higher, emit two notes. //e.Time is in ticks, tempo is in microseconds per tick. //e.Time * tempo / m.TicksPerQuarter = the event time in microseconds. //Divide that by how long each block should represent (I use 1/85th of a second) { if (timestamp <= offset) { offset++; } else { offset = timestamp; } //Calculate the X/Y coordinates based on time. This does NOT account for time spent in portals. //Someone whould probably fix that. x = offset / distance + 2; y = offset % distance + 3; if (chans [(e.Status & 0xF)]) { //If the channel is 9 (AKA MIDI channel 10) if ((e.Status & 0xF) == 9) { //Deal with drums if (note >= 35 && note <= 81) { con.Send(WorldKey, 0, x, y, 83, percussion [note - 35]); System.Threading.Thread.Sleep(30); break; } } else { //C3 is note 48 in MIDI, and note 0 in EE con.Send(WorldKey, 0, x, y, 77, note - 48); System.Threading.Thread.Sleep(30); } } } } //If there's space missing portals... if (x > xprev) { for (long i = xprev; i < x; ++i) { //Add in portals to the slack space con.Send(WorldKey, 0, i, 2, 242, 1, id, id - 1); System.Threading.Thread.Sleep(30); con.Send(WorldKey, 0, i, WorldHeight - 3, 242, 1, id + 1, id + 2); System.Threading.Thread.Sleep(30); id += 2; } } xprev = x; } //Fill in the last column of portals con.Send(WorldKey, 0, x, 2, 242, 1, id, id - 1); System.Threading.Thread.Sleep(30); con.Send(WorldKey, 0, x, WorldHeight - 3, 242, 1, id + 1, 3); stop = 0; }
public Worker(PlayerIOClient.Connection othercon, Midi otherm) { con = othercon; m = otherm; }
public static void Write(PlayerIOClient.Connection con, Midi m) { //If calling from multiple threads, ensure the existing one aborts. if (stop == 2) { stop = 1; while (stop == 1) { System.Threading.Thread.Sleep (100); } } stop = 2; con.Send ("clear"); System.Threading.Thread.Sleep (100); long x = 1; long y = 2; long offset = 0; int distance = WorldHeight - 6; int id = 1; long xprev = 1; long tempo = 60000000 / 120; //Default microseconds per beat (120 bpm) long tempobase = 0; // long tempooffset = 0; // while (stop == 2) { if (Console.KeyAvailable) { if (Console.ReadKey ().KeyChar == 'q') { break; } } Midi.Event e = m.NextEvent (); if (!e.Valid) { break; } long timestamp = (e.Time - tempobase) * tempo / m.TicksPerQuarter / (1000000L / 85L) + tempooffset; if (e.Status == 0xFF) { //On meta event, I only really care about tempo here. if (e.Arg1 == 0x51 && e.Arg2 == 3) { tempooffset = timestamp; tempobase = e.Time; tempo = (e.Data [0] << 16 ) + (e.Data[1] << 8 ) + e.Data[2]; } } if ((e.Status >> 4) == 9) { int note = e.Arg1; for (int i = 3; i < e.Arg2; i += 805) { //If the note volume is 3 or less, don't emit a note. //If the note volume is 88 or higher, emit two notes. //e.Time is in ticks, tempo is in microseconds per tick. //e.Time * tempo / m.TicksPerQuarter = the event time in microseconds. //Divide that by how long each block should represent (I use 1/85th of a second) if (timestamp <= offset) { offset++; } else { offset = timestamp; } //Calculate the X/Y coordinates based on time. This does NOT account for time spent in portals. //Someone whould probably fix that. x = offset / distance + 2; y = offset % distance + 3; if (chans [(e.Status & 0xF)]) { //If the channel is 9 (AKA MIDI channel 10) if ((e.Status & 0xF) == 9) { //Deal with drums if (note >= 35 && note <= 81) { con.Send ("b",0, x, y, 83, percussion [note - 35]); System.Threading.Thread.Sleep (30); break; } } else { //C3 is note 48 in MIDI, and note 0 in EE con.Send ("b",0, x, y, 77, note - 48); System.Threading.Thread.Sleep (30); } } } } //If there's space missing portals... if (x > xprev) { for (long i = xprev; i < x; ++i) { //Add in portals to the slack space con.Send ("b",0, i, 2, 242, 1, id, id - 1); System.Threading.Thread.Sleep (30); con.Send ("b",0, i, WorldHeight - 3, 242, 1, id + 1, id + 2); System.Threading.Thread.Sleep (30); id += 2; } } xprev = x; } //Fill in the last column of portals con.Send ("b",0, x, 2, 242, 1, id, id - 1); System.Threading.Thread.Sleep (30); con.Send ("b",0, x, WorldHeight - 3, 242, 1, id + 1, 3); stop = 0; }
private static void HandleConnection(PlayerIOClient.Connection con) { con.OnMessage += new MessageReceivedEventHandler (onMessage); con.Send ("init"); con.Send ("init2"); int dots = 0; while (WorldHeight == 0) { Console.Write ("Connecting"); for (int i = 0; i < dots; ++i) { Console.Write ("."); } Console.Write ("\r"); System.Threading.Thread.Sleep (250); dots = (dots + 1) % 4; } Console.Write ("\n"); Console.WriteLine ("Connected to room"); Midi m = new Midi (); while (true) { Console.Write ("Command: "); string cmd = Console.ReadLine (); string[] cmds = cmd.Split (' '); switch (cmds [0]) { case "midi": try{ for( int i = 0; i < 16; ++i ){ chans[i] = true; } m = new Midi (); Console.WriteLine ("Successfully loaded."); if (!m.Read (cmd.Substring(5))) { Console.WriteLine ("Failed to load file. Reason:"); Console.WriteLine (m.LastError); } } catch( Exception e ){ Console.WriteLine (e.Message); } break; case "write": Console.WriteLine ("Writing to world (This will take a while...)\nPress q to abort."); m.Rewind (); Write (con, m); Console.WriteLine ("Done!"); break; case "quit": case "exit": case "close": throw new Exception ("Exiting..."); case "strip": foreach (string s in cmds) { try { chans [Convert.ToInt16 (s)] = false; } catch { } } break; case "unstrip": foreach (string s in cmds) { try { chans [Convert.ToInt16 (s)] = true; } catch { } } break; case "help": Console.WriteLine("Available commands are:\n" + " midi [path to midi file] - Load a midi file\n" + " write - Save the midi file to the level\n" + " strip [0-15] - Remove a channel from the midi file (9 is drums)\n" + " unstrip [0-15] - Add a channel back to the midi\n" + " help - Display this message.\n"); break; default: Console.WriteLine("Unknown command. Try `help`."); break; } } }