Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace YMextractor
- {
- public partial class Form1 : Form
- {
- OpenFileDialog InputFile = new OpenFileDialog();
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- InputFile.RestoreDirectory = true;
- InputFile.Filter = "YM-Dateien (*.YM)|*.YM";
- InputFile.ShowDialog();
- if (File.Exists(InputFile.FileName.ToString()))
- {
- FileNameBox.Text = InputFile.FileName.ToString();
- button2.Enabled = true;
- }
- else
- {
- button2.Enabled = false;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Extractor.Extract(InputFile.FileName.ToString());
- MessageBox.Show("Done");
- }
- }
- public partial class Extractor
- {
- public static void Extract(string filename)
- {
- BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open));
- BinaryWriter bw = new BinaryWriter(File.Open(filename.Remove(filename.LastIndexOf('.')) + ".hex", FileMode.Create));
- 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)); //Type
- tempString = encoding.GetString(br.ReadBytes(8)); //Check string
- frameCount = (int)SwapByteOrder(br.ReadUInt32()); //Frame count
- tempInt = SwapByteOrder(br.ReadUInt32()); //Song attributes
- tempShort = SwapByteOrder(br.ReadUInt16()); //Digidrums samples
- tempInt = SwapByteOrder(br.ReadUInt32()); //YM Frequency
- tempShort = SwapByteOrder(br.ReadUInt16()); //Frame rate
- loopFrame = (int)SwapByteOrder(br.ReadUInt32()); //Loop Frame Number
- br.ReadUInt16(); //Unused bytes
- //Song Info
- tempString = ReadNullTerminationString(br, encoding); //Title
- tempString = ReadNullTerminationString(br, encoding); //Artist
- tempString = ReadNullTerminationString(br, encoding); //Comments
- //Read interleaved frames
- byte[] output = ReadAllFrames(br, frameCount);
- byte[] outputByte = new byte[1];
- br.Close();
- //Stream frames
- for(int i = 0; i < frameCount; i++)
- {
- for(int j = 0; j < 16; j++)
- {
- outputByte[0] = output[i * 16 + j];
- bw.Write(outputByte);
- }
- TimeSpan ts = TimeSpan.FromSeconds(((i + 1) / 50));
- //infinite loop
- if(i == (frameCount - 1)) {}
- }
- //mute YM2149
- for(int j = 0; j < 16; j++)
- {
- outputByte[0] = 0;
- bw.Write(outputByte);
- }
- bw.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