C++ [C+;+;]我想从wav文件中获取PCM数据
我知道波形文件的结构。但我不知道PCM数据的确切结构C++ [C+;+;]我想从wav文件中获取PCM数据,c++,audio,pcm,C++,Audio,Pcm,我知道波形文件的结构。但我不知道PCM数据的确切结构 #include<iostream> #include<fstream> using namespace std; struct WAVE_HEADER{ char Chunk[4]; int ChunkSize; char format[4]; char Sub_chunk1ID[4]; int Sub_chunk1Size; short int AudioForma
#include<iostream>
#include<fstream>
using namespace std;
struct WAVE_HEADER{
char Chunk[4];
int ChunkSize;
char format[4];
char Sub_chunk1ID[4];
int Sub_chunk1Size;
short int AudioFormat;
short int NumChannels;
int SampleRate;
int ByteRate;
short int BlockAlign;
short int BitsPerSample;
char Sub_chunk2ID[4];
int Sub_chunk2Size;
};
struct WAVE_HEADER waveheader;
int main(){
FILE *sound;
sound = fopen("music.wav","rb");
short D;
fread(&waveheader,sizeof(waveheader),1,sound);
cout << "BitsPerSample : " << waveheader.BitsPerSample << endl;
while(!feof(sound)){
fread(&D,sizeof(waveheader.BitsPerSample),1,sound);
cout << int(D) << endl;
}
}
#包括
#包括
使用名称空间std;
结构波头{
字符块[4];
整数块大小;
字符格式[4];
char Sub_chunk1ID[4];
int Sub_chunk1Size;
短整数音频格式;
短整数通道;
int采样器;
int ByteRate;
短整型块对齐;
短整数位样本;
char Sub_chunk2ID[4];
int Sub_chunk2Size;
};
结构波头波头;
int main(){
文件*声音;
声音=fopen(“music.wav”、“rb”);
短D;
fread(&waveheader,sizeof(waveheader),1,声音);
cout如中所示,对于分辨率大于8位的每个采样,PCM数据使用小尾端字节顺序和2的补码进行存储。换句话说,在Intel处理器上,16位采样通常对应于有符号短
。此外,对于立体声通道,数据是交错的(左/右采样)
考虑到这一点,假设“music.wav”确实包含16位PCM样本,并且您正在使用
sizeof(short)的编译器在一个小小的endian平台上读取数据==2,则您发布的代码应能正确读取样本。PCM没有结构。原始样本以8、16、24或32位有符号或无符号整数(或较少出现的float
s或double
s)的形式相互跟随请记住,并非所有.wav文件都包含交错的8,16,24,32 int PCM数据。请检查短int AudioFormat,如果它不等于1,则它是其他格式,并且几乎总是压缩的,并且具有扩展头。如果不为其实现编解码器,您将无法读取压缩数据。或者使用外部音频格式将其转换为PCM的程序。