私は現在が、運と、ここでは、このアルゴリズムにC#のピッチシフト
https://sites.google.com/site/mikescoderama/pitch-shifting
を使用して上記の実装を使用する私のコードをWAVEファイルのピッチシフトをやろうとしています。出力された波形ファイルが破損しているか、有効ではないようです。
コードがピッチシフトアルゴリズムを除いて、非常に簡単である:)
- これはWAVEファイルをロードし、それが波のファイルデータを読み出し、 バイト[]配列に入れ。
- 次に、バイト・データを-1.0f~1.0fフォーマット(ピッチ・シフト・アルゴリズムの作成者が要求した )に「正規化」します。
- ピッチシフトアルゴリズムを適用し、 正規化されたデータをbytes []配列に変換し戻します。
- 最後に、元のウェーブ ファイルの同じヘッダーとピッチシフトされたデータを含むウェーブファイルを保存します。
何か不足していますか?
static void Main(string[] args)
{
// Read the wave file data bytes
byte[] waveheader = null;
byte[] wavedata = null;
using (BinaryReader reader = new BinaryReader(File.OpenRead("sound.wav")))
{
// Read first 44 bytes (header);
waveheader= reader.ReadBytes(44);
// Read data
wavedata = reader.ReadBytes((int)reader.BaseStream.Length - 44);
}
short nChannels = BitConverter.ToInt16(waveheader, 22);
int sampleRate = BitConverter.ToInt32(waveheader, 24);
short bitRate = BitConverter.ToInt16(waveheader, 34);
// Normalized data store. Store values in the format -1.0 to 1.0
float[] in_data = new float[wavedata.Length/2];
// Normalize wave data into -1.0 to 1.0 values
using(BinaryReader reader = new BinaryReader(new MemoryStream(wavedata)))
{
for (int i = 0; i < in_data.Length; i++)
{
if(bitRate == 16)
in_data[i] = reader.ReadInt16()/32768f;
if (bitRate == 8)
in_data[i] = (reader.ReadByte() - 128)/128f;
}
}
//PitchShifter.PitchShift(1f, in_data.Length, (long)1024, (long)32, sampleRate, in_data);
// Backup wave data
byte[] copydata = new byte[wavedata.Length];
Array.Copy(wavedata, copydata, wavedata.Length);
// Revert data to byte format
Array.Clear(wavedata, 0, wavedata.Length);
using (BinaryWriter writer = new BinaryWriter(new MemoryStream(wavedata)))
{
for (int i = 0; i < in_data.Length; i++)
{
if(bitRate == 16)
writer.Write((short)(in_data[i] * 32768f));
if (bitRate == 8)
writer.Write((byte)((in_data[i] * 128f) + 128));
}
}
// Compare new wavedata with copydata
if (wavedata.SequenceEqual(copydata))
{
Console.WriteLine("Data has no changes");
}
else
{
Console.WriteLine("Data has changed!");
}
// Save modified wavedata
string targetFilePath = "sound_low.wav";
if (File.Exists(targetFilePath))
File.Delete(targetFilePath);
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(targetFilePath)))
{
writer.Write(waveheader);
writer.Write(wavedata);
}
Console.ReadLine();
}
オーディオファイルのヘッダーは44バイトですか?このページhttp://www.sonicspot.com/guide/wavefiles.htmlによれば、それは多くのものに依存し、適切に解析される必要があります。 – Neil
あなたは正しいです!私は正しい使い方を投稿するために私の質問に自動的に答えるつもりです。 – John