我计划创建一个程序,将可视化的音频波形的。wav文件。
到目前为止,我已经从正确阅读上述wav文件的头部分开始。我使用的代码如下:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
using std::string;
using std::fstream;
typedef struct WAV_HEADER{
char RIFF[4]; // RIFF Header Magic header
unsigned long ChunkSize; // RIFF Chunk Size
char WAVE[4]; // WAVE Header
char fmt[4]; // FMT header
unsigned long Subchunk1Size; // Size of the fmt chunk
unsigned short AudioFormat; // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
unsigned short NumOfChan; // Number of channels 1=Mono 2=Sterio
unsigned long SamplesPerSec; // Sampling Frequency in Hz
unsigned long bytesPerSec; // bytes per second
unsigned short blockAlign; // 2=16-bit mono, 4=16-bit stereo
unsigned short bitsPerSample; // Number of bits per sample
char Subchunk2ID[4]; // "data" string
unsigned long Subchunk2Size; // Sampled data length
}wav_hdr;
// Function prototypes
int getFileSize(FILE *inFile);
int main(int argc,char *argv[]){
wav_hdr wavHeader;
FILE *wavFile;
int headerSize = sizeof(wav_hdr),filelength = 0;
string answer;
do{
string input;
string answer;
const char* filePath;
cout << "Pick wav file from the Windows Media File: ";
cin >> input;
cin.get();
cout << endl;
path = "C:\\Windows\\Media\\" + input + ".wav";
filePath = path.c_str();
wavFile = fopen( filePath , "r" );
if(wavFile == NULL){
printf("Can not able to open wave file\n");
//exit(EXIT_FAILURE);
}
fread(&wavHeader,headerSize,1,wavFile);
filelength = getFileSize(wavFile);
fclose(wavFile);
cout << "File is :" << filelength << " bytes." << endl;
cout << "RIFF header :" << wavHeader.RIFF[0]
<< wavHeader.RIFF[1]
<< wavHeader.RIFF[2]
<< wavHeader.RIFF[3] << endl;
cout << "WAVE header :" << wavHeader.WAVE[0]
<< wavHeader.WAVE[1]
<< wavHeader.WAVE[2]
<< wavHeader.WAVE[3]
<< endl;
cout << "FMT :" << wavHeader.fmt[0]
<< wavHeader.fmt[1]
<< wavHeader.fmt[2]
<< wavHeader.fmt[3]
<< endl;
cout << "Data size :" << wavHeader.ChunkSize << endl;
// Display the sampling Rate form the header
cout << "Sampling Rate :" << wavHeader.SamplesPerSec << endl;
cout << "Number of bits used :" << wavHeader.bitsPerSample << endl;
cout << "Number of channels :" << wavHeader.NumOfChan << endl;
cout << "Number of bytes per second :" << wavHeader.bytesPerSec << endl;
cout << "Data length :" << wavHeader.Subchunk2Size << endl;
cout << "Audio Format :" << wavHeader.AudioFormat << endl;
// Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
cout << "Block align :" << wavHeader.blockAlign << endl;
cout << "Data string :" << wavHeader.Subchunk2ID[0]
<< wavHeader.Subchunk2ID[1]
<< wavHeader.Subchunk2ID[2]
<< wavHeader.Subchunk2ID[3]
<< endl;
cout << endl << endl << "Try something else? (y/n)";
cin >> answer;
//cin.get();
cout << endl << endl;
}while( answer == "y" );
getchar();
return 0;
}
// find the file size
int getFileSize(FILE *inFile){
int fileSize = 0;
fseek(inFile,0,SEEK_END);
fileSize=ftell(inFile);
fseek(inFile,0,SEEK_SET);
return fileSize;
}
我试过几次,它给出的数据似乎通过Windows文件夹中的Media文件夹中的不同WAV文件一致。
下一步就是把wav文件的实际数据存储到一个矢量中,但是我对如何存储还不太清楚,我找到的在线解决方案只是阅读头文件。
关于如何存储(并希望显示)wav文件的实际数据有什么想法吗?谢谢!
3条答案
按热度按时间yshpjwxd1#
此图像取自a Stanford course
因此,您可以看到音频数据紧跟在您已经读取的标头之后,并且将有
Subchunk2Size
字节的音频数据。此操作的伪代码为
如果音频是立体声的,
data
中会有两个音频流,如果音频是压缩的(mp3,aac等),你必须先解压缩。q3aa05252#
我知道这是一个老职位,但你的fread参数切换,这里是一个更正确的版本(需要g++-4.7或更高的-std=c11标志,如“g -std=c++11 WaveReader.cpp -o WaveReader”).
w8f9ii693#
如果您想要读取WAV文件,同时涵盖
fmt
和data
块与其他块“混合”的情况:**NB:**我假设这里是单通道16位样本。虽然它可以适应处理更多的格式...