当前位置:网站首页>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 .
边栏推荐
- 网上股票开户安全吗?是否可靠?
- Bernoulli distribution (a discrete distribution)
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- Equipment simulation and deduction training system software
- 期货账户的资金安全吗?怎么开户?
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
- Data query language (DQL)
- Common design parameters of solid rocket motor
- L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
- Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
猜你喜欢

Mujoco's biped robot Darwin model

Localization through custom services in the shuttle application

Data warehouse (3) star model and dimension modeling of data warehouse modeling

主成分计算权重

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

Bug of QQ browser article comment: the commentator is wrong

Calculation of intersection of two line segments

Flex layout

What are the legal risks of NFT brought by stars such as curry and O'Neill?

Extract the compressed package file and retrieve the password
随机推荐
NSI packaging script add file details
Blue Bridge Cup real problem: word analysis
Fix the problem that easycvr device video cannot be played
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
Depth first search - DFS (burst search)
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
MFC obtains local IP (used more in network communication)
Database - MySQL advanced SQL statement (I)
因子分析怎么计算权重?
Detailed explanation of select in golang
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
C operator overloads the query table
Step size of ode45 and reltol abstol
网上股票开户安全吗?是否可靠?
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
Calculation of intersection of two line segments
Draw drawing process of UI drawing process