我正在尝试“解码”并从 Audacity 原始导出文件中提取原始 pcm 音频。 下面的代码打开这样的文件并生成一个新文件,该文件可以由 Audacity 打开并再次重现声音。
Audacity 的原始数据比 Audacity 导出的“wav”文件小约 1000 字节。
我也在尝试打印数据,如何正确连接“字节”以形成带符号的16位或短值并将它们打印在一行上,以便在硬件中使用。
因此可以测试它们是否已签名。 https://en.cppreference.com/w/cpp/types/is_signed
#include <QCoreApplication>
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <bitset>
#include <typeinfo>
int main () {
std::ifstream ifs;
// ifs.open ("AH-DL-TL.raw", std::ios::binary);
std::ifstream is ("AH-DL-TL.raw", std::ifstream::binary);
//std::ifstream is(".//r909hh.wav", std::ifstream::binary);
if (is) {
std::cout << "Opening File" << std::endl;
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
std::cout << "Reading " << length << " characters... " << std::endl;
// allocate memory:
char * buffer = new char [length];
// https://cplusplus.com/reference/istream/istream/read/
// read data as a block:
is.read (buffer,length);
is.close();
for (int n=length; n>0; n--) {
//std::cout << n << ", " << std::endl;
if(n % 2 == 0){
temp = buffer[n];
std::bitset<16> b2(temp);
std::cout << "temp: " << b2 /*temp*/ << std::endl;
}
else{
temp = 0;
temp = buffer[n] << 8;
}
try
{
std::cout << "data: " << typeid(buffer[n]).name() << std::endl;
}
catch (std::bad_typeid buffer[n])
{
std::cerr << "bad_typeid caught: " << buffer[n].what() << '\n';
}
std::bitset<8> b1(buffer[n]);
std::cout << "data: " << b1 << " " << ((n % 2 == 0) ? "even " : "odd") << std::endl ;
}
// print content:
//std::cout.write (buffer,length); // gibberish.
std::ofstream wf("student.raw", std::ios::out | std::ios::binary);
if(!wf) {
std::cout << "Cannot open file!" << std::endl;
return 1;
}
wf.write (buffer,length);
/*
for(int i = 0; i < length; i++){
std::cout << "write line: " << i << " length:" << length << std::endl;
wf.write((char *) &buffer[1], length);
}
*/
wf.close();
if(!wf.good()) {
std::cout << "Error occurred at writing time!" << std::endl;
}
delete[] buffer;
}
return 0;
}