当前位置:网站首页>Implementation of converting PCM file to WAV
Implementation of converting PCM file to WAV
2022-07-01 18:34:00 【lanbojini1209】
#include <iostream>
using namespace std;
typedef struct WAV_HEADER
{
char chunkid[4];
unsigned long chunksize;
char format[4];
} pcmHeader;
typedef struct WAV_FMT
{
char subformat[4];
unsigned long sbusize;
unsigned short audioFormat;
unsigned short numchannels;
unsigned long sampleRate;
unsigned long byteRate;
unsigned long blockAlign;
unsigned long bitPerSample;
} pcmFmt;
typedef struct WAV_DATA
{
char wavdata[4];
unsigned long dataSize;
} pcmData;
long getFileSize(char* fileName){
FILE* fp = fopen(fileName, "r");
if(!fp){
return -1;
}
fseek(fp, 0, SEEK_END); // Go to the end of the file
long size = ftell(fp); // Returns the byte length of the file
fclose(fp);
return size;
}
int pcvToWav(const char *pcmpath, int channels, int sample_tate, int fmtSize, const char* wavpath)
{
// Open file
FILE * fp, *fout;
fp = fopen(pcmpath, "rb");
if(fp== NULL) {
cout<<"fopen input failed"<<endl;
return -1;
}
pcmHeader header;
pcmFmt fmt;
pcmData data;
memcpy(header.chunkid, "RIFF", strlen("RIFF"));
header.chunksize = getFileSize((char *)pcmpath) + 44 -8;
memcpy(header.format, "WAVE", strlen("WAVE"));
memcpy(fmt.subformat, "fmt ", strlen("fmt "));
fmt.sbusize = fmtSize; //16 Bit storage bit width
fmt.audioFormat = 1; //pcm The data is 1
fmt.numchannels = channels;
fmt.bitPerSample = sample_tate;
fmt.byteRate = sample_tate * channels * 16 / 8;
fmt.blockAlign = channels * 16 /8;
fmt.bitPerSample = 16;
memcpy(data.wavdata, "data", strlen("data"));
data.dataSize = getFileSize((char*)pcmpath);
// Write data 1、 Write header information 2、 write in PCM data
// 1、 Open file
fout = fopen(wavpath, "wb");
if (fout ==NULL){
cout<<"fopen out failed"<<endl;
return -1;
}
fwrite(&header,sizeof(header), 1,fout);
fwrite(&fmt,sizeof(fmt), 1,fout);
fwrite(&data,sizeof(data), 1,fout);
// write in PCM file
char* buff = (char*)malloc(512);
int len;
// Read pcm Of documents to buff in
while((len = fread(buff, sizeof(char), 512, fp)) != 0) {
fwrite(buff, sizeof(char), len, fout);
}
free(buff);
fclose(fp);
fclose(fout);
cout<<"finish"<<endl;
}
int main() {
pcvToWav("../audio.pcm",2,44100, 16,"../outfile.wav");
return 0;
}Knowledge points to learn
1、 The method used to obtain the byte length of the file is fseek(fp, 0, SEEK_END), ftell(fp);
2、 The common copy of string is memcy and strcy.
3、fwrite How to write the structure ;
4、 Read the file according to the character array fread And fwrite Use .
边栏推荐
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
- What impact will multinational encryption regulation bring to the market in 2022
- The latest intelligent factory MES management system software solution
- Batch export all pictures in PPT in one second
- Equipment simulation and deduction training system software
- Pytorch crossentropyloss learning
- D @ safety and dip1000
- Localization through custom services in the shuttle application
- Check log4j problems using stain analysis
- Search 2D matrix 2
猜你喜欢

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes

Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application

Review Net 20th anniversary development and 51aspx growth

2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

12种数据量纲化处理方式
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/92/f3a70e7086aeedf41eea3eef98b5aa.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database

From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
随机推荐
Data query language (DQL)
MySQL + JSON = King fried
Mujoco XML modeling
Nielseniq found that 60% of the re launched products had poor returns
MySQL connection tools
LeetCode 148. Sort linked list
目前炒期货在哪里开户最正规安全?怎么期货开户?
Is it safe to open a securities account? Is there any danger
Relationship between sensor size, pixel, dpi resolution, inch and millimeter
網上股票開戶安全嗎?是否可靠?
What impact will multinational encryption regulation bring to the market in 2022
APK签名流程介绍[通俗易懂]
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
540. Single element in ordered array
Mujoco's biped robot Darwin model
Samba basic usage
主成分之综合竞争力案例分析
Rotation order and universal lock of unity panel
因子分析怎么计算权重?
主成分计算权重