public virtual IList <OutputFile> ProcessStream(Stream input, ConvertOptions options) { List <RPCCommand> rpc = new List <RPCCommand>(); using (BinaryReader reader = new BinaryReader(input)) { reader.ReadInt16(); int length = reader.ReadInt16(); byte lastb = 0; int delay = 0; for (int i = 0; i < length; i++) { byte b = reader.ReadByte(); if (b != lastb) { rpc.Add(RPCCommand.Delay(delay * 1000 / 140)); delay = 0; } if (b == 0) { rpc.Add(RPCCommand.ClearCountdown()); } else { double freq = b * 15; //frequencies[b]; rpc.Add(RPCCommand.SetCountdown(LoadMDT.FrequencyToCountdown(freq))); } delay += 1; lastb = b; } rpc.Add(RPCCommand.Delay(delay * 1000 / 140)); } return(LoadPCS.ProcessRPC(rpc, options)); }
public static short[] CreateSong(IList <RPCCommand> data, WaveFunction waveType, double volume, bool clip, int frequency) { var song = new WaveSong(); song.NoClipping = !clip; song.Volume = volume; int time = 0; var channels = new Dictionary <int, WaveSong.Track>(); foreach (var cmd in data) { WaveSong.Track playing; switch (cmd.Type) { case RPCCommandType.Delay: time += cmd.DelayValue; break; case RPCCommandType.SetCountdown: double freq = LoadMDT.CountdownToFrequency(cmd.Data); if (!channels.TryGetValue(cmd.Channel, out playing) || playing.Wave.Frequency != freq) { if (playing.Wave != null) { playing.Wave.Duration = time - playing.Start; } playing = new WaveSong.Track(time, new Wave(freq, 0) { Type = waveType }); song.Waves.Add(playing); channels[cmd.Channel] = playing; } break; case RPCCommandType.ClearCountdown: if (channels.TryGetValue(cmd.Channel, out playing)) { if (playing.Wave != null) { playing.Wave.Duration = time - playing.Start; } channels.Remove(cmd.Channel); } break; } } foreach (WaveSong.Track playing in channels.Values) { playing.Wave.Duration = time - playing.Start; } return(song.GetSamples <short>(frequency)); }
public override void ProcessFile(OutputFile file, ConvertOptions options) { using (BinaryWriter writer = new BinaryWriter(new FileStream(file.Path, FileMode.Create), Encoding.ASCII)) { writer.Write((byte)0x08); writer.Write("MONOTONE".ToCharArray()); writer.Write(new byte[82]); writer.Write(new byte[] { 1, 1, 1, 2, 0 }); writer.Write(Enumerable.Repeat((byte)0xFF, 255).ToArray()); byte lastb = 0; foreach (var cmd in file.Data) { switch (cmd.Type) { case RPCCommandType.SetCountdown: double freq = LoadMDT.CountdownToFrequency(cmd.Data); var bval = freq / 15; if (bval > 255) { lastb = 0; } else { lastb = (byte)bval; } break; case RPCCommandType.ClearCountdown: lastb = 0; break; case RPCCommandType.Delay: for (int i = 0; i < cmd.DelayValue * 60 / 1000.0; i++) { writer.Write((byte)0); writer.Write(lastb); } break; } } } }
public virtual void ProcessFile(OutputFile file, ConvertOptions options) { using (var stream = new FileStream(file.Path, FileMode.Create)) { var writer = new BinaryWriter(stream); writer.Write(0); byte lastb = 0; foreach (var cmd in file.Data) { switch (cmd.Type) { case RPCCommandType.SetCountdown: double freq = LoadMDT.CountdownToFrequency(cmd.Data); var bval = freq / 15; if (bval > 255) { lastb = 0; } else { lastb = (byte)bval; } break; case RPCCommandType.ClearCountdown: lastb = 0; break; case RPCCommandType.Delay: for (int i = 0; i < cmd.DelayValue * 140 / 1000.0; i++) { writer.Write(lastb); } break; } } stream.Position = 2; writer.Write((short)(stream.Length - 4)); } }
public IEnumerable <RPCCommand> GetCommands(string ppMusicString) { int pos = 0; while (NextNote(ppMusicString, ref pos)) { double freq = Note2Freq(Note) * 2; NoteTime += NoteDuration; State = PlayerState.Parsing; double silence = 0; if (Mode == PlayerMode.Normal) { silence = NoteDuration / 8; } else if (Mode == PlayerMode.Staccato) { silence = NoteDuration / 4; } if (freq > 0) { yield return(RPCCommand.SetCountdown(LoadMDT.FrequencyToCountdown(freq))); } else { yield return(RPCCommand.ClearCountdown()); } yield return(RPCCommand.Delay((int)Math.Round((NoteDuration - silence) * 1000))); if (silence > 0) { yield return(RPCCommand.ClearCountdown()); yield return(RPCCommand.Delay((int)Math.Round(silence * 1000))); } } }
public static short[] CreateSong(IList <RPCCommand> data, WaveFunction waveType, double volume, bool clip, int frequency, double?clickLength, bool temper) { var song = new WaveSong(); song.NoClipping = !clip; song.Volume = volume; bool informed = false; int time = 0; var channels = new Dictionary <int, WaveSong.Track>(); foreach (var cmd in data) { WaveSong.Track playing; switch (cmd.Type) { case RPCCommandType.Delay: time += cmd.DelayValue; break; case RPCCommandType.SetCountdown: double freq = LoadMDT.CountdownToFrequency(cmd.Data); if (temper) { freq = TemperFrequency(freq); } if (!channels.TryGetValue(cmd.Channel, out playing) || playing.Wave.Frequency != freq) { double phase = 0; if (playing.Wave != null) { playing.Wave.Duration = time - playing.Start; phase = playing.Wave.RemainingPhaseShift; } playing = new WaveSong.Track(time, new Wave(freq, 0) { Type = waveType, PhaseShiftCoef = phase }); song.Waves.Add(playing); channels[cmd.Channel] = playing; } break; case RPCCommandType.ClearCountdown: if (channels.TryGetValue(cmd.Channel, out playing)) { if (playing.Wave != null) { playing.Wave.Duration = time - playing.Start; if (playing.Wave.Duration == 0) { if (clickLength != null) { playing.Wave.Duration = 1000 * clickLength.Value / playing.Wave.Frequency; } else if (!informed) { Console.WriteLine("Song contains zero-length waves. Use --clicks 0.5 to render them as clicks."); informed = true; } } } channels.Remove(cmd.Channel); } break; } } foreach (WaveSong.Track playing in channels.Values) { playing.Wave.Duration = time - playing.Start; } return(song.GetSamples <short>(frequency)); }