wav ファイルを開いて読み取り、バッファを整数配列に変換してから、変換して書き込もうとしています。
int main(){
ifstream file ("C:\\Documents\\ParadigmE3-shortened.wav",std::ios_base::out | std::ios_base::binary);
char * header = new char[50000044];
file.read(header, 50000044);
cout << header[0] << endl;
unsigned int * header_int = new unsigned int[50000044];
for(unsigned int i = 0; i < sizeof(header); i++){
header_int[i] = header[i];
}
char * writefile = new char[50000044];
for(unsigned int i = 0; i < sizeof(header); i++){
itoa(header_int[i], &writefile[i], 10);
}
cout << writefile[0] << endl;
ofstream newfile ("C:\\Documents\\ParadigmE3-modified.wav", std::ios_base::out | std::ios_base::binary);
newfile.write(writefile, 50000044);
}
現在、これは以下を出力します。
R
8
変換中にデータが変更されたことを示します。これを正しく機能させるにはどうすればよいですか?
いくつかの提案と、char
変数に対して計算を実行できることを学んだ後、コードを再構成しました。現在は次のようになっています。
int main(){
// Create file variable with file
ifstream file ("C:\\Documents\\ParadigmE3-shortened.wav",std::ios_base::out | std::ios_base::binary);
// Read the first 15040512 bytes to char array pointer, called header
char * header = new char[15040512];
file.read(header, 15040512);
// Copy contents of header to writefile, after the 44'th byte, multiply the value by 2
char * writefile = new char[15040512];
for(int i = 0; i < sizeof(header); i++){
if(i<44) writefile[i] = header[i];
if(i>=44) writefile[i] = 2 * header[i];
}
// Copy the contents of writefile, but at the 44th byte, divide it by 2, returning it to its original value
for(int i = 0; i < sizeof(header); i++){
if(i<44) writefile[i] = writefile[i];
if(i>=44) writefile[i] = .5 * writefile[i];
}
// Create file to write to
ofstream newfile ("C:\\Documents\\ParadigmE3-modified.wav", std::ios_base::out | std::ios_base::binary);
// Write writefile to file
newfile.write(writefile, 15040512);
}
ただし、(Windows Media Player で) 再生しても再生されないため、元のファイルではないことは明らかです。