view modules/bluespec/Pygar/core/SndfileWavUtil.cpp @ 57:b5be746a0d74 pygar svn.58

[svn r58] removed throttle in main
author punk
date Mon, 10 May 2010 12:17:52 -0400
parents 6d461680c6d9
children
line wrap: on
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <math.h>
5 #include <sndfile.h>
6 #include "SndfileWavUtil.h"
8 void
9 generate_wav(const char * pcmfilename, const char * samplewavfilename, const char * outputwavfilename)
10 {
11 char outfilename[2048];
12 SNDFILE * outfile ;
13 SNDFILE * wavfile ;
14 SNDFILE * pcmfile ;
15 SF_INFO wavinfo ;
16 SF_INFO pcminfo ;
17 int buff;
18 SF_INSTRUMENT inst ;
20 memset (&wavinfo, 0, sizeof (wavinfo)) ;
23 wavfile = sf_open(samplewavfilename, SFM_READ, &wavinfo);
25 if (wavfile == NULL){
26 printf ("\nERROR : Not able to open wav file named '%s' : %s/\n", samplewavfilename, sf_strerror (NULL)) ;
27 exit (1) ;
28 } ;
30 printf("WAV format: %x\n", wavinfo.format);
32 if (!((wavinfo.format & SF_FORMAT_PCM_16) && (wavinfo.channels == 1) &&
33 (wavinfo.format & SF_FORMAT_WAV))){
34 printf("\nERROR : .wav file must be SF_FORMAT_PCM_16 in mono\n");
35 }
37 pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
38 pcminfo.samplerate = wavinfo.samplerate;
39 pcminfo.channels = wavinfo.channels;
41 pcmfile = sf_open(pcmfilename, SFM_READ, &pcminfo);
43 if (pcmfile == NULL){
44 printf ("\nERROR : Not able to open pcm file named '%s' : %s/\n", pcmfilename, sf_strerror (NULL)) ;
45 exit (1) ;
46 } ;
50 outfile = sf_open(outputwavfilename, SFM_WRITE, &wavinfo);
52 memset (&inst, 0, sizeof (inst)) ;
54 for(int i = SF_STR_FIRST; i <= SF_STR_LAST; i = i + 1) {
55 const char * str = sf_get_string(wavfile,i);
56 if(str != NULL) {
57 sf_set_string(outfile,i,str);
58 }
59 }
61 if (outfile == NULL){
62 printf ("\nERROR : Not able to create wav file named '%s' : %s/\n", outfilename, sf_strerror (NULL)) ;
63 exit (1) ;
64 } ;
66 while(sf_read_int(pcmfile, &buff, 1) == 1){
67 if(sf_write_int(outfile, &buff, 1) != 1){
68 printf("\nERROR : unable to write to '%s' : %s/\n", outfilename, sf_strerror(NULL));
69 }
70 }
72 sf_close (wavfile) ;
73 sf_close (outfile) ;
74 sf_close (pcmfile) ;
76 }
79 void
80 generate_pcm (const char * wavfilename, const char * pcmfilename)
81 {
82 SNDFILE * wavfile ;
83 SNDFILE * pcmfile ;
84 SF_INFO wavinfo ;
85 SF_INFO pcminfo ;
86 int buff;
88 memset (&wavinfo, 0, sizeof (wavinfo)) ;
89 memset (&pcminfo, 0, sizeof (pcminfo)) ;
91 wavfile = sf_open (wavfilename, SFM_READ, &wavinfo) ;
93 if (wavfile == NULL){
94 printf ("\nERROR : Not able to open wav file named '%s' : %s/\n", wavfilename, sf_strerror (NULL)) ;
95 exit (1) ;
96 } ;
98 pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
99 pcminfo.samplerate = wavinfo.samplerate;
100 pcminfo.channels = wavinfo.channels;
102 if ((!wavinfo.format & SF_FORMAT_PCM_16) || (!wavinfo.channels == 1)){
103 printf("\nERROR : .wav file must be SF_FORMAT_PCM_16 and mono\n");
104 }
106 pcmfile = sf_open (pcmfilename, SFM_WRITE, &pcminfo) ;
108 if (pcmfile == NULL){
109 printf ("\nERROR : Not able to create pcm file named '%s' : %s/\n", pcmfilename, sf_strerror (NULL)) ;
110 exit (1) ;
111 } ;
113 while(sf_read_int(wavfile, &buff, 1) == 1){
114 if(sf_write_int(pcmfile, &buff, 1) != 1){
115 printf("\nERROR : unable to write to '%s' : %s/\n", pcmfilename, sf_strerror(NULL));
116 }
117 }
119 sf_close (wavfile) ;
120 sf_close (pcmfile) ;
121 }