Skip to content

Instantly share code, notes, and snippets.

@Osinko
Last active April 28, 2020 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Osinko/f38ee9e0ea922df1dc2fab7bc4f5ae0c to your computer and use it in GitHub Desktop.
Save Osinko/f38ee9e0ea922df1dc2fab7bc4f5ae0c to your computer and use it in GitHub Desktop.
PDXデーターをTX16Wxへ変換
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class PDX2TX16WX2 : MonoBehaviour
{
void Start()
{
string folder = Application.dataPath; //これだけでunityの実行ファイルがあるフォルダがわかる
string[] note = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
int set = 96; //8オクターブの12キーの鍵盤に対して割り当てた音
uint[] pointer = new uint[set]; //TX16WXのスライスに入力する値
uint[] length = new uint[set];
//テンポラリ
byte[] littleBinaryPointer = new byte[4];
byte[] littleBinaryLength = new byte[2];
//読み込む処理
//PDXファイルフォーマットの資料
//https://vgmrips.net/wiki/PDX_File_Format
//部分的に違う?PCM8等があるので仕様が複数ある可能性
using (var binReader = new BinaryReader(new FileStream(folder + "/" + "bos.pdx", FileMode.Open)))
{
binReader.BaseStream.Position = 0x00; //PDXバイナリファイルの先頭からスライス座標がある
for (int i = 0; i < set; i++)
{
binReader.Read(littleBinaryPointer, 0, 4);
binReader.BaseStream.Position += 0x2; //2バイト飛ばす
binReader.Read(littleBinaryLength, 0, 2);
Array.Reverse(littleBinaryPointer);
Array.Reverse(littleBinaryLength);
//リバースしたのでx68kビッグエンディアンからwinPCのリトルエンディアンになっている
//それを値として取り出す
pointer[i] = BitConverter.ToUInt32(littleBinaryPointer, 0) * 2;
length[i] = pointer[i] + (uint)(BitConverter.ToUInt16(littleBinaryLength, 0) * 2);
}
}
List<string> sliceList = new List<string>();
List<string> regionList = new List<string>();
int count = 0;
for (int i = 0; i < set; i++)
{
print(string.Format("{0}{1}{2,16} ,{3,16}", note[i % 12], i / 12, pointer[i], length[i]));
if (length[i] != 0 || pointer[i] != 0)
{
count++;
sliceList.Add(
string.Format("<tx:loop tx:name=\"Slice {0}\" tx:end =\"{1}\" tx:mode =\"PinnedSlice\" tx:start = \"{2}\" />"
, count, length[i], pointer[i]));
regionList.Add(
string.Format("<tx:region tx:mode=\"Slice\" tx:attenuation=\"0 dB\" tx:pan=\"0%\" tx:loop=\"{0}\" tx:release=\"{0}\" tx:wave=\"1\">\n" +
"<tx:bounds tx:high-vel=\"127\" tx:low-key=\"{1}{2}\" tx:low-vel=\"0\" tx:high-key=\"{1}{2}\" />\n" +
"</tx:region >", count - 1, note[i % 12], i / 12));
}
}
sliceList.AddRange(regionList); //リストを連結
SaveText(folder, @"\slice2TX16W.txt", sliceList.ToArray());
}
//資料:StreamWriter クラス (System.IO)
//http://msdn.microsoft.com/ja-jp/library/system.io.streamwriter(v=vs.110).aspx
//テキストファイルとしてセーブ
public void SaveText(string fileFolder, string filename, string[] dataStr)
{
using (StreamWriter w = new StreamWriter(fileFolder + filename, false, System.Text.Encoding.GetEncoding("shift_jis")))
{
foreach (var item in dataStr)
{
w.WriteLine(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment