Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pcm2wav.c

170 views
Skip to first unread message

paul...@yahoo.ca

unread,
Dec 8, 2006, 8:53:53 PM12/8/06
to
// I see people asking for pcm2wav.c on the web, but found nothing a
simple one page code, here it is, should work for little-endian 32bit
gcc environment, easy to change

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct WAVE {
char riff[4]; int length; char wave[4]; char fmt[4]; int fmtLen; short
compression_code;
short numChannels; int sampleRate; int bytePerSecond; short
bytePerSample;
short bitPerSample; char data[4]; int dataLen;
} wav;
int main(int argc,char** argv){
if (argc<3) {printf("pcm2wav infile outfile\n");return 1;}
fprintf(stderr,"argc=%d [%s] [%s]\n",argc,argv[1],argv[2]);
char c[4096];
int f1,f2,l,k;l=0;
f1=open(argv[1],O_RDONLY);
if (f1<3) {fprintf(stderr,"open infile error\n");return 1;}
while((k=read(f1,c,4096))>0) l+=k;
close(f1);
f1=open(argv[1],O_RDONLY);
f2=open(argv[2],O_CREAT|O_WRONLY|O_TRUNC);
if (f2<3) {fprintf(stderr,"open outfile error\n");return 1;}
strcpy(wav.riff,"RIFF");
wav.length=l+44-8;
strcpy(wav.wave,"WAVE");
strcpy(wav.fmt,"fmt ");
wav.fmtLen=16;
wav.compression_code=1;
wav.numChannels=2; // change this
wav.sampleRate=22050; // change this
wav.bytePerSecond=88200; // change this
wav.bytePerSample=4; // change this
wav.bitPerSample=16; // change this
strcpy(wav.data,"data");
wav.dataLen=l+44-44;
write(f2,wav.riff,44);
while((k=read(f1,c,4096))>0) write(f2,c,k);
}

paul...@yahoo.ca

unread,
Dec 8, 2006, 9:04:09 PM12/8/06
to

dd if=1.wav of=1.pcm bs=1 skip=44
for wav2pcm

Andre Majorel

unread,
Dec 9, 2006, 4:49:00 PM12/9/06
to
On 2006-12-09, paul...@yahoo.ca <paul...@yahoo.ca> wrote:

> // I see people asking for pcm2wav.c on the web, but found nothing a
> simple one page code, here it is, should work for little-endian 32bit
> gcc environment, easy to change

Or, if you have no compiler but sox installed,

sox -traw -r44100 -sw -c"$channels" in.pcm -twav out.wav

(Replace "$channels" by 1 for mono, 2 for stereo.)

--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
(Counterfeit: mof...@burton.com yl...@commonwealth.com)
Religion: a magic device for turning unanswerable questions into
unquestionable answers. -- Art Gecko

0 new messages