当前位置:网站首页>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 .
边栏推荐
- Apache iceberg source code analysis: schema evolution
- 主成分计算权重
- Bug of QQ browser article comment: the commentator is wrong
- [today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
- Redis主从实现10秒检查与恢复
- MySQL connection tools
- Pytorch crossentropyloss learning
- Subnet division and summary
- [CF1476F]Lanterns
- Record 3 - the state machine realizes key control and measures the number of external pulses
猜你喜欢

必看,时间序列分析

Happy new year | 202112 monthly summary

Localization through custom services in the shuttle application
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/92/f3a70e7086aeedf41eea3eef98b5aa.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)

Work and leisure suggestions of old programmers

Cloud computing - make learning easier

Record 3 - the state machine realizes key control and measures the number of external pulses

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

Product service, operation characteristics
随机推荐
February 16, 2022 Daily: graph neural network self training method under distribution and migration
ArrayList扩容详解
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
Step size of ode45 and reltol abstol
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
Record 3 - the state machine realizes key control and measures the number of external pulses
ZABBIX alarm execute remote command
Terms related to K line
[noip2015] jumping stone
Is Alipay wallet convenient to use?
Convert the robot's URDF file to mujoco model
C# SelfHost WebAPI (2)
[CF1476F]Lanterns
聊聊项目经理最爱使用的工具
Blue Bridge Cup real problem: word analysis
Blue Bridge Cup real question: score statistics
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
Extract the compressed package file and retrieve the password