-
Notifications
You must be signed in to change notification settings - Fork 3
/
mus_mp3.cpp
292 lines (210 loc) · 4.98 KB
/
mus_mp3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* 3dyne Legacy Engine GPL Source Code
*
* Copyright (C) 1996-2012 Matthias C. Berger & Simon Berger.
*
* This file is part of the 3dyne Legacy Engine GPL Source Code ("3dyne Legacy
* Engine Source Code").
*
* 3dyne Legacy Engine Source Code is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* 3dyne Legacy Engine Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* 3dyne Legacy Engine Source Code. If not, see
* <http://www.gnu.org/licenses/>.
*
* In addition, the 3dyne Legacy Engine Source Code is also subject to certain
* additional terms. You should have received a copy of these additional terms
* immediately following the terms and conditions of the GNU General Public
* License which accompanied the 3dyne Legacy Engine Source Code.
*
* Contributors:
* Matthias C. Berger ([email protected]) - initial API and implementation
* Simon Berger ([email protected]) - initial API and implementation
*/
// mus_mp3.c
#include "interfaces.h"
//#include "mpglib/mpg123.h"
//#include "mpglib/mpglib.h"
#define MUS_FIFOSIZE ( 256 * 256 )
static int mus_uptr, mus_size;
static int mus_new = 0;
static res_mp3_cache_t *mus_cache;
//static struct mpstr mus_mp;
static FILE *dh;
static char mus_fifo[MUS_FIFOSIZE];
static unsigned int fr, fw;
static int mp3_hasdata;
void MUS_FifoInit()
{
fr = fw = 0;
}
int MUS_FifoValidSize()
{
unsigned int v;
if( fr > fw )
{
__warning( "fr > fw. flatting fifo ...\n" );
MUS_FifoInit();
return 0;
}
v = fw - fr;
return v;
}
void MUS_FifoWrite( char *buf, int size )
{
int v, f;
int i;
if( fr > fw )
{
__warning( "fr > fw. flatting fifo ...\n" );
MUS_FifoInit();
return ;
}
v = MUS_FifoValidSize();
f = MUS_FIFOSIZE - v;
printf( "free in fifo: %d\n", f );
if( size > f )
{
__warning( "fifo overrun\n" );
return;
}
for( i = 0; i < size; i++ )
{
mus_fifo[(i + fw)%MUS_FIFOSIZE] = buf[i];
}
fw += size;
printf( "fw: %d\n", fw );
}
void MUS_FifoRead( char *buf, int size )
{
int v;
int i;
v = MUS_FifoValidSize();
if( size > v )
{
__warning( "fifo underrun\n" );
return;
}
for( i = 0; i < size; i++ )
{
buf[i] = mus_fifo[(i+fw)%MUS_FIFOSIZE];
}
fr += size;
}
void MUS_StartUp()
{
mus_cache = NULL;
// InitMP3( &mus_mp );
}
void MUS_ShutDown()
{
// ExitMP3( &mus_mp );
}
int MUS_Mp3Unpack( char *buf, int size )
{
int rsize;
if( !mus_cache )
{
return 0;
}
if( mus_cache->size - mus_uptr < size )
{
rsize = mus_cache->size - mus_uptr;
} else
rsize = size;
fprintf( stderr, "rsize: %d\n", rsize );
memcpy( buf, &mus_cache->data[mus_uptr], rsize );
// fwrite( buf, rsize, 1, dh );
fprintf( stderr, "ready.\n" );
mus_uptr += rsize;
return rsize;
}
void MUS_PlayByName( const char *name )
{
char bla[8192];
g_resource_t *res;
MUS_FifoInit();
// ExitMP3( &mus_mp );
//InitMP3( &mus_mp );
res = G_ResourceSearch( g_rs, name );
mus_cache = (res_mp3_cache_t*)res->res_cache;
mus_new = 1;
mus_uptr = 0;
mus_size = 0;
memset( bla, 0, 8192 );
dh = fopen( name, "wb" );
mp3_hasdata = 0;
}
void MUS_PaintStereo16( samplepair_t *pb, int frames )
{
#if 0
int i;
int ret;
char out[8192], obuf[8192], buf[16384];
int size;
int isize;
printf( "======\n" );
if( frames != 2048 )
{
__warning( "framenum != 2048\n" );
return;
}
size = frames * 4;
for( ;; )
{
int v;
v = MUS_FifoValidSize();
printf( "valid in fifo: %d\n", v );
if( v >= size )
{
printf( "corping fifo\n" );
MUS_FifoRead( out, 8192 );
printf( "valid in fifo: %d\n", MUS_FifoValidSize() );
break;
} else
{
// printf( "decoding ...\n" );
if( !mp3_hasdata )
{
printf( "first write\n" );
isize = MUS_Mp3Unpack( buf, 16384 );
printf( "isize: %d\n", isize );
ret = decodeMP3( &mus_mp, buf, isize, obuf, 8192, &mus_size );
printf( "mus_size: %d\n", mus_size );
MUS_FifoWrite( obuf, mus_size );
mp3_hasdata = 1;
} else
{
ret = decodeMP3( &mus_mp, NULL, 0, obuf, 8192, &mus_size );
if( ret != MP3_OK )
{
fprintf( stderr, "mp3 needs data\n" );
isize = MUS_Mp3Unpack( buf, 16384 );
fprintf( stderr, "isize: %d ===========================\n", isize );
ret = decodeMP3( &mus_mp, buf, isize, obuf, 8192, &mus_size );
}
MUS_FifoWrite( obuf, mus_size );
}
}
}
printf( "mus_size: %d\n", size );
fwrite( out, size, 1, dh );
#if 1
for( i = 0; i < frames; i++ )
{
pb[i].right += ( short )(out[i*2]);
// out = *(out[2]);
pb[i].left += ( short )(out[i*2 + 2]);
// out = *out[2];
}
#endif
#endif
}