Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.IO.Ports;
- using System.Text;
- namespace YMSerial
- {
- class MainClass
- {
- public static void Main (string[] args)
- {
- string fileName = "THINGS.Y5";
- Console.WriteLine ("YMSerial, simple streamer for YM2149.");
- Console.WriteLine ("Opening serial port");
- SerialPort sp = new SerialPort("/dev/cu.SLAB_USBtoUART",115200);
- sp.Open();
- //Arduino reset
- sp.RtsEnable = false;
- System.Threading.Thread.Sleep(500);
- sp.RtsEnable = true;
- Console.WriteLine ("Opening file");
- BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
- Console.WriteLine ();
- string tempString;
- uint tempInt; //4 bytes
- ushort tempShort; //2 bytes
- int frameCount;
- int loopFrame;
- //Header
- ASCIIEncoding encoding = new ASCIIEncoding();
- tempString = encoding.GetString(br.ReadBytes(4));
- Console.WriteLine ("Type: " + tempString);
- tempString = encoding.GetString(br.ReadBytes(8));
- Console.WriteLine ("Check string: " + tempString);
- frameCount = (int)SwapByteOrder(br.ReadUInt32());
- Console.WriteLine ("Frame count: " + frameCount);
- tempInt = SwapByteOrder(br.ReadUInt32());
- Console.WriteLine ("Song attributes: " + tempInt);
- tempShort = SwapByteOrder(br.ReadUInt16());
- Console.WriteLine ("Digidrums samples: " + tempShort);
- tempInt = SwapByteOrder(br.ReadUInt32());
- Console.WriteLine ("YM Frequency: " + tempInt + "Hz");
- tempShort = SwapByteOrder(br.ReadUInt16());
- Console.WriteLine ("Frame rate: " + tempShort +"Hz");
- loopFrame = (int)SwapByteOrder(br.ReadUInt32());
- Console.WriteLine ("Loop Frame Number: " + loopFrame);
- br.ReadUInt16(); //Unused bytes
- //Song Info
- Console.WriteLine ();
- tempString = ReadNullTerminationString(br,encoding);
- Console.WriteLine ("Title: " + tempString);
- tempString = ReadNullTerminationString(br,encoding);
- Console.WriteLine ("Artist: " + tempString);
- tempString = ReadNullTerminationString(br,encoding);
- Console.WriteLine ("Comments: " + tempString);
- //Read interleaved frames
- byte[] output = ReadAllFrames(br,frameCount);
- byte[] outputByte = new byte[1];
- br.Close();
- string totalTime = TimeSpan.FromSeconds((frameCount/50)).ToString();
- //Stream frames
- for (int i = 0; i < frameCount; i++) {
- for (int j = 0; j < 16; j++) {
- outputByte[0] = output[i*16+j];
- sp.Write(outputByte,0,1);
- }
- TimeSpan ts = TimeSpan.FromSeconds(((i+1)/50));
- Console.Write("\r{2}/{3} - Frame {0}/{1} ", i+1, frameCount, ts.ToString(), totalTime);
- System.Threading.Thread.Sleep(20);
- //infinite loop
- if (i == (frameCount-1)) {
- i = loopFrame-1;
- }
- }
- //mute YM2149
- for (int j = 0; j < 16; j++) {
- outputByte[0] = 0;
- sp.Write(outputByte,0,1);
- }
- sp.Close();
- }
- public static byte[] ReadAllFrames(BinaryReader reader, int frameCount)
- {
- int totalBytes = 16 * frameCount;
- int currentRegisterStreamSize = totalBytes/16;
- byte[] tempArray = new byte[totalBytes];
- byte[] finalArray = new byte[totalBytes];
- tempArray = reader.ReadBytes(totalBytes);
- byte currentValue;
- for (int currentRegister = 0; currentRegister < 16; currentRegister++) {
- int offset = (currentRegisterStreamSize*(currentRegister));
- for (int i = 0; i < currentRegisterStreamSize; i++)
- {
- currentValue = tempArray[i+offset];
- finalArray[16*i + currentRegister] = currentValue;
- }
- }
- return finalArray;
- }
- public static string ReadNullTerminationString(BinaryReader reader, Encoding encoding)
- {
- byte[] temp = new byte[1];
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- temp[0] = reader.ReadByte();
- if (temp[0] == 0x00) {
- return sb.ToString();
- } else {
- sb.Append(encoding.GetString(temp));
- }
- }
- }
- public static uint SwapByteOrder(uint valueToSwap)
- {
- uint uvalue = valueToSwap;
- uint swapped =
- ( (0x000000FF) & (uvalue >> 24)
- | (0x0000FF00) & (uvalue >> 8)
- | (0x00FF0000) & (uvalue << 8)
- | (0xFF000000) & (uvalue << 24)
- );
- return swapped;
- }
- public static ushort SwapByteOrder(ushort valueToSwap)
- {
- var uvalue = valueToSwap;
- var swapped =
- ( (0x00FF) & (uvalue >> 8)
- | (0xFF00) & (uvalue << 8)
- );
- return (ushort)swapped;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement