当前位置:网站首页>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 .
边栏推荐
- MySQL connection tools
- Oracle TRUNC function processing date format
- 主成分之综合竞争力案例分析
- SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
- js如何将带有分割符的字符串转化成一个n维数组
- Nielseniq found that 60% of the re launched products had poor returns
- Draw drawing process of UI drawing process
- Depth first search - DFS (burst search)
- 传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
- PTA year of birth
猜你喜欢

Yolov5 practice: teach object detection by hand

Definition of rotation axis in mujoco

Apache iceberg source code analysis: schema evolution

How to retrieve the password for opening Excel files

PTA year of birth

Calculation of intersection of two line segments

Bernoulli distribution (a discrete distribution)
![[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out](/img/f3/20b73f3545cdd17b9fbc52bf493ab4.jpg)
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out

Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash

Classpath classpath
随机推荐
Explain in detail the process of realizing Chinese text classification by CNN
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
[acnoi2022] color ball
PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
Relationship between sensor size, pixel, dpi resolution, inch and millimeter
What impact will multinational encryption regulation bring to the market in 2022
How to learn automated testing?
ArrayList扩容详解
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
About selenium element positioning being overwritten
Software construction scheme of smart factory collaborative management and control application system
Is Alipay wallet convenient to use?
Review Net 20th anniversary development and 51aspx growth
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Redis master-slave realizes 10 second check and recovery
Classpath classpath
Static timing analysis (STA) in ic/fpga design
Vue uses keep alive to cache page optimization projects
Cassette helicopter and alternating electric field magnetic manometer DPC