Advertisement
Guest User

YM2149 Steamer

a guest
Apr 9th, 2012
2,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Ports;
  4. using System.Text;
  5.  
  6. namespace YMSerial
  7. {
  8.     class MainClass
  9.     {
  10.        
  11.         public static void Main (string[] args)
  12.         {
  13.            
  14.             string fileName = "THINGS.Y5";
  15.            
  16.             Console.WriteLine ("YMSerial, simple streamer for YM2149.");
  17.            
  18.             Console.WriteLine ("Opening serial port");
  19.             SerialPort sp = new SerialPort("/dev/cu.SLAB_USBtoUART",115200);
  20.             sp.Open();
  21.            
  22.             //Arduino reset
  23.             sp.RtsEnable = false;
  24.             System.Threading.Thread.Sleep(500);
  25.             sp.RtsEnable = true;
  26.            
  27.             Console.WriteLine ("Opening file");
  28.             BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
  29.             Console.WriteLine ();
  30.            
  31.             string tempString;
  32.             uint tempInt;     //4 bytes
  33.             ushort tempShort; //2 bytes
  34.            
  35.             int frameCount;
  36.             int loopFrame;
  37.            
  38.             //Header
  39.             ASCIIEncoding encoding = new ASCIIEncoding();
  40.            
  41.             tempString = encoding.GetString(br.ReadBytes(4));
  42.            
  43.             Console.WriteLine ("Type: " + tempString);
  44.            
  45.             tempString = encoding.GetString(br.ReadBytes(8));
  46.             Console.WriteLine ("Check string: " + tempString);
  47.  
  48.             frameCount = (int)SwapByteOrder(br.ReadUInt32());
  49.             Console.WriteLine ("Frame count: " + frameCount);
  50.            
  51.             tempInt = SwapByteOrder(br.ReadUInt32());
  52.             Console.WriteLine ("Song attributes: " + tempInt);
  53.            
  54.             tempShort = SwapByteOrder(br.ReadUInt16());
  55.             Console.WriteLine ("Digidrums samples: " + tempShort);
  56.            
  57.             tempInt = SwapByteOrder(br.ReadUInt32());
  58.             Console.WriteLine ("YM Frequency: " + tempInt + "Hz");         
  59.  
  60.             tempShort = SwapByteOrder(br.ReadUInt16());
  61.             Console.WriteLine ("Frame rate: " + tempShort +"Hz");  
  62.            
  63.             loopFrame = (int)SwapByteOrder(br.ReadUInt32());
  64.             Console.WriteLine ("Loop Frame Number: " + loopFrame);
  65.            
  66.             br.ReadUInt16(); //Unused bytes
  67.            
  68.             //Song Info
  69.             Console.WriteLine ();
  70.             tempString = ReadNullTerminationString(br,encoding);
  71.             Console.WriteLine ("Title: " + tempString);
  72.             tempString = ReadNullTerminationString(br,encoding);
  73.             Console.WriteLine ("Artist: " + tempString);
  74.             tempString = ReadNullTerminationString(br,encoding);
  75.             Console.WriteLine ("Comments: " + tempString);
  76.            
  77.             //Read interleaved frames
  78.             byte[] output = ReadAllFrames(br,frameCount);          
  79.             byte[] outputByte = new byte[1];
  80.            
  81.             br.Close();
  82.            
  83.             string totalTime = TimeSpan.FromSeconds((frameCount/50)).ToString();
  84.            
  85.             //Stream frames
  86.             for (int i = 0; i < frameCount; i++) {
  87.                
  88.                 for (int j = 0; j < 16; j++) {
  89.                     outputByte[0] = output[i*16+j];
  90.                     sp.Write(outputByte,0,1);
  91.                 }
  92.                
  93.                 TimeSpan ts = TimeSpan.FromSeconds(((i+1)/50));
  94.                
  95.                 Console.Write("\r{2}/{3} - Frame {0}/{1}    ", i+1, frameCount, ts.ToString(), totalTime);
  96.                 System.Threading.Thread.Sleep(20);
  97.                
  98.                 //infinite loop
  99.                 if (i == (frameCount-1)) {
  100.                     i = loopFrame-1;
  101.                 }
  102.             }
  103.            
  104.             //mute YM2149
  105.             for (int j = 0; j < 16; j++) {
  106.                 outputByte[0] = 0;
  107.                 sp.Write(outputByte,0,1);
  108.             }
  109.            
  110.             sp.Close();
  111.         }
  112.        
  113.         public static byte[] ReadAllFrames(BinaryReader reader, int frameCount)
  114.         {
  115.             int totalBytes = 16 * frameCount;
  116.             int currentRegisterStreamSize = totalBytes/16;
  117.            
  118.             byte[] tempArray = new byte[totalBytes];
  119.             byte[] finalArray = new byte[totalBytes];
  120.            
  121.             tempArray = reader.ReadBytes(totalBytes);
  122.            
  123.             byte currentValue;
  124.            
  125.             for (int currentRegister = 0; currentRegister < 16; currentRegister++) {
  126.                
  127.                 int offset = (currentRegisterStreamSize*(currentRegister));
  128.                 for (int i = 0; i < currentRegisterStreamSize; i++)
  129.                 {  
  130.                     currentValue = tempArray[i+offset];
  131.                     finalArray[16*i + currentRegister] = currentValue;
  132.                 }      
  133.             }
  134.             return finalArray;
  135.         }
  136.        
  137.         public static string ReadNullTerminationString(BinaryReader reader, Encoding encoding)
  138.         {
  139.             byte[] temp = new byte[1];
  140.             StringBuilder sb = new StringBuilder();
  141.             while (true)
  142.             {
  143.                 temp[0] = reader.ReadByte();
  144.                
  145.                 if (temp[0] == 0x00) {
  146.                     return sb.ToString();
  147.                 } else {
  148.                     sb.Append(encoding.GetString(temp));
  149.                 }
  150.             }
  151.         }
  152.        
  153.         public static uint SwapByteOrder(uint valueToSwap)
  154.         {
  155.             uint uvalue = valueToSwap;
  156.             uint swapped =
  157.                 ( (0x000000FF) & (uvalue >> 24)
  158.                 | (0x0000FF00) & (uvalue >> 8)
  159.                 | (0x00FF0000) & (uvalue << 8)
  160.                 | (0xFF000000) & (uvalue << 24)
  161.                 );
  162.             return swapped;
  163.         }
  164.        
  165.         public static ushort SwapByteOrder(ushort valueToSwap)
  166.         {
  167.             var uvalue = valueToSwap;
  168.             var swapped =
  169.                 ( (0x00FF) & (uvalue >> 8)
  170.                 | (0xFF00) & (uvalue << 8)
  171.                 );
  172.             return (ushort)swapped;
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement