annotate music2/wavfile.cc @ 26:f45da03ca631

* Corrected --disable-vorbis in configure.ac * Added sound configuration * Added misc functions
author thib
date Sun, 01 Mar 2009 19:08:02 +0000
parents 55b577e5f5b5
children 3a6aaeab7b4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * wavfile.c WAV file check
223b71206888 Initial import
thib
parents:
diff changeset
3 *
223b71206888 Initial import
thib
parents:
diff changeset
4 * Copyright: wavfile.c (c) Erik de Castro Lopo erikd@zip.com.au
223b71206888 Initial import
thib
parents:
diff changeset
5 *
223b71206888 Initial import
thib
parents:
diff changeset
6 * Modified : 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
223b71206888 Initial import
thib
parents:
diff changeset
7 * 1998- <masaki-c@is.aist-nara.ac.jp>
223b71206888 Initial import
thib
parents:
diff changeset
8 * 2000- Kazunori Ueno(JAGARL) <jagarl@createor.club.ne.jp>
223b71206888 Initial import
thib
parents:
diff changeset
9 *
223b71206888 Initial import
thib
parents:
diff changeset
10 * This program is free software; you can redistribute it and/or modify
223b71206888 Initial import
thib
parents:
diff changeset
11 * it under the terms of the GNU General Public License as published by
223b71206888 Initial import
thib
parents:
diff changeset
12 * the Free Software Foundation; either version 2 of the License, or
223b71206888 Initial import
thib
parents:
diff changeset
13 * (at your option) any later version.
223b71206888 Initial import
thib
parents:
diff changeset
14 *
223b71206888 Initial import
thib
parents:
diff changeset
15 * This program is distributed in the hope that it will be useful,
223b71206888 Initial import
thib
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
223b71206888 Initial import
thib
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
223b71206888 Initial import
thib
parents:
diff changeset
18 * GNU General Public License for more details.
223b71206888 Initial import
thib
parents:
diff changeset
19 *
223b71206888 Initial import
thib
parents:
diff changeset
20 * You should have received a copy of the GNU General Public License
223b71206888 Initial import
thib
parents:
diff changeset
21 * along with this program; if not, write to the Free Software
223b71206888 Initial import
thib
parents:
diff changeset
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
223b71206888 Initial import
thib
parents:
diff changeset
23 *
223b71206888 Initial import
thib
parents:
diff changeset
24 */
223b71206888 Initial import
thib
parents:
diff changeset
25
223b71206888 Initial import
thib
parents:
diff changeset
26 #include <stdarg.h>
223b71206888 Initial import
thib
parents:
diff changeset
27 #include <stdio.h>
223b71206888 Initial import
thib
parents:
diff changeset
28 #include <stdlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
29 #include <errno.h>
223b71206888 Initial import
thib
parents:
diff changeset
30 #include <sys/types.h>
223b71206888 Initial import
thib
parents:
diff changeset
31 #include <unistd.h>
223b71206888 Initial import
thib
parents:
diff changeset
32 #include <string.h>
223b71206888 Initial import
thib
parents:
diff changeset
33 #include "wavfile.h"
223b71206888 Initial import
thib
parents:
diff changeset
34 #include "system/file.h"
223b71206888 Initial import
thib
parents:
diff changeset
35 #include "music.h"
223b71206888 Initial import
thib
parents:
diff changeset
36
223b71206888 Initial import
thib
parents:
diff changeset
37 #define BUFFERSIZE 1024
223b71206888 Initial import
thib
parents:
diff changeset
38 #define PCM_WAVE_FORMAT 1
223b71206888 Initial import
thib
parents:
diff changeset
39
223b71206888 Initial import
thib
parents:
diff changeset
40 /*******************************************************
223b71206888 Initial import
thib
parents:
diff changeset
41 **
223b71206888 Initial import
thib
parents:
diff changeset
42 ** WAVE Header
223b71206888 Initial import
thib
parents:
diff changeset
43 */
223b71206888 Initial import
thib
parents:
diff changeset
44
223b71206888 Initial import
thib
parents:
diff changeset
45 inline int LittleEndian_getDW(const char *b,int index) {
223b71206888 Initial import
thib
parents:
diff changeset
46 int c0, c1, c2, c3;
223b71206888 Initial import
thib
parents:
diff changeset
47 int d0, d1;
223b71206888 Initial import
thib
parents:
diff changeset
48 c0 = *(const unsigned char*)(b + index + 0);
223b71206888 Initial import
thib
parents:
diff changeset
49 c1 = *(const unsigned char*)(b + index + 1);
223b71206888 Initial import
thib
parents:
diff changeset
50 c2 = *(const unsigned char*)(b + index + 2);
223b71206888 Initial import
thib
parents:
diff changeset
51 c3 = *(const unsigned char*)(b + index + 3);
223b71206888 Initial import
thib
parents:
diff changeset
52 d0 = c0 + (c1 << 8);
223b71206888 Initial import
thib
parents:
diff changeset
53 d1 = c2 + (c3 << 8);
223b71206888 Initial import
thib
parents:
diff changeset
54 return d0 + (d1 << 16);
223b71206888 Initial import
thib
parents:
diff changeset
55 }
223b71206888 Initial import
thib
parents:
diff changeset
56
223b71206888 Initial import
thib
parents:
diff changeset
57 inline int LittleEndian_get3B(const char *b,int index) {
223b71206888 Initial import
thib
parents:
diff changeset
58 int c0, c1, c2;
223b71206888 Initial import
thib
parents:
diff changeset
59 c0 = *(const unsigned char*)(b + index + 0);
223b71206888 Initial import
thib
parents:
diff changeset
60 c1 = *(const unsigned char*)(b + index + 1);
223b71206888 Initial import
thib
parents:
diff changeset
61 c2 = *(const unsigned char*)(b + index + 2);
223b71206888 Initial import
thib
parents:
diff changeset
62 return c0 + (c1 << 8) + (c2 << 16);
223b71206888 Initial import
thib
parents:
diff changeset
63 }
223b71206888 Initial import
thib
parents:
diff changeset
64
223b71206888 Initial import
thib
parents:
diff changeset
65 inline int LittleEndian_getW(const char *b,int index) {
223b71206888 Initial import
thib
parents:
diff changeset
66 int c0, c1;
223b71206888 Initial import
thib
parents:
diff changeset
67 c0 = *(const unsigned char*)(b + index + 0);
223b71206888 Initial import
thib
parents:
diff changeset
68 c1 = *(const unsigned char*)(b + index + 1);
223b71206888 Initial import
thib
parents:
diff changeset
69 return c0 + (c1 << 8);
223b71206888 Initial import
thib
parents:
diff changeset
70 }
223b71206888 Initial import
thib
parents:
diff changeset
71
223b71206888 Initial import
thib
parents:
diff changeset
72 inline void LittleEndian_putW(int num, char *b, int index) {
223b71206888 Initial import
thib
parents:
diff changeset
73 int c0, c1;
223b71206888 Initial import
thib
parents:
diff changeset
74 num %= 65536;
223b71206888 Initial import
thib
parents:
diff changeset
75 c0 = num % 256;
223b71206888 Initial import
thib
parents:
diff changeset
76 c1 = num / 256;
223b71206888 Initial import
thib
parents:
diff changeset
77 b[index] = c0; b[index+1] = c1;
223b71206888 Initial import
thib
parents:
diff changeset
78 }
223b71206888 Initial import
thib
parents:
diff changeset
79
223b71206888 Initial import
thib
parents:
diff changeset
80 typedef struct
223b71206888 Initial import
thib
parents:
diff changeset
81 { u_long dwSize ;
223b71206888 Initial import
thib
parents:
diff changeset
82 u_short wFormatTag ;
223b71206888 Initial import
thib
parents:
diff changeset
83 u_short wChannels ;
223b71206888 Initial import
thib
parents:
diff changeset
84 u_long dwSamplesPerSec ;
223b71206888 Initial import
thib
parents:
diff changeset
85 u_long dwAvgBytesPerSec ;
223b71206888 Initial import
thib
parents:
diff changeset
86 u_short wBlockAlign ;
223b71206888 Initial import
thib
parents:
diff changeset
87 u_short wBitsPerSample ;
223b71206888 Initial import
thib
parents:
diff changeset
88 } WAVEFORMAT ;
223b71206888 Initial import
thib
parents:
diff changeset
89
223b71206888 Initial import
thib
parents:
diff changeset
90 typedef struct
223b71206888 Initial import
thib
parents:
diff changeset
91 { char RiffID [4] ;
223b71206888 Initial import
thib
parents:
diff changeset
92 u_long RiffSize ;
223b71206888 Initial import
thib
parents:
diff changeset
93 char WaveID [4] ;
223b71206888 Initial import
thib
parents:
diff changeset
94 char FmtID [4] ;
223b71206888 Initial import
thib
parents:
diff changeset
95 u_long FmtSize ;
223b71206888 Initial import
thib
parents:
diff changeset
96 u_short wFormatTag ;
223b71206888 Initial import
thib
parents:
diff changeset
97 u_short nChannels ;
223b71206888 Initial import
thib
parents:
diff changeset
98 u_long nSamplesPerSec ;
223b71206888 Initial import
thib
parents:
diff changeset
99 u_long nAvgBytesPerSec ;
223b71206888 Initial import
thib
parents:
diff changeset
100 u_short nBlockAlign ;
223b71206888 Initial import
thib
parents:
diff changeset
101 u_short wBitsPerSample ;
223b71206888 Initial import
thib
parents:
diff changeset
102 char DataID [4] ;
223b71206888 Initial import
thib
parents:
diff changeset
103 u_long nDataBytes ;
223b71206888 Initial import
thib
parents:
diff changeset
104 } WAVE_HEADER ;
223b71206888 Initial import
thib
parents:
diff changeset
105
223b71206888 Initial import
thib
parents:
diff changeset
106
223b71206888 Initial import
thib
parents:
diff changeset
107 static void waveFormatCopy( WAVEFORMAT* wav, char *ptr );
223b71206888 Initial import
thib
parents:
diff changeset
108 static char* findchunk (char* s1, char* s2, size_t n) ;
223b71206888 Initial import
thib
parents:
diff changeset
109
223b71206888 Initial import
thib
parents:
diff changeset
110 static int WaveHeaderCheck (char *wave_buf,int* channels, u_long* samplerate, int* samplebits, u_long* samples,u_long* datastart)
223b71206888 Initial import
thib
parents:
diff changeset
111 {
223b71206888 Initial import
thib
parents:
diff changeset
112 static WAVEFORMAT waveformat ;
223b71206888 Initial import
thib
parents:
diff changeset
113 char* ptr ;
223b71206888 Initial import
thib
parents:
diff changeset
114 u_long databytes ;
223b71206888 Initial import
thib
parents:
diff changeset
115
223b71206888 Initial import
thib
parents:
diff changeset
116 if (findchunk (wave_buf, "RIFF", BUFFERSIZE) != wave_buf) {
223b71206888 Initial import
thib
parents:
diff changeset
117 fprintf(stderr, "Bad format: Cannot find RIFF file marker");
223b71206888 Initial import
thib
parents:
diff changeset
118 return WR_BADRIFF ;
223b71206888 Initial import
thib
parents:
diff changeset
119 }
223b71206888 Initial import
thib
parents:
diff changeset
120
223b71206888 Initial import
thib
parents:
diff changeset
121 if (! findchunk (wave_buf, "WAVE", BUFFERSIZE)) {
223b71206888 Initial import
thib
parents:
diff changeset
122 fprintf(stderr, "Bad format: Cannot find WAVE file marker");
223b71206888 Initial import
thib
parents:
diff changeset
123 return WR_BADWAVE ;
223b71206888 Initial import
thib
parents:
diff changeset
124 }
223b71206888 Initial import
thib
parents:
diff changeset
125
223b71206888 Initial import
thib
parents:
diff changeset
126 ptr = findchunk (wave_buf, "fmt ", BUFFERSIZE) ;
223b71206888 Initial import
thib
parents:
diff changeset
127
223b71206888 Initial import
thib
parents:
diff changeset
128 if (! ptr) {
223b71206888 Initial import
thib
parents:
diff changeset
129 fprintf(stderr, "Bad format: Cannot find 'fmt' file marker");
223b71206888 Initial import
thib
parents:
diff changeset
130 return WR_BADFORMAT ;
223b71206888 Initial import
thib
parents:
diff changeset
131 }
223b71206888 Initial import
thib
parents:
diff changeset
132
223b71206888 Initial import
thib
parents:
diff changeset
133 ptr += 4 ; /* Move past "fmt ".*/
223b71206888 Initial import
thib
parents:
diff changeset
134 waveFormatCopy( &waveformat, ptr );
223b71206888 Initial import
thib
parents:
diff changeset
135
223b71206888 Initial import
thib
parents:
diff changeset
136 if (waveformat.dwSize != (sizeof (WAVEFORMAT) - sizeof (u_long))) {
223b71206888 Initial import
thib
parents:
diff changeset
137 /* fprintf(stderr, "Bad format: Bad fmt size"); */
223b71206888 Initial import
thib
parents:
diff changeset
138 /* return WR_BADFORMATSIZE ; */
223b71206888 Initial import
thib
parents:
diff changeset
139 }
223b71206888 Initial import
thib
parents:
diff changeset
140
223b71206888 Initial import
thib
parents:
diff changeset
141 if (waveformat.wFormatTag != PCM_WAVE_FORMAT) {
223b71206888 Initial import
thib
parents:
diff changeset
142 fprintf(stderr, "Only supports PCM wave format");
223b71206888 Initial import
thib
parents:
diff changeset
143 return WR_NOTPCMFORMAT ;
223b71206888 Initial import
thib
parents:
diff changeset
144 }
223b71206888 Initial import
thib
parents:
diff changeset
145
223b71206888 Initial import
thib
parents:
diff changeset
146 ptr = findchunk (wave_buf, "data", BUFFERSIZE) ;
223b71206888 Initial import
thib
parents:
diff changeset
147
223b71206888 Initial import
thib
parents:
diff changeset
148 if (! ptr) {
223b71206888 Initial import
thib
parents:
diff changeset
149 fprintf(stderr,"Bad format: unable to find 'data' file marker");
223b71206888 Initial import
thib
parents:
diff changeset
150 return WR_NODATACHUNK ;
223b71206888 Initial import
thib
parents:
diff changeset
151 }
223b71206888 Initial import
thib
parents:
diff changeset
152
223b71206888 Initial import
thib
parents:
diff changeset
153 ptr += 4 ; /* Move past "data".*/
223b71206888 Initial import
thib
parents:
diff changeset
154 databytes = LittleEndian_getDW(ptr, 0);
223b71206888 Initial import
thib
parents:
diff changeset
155
223b71206888 Initial import
thib
parents:
diff changeset
156 /* Everything is now cool, so fill in output data.*/
223b71206888 Initial import
thib
parents:
diff changeset
157
223b71206888 Initial import
thib
parents:
diff changeset
158 *channels = waveformat.wChannels;
223b71206888 Initial import
thib
parents:
diff changeset
159 *samplerate = waveformat.dwSamplesPerSec ;
223b71206888 Initial import
thib
parents:
diff changeset
160 *samplebits = waveformat.wBitsPerSample ;
223b71206888 Initial import
thib
parents:
diff changeset
161 *samples = databytes / waveformat.wBlockAlign ;
223b71206888 Initial import
thib
parents:
diff changeset
162
223b71206888 Initial import
thib
parents:
diff changeset
163 *datastart = (u_long)(ptr) + 4;
223b71206888 Initial import
thib
parents:
diff changeset
164
223b71206888 Initial import
thib
parents:
diff changeset
165 if (waveformat.dwSamplesPerSec != waveformat.dwAvgBytesPerSec / waveformat.wBlockAlign) {
223b71206888 Initial import
thib
parents:
diff changeset
166 fprintf(stderr, "Bad file format");
223b71206888 Initial import
thib
parents:
diff changeset
167 return WR_BADFORMATDATA ;
223b71206888 Initial import
thib
parents:
diff changeset
168 }
223b71206888 Initial import
thib
parents:
diff changeset
169
223b71206888 Initial import
thib
parents:
diff changeset
170 if (waveformat.dwSamplesPerSec != waveformat.dwAvgBytesPerSec / waveformat.wChannels / ((waveformat.wBitsPerSample == 16) ? 2 : 1)) {
223b71206888 Initial import
thib
parents:
diff changeset
171 fprintf(stderr, "Bad file format");
223b71206888 Initial import
thib
parents:
diff changeset
172 return WR_BADFORMATDATA ;
223b71206888 Initial import
thib
parents:
diff changeset
173 }
223b71206888 Initial import
thib
parents:
diff changeset
174
223b71206888 Initial import
thib
parents:
diff changeset
175 return 0 ;
223b71206888 Initial import
thib
parents:
diff changeset
176 } ; /* WaveHeaderCheck*/
223b71206888 Initial import
thib
parents:
diff changeset
177
223b71206888 Initial import
thib
parents:
diff changeset
178
223b71206888 Initial import
thib
parents:
diff changeset
179 static char* findchunk (char* pstart, char* fourcc, size_t n)
223b71206888 Initial import
thib
parents:
diff changeset
180 { char *pend ;
223b71206888 Initial import
thib
parents:
diff changeset
181 int k, test ;
223b71206888 Initial import
thib
parents:
diff changeset
182
223b71206888 Initial import
thib
parents:
diff changeset
183 pend = pstart + n ;
223b71206888 Initial import
thib
parents:
diff changeset
184
223b71206888 Initial import
thib
parents:
diff changeset
185 while (pstart < pend)
223b71206888 Initial import
thib
parents:
diff changeset
186 {
223b71206888 Initial import
thib
parents:
diff changeset
187 if (*pstart == *fourcc) /* found match for first char*/
223b71206888 Initial import
thib
parents:
diff changeset
188 { test = 1 ;
223b71206888 Initial import
thib
parents:
diff changeset
189 for (k = 1 ; fourcc [k] != 0 ; k++)
223b71206888 Initial import
thib
parents:
diff changeset
190 test = (test ? ( pstart [k] == fourcc [k] ) : 0) ;
223b71206888 Initial import
thib
parents:
diff changeset
191 if (test)
223b71206888 Initial import
thib
parents:
diff changeset
192 return pstart ;
223b71206888 Initial import
thib
parents:
diff changeset
193 } ; /* if*/
223b71206888 Initial import
thib
parents:
diff changeset
194 pstart ++ ;
223b71206888 Initial import
thib
parents:
diff changeset
195 } ; /* while lpstart*/
223b71206888 Initial import
thib
parents:
diff changeset
196
223b71206888 Initial import
thib
parents:
diff changeset
197 return NULL ;
223b71206888 Initial import
thib
parents:
diff changeset
198 } ; /* findchuck*/
223b71206888 Initial import
thib
parents:
diff changeset
199
223b71206888 Initial import
thib
parents:
diff changeset
200 static void waveFormatCopy( WAVEFORMAT* wav, char *ptr ) {
223b71206888 Initial import
thib
parents:
diff changeset
201 wav->dwSize = LittleEndian_getDW( ptr, 0 );
223b71206888 Initial import
thib
parents:
diff changeset
202 wav->wFormatTag = LittleEndian_getW( ptr, 4 );
223b71206888 Initial import
thib
parents:
diff changeset
203 wav->wChannels = LittleEndian_getW( ptr, 6 );
223b71206888 Initial import
thib
parents:
diff changeset
204 wav->dwSamplesPerSec = LittleEndian_getDW( ptr, 8 );
223b71206888 Initial import
thib
parents:
diff changeset
205 wav->dwAvgBytesPerSec = LittleEndian_getDW( ptr, 12 );
223b71206888 Initial import
thib
parents:
diff changeset
206 wav->wBlockAlign = LittleEndian_getW( ptr, 16 );
223b71206888 Initial import
thib
parents:
diff changeset
207 wav->wBitsPerSample = LittleEndian_getW( ptr, 18 );
223b71206888 Initial import
thib
parents:
diff changeset
208 }
223b71206888 Initial import
thib
parents:
diff changeset
209
223b71206888 Initial import
thib
parents:
diff changeset
210 static char* WavGetInfo(WAVFILE* wfile, char *data) {
223b71206888 Initial import
thib
parents:
diff changeset
211 int e; /* Saved errno value */
223b71206888 Initial import
thib
parents:
diff changeset
212 int channels; /* Channels recorded in this wav file */
223b71206888 Initial import
thib
parents:
diff changeset
213 u_long samplerate; /* Sampling rate */
223b71206888 Initial import
thib
parents:
diff changeset
214 int sample_bits; /* data bit size (8/12/16) */
223b71206888 Initial import
thib
parents:
diff changeset
215 u_long samples; /* The number of samples in this file */
223b71206888 Initial import
thib
parents:
diff changeset
216 u_long datastart; /* The offset to the wav data */
223b71206888 Initial import
thib
parents:
diff changeset
217
223b71206888 Initial import
thib
parents:
diff changeset
218 if ( (e = WaveHeaderCheck(data,
223b71206888 Initial import
thib
parents:
diff changeset
219 &channels,&samplerate,
223b71206888 Initial import
thib
parents:
diff changeset
220 &sample_bits,&samples,&datastart) != 0 )) {
223b71206888 Initial import
thib
parents:
diff changeset
221 fprintf(stderr,"WavGetInfo(): Reading WAV header\n");
223b71206888 Initial import
thib
parents:
diff changeset
222 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
223 }
223b71206888 Initial import
thib
parents:
diff changeset
224
223b71206888 Initial import
thib
parents:
diff changeset
225 /*
223b71206888 Initial import
thib
parents:
diff changeset
226 * Copy WAV data over to WAVFILE struct:
223b71206888 Initial import
thib
parents:
diff changeset
227 */
223b71206888 Initial import
thib
parents:
diff changeset
228 wfile->wavinfo.Channels = channels;
223b71206888 Initial import
thib
parents:
diff changeset
229
223b71206888 Initial import
thib
parents:
diff changeset
230 wfile->wavinfo.SamplingRate = (unsigned int) samplerate;
223b71206888 Initial import
thib
parents:
diff changeset
231 wfile->wavinfo.DataBits = (unsigned short) sample_bits;
223b71206888 Initial import
thib
parents:
diff changeset
232
223b71206888 Initial import
thib
parents:
diff changeset
233 return (char *) datastart;
223b71206888 Initial import
thib
parents:
diff changeset
234 }
223b71206888 Initial import
thib
parents:
diff changeset
235
223b71206888 Initial import
thib
parents:
diff changeset
236 /************************************************************:
223b71206888 Initial import
thib
parents:
diff changeset
237 **
223b71206888 Initial import
thib
parents:
diff changeset
238 ** WAVFILE stream reader
223b71206888 Initial import
thib
parents:
diff changeset
239 */
223b71206888 Initial import
thib
parents:
diff changeset
240
223b71206888 Initial import
thib
parents:
diff changeset
241 #include<SDL_mixer.h>
223b71206888 Initial import
thib
parents:
diff changeset
242 WAVFILE::WAVFILE(void) {
223b71206888 Initial import
thib
parents:
diff changeset
243 wavinfo.SamplingRate=0;
223b71206888 Initial import
thib
parents:
diff changeset
244 wavinfo.Channels=1;
223b71206888 Initial import
thib
parents:
diff changeset
245 wavinfo.DataBits=0;
223b71206888 Initial import
thib
parents:
diff changeset
246 }
223b71206888 Initial import
thib
parents:
diff changeset
247
223b71206888 Initial import
thib
parents:
diff changeset
248 int WAVFILE_Stream::Read(char* in_buf, int blksize, int length) {
223b71206888 Initial import
thib
parents:
diff changeset
249 /* ¥Õ¥¡¥¤¥ë¤ÎÆɤ߹þ¤ß */
223b71206888 Initial import
thib
parents:
diff changeset
250 if (data_length == 0 && stream_length == 0) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
251 /* wf->data ¤Ë¥Ç¡¼¥¿¤Î»Ä¤ê¤¬¤¢¤ì¤Ð¤½¤ì¤âÆɤ߹þ¤à */
223b71206888 Initial import
thib
parents:
diff changeset
252 if (data_length > blksize*length) {
223b71206888 Initial import
thib
parents:
diff changeset
253 memcpy(in_buf, data, blksize*length);
223b71206888 Initial import
thib
parents:
diff changeset
254 data += blksize * length;
223b71206888 Initial import
thib
parents:
diff changeset
255 data_length -= blksize * length;
223b71206888 Initial import
thib
parents:
diff changeset
256 return length;
223b71206888 Initial import
thib
parents:
diff changeset
257 }
223b71206888 Initial import
thib
parents:
diff changeset
258 memcpy(in_buf, data, data_length);
223b71206888 Initial import
thib
parents:
diff changeset
259 if (stream_length != -1 && stream_length < blksize*length-data_length) {
223b71206888 Initial import
thib
parents:
diff changeset
260 length = (stream_length+data_length+blksize-1)/blksize;
223b71206888 Initial import
thib
parents:
diff changeset
261 }
223b71206888 Initial import
thib
parents:
diff changeset
262 int read_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
263 if (blksize*length-data_length > 0) {
223b71206888 Initial import
thib
parents:
diff changeset
264 read_len = fread(in_buf+data_length, 1, blksize*length-data_length, stream);
223b71206888 Initial import
thib
parents:
diff changeset
265 if (stream_length != -1 && stream_length > read_len) stream_length -= read_len;
223b71206888 Initial import
thib
parents:
diff changeset
266 if (feof(stream)) stream_length = 0; // end of file
223b71206888 Initial import
thib
parents:
diff changeset
267 } else {
223b71206888 Initial import
thib
parents:
diff changeset
268 stream_length = 0; // all data were read
223b71206888 Initial import
thib
parents:
diff changeset
269 }
223b71206888 Initial import
thib
parents:
diff changeset
270 int blklen = (read_len + data_length) / blksize;
223b71206888 Initial import
thib
parents:
diff changeset
271 data_length = 0;
223b71206888 Initial import
thib
parents:
diff changeset
272 return blklen;
223b71206888 Initial import
thib
parents:
diff changeset
273 }
223b71206888 Initial import
thib
parents:
diff changeset
274 void WAVFILE_Stream::Seek(int count) {
223b71206888 Initial import
thib
parents:
diff changeset
275 int blksize = 1;
223b71206888 Initial import
thib
parents:
diff changeset
276 /* block size ¤ÎÀßÄê */
223b71206888 Initial import
thib
parents:
diff changeset
277 blksize *= wavinfo.Channels * (wavinfo.DataBits/8);
223b71206888 Initial import
thib
parents:
diff changeset
278 data_length = 0;
223b71206888 Initial import
thib
parents:
diff changeset
279 stream_length = stream_length_orig - stream_top - count*blksize;
223b71206888 Initial import
thib
parents:
diff changeset
280 fseek(stream, count*blksize+stream_top, 0);
223b71206888 Initial import
thib
parents:
diff changeset
281 }
223b71206888 Initial import
thib
parents:
diff changeset
282 WAVFILE_Stream::WAVFILE_Stream(FILE* _stream, int _length) {
223b71206888 Initial import
thib
parents:
diff changeset
283 stream = _stream;
223b71206888 Initial import
thib
parents:
diff changeset
284 stream_length = _length;
223b71206888 Initial import
thib
parents:
diff changeset
285 stream_length_orig = _length;
223b71206888 Initial import
thib
parents:
diff changeset
286 data_orig = new char[1024];
223b71206888 Initial import
thib
parents:
diff changeset
287 data = data_orig;
223b71206888 Initial import
thib
parents:
diff changeset
288 data_length = 1024;
223b71206888 Initial import
thib
parents:
diff changeset
289 if (stream_length != -1 && stream_length < data_length) {
223b71206888 Initial import
thib
parents:
diff changeset
290 data_length = stream_length;
223b71206888 Initial import
thib
parents:
diff changeset
291 }
223b71206888 Initial import
thib
parents:
diff changeset
292 fread(data, data_length, 1, stream);
223b71206888 Initial import
thib
parents:
diff changeset
293 if (stream_length != -1)
223b71206888 Initial import
thib
parents:
diff changeset
294 stream_length -= data_length;
223b71206888 Initial import
thib
parents:
diff changeset
295 data = WavGetInfo(this, data);
223b71206888 Initial import
thib
parents:
diff changeset
296 if (data == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
297 stream_length = 0;
223b71206888 Initial import
thib
parents:
diff changeset
298 data_length = 0;
223b71206888 Initial import
thib
parents:
diff changeset
299 return;
223b71206888 Initial import
thib
parents:
diff changeset
300 }
223b71206888 Initial import
thib
parents:
diff changeset
301 stream_top = data - data_orig;
223b71206888 Initial import
thib
parents:
diff changeset
302 data_length -= data - data_orig;
223b71206888 Initial import
thib
parents:
diff changeset
303 return;
223b71206888 Initial import
thib
parents:
diff changeset
304 }
223b71206888 Initial import
thib
parents:
diff changeset
305 WAVFILE_Stream::~WAVFILE_Stream() {
223b71206888 Initial import
thib
parents:
diff changeset
306 if (data_orig) delete data_orig;
223b71206888 Initial import
thib
parents:
diff changeset
307 if (stream) fclose(stream);
223b71206888 Initial import
thib
parents:
diff changeset
308 return;
223b71206888 Initial import
thib
parents:
diff changeset
309 }
223b71206888 Initial import
thib
parents:
diff changeset
310 /************************************************************:
223b71206888 Initial import
thib
parents:
diff changeset
311 **
223b71206888 Initial import
thib
parents:
diff changeset
312 ** WAVE format converter with SDL_audio
223b71206888 Initial import
thib
parents:
diff changeset
313 */
223b71206888 Initial import
thib
parents:
diff changeset
314 WAVFILE* WAVFILE::MakeConverter(WAVFILE* new_reader) {
223b71206888 Initial import
thib
parents:
diff changeset
315 bool need = false;
223b71206888 Initial import
thib
parents:
diff changeset
316 if (new_reader->wavinfo.SamplingRate != freq) need = true;
223b71206888 Initial import
thib
parents:
diff changeset
317 if (new_reader->wavinfo.Channels != channels) need = true;
223b71206888 Initial import
thib
parents:
diff changeset
318 if (format == AUDIO_S8) {
223b71206888 Initial import
thib
parents:
diff changeset
319 if (new_reader->wavinfo.DataBits != 8) need = true;
223b71206888 Initial import
thib
parents:
diff changeset
320 } else if (format == AUDIO_S16) {
223b71206888 Initial import
thib
parents:
diff changeset
321 if (new_reader->wavinfo.DataBits != 16) need = true;
223b71206888 Initial import
thib
parents:
diff changeset
322 } else {
223b71206888 Initial import
thib
parents:
diff changeset
323 need = true;
223b71206888 Initial import
thib
parents:
diff changeset
324 }
223b71206888 Initial import
thib
parents:
diff changeset
325 if (!need) return new_reader;
223b71206888 Initial import
thib
parents:
diff changeset
326 /* ÊÑ´¹¤â¤È¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤òÆÀ¤ë */
223b71206888 Initial import
thib
parents:
diff changeset
327 int from_format;
223b71206888 Initial import
thib
parents:
diff changeset
328 if (new_reader->wavinfo.DataBits == 8) from_format = AUDIO_S8;
223b71206888 Initial import
thib
parents:
diff changeset
329 else from_format = AUDIO_S16;
223b71206888 Initial import
thib
parents:
diff changeset
330 SDL_AudioCVT* cvt = new SDL_AudioCVT;
223b71206888 Initial import
thib
parents:
diff changeset
331 int ret = SDL_BuildAudioCVT(cvt, from_format, new_reader->wavinfo.Channels, freq,
223b71206888 Initial import
thib
parents:
diff changeset
332 format, 2, freq);
223b71206888 Initial import
thib
parents:
diff changeset
333 if (ret == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
334 delete cvt;
223b71206888 Initial import
thib
parents:
diff changeset
335 fprintf(stderr,"Cannot make wave file converter!!!\n");
223b71206888 Initial import
thib
parents:
diff changeset
336 return new_reader;
223b71206888 Initial import
thib
parents:
diff changeset
337 }
223b71206888 Initial import
thib
parents:
diff changeset
338 WAVFILE_Converter* conv = new WAVFILE_Converter(new_reader, cvt);
223b71206888 Initial import
thib
parents:
diff changeset
339 return conv;
223b71206888 Initial import
thib
parents:
diff changeset
340 }
223b71206888 Initial import
thib
parents:
diff changeset
341 WAVFILE_Converter::WAVFILE_Converter(WAVFILE* _orig, SDL_AudioCVT* _cvt) {
223b71206888 Initial import
thib
parents:
diff changeset
342 original = _orig;
223b71206888 Initial import
thib
parents:
diff changeset
343 cvt = _cvt;
223b71206888 Initial import
thib
parents:
diff changeset
344 //datasize = 4096*4;
223b71206888 Initial import
thib
parents:
diff changeset
345 datasize = 48000;
223b71206888 Initial import
thib
parents:
diff changeset
346 cvt->buf = new Uint8[datasize*cvt->len_mult];
223b71206888 Initial import
thib
parents:
diff changeset
347 cvt->len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
348 tmpbuf = new char[datasize*cvt->len_mult + 1024];
223b71206888 Initial import
thib
parents:
diff changeset
349 memset(tmpbuf, 0, datasize*cvt->len_mult+1024);
223b71206888 Initial import
thib
parents:
diff changeset
350 };
223b71206888 Initial import
thib
parents:
diff changeset
351
223b71206888 Initial import
thib
parents:
diff changeset
352 static int conv_wave_rate(short* in_buf, int length, int in_rate, int out_rate, char* tmpbuf);
223b71206888 Initial import
thib
parents:
diff changeset
353 WAVFILE_Converter::~WAVFILE_Converter() {
223b71206888 Initial import
thib
parents:
diff changeset
354 if (cvt) {
7
fa8511a21d05 Fixes somes memory leaks
thib
parents: 0
diff changeset
355 if (cvt->buf) delete[] cvt->buf;
0
223b71206888 Initial import
thib
parents:
diff changeset
356 delete cvt;
223b71206888 Initial import
thib
parents:
diff changeset
357 cvt = 0;
223b71206888 Initial import
thib
parents:
diff changeset
358 }
8
55b577e5f5b5 Some memory leaks fixed
thib
parents: 7
diff changeset
359 delete[] tmpbuf;
0
223b71206888 Initial import
thib
parents:
diff changeset
360 if (original) delete original;
223b71206888 Initial import
thib
parents:
diff changeset
361 original = 0;
223b71206888 Initial import
thib
parents:
diff changeset
362 }
223b71206888 Initial import
thib
parents:
diff changeset
363 int WAVFILE_Converter::Read(char* buf, int blksize, int blklen) {
223b71206888 Initial import
thib
parents:
diff changeset
364 if (original == 0 || cvt == 0) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
365 int copied_length = 0;
223b71206888 Initial import
thib
parents:
diff changeset
366 if (cvt->len < blksize*blklen) {
223b71206888 Initial import
thib
parents:
diff changeset
367 memcpy(buf, cvt->buf, cvt->len);
223b71206888 Initial import
thib
parents:
diff changeset
368 copied_length += cvt->len;
223b71206888 Initial import
thib
parents:
diff changeset
369 do {
223b71206888 Initial import
thib
parents:
diff changeset
370 int cnt = original->Read((char*)cvt->buf, 1, datasize);
223b71206888 Initial import
thib
parents:
diff changeset
371 if (cnt <= 0) {
223b71206888 Initial import
thib
parents:
diff changeset
372 cvt->len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
373 break;
223b71206888 Initial import
thib
parents:
diff changeset
374 }
223b71206888 Initial import
thib
parents:
diff changeset
375 cvt->len = cnt;
223b71206888 Initial import
thib
parents:
diff changeset
376 SDL_ConvertAudio(cvt);
223b71206888 Initial import
thib
parents:
diff changeset
377 if (freq < original->wavinfo.SamplingRate) { // rate conversion ¤Ï SDL_ConvertAudio ¤Ç¤Ï¤¦¤Þ¤¯¹Ô¤«¤Ê¤¤
223b71206888 Initial import
thib
parents:
diff changeset
378 // 48000Hz -> 44100Hz or 22050Hz ¤Ê¤É¤òÁÛÄê
223b71206888 Initial import
thib
parents:
diff changeset
379 // Ťµ¤Ïû¤¯¤Ê¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢Æä˽èÍý¤Ï¤Ê¤·
223b71206888 Initial import
thib
parents:
diff changeset
380 cvt->len = conv_wave_rate( (short*)(cvt->buf), cvt->len_cvt/4, original->wavinfo.SamplingRate, freq, tmpbuf);
223b71206888 Initial import
thib
parents:
diff changeset
381 cvt->len *= 4;
223b71206888 Initial import
thib
parents:
diff changeset
382 } else {
223b71206888 Initial import
thib
parents:
diff changeset
383 cvt->len = cvt->len_cvt;
223b71206888 Initial import
thib
parents:
diff changeset
384 }
223b71206888 Initial import
thib
parents:
diff changeset
385 if (cvt->len+copied_length > blksize*blklen) break;
223b71206888 Initial import
thib
parents:
diff changeset
386 memcpy(buf+copied_length, cvt->buf, cvt->len);
223b71206888 Initial import
thib
parents:
diff changeset
387 copied_length += cvt->len;
223b71206888 Initial import
thib
parents:
diff changeset
388 } while(1);
223b71206888 Initial import
thib
parents:
diff changeset
389 }
223b71206888 Initial import
thib
parents:
diff changeset
390 if (cvt->len == 0 && copied_length == 0) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
391 else if (cvt->len > 0) {
223b71206888 Initial import
thib
parents:
diff changeset
392 int len = blksize * blklen - copied_length;
223b71206888 Initial import
thib
parents:
diff changeset
393 memcpy(buf+copied_length, cvt->buf, len);
223b71206888 Initial import
thib
parents:
diff changeset
394 memmove(cvt->buf, cvt->buf+len, cvt->len-len);
223b71206888 Initial import
thib
parents:
diff changeset
395 copied_length += len;
223b71206888 Initial import
thib
parents:
diff changeset
396 cvt->len -= len;
223b71206888 Initial import
thib
parents:
diff changeset
397 }
223b71206888 Initial import
thib
parents:
diff changeset
398 return copied_length / blksize;
223b71206888 Initial import
thib
parents:
diff changeset
399 }
223b71206888 Initial import
thib
parents:
diff changeset
400 /* format ¤Ï signed, 16bit, little endian, stereo ¤È·è¤á¤¦¤Á
223b71206888 Initial import
thib
parents:
diff changeset
401 ** ¾ì¹ç¤Ë¤è¤Ã¤Æ¤¤¤Ï big endian ¤Ë¤Ê¤ë¤³¤È¤â¤¢¤ë¤«¤â¡£
223b71206888 Initial import
thib
parents:
diff changeset
402 */
223b71206888 Initial import
thib
parents:
diff changeset
403 static int conv_wave_rate(short* in_buf, int length, int in_rate, int out_rate, char* tmpbuf) {
223b71206888 Initial import
thib
parents:
diff changeset
404 int input_rate = in_rate;
223b71206888 Initial import
thib
parents:
diff changeset
405 int output_rate = out_rate;
223b71206888 Initial import
thib
parents:
diff changeset
406 double input_rate_d = input_rate, output_rate_d = output_rate;
223b71206888 Initial import
thib
parents:
diff changeset
407 double dtime; int outlen; short* out, * out_orig; int next_sample1, next_sample2;
223b71206888 Initial import
thib
parents:
diff changeset
408 short* in_buf_orig = in_buf;
223b71206888 Initial import
thib
parents:
diff changeset
409 int i; int time;
223b71206888 Initial import
thib
parents:
diff changeset
410
223b71206888 Initial import
thib
parents:
diff changeset
411 if (input_rate == output_rate) return length;
223b71206888 Initial import
thib
parents:
diff changeset
412 if (length <= 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
413 /* °ìÈ̤μþÇÈ¿ôÊÑ´¹¡§Àþ·¿Êä´° */
223b71206888 Initial import
thib
parents:
diff changeset
414 int& first_flag = *(int*)(tmpbuf);
223b71206888 Initial import
thib
parents:
diff changeset
415 int& prev_time = *(int*)(tmpbuf+4);
223b71206888 Initial import
thib
parents:
diff changeset
416 int& prev_sample1 = *(int*)(tmpbuf+8);
223b71206888 Initial import
thib
parents:
diff changeset
417 int& prev_sample2 = *(int*)(tmpbuf+12);
223b71206888 Initial import
thib
parents:
diff changeset
418 out = (short*)(tmpbuf+16);
223b71206888 Initial import
thib
parents:
diff changeset
419 /* ½é¤á¤Æ¤Ê¤é¥Ç¡¼¥¿¤ò½é´ü²½ */
223b71206888 Initial import
thib
parents:
diff changeset
420 if (first_flag == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
421 first_flag = 1;
223b71206888 Initial import
thib
parents:
diff changeset
422 prev_time = 0;
223b71206888 Initial import
thib
parents:
diff changeset
423 prev_sample1 = short(read_little_endian_short((char*)(in_buf++)));
223b71206888 Initial import
thib
parents:
diff changeset
424 prev_sample2 = short(read_little_endian_short((char*)(in_buf++)));
223b71206888 Initial import
thib
parents:
diff changeset
425 length--;
223b71206888 Initial import
thib
parents:
diff changeset
426 }
223b71206888 Initial import
thib
parents:
diff changeset
427 /* º£²óºîÀ®¤¹¤ë¥Ç¡¼¥¿Î̤òÆÀ¤ë */
223b71206888 Initial import
thib
parents:
diff changeset
428 dtime = prev_time + length * output_rate_d;
223b71206888 Initial import
thib
parents:
diff changeset
429 outlen = (int)(dtime / input_rate_d);
223b71206888 Initial import
thib
parents:
diff changeset
430 out_orig = out;
223b71206888 Initial import
thib
parents:
diff changeset
431 if (first_flag == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
432 write_little_endian_short((char*)out, prev_sample1);
223b71206888 Initial import
thib
parents:
diff changeset
433 out++;
223b71206888 Initial import
thib
parents:
diff changeset
434 write_little_endian_short((char*)out, prev_sample2);
223b71206888 Initial import
thib
parents:
diff changeset
435 out++;
223b71206888 Initial import
thib
parents:
diff changeset
436 }
223b71206888 Initial import
thib
parents:
diff changeset
437 dtime -= input_rate_d*outlen; /* ¼¡¤Î prev_time */
223b71206888 Initial import
thib
parents:
diff changeset
438
223b71206888 Initial import
thib
parents:
diff changeset
439 time=0;
223b71206888 Initial import
thib
parents:
diff changeset
440 next_sample1 = short(read_little_endian_short((char*)(in_buf++)));
223b71206888 Initial import
thib
parents:
diff changeset
441 next_sample2 = short(read_little_endian_short((char*)(in_buf++)));
223b71206888 Initial import
thib
parents:
diff changeset
442 for (i=0; i<outlen; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
443 /* double ¤Ç·×»»¤·¤Æ¤ß¤¿¤±¤É¤½¤¦´Êñ¤Ë¤Ï¹â®²½¤Ï̵Íý¤é¤·¤¤ */
223b71206888 Initial import
thib
parents:
diff changeset
444 /* ¤Ê¤ª¡¢ÊÑ´¹¤Ï 1ʬ¤Î¥Ç¡¼¥¿¤Ë1ÉÃÄøÅÙ¤«¤«¤ë(Celeron 700MHz) */
223b71206888 Initial import
thib
parents:
diff changeset
445 time += input_rate;
223b71206888 Initial import
thib
parents:
diff changeset
446 while(time-prev_time>output_rate) {
223b71206888 Initial import
thib
parents:
diff changeset
447 prev_sample1 = next_sample1;
223b71206888 Initial import
thib
parents:
diff changeset
448 next_sample1 = short(read_little_endian_short((char*)(in_buf++)));
223b71206888 Initial import
thib
parents:
diff changeset
449 prev_sample2 = next_sample2;
223b71206888 Initial import
thib
parents:
diff changeset
450 next_sample2 = short(read_little_endian_short((char*)(in_buf++)));
223b71206888 Initial import
thib
parents:
diff changeset
451 prev_time += output_rate;
223b71206888 Initial import
thib
parents:
diff changeset
452 }
223b71206888 Initial import
thib
parents:
diff changeset
453 write_little_endian_short((char*)out,
223b71206888 Initial import
thib
parents:
diff changeset
454 ((time-prev_time)*next_sample1 +
223b71206888 Initial import
thib
parents:
diff changeset
455 (input_rate-time+prev_time)*prev_sample1) / input_rate);
223b71206888 Initial import
thib
parents:
diff changeset
456 out++;
223b71206888 Initial import
thib
parents:
diff changeset
457 write_little_endian_short((char*)out,
223b71206888 Initial import
thib
parents:
diff changeset
458 ((time-prev_time)*next_sample2 +
223b71206888 Initial import
thib
parents:
diff changeset
459 (input_rate-time+prev_time)*prev_sample2) / input_rate);
223b71206888 Initial import
thib
parents:
diff changeset
460 *out++;
223b71206888 Initial import
thib
parents:
diff changeset
461 }
223b71206888 Initial import
thib
parents:
diff changeset
462 prev_time += output_rate; prev_time -= input_rate * outlen;
223b71206888 Initial import
thib
parents:
diff changeset
463 prev_sample1 = next_sample1; prev_sample2 = next_sample2;
223b71206888 Initial import
thib
parents:
diff changeset
464 if (first_flag == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
465 outlen++; first_flag = 2;
223b71206888 Initial import
thib
parents:
diff changeset
466 }
223b71206888 Initial import
thib
parents:
diff changeset
467 memcpy(in_buf_orig, out_orig, outlen*2*sizeof(short));
223b71206888 Initial import
thib
parents:
diff changeset
468 return outlen;
223b71206888 Initial import
thib
parents:
diff changeset
469 }
223b71206888 Initial import
thib
parents:
diff changeset
470
223b71206888 Initial import
thib
parents:
diff changeset
471
223b71206888 Initial import
thib
parents:
diff changeset
472 /************************************************************:
223b71206888 Initial import
thib
parents:
diff changeset
473 **
223b71206888 Initial import
thib
parents:
diff changeset
474 ** MP3FILE stream reader
223b71206888 Initial import
thib
parents:
diff changeset
475 */
223b71206888 Initial import
thib
parents:
diff changeset
476
223b71206888 Initial import
thib
parents:
diff changeset
477 int WAVFILE::freq = 48000;
223b71206888 Initial import
thib
parents:
diff changeset
478 int WAVFILE::channels = 2;
223b71206888 Initial import
thib
parents:
diff changeset
479 int WAVFILE::format = MIX_DEFAULT_FORMAT;
223b71206888 Initial import
thib
parents:
diff changeset
480
223b71206888 Initial import
thib
parents:
diff changeset
481 #if HAVE_LIBMAD
223b71206888 Initial import
thib
parents:
diff changeset
482
223b71206888 Initial import
thib
parents:
diff changeset
483 #include<mad.h>
223b71206888 Initial import
thib
parents:
diff changeset
484 #define MPEG_BUFSZ 40000 /* 2.5 s at 128 kbps; 1 s at 320 kbps */
223b71206888 Initial import
thib
parents:
diff changeset
485 struct MP3FILE_impl {
223b71206888 Initial import
thib
parents:
diff changeset
486 enum { PREPARE, RUN, WRITE, DONE} status;
223b71206888 Initial import
thib
parents:
diff changeset
487 struct mad_decoder decoder;
223b71206888 Initial import
thib
parents:
diff changeset
488 char* data;
223b71206888 Initial import
thib
parents:
diff changeset
489 int data_len;
223b71206888 Initial import
thib
parents:
diff changeset
490 char* write_data;
223b71206888 Initial import
thib
parents:
diff changeset
491 unsigned int write_data_len;
223b71206888 Initial import
thib
parents:
diff changeset
492 unsigned int write_pointer;
223b71206888 Initial import
thib
parents:
diff changeset
493 unsigned int src_pointer;
223b71206888 Initial import
thib
parents:
diff changeset
494 FILE* stream;
223b71206888 Initial import
thib
parents:
diff changeset
495 MP3FILE_impl(FILE*);
223b71206888 Initial import
thib
parents:
diff changeset
496 ~MP3FILE_impl();
223b71206888 Initial import
thib
parents:
diff changeset
497 static enum mad_flow callback_read(void *data, struct mad_stream *stream);
223b71206888 Initial import
thib
parents:
diff changeset
498 static enum mad_flow callback_error(void *data, struct mad_stream *stream, struct mad_frame *frame);
223b71206888 Initial import
thib
parents:
diff changeset
499 static enum mad_flow callback_write(void *data, struct mad_header const *header, struct mad_pcm *pcm);
223b71206888 Initial import
thib
parents:
diff changeset
500 enum mad_flow callback_write_impl(struct mad_pcm *pcm);
223b71206888 Initial import
thib
parents:
diff changeset
501 void run(void);
223b71206888 Initial import
thib
parents:
diff changeset
502 };
223b71206888 Initial import
thib
parents:
diff changeset
503
223b71206888 Initial import
thib
parents:
diff changeset
504 MP3FILE_impl::MP3FILE_impl(FILE* _stream) {
223b71206888 Initial import
thib
parents:
diff changeset
505 stream = _stream;
223b71206888 Initial import
thib
parents:
diff changeset
506 data = new char[MPEG_BUFSZ];
223b71206888 Initial import
thib
parents:
diff changeset
507 data_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
508 src_pointer = 0;
223b71206888 Initial import
thib
parents:
diff changeset
509 write_data = 0;
223b71206888 Initial import
thib
parents:
diff changeset
510 write_data_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
511 write_pointer = 0;
223b71206888 Initial import
thib
parents:
diff changeset
512
223b71206888 Initial import
thib
parents:
diff changeset
513 /* initialize decoder */
223b71206888 Initial import
thib
parents:
diff changeset
514 mad_decoder_init(&decoder, (void*)this, callback_read, 0 /* header */, 0 /* filter */, callback_write,
223b71206888 Initial import
thib
parents:
diff changeset
515 callback_error, 0 /* message */);
223b71206888 Initial import
thib
parents:
diff changeset
516 /* prepare stream */
223b71206888 Initial import
thib
parents:
diff changeset
517 status = PREPARE;
223b71206888 Initial import
thib
parents:
diff changeset
518 *(void**)(&decoder.sync) = malloc(sizeof(*decoder.sync));
223b71206888 Initial import
thib
parents:
diff changeset
519
223b71206888 Initial import
thib
parents:
diff changeset
520 mad_stream_init(&decoder.sync->stream);
223b71206888 Initial import
thib
parents:
diff changeset
521 mad_frame_init(&decoder.sync->frame);
223b71206888 Initial import
thib
parents:
diff changeset
522 mad_synth_init(&decoder.sync->synth);
223b71206888 Initial import
thib
parents:
diff changeset
523
223b71206888 Initial import
thib
parents:
diff changeset
524 mad_stream_options(&decoder.sync->stream, decoder.options);
223b71206888 Initial import
thib
parents:
diff changeset
525
223b71206888 Initial import
thib
parents:
diff changeset
526 while(status != WRITE && status != DONE) run();
223b71206888 Initial import
thib
parents:
diff changeset
527 }
223b71206888 Initial import
thib
parents:
diff changeset
528 MP3FILE_impl::~MP3FILE_impl() {
223b71206888 Initial import
thib
parents:
diff changeset
529 free(decoder.sync);
223b71206888 Initial import
thib
parents:
diff changeset
530 mad_decoder_finish(&decoder);
223b71206888 Initial import
thib
parents:
diff changeset
531 delete[] data;
223b71206888 Initial import
thib
parents:
diff changeset
532 return;
223b71206888 Initial import
thib
parents:
diff changeset
533 }
223b71206888 Initial import
thib
parents:
diff changeset
534
223b71206888 Initial import
thib
parents:
diff changeset
535 void MP3FILE_impl::run(void) {
223b71206888 Initial import
thib
parents:
diff changeset
536 if (status == DONE) return;
223b71206888 Initial import
thib
parents:
diff changeset
537 struct mad_stream *stream = &decoder.sync->stream;
223b71206888 Initial import
thib
parents:
diff changeset
538 struct mad_frame *frame = &decoder.sync->frame;
223b71206888 Initial import
thib
parents:
diff changeset
539 struct mad_synth *synth = &decoder.sync->synth;
223b71206888 Initial import
thib
parents:
diff changeset
540 if (status == PREPARE) {
223b71206888 Initial import
thib
parents:
diff changeset
541 switch (decoder.input_func(decoder.cb_data, stream)) {
223b71206888 Initial import
thib
parents:
diff changeset
542 case MAD_FLOW_STOP:
223b71206888 Initial import
thib
parents:
diff changeset
543 case MAD_FLOW_BREAK:
223b71206888 Initial import
thib
parents:
diff changeset
544 goto done;
223b71206888 Initial import
thib
parents:
diff changeset
545 case MAD_FLOW_CONTINUE:
223b71206888 Initial import
thib
parents:
diff changeset
546 status = RUN;
223b71206888 Initial import
thib
parents:
diff changeset
547 case MAD_FLOW_IGNORE:
223b71206888 Initial import
thib
parents:
diff changeset
548 break;
223b71206888 Initial import
thib
parents:
diff changeset
549 }
223b71206888 Initial import
thib
parents:
diff changeset
550 return;
223b71206888 Initial import
thib
parents:
diff changeset
551 }
223b71206888 Initial import
thib
parents:
diff changeset
552 if (status == RUN) {
223b71206888 Initial import
thib
parents:
diff changeset
553 if (mad_frame_decode(frame, stream) == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
554 if (!MAD_RECOVERABLE(stream->error)) {
223b71206888 Initial import
thib
parents:
diff changeset
555 status = PREPARE;
223b71206888 Initial import
thib
parents:
diff changeset
556 return;
223b71206888 Initial import
thib
parents:
diff changeset
557 }
223b71206888 Initial import
thib
parents:
diff changeset
558 switch (decoder.error_func((void*)this, stream, frame)) {
223b71206888 Initial import
thib
parents:
diff changeset
559 case MAD_FLOW_STOP:
223b71206888 Initial import
thib
parents:
diff changeset
560 case MAD_FLOW_BREAK:
223b71206888 Initial import
thib
parents:
diff changeset
561 goto done;
223b71206888 Initial import
thib
parents:
diff changeset
562 case MAD_FLOW_IGNORE:
223b71206888 Initial import
thib
parents:
diff changeset
563 status = PREPARE;
223b71206888 Initial import
thib
parents:
diff changeset
564 return;
223b71206888 Initial import
thib
parents:
diff changeset
565 case MAD_FLOW_CONTINUE:
223b71206888 Initial import
thib
parents:
diff changeset
566 default:
223b71206888 Initial import
thib
parents:
diff changeset
567 return;
223b71206888 Initial import
thib
parents:
diff changeset
568 }
223b71206888 Initial import
thib
parents:
diff changeset
569 }
223b71206888 Initial import
thib
parents:
diff changeset
570
223b71206888 Initial import
thib
parents:
diff changeset
571 mad_synth_frame(synth, frame);
223b71206888 Initial import
thib
parents:
diff changeset
572 src_pointer = 0;
223b71206888 Initial import
thib
parents:
diff changeset
573 status = WRITE;
223b71206888 Initial import
thib
parents:
diff changeset
574 return;
223b71206888 Initial import
thib
parents:
diff changeset
575 }
223b71206888 Initial import
thib
parents:
diff changeset
576 if (status == WRITE) {
223b71206888 Initial import
thib
parents:
diff changeset
577 switch (decoder.output_func(decoder.cb_data, &frame->header, &synth->pcm)) {
223b71206888 Initial import
thib
parents:
diff changeset
578 case MAD_FLOW_STOP:
223b71206888 Initial import
thib
parents:
diff changeset
579 case MAD_FLOW_BREAK:
223b71206888 Initial import
thib
parents:
diff changeset
580 goto done;
223b71206888 Initial import
thib
parents:
diff changeset
581 case MAD_FLOW_IGNORE:
223b71206888 Initial import
thib
parents:
diff changeset
582 return;
223b71206888 Initial import
thib
parents:
diff changeset
583 case MAD_FLOW_CONTINUE:
223b71206888 Initial import
thib
parents:
diff changeset
584 status = RUN;
223b71206888 Initial import
thib
parents:
diff changeset
585 break;
223b71206888 Initial import
thib
parents:
diff changeset
586 }
223b71206888 Initial import
thib
parents:
diff changeset
587 if (stream->error == MAD_ERROR_BUFLEN) {
223b71206888 Initial import
thib
parents:
diff changeset
588 stream->error = MAD_ERROR_NONE;
223b71206888 Initial import
thib
parents:
diff changeset
589 status = PREPARE;
223b71206888 Initial import
thib
parents:
diff changeset
590 }
223b71206888 Initial import
thib
parents:
diff changeset
591 return;
223b71206888 Initial import
thib
parents:
diff changeset
592 }
223b71206888 Initial import
thib
parents:
diff changeset
593 done:
223b71206888 Initial import
thib
parents:
diff changeset
594 status = DONE;
223b71206888 Initial import
thib
parents:
diff changeset
595 mad_synth_finish(&decoder.sync->synth);
223b71206888 Initial import
thib
parents:
diff changeset
596 mad_frame_finish(&decoder.sync->frame);
223b71206888 Initial import
thib
parents:
diff changeset
597 mad_stream_finish(&decoder.sync->stream);
223b71206888 Initial import
thib
parents:
diff changeset
598 return;
223b71206888 Initial import
thib
parents:
diff changeset
599 }
223b71206888 Initial import
thib
parents:
diff changeset
600
223b71206888 Initial import
thib
parents:
diff changeset
601 enum mad_flow MP3FILE_impl::callback_read(void *data, struct mad_stream *stream)
223b71206888 Initial import
thib
parents:
diff changeset
602 {
223b71206888 Initial import
thib
parents:
diff changeset
603 MP3FILE_impl* impl = (MP3FILE_impl*)data;
223b71206888 Initial import
thib
parents:
diff changeset
604 if (stream->next_frame) {
223b71206888 Initial import
thib
parents:
diff changeset
605 impl->data_len -= (char*)stream->next_frame - impl->data;
223b71206888 Initial import
thib
parents:
diff changeset
606 memmove(impl->data, (char*)stream->next_frame, impl->data_len);
223b71206888 Initial import
thib
parents:
diff changeset
607 } else {
223b71206888 Initial import
thib
parents:
diff changeset
608 impl->data_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
609 }
223b71206888 Initial import
thib
parents:
diff changeset
610 int count;
223b71206888 Initial import
thib
parents:
diff changeset
611 if (feof(impl->stream)) {
223b71206888 Initial import
thib
parents:
diff changeset
612 if (stream->next_frame && (char*)stream->next_frame - impl->data > 0) {
223b71206888 Initial import
thib
parents:
diff changeset
613 // There is under processing data
223b71206888 Initial import
thib
parents:
diff changeset
614 count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
615 } else {
223b71206888 Initial import
thib
parents:
diff changeset
616 // all data were processed
223b71206888 Initial import
thib
parents:
diff changeset
617 return MAD_FLOW_STOP;
223b71206888 Initial import
thib
parents:
diff changeset
618 }
223b71206888 Initial import
thib
parents:
diff changeset
619 } else {
223b71206888 Initial import
thib
parents:
diff changeset
620 count = fread(impl->data + impl->data_len, 1, MPEG_BUFSZ-impl->data_len, impl->stream);
223b71206888 Initial import
thib
parents:
diff changeset
621 if (count <= 0) {
223b71206888 Initial import
thib
parents:
diff changeset
622 return MAD_FLOW_BREAK;
223b71206888 Initial import
thib
parents:
diff changeset
623 }
223b71206888 Initial import
thib
parents:
diff changeset
624 }
223b71206888 Initial import
thib
parents:
diff changeset
625 impl->data_len += count;
223b71206888 Initial import
thib
parents:
diff changeset
626 if (impl->data_len < MPEG_BUFSZ) {
223b71206888 Initial import
thib
parents:
diff changeset
627 memset(impl->data + impl->data_len, 0, MPEG_BUFSZ-impl->data_len);
223b71206888 Initial import
thib
parents:
diff changeset
628 }
223b71206888 Initial import
thib
parents:
diff changeset
629 mad_stream_buffer(stream, (unsigned char*)impl->data, impl->data_len);
223b71206888 Initial import
thib
parents:
diff changeset
630 return MAD_FLOW_CONTINUE;
223b71206888 Initial import
thib
parents:
diff changeset
631 }
223b71206888 Initial import
thib
parents:
diff changeset
632
223b71206888 Initial import
thib
parents:
diff changeset
633 enum mad_flow MP3FILE_impl::callback_error(void *data, struct mad_stream *stream, struct mad_frame *frame)
223b71206888 Initial import
thib
parents:
diff changeset
634 {
223b71206888 Initial import
thib
parents:
diff changeset
635 MP3FILE_impl* impl = (MP3FILE_impl*)data;
223b71206888 Initial import
thib
parents:
diff changeset
636 fprintf(stdout, "decoding error 0x%04x (%s) at byte offset %u\n",
223b71206888 Initial import
thib
parents:
diff changeset
637 stream->error, mad_stream_errorstr(stream),
223b71206888 Initial import
thib
parents:
diff changeset
638 ftell(impl->stream) - ((impl->data+impl->data_len)-(char*)stream->this_frame));
223b71206888 Initial import
thib
parents:
diff changeset
639 /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
223b71206888 Initial import
thib
parents:
diff changeset
640 return MAD_FLOW_CONTINUE;
223b71206888 Initial import
thib
parents:
diff changeset
641 }
223b71206888 Initial import
thib
parents:
diff changeset
642 signed int scale(mad_fixed_t sample)
223b71206888 Initial import
thib
parents:
diff changeset
643 {
223b71206888 Initial import
thib
parents:
diff changeset
644 /* round */
223b71206888 Initial import
thib
parents:
diff changeset
645 sample += (1L << (MAD_F_FRACBITS - 16));
223b71206888 Initial import
thib
parents:
diff changeset
646
223b71206888 Initial import
thib
parents:
diff changeset
647 /* clip */
223b71206888 Initial import
thib
parents:
diff changeset
648 if (sample >= MAD_F_ONE)
223b71206888 Initial import
thib
parents:
diff changeset
649 sample = MAD_F_ONE - 1;
223b71206888 Initial import
thib
parents:
diff changeset
650 else if (sample < -MAD_F_ONE)
223b71206888 Initial import
thib
parents:
diff changeset
651 sample = -MAD_F_ONE;
223b71206888 Initial import
thib
parents:
diff changeset
652
223b71206888 Initial import
thib
parents:
diff changeset
653 /* quantize */
223b71206888 Initial import
thib
parents:
diff changeset
654 return sample >> (MAD_F_FRACBITS + 1 - 16);
223b71206888 Initial import
thib
parents:
diff changeset
655 }
223b71206888 Initial import
thib
parents:
diff changeset
656 enum mad_flow MP3FILE_impl::callback_write(void *data, struct mad_header const *header, struct mad_pcm *pcm)
223b71206888 Initial import
thib
parents:
diff changeset
657 {
223b71206888 Initial import
thib
parents:
diff changeset
658 MP3FILE_impl* pimpl = (MP3FILE_impl*)data;
223b71206888 Initial import
thib
parents:
diff changeset
659 return pimpl->callback_write_impl(pcm);
223b71206888 Initial import
thib
parents:
diff changeset
660 }
223b71206888 Initial import
thib
parents:
diff changeset
661 enum mad_flow MP3FILE_impl::callback_write_impl(struct mad_pcm *pcm)
223b71206888 Initial import
thib
parents:
diff changeset
662 {
223b71206888 Initial import
thib
parents:
diff changeset
663 if (write_data_len == 0) return MAD_FLOW_IGNORE;
223b71206888 Initial import
thib
parents:
diff changeset
664 mad_fixed_t const *left_ch = pcm->samples[0] + src_pointer;
223b71206888 Initial import
thib
parents:
diff changeset
665 mad_fixed_t const *right_ch = pcm->samples[1] + src_pointer;
223b71206888 Initial import
thib
parents:
diff changeset
666
223b71206888 Initial import
thib
parents:
diff changeset
667 unsigned int nchannels = pcm->channels;
223b71206888 Initial import
thib
parents:
diff changeset
668 unsigned int nsamples = pcm->length - src_pointer;
223b71206888 Initial import
thib
parents:
diff changeset
669 if (write_pointer + nsamples * nchannels * 2 > write_data_len) {
223b71206888 Initial import
thib
parents:
diff changeset
670 nsamples = (write_data_len - write_pointer) / nchannels / 2;
223b71206888 Initial import
thib
parents:
diff changeset
671 }
223b71206888 Initial import
thib
parents:
diff changeset
672 write_data_len &= ~(nchannels*2-1); /* write_data_len ¤Ï¤¢¤é¤«¤¸¤á´Ý¤á¤Æ¤ª¤¯ */
223b71206888 Initial import
thib
parents:
diff changeset
673 src_pointer += nsamples;
223b71206888 Initial import
thib
parents:
diff changeset
674 if (write_data == 0) { // skip data write
223b71206888 Initial import
thib
parents:
diff changeset
675 write_pointer += nsamples*2*2;
223b71206888 Initial import
thib
parents:
diff changeset
676 } else while(nsamples--) {
223b71206888 Initial import
thib
parents:
diff changeset
677 signed int sample = scale(*left_ch++);
223b71206888 Initial import
thib
parents:
diff changeset
678 write_data[write_pointer++] = sample & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
679 write_data[write_pointer++] = (sample>>8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
680 if (nchannels == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
681 sample = scale(*right_ch++);
223b71206888 Initial import
thib
parents:
diff changeset
682 }
223b71206888 Initial import
thib
parents:
diff changeset
683 write_data[write_pointer++] = sample & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
684 write_data[write_pointer++] = (sample>>8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
685 }
223b71206888 Initial import
thib
parents:
diff changeset
686 if (write_pointer >= write_data_len) return MAD_FLOW_IGNORE;
223b71206888 Initial import
thib
parents:
diff changeset
687 else return MAD_FLOW_CONTINUE;
223b71206888 Initial import
thib
parents:
diff changeset
688 }
223b71206888 Initial import
thib
parents:
diff changeset
689
223b71206888 Initial import
thib
parents:
diff changeset
690 MP3FILE::MP3FILE(FILE* stream, int len) {
223b71206888 Initial import
thib
parents:
diff changeset
691 pimpl = new MP3FILE_impl(stream);
223b71206888 Initial import
thib
parents:
diff changeset
692 if (pimpl->status == MP3FILE_impl::DONE) {
223b71206888 Initial import
thib
parents:
diff changeset
693 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
694 pimpl = 0;
223b71206888 Initial import
thib
parents:
diff changeset
695 fclose(stream);
223b71206888 Initial import
thib
parents:
diff changeset
696 return;
223b71206888 Initial import
thib
parents:
diff changeset
697 }
223b71206888 Initial import
thib
parents:
diff changeset
698 wavinfo.SamplingRate = pimpl->decoder.sync->synth.pcm.samplerate;
223b71206888 Initial import
thib
parents:
diff changeset
699 wavinfo.Channels = 2;
223b71206888 Initial import
thib
parents:
diff changeset
700 wavinfo.DataBits = 16;
223b71206888 Initial import
thib
parents:
diff changeset
701 }
223b71206888 Initial import
thib
parents:
diff changeset
702 MP3FILE::~MP3FILE() {
223b71206888 Initial import
thib
parents:
diff changeset
703 if (pimpl) {
223b71206888 Initial import
thib
parents:
diff changeset
704 FILE* s = pimpl->stream;
223b71206888 Initial import
thib
parents:
diff changeset
705 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
706 fclose(s);
223b71206888 Initial import
thib
parents:
diff changeset
707 }
223b71206888 Initial import
thib
parents:
diff changeset
708 pimpl = 0;
223b71206888 Initial import
thib
parents:
diff changeset
709 }
223b71206888 Initial import
thib
parents:
diff changeset
710 int MP3FILE::Read(char* buf, int blksize, int blklen) {
223b71206888 Initial import
thib
parents:
diff changeset
711 if (pimpl == 0) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
712 pimpl->write_data = buf;
223b71206888 Initial import
thib
parents:
diff changeset
713 pimpl->write_data_len = blksize*blklen;
223b71206888 Initial import
thib
parents:
diff changeset
714 pimpl->write_pointer = 0;
223b71206888 Initial import
thib
parents:
diff changeset
715 do {
223b71206888 Initial import
thib
parents:
diff changeset
716 pimpl->run();
223b71206888 Initial import
thib
parents:
diff changeset
717 } while(pimpl->status != MP3FILE_impl::DONE && pimpl->write_pointer < pimpl->write_data_len);
223b71206888 Initial import
thib
parents:
diff changeset
718 return pimpl->write_pointer / blksize;
223b71206888 Initial import
thib
parents:
diff changeset
719 }
223b71206888 Initial import
thib
parents:
diff changeset
720 void MP3FILE::Seek(int count) {
223b71206888 Initial import
thib
parents:
diff changeset
721 FILE* stream = pimpl->stream;
223b71206888 Initial import
thib
parents:
diff changeset
722 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
723 fseek(stream,0,0);
223b71206888 Initial import
thib
parents:
diff changeset
724 pimpl = new MP3FILE_impl(stream);
223b71206888 Initial import
thib
parents:
diff changeset
725 if (pimpl->status == MP3FILE_impl::DONE) {
223b71206888 Initial import
thib
parents:
diff changeset
726 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
727 pimpl = 0;
223b71206888 Initial import
thib
parents:
diff changeset
728 fclose(stream);
223b71206888 Initial import
thib
parents:
diff changeset
729 return;
223b71206888 Initial import
thib
parents:
diff changeset
730 }
223b71206888 Initial import
thib
parents:
diff changeset
731 int blksize = 1;
223b71206888 Initial import
thib
parents:
diff changeset
732 blksize *= wavinfo.Channels * (wavinfo.DataBits/8);
223b71206888 Initial import
thib
parents:
diff changeset
733 pimpl->write_data = 0;
223b71206888 Initial import
thib
parents:
diff changeset
734 pimpl->write_data_len = count * blksize;
223b71206888 Initial import
thib
parents:
diff changeset
735 pimpl->write_pointer = 0;
223b71206888 Initial import
thib
parents:
diff changeset
736 do {
223b71206888 Initial import
thib
parents:
diff changeset
737 pimpl->run();
223b71206888 Initial import
thib
parents:
diff changeset
738 } while(pimpl->status != MP3FILE_impl::DONE && pimpl->write_pointer < pimpl->write_data_len);
223b71206888 Initial import
thib
parents:
diff changeset
739 return;
223b71206888 Initial import
thib
parents:
diff changeset
740 }
223b71206888 Initial import
thib
parents:
diff changeset
741 #elif USE_SMPEG
223b71206888 Initial import
thib
parents:
diff changeset
742 #include<smpeg/smpeg.h>
223b71206888 Initial import
thib
parents:
diff changeset
743
223b71206888 Initial import
thib
parents:
diff changeset
744 struct MP3FILE_impl {
223b71206888 Initial import
thib
parents:
diff changeset
745 SMPEG* info;
223b71206888 Initial import
thib
parents:
diff changeset
746 FILE* stream;
223b71206888 Initial import
thib
parents:
diff changeset
747 MP3FILE_impl(FILE*);
223b71206888 Initial import
thib
parents:
diff changeset
748 };
223b71206888 Initial import
thib
parents:
diff changeset
749
223b71206888 Initial import
thib
parents:
diff changeset
750 MP3FILE_impl::MP3FILE_impl(FILE* _stream) {
223b71206888 Initial import
thib
parents:
diff changeset
751 stream = _stream;
223b71206888 Initial import
thib
parents:
diff changeset
752 info = SMPEG_new_descr(fileno(stream), NULL, 0);
223b71206888 Initial import
thib
parents:
diff changeset
753 fprintf(stderr,"mp3 %x\n",info);
223b71206888 Initial import
thib
parents:
diff changeset
754 if (info && SMPEG_error(info) ) info = 0;
223b71206888 Initial import
thib
parents:
diff changeset
755 SMPEG_enableaudio(info, 0);
223b71206888 Initial import
thib
parents:
diff changeset
756 SMPEG_enableaudio(info, 1);
223b71206888 Initial import
thib
parents:
diff changeset
757 SMPEG_play(info);
223b71206888 Initial import
thib
parents:
diff changeset
758 }
223b71206888 Initial import
thib
parents:
diff changeset
759
223b71206888 Initial import
thib
parents:
diff changeset
760 MP3FILE::MP3FILE(FILE* stream, int len) {
223b71206888 Initial import
thib
parents:
diff changeset
761 pimpl = new MP3FILE_impl(stream);
223b71206888 Initial import
thib
parents:
diff changeset
762 if (pimpl->info == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
763 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
764 fclose(stream);
223b71206888 Initial import
thib
parents:
diff changeset
765 return;
223b71206888 Initial import
thib
parents:
diff changeset
766 }
223b71206888 Initial import
thib
parents:
diff changeset
767 SDL_AudioSpec fmt;
223b71206888 Initial import
thib
parents:
diff changeset
768 SMPEG_wantedSpec(pimpl->info, &fmt);
223b71206888 Initial import
thib
parents:
diff changeset
769 wavinfo.SamplingRate = fmt.freq;
223b71206888 Initial import
thib
parents:
diff changeset
770 wavinfo.Channels = fmt.channels;
223b71206888 Initial import
thib
parents:
diff changeset
771 wavinfo.DataBits = (fmt.format == AUDIO_S8) ? 8:16;
223b71206888 Initial import
thib
parents:
diff changeset
772 }
223b71206888 Initial import
thib
parents:
diff changeset
773 MP3FILE::~MP3FILE() {
223b71206888 Initial import
thib
parents:
diff changeset
774 if (pimpl && pimpl->info) {
223b71206888 Initial import
thib
parents:
diff changeset
775 if (SMPEG_status(pimpl->info) == SMPEG_PLAYING) SMPEG_stop(pimpl->info);
223b71206888 Initial import
thib
parents:
diff changeset
776 SMPEG_delete(pimpl->info);
223b71206888 Initial import
thib
parents:
diff changeset
777 }
223b71206888 Initial import
thib
parents:
diff changeset
778 if (pimpl) {
223b71206888 Initial import
thib
parents:
diff changeset
779 fclose(pimpl->stream);
223b71206888 Initial import
thib
parents:
diff changeset
780 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
781 pimpl = 0;
223b71206888 Initial import
thib
parents:
diff changeset
782 }
223b71206888 Initial import
thib
parents:
diff changeset
783 }
223b71206888 Initial import
thib
parents:
diff changeset
784 int MP3FILE::Read(char* buf, int blksize, int blklen) {
223b71206888 Initial import
thib
parents:
diff changeset
785 if (pimpl == 0 || pimpl->info == 0) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
786 int r = SMPEG_playAudio(pimpl->info, (Uint8*)buf, blksize*blklen);
223b71206888 Initial import
thib
parents:
diff changeset
787 if (r <= 0) { // end of file
223b71206888 Initial import
thib
parents:
diff changeset
788 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
789 }
223b71206888 Initial import
thib
parents:
diff changeset
790 return r / blksize;
223b71206888 Initial import
thib
parents:
diff changeset
791 }
223b71206888 Initial import
thib
parents:
diff changeset
792 void MP3FILE::Seek(int count) {
223b71206888 Initial import
thib
parents:
diff changeset
793 if (pimpl == 0 || pimpl->info == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
794 SMPEG_stop(pimpl->info);
223b71206888 Initial import
thib
parents:
diff changeset
795 SMPEG_rewind(pimpl->info);
223b71206888 Initial import
thib
parents:
diff changeset
796 SMPEG_play(pimpl->info);
223b71206888 Initial import
thib
parents:
diff changeset
797 count /= 4;
223b71206888 Initial import
thib
parents:
diff changeset
798 count *= 4; // reduce noise; possibly SMPEG error
223b71206888 Initial import
thib
parents:
diff changeset
799 char* d = new char[count*channels*2];
223b71206888 Initial import
thib
parents:
diff changeset
800 Read(d,count,channels*2);
223b71206888 Initial import
thib
parents:
diff changeset
801 delete[] d;
223b71206888 Initial import
thib
parents:
diff changeset
802 return;
223b71206888 Initial import
thib
parents:
diff changeset
803 }
223b71206888 Initial import
thib
parents:
diff changeset
804 #else /* SMPEG */
223b71206888 Initial import
thib
parents:
diff changeset
805 MP3FILE::MP3FILE(FILE* stream, int len) {pimpl = 0;}
223b71206888 Initial import
thib
parents:
diff changeset
806 MP3FILE::~MP3FILE(){}
223b71206888 Initial import
thib
parents:
diff changeset
807 void MP3FILE::Seek(int count){}
223b71206888 Initial import
thib
parents:
diff changeset
808 int MP3FILE::Read(char* buf, int blksize, int blklen){return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
809 #endif /* SMPEG */