Advertisement
lpdunwell

YM2HEX

Apr 15th, 2012
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.IO;
  5.  
  6. namespace YMextractor
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         OpenFileDialog InputFile = new OpenFileDialog();
  11.  
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.  
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             InputFile.RestoreDirectory  = true;
  20.             InputFile.Filter = "YM-Dateien (*.YM)|*.YM";
  21.             InputFile.ShowDialog();
  22.  
  23.             if (File.Exists(InputFile.FileName.ToString()))
  24.             {
  25.                 FileNameBox.Text = InputFile.FileName.ToString();
  26.                 button2.Enabled = true;
  27.             }
  28.             else
  29.             {
  30.                 button2.Enabled = false;
  31.             }
  32.         }
  33.  
  34.         private void button2_Click(object sender, EventArgs e)
  35.         {
  36.             Extractor.Extract(InputFile.FileName.ToString());
  37.             MessageBox.Show("Done");
  38.         }
  39.     }
  40.  
  41.     public partial class Extractor
  42.     {
  43.         public static void Extract(string filename)
  44.         {
  45.             BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open));
  46.             BinaryWriter bw = new BinaryWriter(File.Open(filename.Remove(filename.LastIndexOf('.')) + ".hex", FileMode.Create));
  47.            
  48.             string tempString;
  49.             uint tempInt;       //4 bytes
  50.             ushort tempShort;   //2 bytes
  51.  
  52.             int frameCount;
  53.             int loopFrame;
  54.  
  55.             //Header
  56.             ASCIIEncoding encoding = new ASCIIEncoding();
  57.  
  58.             tempString = encoding.GetString(br.ReadBytes(4));       //Type
  59.             tempString = encoding.GetString(br.ReadBytes(8));       //Check string
  60.             frameCount = (int)SwapByteOrder(br.ReadUInt32());       //Frame count
  61.             tempInt = SwapByteOrder(br.ReadUInt32());               //Song attributes
  62.             tempShort = SwapByteOrder(br.ReadUInt16());             //Digidrums samples
  63.             tempInt = SwapByteOrder(br.ReadUInt32());               //YM Frequency
  64.             tempShort = SwapByteOrder(br.ReadUInt16());             //Frame rate
  65.             loopFrame = (int)SwapByteOrder(br.ReadUInt32());        //Loop Frame Number
  66.             br.ReadUInt16();                                        //Unused bytes
  67.            
  68.             //Song Info
  69.             tempString = ReadNullTerminationString(br, encoding);   //Title
  70.             tempString = ReadNullTerminationString(br, encoding);   //Artist
  71.             tempString = ReadNullTerminationString(br, encoding);   //Comments
  72.            
  73.             //Read interleaved frames
  74.             byte[] output = ReadAllFrames(br, frameCount);
  75.             byte[] outputByte = new byte[1];
  76.  
  77.             br.Close();
  78.  
  79.             //Stream frames
  80.             for(int i = 0; i < frameCount; i++)
  81.             {
  82.                 for(int j = 0; j < 16; j++)
  83.                 {
  84.                     outputByte[0] = output[i * 16 + j];
  85.                     bw.Write(outputByte);
  86.                 }
  87.  
  88.                 TimeSpan ts = TimeSpan.FromSeconds(((i + 1) / 50));
  89.  
  90.                 //infinite loop
  91.                 if(i == (frameCount - 1)) {}
  92.             }
  93.  
  94.             //mute YM2149
  95.             for(int j = 0; j < 16; j++)
  96.             {
  97.                 outputByte[0] = 0;
  98.                 bw.Write(outputByte);
  99.             }
  100.  
  101.             bw.Close();
  102.         }
  103.  
  104.         public static byte[] ReadAllFrames(BinaryReader reader, int frameCount)
  105.         {
  106.             int totalBytes = 16 * frameCount;
  107.             int currentRegisterStreamSize = totalBytes / 16;
  108.  
  109.             byte[] tempArray = new byte[totalBytes];
  110.             byte[] finalArray = new byte[totalBytes];
  111.  
  112.             tempArray = reader.ReadBytes(totalBytes);
  113.  
  114.             byte currentValue;
  115.  
  116.             for(int currentRegister = 0; currentRegister < 16; currentRegister++)
  117.             {
  118.                 int offset = (currentRegisterStreamSize * (currentRegister));
  119.                 for(int i = 0; i < currentRegisterStreamSize; i++)
  120.                 {
  121.                     currentValue = tempArray[i + offset];
  122.                     finalArray[16 * i + currentRegister] = currentValue;
  123.                 }
  124.             }
  125.             return finalArray;
  126.         }
  127.  
  128.         public static string ReadNullTerminationString(BinaryReader reader, Encoding encoding)
  129.         {
  130.             byte[] temp = new byte[1];
  131.             StringBuilder sb = new StringBuilder();
  132.  
  133.             while(true)
  134.             {
  135.                 temp[0] = reader.ReadByte();
  136.  
  137.                 if(temp[0] == 0x00) return sb.ToString();
  138.                 else sb.Append(encoding.GetString(temp));
  139.             }
  140.         }
  141.  
  142.         public static uint SwapByteOrder(uint valueToSwap)
  143.         {
  144.             uint uvalue = valueToSwap;
  145.             uint swapped =
  146.                 ((0x000000FF) & (uvalue >> 24)
  147.               | (0x0000FF00) & (uvalue >> 8)
  148.               | (0x00FF0000) & (uvalue << 8)
  149.               | (0xFF000000) & (uvalue << 24));
  150.             return swapped;
  151.         }
  152.  
  153.         public static ushort SwapByteOrder(ushort valueToSwap)
  154.         {
  155.             var uvalue = valueToSwap;
  156.             var swapped =
  157.                 ((0x00FF) & (uvalue >> 8)
  158.               | (0xFF00) & (uvalue << 8));
  159.             return (ushort)swapped;
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement