Mercurial > pygar
comparison modules/bluespec/Pygar/core/SndfileWavUtil.cpp @ 13:6d461680c6d9 pygar svn.14
[svn r14] more stuff
author | punk |
---|---|
date | Tue, 27 Apr 2010 09:03:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
12:394aa40fd812 | 13:6d461680c6d9 |
---|---|
1 #include <stdlib.h> | |
2 #include <string.h> | |
3 #include <errno.h> | |
4 #include <math.h> | |
5 #include <sndfile.h> | |
6 #include "SndfileWavUtil.h" | |
7 | |
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 ; | |
19 | |
20 memset (&wavinfo, 0, sizeof (wavinfo)) ; | |
21 | |
22 | |
23 wavfile = sf_open(samplewavfilename, SFM_READ, &wavinfo); | |
24 | |
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 } ; | |
29 | |
30 printf("WAV format: %x\n", wavinfo.format); | |
31 | |
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 } | |
36 | |
37 pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16; | |
38 pcminfo.samplerate = wavinfo.samplerate; | |
39 pcminfo.channels = wavinfo.channels; | |
40 | |
41 pcmfile = sf_open(pcmfilename, SFM_READ, &pcminfo); | |
42 | |
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 } ; | |
47 | |
48 | |
49 | |
50 outfile = sf_open(outputwavfilename, SFM_WRITE, &wavinfo); | |
51 | |
52 memset (&inst, 0, sizeof (inst)) ; | |
53 | |
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 } | |
60 | |
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 } ; | |
65 | |
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 } | |
71 | |
72 sf_close (wavfile) ; | |
73 sf_close (outfile) ; | |
74 sf_close (pcmfile) ; | |
75 | |
76 } | |
77 | |
78 | |
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; | |
87 | |
88 memset (&wavinfo, 0, sizeof (wavinfo)) ; | |
89 memset (&pcminfo, 0, sizeof (pcminfo)) ; | |
90 | |
91 wavfile = sf_open (wavfilename, SFM_READ, &wavinfo) ; | |
92 | |
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 } ; | |
97 | |
98 pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16; | |
99 pcminfo.samplerate = wavinfo.samplerate; | |
100 pcminfo.channels = wavinfo.channels; | |
101 | |
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 } | |
105 | |
106 pcmfile = sf_open (pcmfilename, SFM_WRITE, &pcminfo) ; | |
107 | |
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 } ; | |
112 | |
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 } | |
118 | |
119 sf_close (wavfile) ; | |
120 sf_close (pcmfile) ; | |
121 } | |
122 | |
123 | |
124 | |
125 |