Qt 对 wav 音频文件进行剪切
生活随笔
收集整理的這篇文章主要介紹了
Qt 对 wav 音频文件进行剪切
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原理: 保存文件頭,再截取數(shù)據(jù), 最后將兩者進行拼接
#pragma pack(push,1)// WAV格式struct WaveHeader {// RIFF 頭;char riff_fileid[4]; // "RIFF" 大端 "RIFF"塊(0x52494646),標記為RIFF文件格式quint32 riff_fileLen; // 塊數(shù)據(jù)域大小(Chunk Size),即從下一個地址開始,到文件末尾的總字節(jié)數(shù),或者文件總字節(jié)數(shù)-8// 數(shù)據(jù)類型標識符;char waveid[4]; // "WAVE" 類型碼(Form Type),WAV文件格式標記,即"WAVE"四個字母// 格式塊中的塊頭;char fmt_chkid[4]; //"fmt" 子塊(0x666D7420),注意末尾的空格quint32 fmt_chkLen; // 子塊數(shù)據(jù)域大小 // 格式塊中的塊數(shù)據(jù);quint16 wFormatTag; /* format type */ // 編碼格式(Audio Format),1代表PCM無損格式quint16 nChannels; /* number of channels (i.e. mono, stereo, etc.) */ // 聲道數(shù)(Channels),1或2quint32 nSamplesPerSec; /* sample rate */ // 傳輸速率(Byte Rate),每秒數(shù)據(jù)字節(jié)數(shù),SampleRate * Channels * BitsPerSample / 8quint32 nAvgBytesPerSec; /* for buffer estimation */ // 每秒數(shù)據(jù)大小 每個采樣所需的字節(jié)數(shù)BlockAlign,BitsPerSample*Channels/8quint16 nBlockAlign; /* block size of data */ // 單個采樣位深(Bits Per Sample),可選8、16或32quint16 wBitsPerSample; // "data"子塊 (0x64617461)char data_chkid[4]; //"DATA" // 子塊數(shù)據(jù)域大小(SubChunk Size) 至此,共44字節(jié)大小 quint32 data_chkLen; // PCM音頻數(shù)據(jù)WaveHeader(){initHeader();}// 初始化WAV格式void initHeader(quint16 nChannelCnt = 2, quint32 nSampleRate = 44100, quint16 nBitsPerSample = 16){strcpy(riff_fileid, "RIFF");riff_fileLen = 36;strcpy(waveid, "WAVE");strcpy(fmt_chkid, "fmt ");fmt_chkLen = 16;wFormatTag = 1;nChannels = nChannelCnt;nSamplesPerSec = nSampleRate;wBitsPerSample = nBitsPerSample;nBlockAlign = nChannels * wBitsPerSample / 8; // 每秒數(shù)據(jù)大小(字節(jié)) = 采樣率 * 聲道數(shù) * sample比特數(shù) / 8nAvgBytesPerSec = nBlockAlign * nSamplesPerSec;strcpy(data_chkid, "data");data_chkLen = 0;}void setDataSize(int len){riff_fileLen += len;data_chkLen += len;} };#define WaveHeaderLength sizeof(WaveHeader)#pragma pack(pop) #include <QCoreApplication> #include <QFile> #include <QDebug>#include <fstream>int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);const qint64 SIZE = 1024 * 800;QString filename = "E:\\test.wav";QFile file(filename);file.open(QIODevice::ReadOnly);QFile fileSave("E:\\save_test.wav");fileSave.open(QIODevice::WriteOnly | QIODevice::Truncate);char data[SIZE];int nHeadSize = sizeof(WaveHeader);char* dataHead = new char[nHeadSize];int nReadHD = file.read(dataHead, nHeadSize);qint64 fileSize = file.size();qint64 seekPos = fileSize / 5; // 文件的起始位置// 11K、22K等的WAV文件要從數(shù)據(jù)的偶數(shù)字節(jié)截取才行。// 而8、16K等可從任意位置截取。// 因此將截取位置點調整在偶數(shù)就可以了。if(0 != seekPos % 2)seekPos -= 1;file.seek(seekPos);// file.seek(8024010);qDebug()<<file.pos();WaveHeader* pHead = (WaveHeader*)dataHead;pHead->data_chkLen = fileSize / 2 - seekPos; // 保存文件長度fileSave.write(dataHead, nHeadSize);// 文件的終止位置 fileSize / 2for (int i = 0; i < (fileSize / 2); i++){int nLen = file.read(data, SIZE);if(nLen > 0)fileSave.write(data, SIZE);elsebreak;}fileSave.close();file.close();delete dataHead;return a.exec(); }總結
以上是生活随笔為你收集整理的Qt 对 wav 音频文件进行剪切的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于进程杀不掉的问题:Waiting f
- 下一篇: php给照片加滤镜,加滤镜、消痘痘!Wi