How to convert a PCM format audio file into a wave format file

Source: Internet
Author: User

Recently in making a voice changer app, which used the PCM format to wave format, the following posted source code, hope that with the needs of children's shoes help!!! This is written in the C + + language, or it can be implemented in Java. Of course, Java calls the native function to use the JNI technique. Specific JNI technology to find information on the Internet itself.

CPP file Pcm2Wave.cpp

#include <stdlib.h>#include<string.h>#include<stdio.h>#include"wave.h"#include"constant.h"classpcm2wave{Private: Wave_head wave_head; FILE*Src_pcm_file; FILE*Dest_wave_file; Public: Pcm2wave () {memcpy (&wave_head.riff_char,"RIFF",4); memcpy (&wave_head.wave,"WAVE",4); memcpy (&AMP;WAVE_HEAD.FMT,"FMT",4); memcpy (&wave_head.fccid,"fact",4); memcpy (&wave_head.data_field,"Data",4); Wave_head.package_len=0; Wave_head.format_len= -; Wave_head.fixed=1; Wave_head.channel_number=1; //sample rate long int samplespersecWave_head.sample_rate =8000 ; //quantify int bitpersamples=8 orWave_head.bits_per_sample = -; //sampling bytes per byte int BlockaligWave_head.byte_per_sample = Wave_head.channel_number * Wave_head.bits_per_sample/8;//Numchannels * BITSPERSAMPLE/8//number of bytes played per second long int bytepersecWave_head.byte_rate = wave_head.sample_rate * Wave_head.channel_number * wave_head.bits_per_sample/8;//samplerate * numchannels * BITSPERSAMPLE/8wave_head.id=4; Wave_head.dwsize=0; Wave_head.data_len=0; } pcm2wave (Wave_head*pwave_head) {        if(Pwave_head! =NULL) {memcpy (&wave_head, Pwave_head,sizeof(*pwave_head)); }    }        ~Pcm2wave () {}voidSet_channel ( ShortPchannel_number) {Wave_head.channel_number=Pchannel_number; Wave_head.byte_per_sample= Wave_head.channel_number * Wave_head.bits_per_sample/8; Wave_head.byte_rate= Wave_head.sample_rate * Wave_head.channel_number * wave_head.bits_per_sample/8; }        voidSet_sample_rate (intpsample_rate) {Wave_head.sample_rate=psample_rate; Wave_head.byte_rate= Wave_head.sample_rate * Wave_head.channel_number * wave_head.bits_per_sample/8; }        voidSet_bits_per_sample ( Shortpbits_per_sample) {Wave_head.bits_per_sample=pbits_per_sample; Wave_head.byte_per_sample= Wave_head.channel_number * Wave_head.bits_per_sample/8; Wave_head.byte_rate= Wave_head.sample_rate * Wave_head.channel_number * wave_head.bits_per_sample/8; }            intChangeConst Char*srcfile,Const Char*dsetfile) {        intNfilelen =0; intNSize =sizeof(Wave_head); printf ("Wave's head size:%d\n", nSize); Src_pcm_file=NULL; Dest_wave_file=NULL; Src_pcm_file= fopen (Srcfile,"RB"); if(Src_pcm_file = =NULL)return-1; Dest_wave_file= fopen (Dsetfile,"wb+"); if(Dest_wave_file = =NULL)return-2; intNwrite = fwrite (&wave_head,1, NSize, dest_wave_file); if(Nwrite! =nSize)            {fclose (src_pcm_file);            Fclose (Dest_wave_file); return-3; }                 while( !feof (Src_pcm_file)) {            Charreadbuf[4096]; intNread = Fread (Readbuf,1,4096, Src_pcm_file); if(Nread >0) {fwrite (Readbuf,1, Nread, dest_wave_file); } Nfilelen+=nread; } printf ("file pointer location:%d\n", Ftell (dest_wave_file)); //point the cursor pointer of the destination file to the beginning of the fileprintf"size of Nfilelen:%d\n nsize Size:%d\n", Nfilelen, nSize); Fseek (Dest_wave_file,0, Seek_set); Wave_head.package_len= Nfilelen-8+nSize; Wave_head.data_len=Nfilelen; Nwrite= Fwrite (&wave_head,1, NSize, dest_wave_file); if(Nwrite! =nSize)            {fclose (src_pcm_file);            Fclose (Dest_wave_file); return-4;        } fclose (Src_pcm_file);                Fclose (Dest_wave_file); return 0; }    };intMainCharargs [])    {Pcm2wave pp; Pp.set_sample_rate (11025); Pp.change ("JJ.PCM","Jj.wav"); return 0 ;}

Wave.h header File

typedefstructwave_head{Charriff_char[4];//"RIFF" char riff_id[4]= "RIFF"    intPackage_len;//Total file length -8 long int size0= -8    Charwave[4]; Charfmt[4]; intFormat_len;  Short fixed;//fmttag=0x01     ShortChannel_number;//channel=1 or 2    intSample_rate;//sample rate long int samplespersec    intByte_rate;//number of bytes played per second long int bytepersec     ShortByte_per_sample;//sampling bytes per byte int blockalign= channels * Quantization number/8     ShortBits_per_sample;//quantify int bitpersamples=8 or    Charfccid[4];//must be "fact"    intId//must be 0x4    intdwsize;//I don't see any use for a moment .    Chardata_field[4];//data_id= "Data"    intData_len;//number of sampled data bytes long int size2= text length -44} wave_head;

How to convert a PCM format audio file into a wave format file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.