C++ 双声道PCM音频分成单声道音频
生活随笔
收集整理的這篇文章主要介紹了
C++ 双声道PCM音频分成单声道音频
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
記錄一下自己寫的雙聲道音頻切分成單聲道音頻的代碼
原始音頻的格式為:采樣率16KHZ 采樣點精度16位 雙聲道
#include <iostream> #include <stdio.h>using namespace std;#define PCMTESTint main() { #ifdef PCMTESTFILE *fp = fopen(".\\srpcm\\test.pcm", "rb"); #elseFILE *fp = fopen(".\\srpcm\\txttxt.txt", "rb"); #endifif (fp == NULL){printf("Open File Failed!\n");return 0;}fseek(fp, 0L, SEEK_END);long fileLength = ftell(fp);fseek(fp,0L, SEEK_SET);printf("獲取文件的長度\n", fileLength);printf("File Length %ld byte\n", fileLength);char* pBuff = new char[fileLength];memset(pBuff, 0, fileLength);int readLength = fread(pBuff, sizeof(char), fileLength, fp);printf("readLength = %d\n", readLength);//采樣精度16bit = 2 Byte,所以這里用了shortshort *pBuffLeft = new short[fileLength / 4];short *pBuffRight = new short[fileLength / 4];memset(pBuffLeft, 0, fileLength / 2);memset(pBuffLeft, 0, fileLength / 2);for (int i = 0; i < (fileLength / 4); i++){pBuffLeft[i] = ((short*)pBuff)[i * 2];pBuffRight[i] = ((short*)pBuff)[(i * 2) + 1];}#ifdef PCMTESTFILE *fpLeft = fopen(".\\srpcm\\testLeft.pcm", "wb");FILE *fpRight = fopen(".\\srpcm\\testRight.pcm", "wb");FILE *fpAll = fopen(".\\srpcm\\testAll.pcm", "wb"); #elseFILE *fpLeft = fopen(".\\srpcm\\txtLeft.txt", "wb");FILE *fpRight = fopen(".\\srpcm\\txtRight.txt", "wb");FILE *fpAll = fopen(".\\srpcm\\txtAll.txt", "wb"); #endiffwrite(pBuffLeft, sizeof(char), fileLength / 2, fpLeft);fwrite(pBuffRight, sizeof(char), fileLength / 2, fpRight);fwrite(pBuff, sizeof(char), fileLength, fpAll);fclose(fpLeft);fclose(fpRight);fclose(fpAll);return 0; }?
?
總結
以上是生活随笔為你收集整理的C++ 双声道PCM音频分成单声道音频的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python检查批量URL是否可以正常访
- 下一篇: 帆软填报报表的使用教程