C++讀取wav文件中的PCM數(shù)據(jù)
前言
wav文件通常會(huì)使用PCM格式數(shù)據(jù)存儲(chǔ)音頻,這種格式的數(shù)據(jù)讀取出來直接就可以播放,要在wav文件中讀取數(shù)據(jù),我們首先要獲取頭部信息,wav的文件結(jié)構(gòu)里面分為多個(gè)chunk,我們要做的就是識(shí)別這些chunk的信息,獲取音頻的格式以及數(shù)據(jù)。
一、如何實(shí)現(xiàn)?
首先需要構(gòu)造wav頭部,wav文件音頻信息全部保存在頭部,我們要做的就是讀取wav頭部信息,并且記錄PCM的相關(guān)參數(shù)。
1.定義頭結(jié)構(gòu)
只定義PCM格式的wav文件頭,對(duì)于PCM格式的數(shù)據(jù)只需要下面3個(gè)結(jié)構(gòu)體即可。
struct WaveRIFF; struct WaveFormat; struct WaveData;
2.讀取頭部信息
打開文件后需要讀取頭部信息,需要獲取聲音的格式以及數(shù)據(jù)長度。
WaveRIFF riff; WaveFormat format; WaveData data; int userDataSize; f= fopen(fileName.c_str(), "rb+"); //讀取頭部信息 fread(&riff, 1, sizeof(riff), f); fread(&format, 1, sizeof(format),f); //判讀頭部信息是否正確 //略 //查找data chunk //略 //記錄數(shù)據(jù)起始位置
3.讀取數(shù)據(jù)
獲取頭部信息后,就知道數(shù)據(jù)在位置及長度了,只需要直接讀文件即可。
//跳到數(shù)據(jù)起始位置 seek(f, _dataOffset, SEEK_SET);
//讀取數(shù)據(jù) fread(buf, 1, bufLength, f);
二、完整代碼
完整代碼總用有3部分,頭結(jié)構(gòu)、WavFileReader.h、WavFileReader.cpp。
1.頭結(jié)構(gòu)
#pragma pack(push,1)
?? ?struct WaveRIFF {
?? ??? ?const?? ?char id[4] = { 'R','I', 'F', 'F' };
?? ??? ?uint32_t fileLength;
?? ??? ?const?? ?char waveFlag[4] = { 'W','A', 'V', 'E' };
?? ?};
?? ?struct WaveFormat
?? ?{
?? ??? ?const?? ?char id[4] = { 'f','m', 't', ' ' };
?? ??? ?uint32_t blockSize = 16;
?? ??? ?uint16_t formatTag;
?? ??? ?uint16_t channels;
?? ??? ?uint32_t samplesPerSec;
?? ??? ?uint32_t avgBytesPerSec;
?? ??? ?uint16_t blockAlign;
?? ??? ?uint16_t ?bitsPerSample;
?? ?};
?? ?struct ?WaveData
?? ?{
?? ??? ?const?? ?char id[4] = { 'd','a', 't', 'a' };
?? ??? ?uint32_t dataLength;
?? ?};
#pragma pack(pop)2.WavFileReader.h
#pragma once
#include<string>
/************************************************************************
* @Project: ??? ?AC.WavFileWriter
* @Decription: ?wav文件讀取工具
* 本版本只支持pcm讀取,且未處理字節(jié)順序。?? ?riff文件是小端,通常在intel的設(shè)備上是沒問題的,在java虛擬機(jī)上則需要處理。
* @Verision: ??? ?v1.0.0.0
* @Author: ??? ?Xin Nie
* @Create: ??? ?2019/4/10 11:10:17
* @LastUpdate: ?2019/4/16 10:45:00
************************************************************************
* Copyright @ 2019. All rights reserved.
************************************************************************/
namespace AC {
?? ?/// <summary>
?? ?/// wav文件讀取對(duì)象
?? ?/// </summary>
?? ?class WavFileReader {
?? ?public:
?? ??? ?/// <summary>
?? ??? ?/// 構(gòu)造方法
?? ??? ?/// </summary>
?? ??? ?WavFileReader();
?? ??? ?/// <summary>
?? ? ? ?/// 析構(gòu)方法
?? ? ? ?/// </summary>
?? ??? ?~WavFileReader();
?? ??? ?/// <summary>
?? ??? ?/// 打開wav文件
?? ??? ?/// </summary>
?? ??? ?/// <param name="fileName">文件名</param>
?? ??? ?/// <returns>是否打開成功</returns>
?? ??? ?bool OpenWavFile(const std::string& fileName);
?? ??? ?/// <summary>
?? ??? ?/// 關(guān)閉文件
?? ??? ?/// </summary>
?? ??? ?void CloseFlie();
?? ??? ?/// <summary>
?? ??? ?/// 讀取音頻數(shù)據(jù)
?? ??? ?/// </summary>
?? ??? ?/// <param name="buf">外部緩存</param>
?? ??? ?/// <param name="bufLength">緩存長度</param>
?? ??? ?/// <returns>讀取長度</returns>
?? ??? ?int ReadData(unsigned char* buf, int bufLength);
?? ??? ?/// <summary>
?? ??? ?/// 設(shè)置讀取位置
?? ??? ?/// </summary>
?? ??? ?/// <param name="position"> 讀取位置</param>
?? ??? ?void SetPosition(int position);
?? ??? ?/// <summary>
?? ??? ?/// 獲取讀取位置
?? ??? ?/// </summary>
?? ??? ?/// <returns>讀取位置</returns>
?? ??? ?int GetPosition();
?? ??? ?/// <summary>
?? ??? ?/// 獲取文件長度
?? ??? ?/// </summary>
?? ??? ?/// <returns>文件長度</returns>
?? ??? ?int GetFileLength();
?? ??? ?/// <summary>
?? ??? ?/// 獲取音頻數(shù)據(jù)長度
?? ??? ?/// </summary>
?? ??? ?/// <returns>音頻數(shù)據(jù)長度</returns>
?? ??? ?int GetDataLength();
?? ??? ?/// <summary>
?? ??? ?/// 獲取聲道數(shù)
?? ??? ?/// </summary>
?? ??? ?/// <returns>聲道數(shù)</returns>
?? ??? ?int GetChannels();
?? ??? ?/// <summary>
?? ??? ?/// 獲取采樣率
?? ??? ?/// </summary>
?? ??? ?/// <returns>采樣率,單位:hz</returns>
?? ??? ?int GetSampleRate();
?? ??? ?/// <summary>
?? ??? ?/// 獲取位深
?? ??? ?/// </summary>
?? ??? ?/// <returns>位深,單位:bits</returns>
?? ??? ?int GetBitsPerSample();
?? ?private:
?? ??? ?void* _file = nullptr;
?? ??? ?uint32_t _fileLength = 0;
?? ??? ?uint32_t _dataLength = 0;
?? ??? ?int _channels = 0;
?? ??? ?int ?_sampleRate = 0;
?? ??? ?int ?_bitsPerSample = 0;
?? ??? ?int _dataOffset = 0;
?? ?};
}3.WavFileReader.cpp
#include"WavFileReader.h"
namespace AC {
?? ?WavFileReader::WavFileReader()
?? ?{
?? ?}
?? ?WavFileReader::~WavFileReader()
?? ?{
?? ??? ?CloseFlie();
?? ?}
?? ?bool WavFileReader::OpenWavFile(const std::string& fileName)
?? ?{
?? ??? ?if (_file)
?? ??? ?{
?? ??? ??? ?printf("已經(jīng)打開了文件!\n");
?? ??? ??? ?return false;
?? ??? ?}
?? ??? ?WaveRIFF riff;
?? ??? ?WaveFormat format;
?? ??? ?WaveData data;
?? ??? ?int userDataSize;
?? ??? ?_file = fopen(fileName.c_str(), "rb+");
?? ??? ?if (!_file)
?? ??? ?{
?? ??? ??? ?printf("打開文件失敗!\n");
?? ??? ??? ?return false;
?? ??? ?}
?? ??? ?//讀取頭部信息
?? ??? ?if (fread(&riff, 1, sizeof(riff), static_cast<FILE*>(_file)) != sizeof(riff))
?? ??? ?{
?? ??? ??? ?printf("文件讀取錯(cuò)誤,讀取riff失敗!\n");
?? ??? ??? ?goto error;
?? ??? ?}
?? ??? ?if (std::string(riff.id, 4) != "RIFF" || std::string(riff.waveFlag, 4) != "WAVE")
?? ??? ?{
?? ??? ??? ?printf("頭部信息不正確,不是wav文件!\n");
?? ??? ??? ?goto error;
?? ??? ?}
?? ??? ?if (fread(&format, 1, sizeof(format), static_cast<FILE*>(_file)) != sizeof(format))
?? ??? ?{
?? ??? ??? ?printf("文件讀取錯(cuò)誤,讀取format失敗!\n");
?? ??? ??? ?goto error;
?? ??? ?}
?? ??? ?if (std::string(format.id, 4) != "fmt ")
?? ??? ?{
?? ??? ??? ?printf("頭部信息不正確,缺少fmt!\n");
?? ??? ??? ?goto error;
?? ??? ?}
?? ??? ?if (format.formatTag != 1)
?? ??? ?{
?? ??? ??? ?printf("程序不支持,數(shù)據(jù)格式非pcm,只支持pcm格式的數(shù)據(jù)!\n");
?? ??? ??? ?goto error;
?? ??? ?}
?? ??? ?userDataSize = format.blockSize - sizeof(format) + 8;
?? ??? ?if (userDataSize < 0)
?? ??? ?{
?? ??? ??? ?printf("頭部信息不正確,blockSize大小異常!\n");
?? ??? ??? ?goto error;
?? ??? ?}
?? ??? ?else if (userDataSize > 0)
?? ??? ?{
?? ??? ??? ?if (fseek(static_cast<FILE*>(_file), userDataSize, SEEK_CUR) != 0)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("文件讀取錯(cuò)誤!\n");
?? ??? ??? ??? ?goto error;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?while (1)
?? ??? ?{
?? ??? ??? ?if (fread(&data, 1, sizeof(data), static_cast<FILE*>(_file)) != sizeof(data))
?? ??? ??? ?{
?? ??? ??? ??? ?printf("文件讀取錯(cuò)誤!\n");
?? ??? ??? ??? ?goto error;
?? ??? ??? ?};
?? ??? ??? ?if (std::string(data.id, 4) != "data")
?? ??? ??? ?{
?? ??? ??? ??? ?if (fseek(static_cast<FILE*>(_file), data.dataLength, SEEK_CUR) != 0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?printf("文件讀取錯(cuò)誤!\n");
?? ??? ??? ??? ??? ?goto error;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?_dataOffset = ftell(static_cast<FILE*>(_file));
?? ??? ?_fileLength = riff.fileLength+8;
?? ??? ?_dataLength = data.dataLength;
?? ??? ?_channels = format.channels;
?? ??? ?_sampleRate = format.samplesPerSec;
?? ??? ?_bitsPerSample = format.bitsPerSample;
?? ??? ?return true;
?? ?error:
?? ??? ?if (fclose(static_cast<FILE*>(_file)) == EOF)
?? ??? ?{
?? ??? ??? ?printf("文件關(guān)閉失敗!\n");
?? ??? ?}
?? ??? ?_file = nullptr;
?? ??? ?return false;
?? ?}
?? ?void WavFileReader::CloseFlie()
?? ?{
?? ??? ?if (_file)
?? ??? ?{
?? ??? ??? ?if (fclose(static_cast<FILE*>(_file)) == EOF)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("文件關(guān)閉失敗!\n");
?? ??? ??? ?}
?? ??? ??? ?_file = nullptr;
?? ??? ?}
?? ?}
?? ?int WavFileReader::ReadData(unsigned char* buf, int bufLength)
?? ?{
?? ??? ?if (ftell(static_cast<FILE*>(_file)) >= _dataOffset + _dataLength)
?? ??? ??? ?return 0;
?? ??? ?return fread(buf, 1, bufLength, static_cast<FILE*>(_file));
?? ?}
?? ?void WavFileReader::SetPosition(int postion)
?? ?{
?? ??? ?if (fseek(static_cast<FILE*>(_file), _dataOffset + postion, SEEK_SET) != 0)
?? ??? ?{
?? ??? ??? ?printf("定位失敗!\n");
?? ??? ?}
?? ?}
?? ?int WavFileReader::GetPosition()
?? ?{
?? ??? ?return ftell(static_cast<FILE*>(_file)) - _dataOffset;
?? ?}
?? ?int WavFileReader::GetFileLength()
?? ?{
?? ??? ?return _fileLength;
?? ?}
?? ?int WavFileReader::GetDataLength()
?? ?{
?? ??? ?return _dataLength;
?? ?}
?? ?int WavFileReader::GetChannels()
?? ?{
?? ??? ?return _channels;
?? ?}
?? ?int WavFileReader::GetSampleRate()
?? ?{
?? ??? ?return _sampleRate;
?? ?}
?? ?int WavFileReader::GetBitsPerSample()
?? ?{
?? ??? ?return _bitsPerSample;
?? ?}
}三、使用示例
1、播放
#include "WavFileReader.h"
int main(int argc, char** argv) {
?? ?AC::WavFileReader read;
?? ?unsigned char buf[1024];
?? ?if (read.OpenWavFile("test_music.wav"))
?? ?{
?? ??? ?int channels, sampleRate, bitsPerSample;
?? ??? ?//獲取聲音格式
?? ??? ?channels = read.GetChannels();
?? ??? ?sampleRate = read.GetSampleRate();
?? ??? ?bitsPerSample = read.GetBitsPerSample();
?? ??? ?//打開聲音設(shè)備(channels,sampleRate,bitsPerSample)
?? ??? ?int size;
?? ??? ?do?
?? ??? ?{
?? ??? ? ? ?//讀取音頻數(shù)據(jù)
?? ??? ??? ?size = read.ReadData(buf,1024);
?? ??? ??? ?if (size > 0)
?? ??? ??? ?{
?? ??? ??? ??? ?//播放(buf,1024)
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?} while (size);
?? ??? ?read.CloseFlie();
?? ?}
?? ?return 0;
}2、循環(huán)播放
#include "WavFileReader.h"
int main(int argc, char** argv) {
?? ?AC::WavFileReader read;
?? ?unsigned char buf[1024];
? ? bool exitFlag = false;
?? ?if (read.OpenWavFile("test_music.wav"))
?? ?{
?? ??? ?int channels, sampleRate, bitsPerSample;
?? ??? ?//獲取聲音格式
?? ??? ?channels = read.GetChannels();
?? ??? ?sampleRate = read.GetSampleRate();
?? ??? ?bitsPerSample = read.GetBitsPerSample();
?? ??? ?//打開聲音設(shè)備(channels,sampleRate,bitsPerSample)
?? ??? ?int size;
?? ??? ?while (!exitFlag)
?? ??? ?{
?? ??? ? ? ?//讀取音頻數(shù)據(jù)
?? ??? ??? ?size = read.ReadData(buf, 1024);
?? ??? ??? ?if (size > 0)
?? ??? ??? ?{
?? ??? ??? ??? ?//播放(buf,1024)
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{ ??
?? ??? ??? ? ? ?//回到數(shù)據(jù)起始位置
?? ??? ??? ??? ?read.SetPosition(0);?? ??? ?
?? ??? ??? ?}
?? ??? ?}?
?? ??? ?read.CloseFlie();
?? ?}
?? ?return 0;
}總結(jié)
以上就是今天要講的內(nèi)容,wav文件中讀取PCM還是相對(duì)較簡單的,只要了解wav頭結(jié)構(gòu),然后自定義其頭結(jié)構(gòu),讀取頭部信息,校驗(yàn)頭部信息,然后再讀取數(shù)據(jù)所在的chunk,就可以實(shí)現(xiàn)這樣一個(gè)功能。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(241.添加括號(hào)的不同方式)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(241.添加括號(hào)的不同方式),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++ new與malloc和delete及free動(dòng)態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動(dòng)態(tài)內(nèi)存管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
C++實(shí)現(xiàn)LeetCode165.版本比較)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode165.版本比較),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
DEV C++自動(dòng)補(bǔ)全文件頭的設(shè)置操作教程
Dev-C++ 是一款輕量級(jí)的集成開發(fā)環(huán)境 (IDE),主要用于 C 和 C++ 的程序編寫,它提供了基本的功能來幫助開發(fā)者更高效地工作,其中包括文件頭的自動(dòng)補(bǔ)全功能,本文就給大家介紹了DEV C++自動(dòng)補(bǔ)全文件頭的設(shè)置操作教程,需要的朋友可以參考下2025-04-04

