当前位置:网站首页>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 .
边栏推荐
- APK签名流程介绍[通俗易懂]
- Is Alipay wallet convenient to use?
- 必看,时间序列分析
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- Localization through custom services in the shuttle application
- Operation of cmake under win
- Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
- Data warehouse (3) star model and dimension modeling of data warehouse modeling
- Rotation order and universal lock of unity panel
- Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
猜你喜欢
Record 3 - the state machine realizes key control and measures the number of external pulses
2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
Data query language (DQL)
Search 2D matrix 2
Data warehouse (3) star model and dimension modeling of data warehouse modeling
Calculation of intersection of two line segments
PCL learning materials
Extract the compressed package file and retrieve the password
Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
随机推荐
Fix the problem that easycvr device video cannot be played
PTA year of birth
Classpath classpath
Database - MySQL advanced SQL statement (I)
Flex layout
The method of real-time tracking the current price of London Silver
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Mujoco's biped robot Darwin model
Session layer of csframework, server and client (1)
SCP -i private key usage
Work and leisure suggestions of old programmers
Detailed explanation of ArrayList expansion
Data warehouse (3) star model and dimension modeling of data warehouse modeling
What impact will multinational encryption regulation bring to the market in 2022
Apache iceberg source code analysis: schema evolution
MFC obtains local IP (used more in network communication)
ArrayList扩容详解
[PHP foundation] realize the connection between PHP and SQL database
Yuancosmos game farmersworld farmers world - core content of the second conference in China!
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系