欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C++读取WAV音频文件的头部数据的实现方法

时间:2021-04-04 10:13:03|栏目:C代码|点击:

C++读取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;
}

 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!

上一篇:C语言简单实现计算字符个数的方法

栏    目:C代码

下一篇:c语言/c++溢出问题浅谈 原创

本文标题:C++读取WAV音频文件的头部数据的实现方法

本文地址:http://www.codeinn.net/misctech/93985.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有