Debian Codegraph @

rplayd/native.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* $Id: native.c,v 1.4 1999/03/10 07:58:03 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "spool.h"
30 #include "sound.h"
31 #include "buffer.h"
32 #include "rplayd.h"
33 #include "native.h"
34 #include "ulaw.h"
35
36 /* Native means 16-bit signed. */
37
38 static int ulaw_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
39 static int s8_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
40 static int u8_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
41 static int big_s16_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
42 static int big_u16_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
43 static int little_s16_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
44 static int little_u16_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
45 static int native_to_native( /* SPOOL *sp, char *native_buf, int nsamples, int nchannels */ );
46 static int native_to_ulaw( /* char *native_buf, int nsamples, int nchannels */ );
47 static int native_to_s8( /* char *native_buf, int nsamples, int nchannels */ );
48 static int native_to_u8( /* char *native_buf, int nsamples, int nchannels */ );
49 static int native_to_s16( /* char *native_buf, int nsamples, int nchannels */ );
50 static int native_to_u16( /* char *native_buf, int nsamples, int nchannels */ );
51 static int native_to_g721( /* char *native_buf, int nsamples, int nchannels */ );
52 static int native_to_g723_3( /* char *native_buf, int nsamples, int nchannels */ );
53 static int native_to_g723_5( /* char *native_buf, int nsamples, int nchannels */ );
54 static int native_to_gsm( /* char *native_buf, int nsamples, int nchannels */ );
55
56 /* Setup the to and from native table. */
57 NATIVE_TABLE native_table[] =
58 {
59 {
60 {0, 0},
61 {0, 0}},
62 {
63 {s8_to_native, s8_to_native},
64 {native_to_s8, native_to_s8}},
65 {
66 {u8_to_native, u8_to_native},
67 {native_to_u8, native_to_u8}},
68 {
69 {big_s16_to_native, little_s16_to_native},
70 {native_to_s16, native_to_s16}},
71 {
72 {big_u16_to_native, little_u16_to_native},
73 {native_to_u16, native_to_u16}},
74 {
75 {ulaw_to_native, ulaw_to_native},
76 {native_to_ulaw, native_to_ulaw}},
77 #ifdef HAVE_ADPCM
78 {
79 {native_to_native, native_to_native},
80 {native_to_g721, native_to_g721}},
81 {
82 {native_to_native, native_to_native},
83 {native_to_g723_3, native_to_g723_5}},
84 {
85 {native_to_native, native_to_native},
86 {native_to_g723_3, native_to_g723_5}},
87 #endif /* HAVE_ADPCM */
88 #ifdef HAVE_GSM
89 {
90 {native_to_native, native_to_native},
91 {native_to_gsm, native_to_gsm}},
92 #endif /* HAVE_GSM */
93 };
94
95 /* Initialize the native audio buffer. */
96 void
97 zero_native(native_buf, nsamples, nchannels)
98 char *native_buf;
99 int nsamples;
100 int nchannels;
101 {
102 memset(native_buf, 0, nsamples * nchannels * 2); /* 2 == 16-bit */
103 }
104
105
106 void
107 level(native_buf, nsamples, nchannels)
108 char *native_buf;
109 int nsamples;
110 int nchannels;
111 {
112 short *p = (short *) native_buf, sample;
113 int n = nsamples * nchannels;
114 long left_max = 0, right_max = 0;
115
116 /* Optimize the cases for stereo and mono output. */
117
118 if (nchannels == 2) /* stereo */
119 {
120 while (n--)
121 {
122 #ifdef FAKE_VOLUME
123 sample = (*p++ >> 7);
124 #else
125 sample = (*p++ * rplay_audio_volume) >> 14;
126 #endif
127 if (n & 1)
128 {
129 if (sample > right_max)
130 {
131 right_max = sample;
132 }
133 }
134 else
135 {
136 if (sample > left_max)
137 {
138 left_max = sample;
139 }
140 }
141 }
142 }
143 else
144 /* mono */
145 {
146 while (n--)
147 {
148 #ifdef FAKE_VOLUME
149 sample = (*p++ >> 7);
150 #else
151 sample = (*p++ * rplay_audio_volume) >> 14;
152 #endif
153 if (sample > left_max)
154 {
155 left_max = sample;
156 }
157 }
158 right_max = left_max;
159 }
160
161 rplay_audio_left_level = left_max;
162 rplay_audio_right_level = right_max;
163 }
164
165
166 #ifdef FAKE_VOLUME
167 /* Simulate hardware volume control. */
168 void
169 fake_volume(native_buf, nsamples, nchannels)
170 char *native_buf;
171 int nsamples;
172 int nchannels;
173 {
174 short *p = (short *) native_buf;
175 int n = nsamples * nchannels;
176
177 while (n--)
178 {
179 *p++ = (*p * rplay_audio_volume) >> 7;
180 }
181 }
182 #endif /* FAKE_VOLUME */
183
184
185 /* Move `sp' to the next sample. */
186 #define next_sample(sp, s) \
187 { \
188 int sample_delta; \
189 \
190 sample_delta = sp->sample_index; \
191 sp->sample_index += sp->sample_factor; \
192 sample_delta = sp->sample_index - sample_delta; \
193 sp->offset = sample_delta * s->output_sample_size; \
194 if (s->samples > 0 \
195 && sp->sample_index >= s->samples \
196 && (s->type == SOUND_FILE || sp->si->eof)) \
197 { \
198 return 0; \
199 } \
200 }
201
202 #define check_buffers(sp) \
203 { \
204 /* Compute the next offset. */ \
205 sp->offset = sp->ptr - sp->ptr_end; \
206 \
207 /* Try to use curr_buffer's next buffer. */ \
208 if (sp->curr_buffer->next && sp->curr_buffer->next->nbytes > 0) \
209 { \
210 BUFFER *b = sp->curr_buffer; \
211 \
212 /* report (REPORT_DEBUG, "* using curr_buffer->next\n"); */ \
213 \
214 sp->curr_buffer = sp->curr_buffer->next; \
215 sp->ptr = (unsigned char *)sp->curr_buffer->buf; \
216 sp->ptr_end = (unsigned char *)sp->curr_buffer->buf \
217 + sp->curr_buffer->nbytes; \
218 \
219 buffer_destroy (b); \
220 } \
221 /* Try to use next_buffer. */ \
222 else if (sp->next_buffer) \
223 { \
224 BUFFER *b = sp->curr_buffer; \
225 \
226 /* report (REPORT_DEBUG, "* using next_buffer\n"); */ \
227 \
228 sp->curr_buffer = sp->next_buffer; \
229 sp->ptr = (unsigned char *)sp->curr_buffer->buf; \
230 sp->ptr_end = (unsigned char *)sp->curr_buffer->buf \
231 + sp->curr_buffer->nbytes; \
232 \
233 sp->next_buffer = NULL; \
234 \
235 buffer_dealloc (b, 0); \
236 } \
237 /* No more buffers are available. */ \
238 else \
239 { \
240 /* report (REPORT_DEBUG, "* no buffers!\n"); */ \
241 \
242 buffer_dealloc (sp->curr_buffer, 0); \
243 sp->curr_buffer = NULL; \
244 \
245 break; \
246 } \
247 }
248
249 /* Return 0 when the sound is over, otherwise return 1. */
250 #define x_to_native(FUNC, SAMPLE_TO_NATIVE) \
251 static int \
252 FUNC (sp, native_buf, nsamples, nchannels) \
253 SPOOL *sp; \
254 short *native_buf; \
255 int nsamples; \
256 int nchannels; \
257 { \
258 SOUND *curr_sound; \
259 short new_volume; \
260 int sample_count = 0; \
261 int curr_channel; \
262 short linear; \
263 short orig_linear; \
264 short *buf = (short *)native_buf; \
265 \
266 /* Find the sound that being played. */ \
267 curr_sound = sp->sound[sp->curr_sound]; \
268 \
269 /* Compute the new volume for the sound using priorities. */ \
270 new_volume = sp->curr_attrs->volume; \
271 if (spool_prio && spool_nplaying > 1) \
272 { \
273 new_volume -= (spool_prio - sp->rp->priority) >> 1; \
274 new_volume = MAX(new_volume, RPLAY_MIN_VOLUME); \
275 } \
276 \
277 /* do oversampling */ \
278 if (sp->sample_factor < 1) \
279 { \
280 while (sp->curr_buffer && sample_count < nsamples) \
281 { \
282 sp->ptr += sp->offset; \
283 if (sp->ptr < sp->ptr_end) \
284 { \
285 for (curr_channel = 0; curr_channel < nchannels; curr_channel++) \
286 { \
287 if (curr_channel < curr_sound->channels) \
288 { \
289 SAMPLE_TO_NATIVE; \
290 orig_linear = linear; \
291 linear = sp->oversample[curr_channel]; \
292 sp->oversample[curr_channel] += sp->oversample_inc[curr_channel]; \
293 if (sp->sample_index + sp->sample_factor > sp->offset + 1) \
294 { \
295 sp->oversample_inc[curr_channel] = (orig_linear - \
296 sp->oversample[curr_channel]) * sp->sample_factor; \
297 } \
298 } \
299 *buf++ += ((linear * new_volume) >> 7); \
300 } \
301 sample_count++; \
302 next_sample (sp, curr_sound); \
303 } \
304 else \
305 { \
306 check_buffers (sp); \
307 } \
308 } \
309 } \
310 /* read the sound as-is */ \
311 else \
312 { \
313 while (sp->curr_buffer && sample_count < nsamples) \
314 { \
315 sp->ptr += sp->offset; \
316 if (sp->ptr < sp->ptr_end) \
317 { \
318 for (curr_channel = 0; curr_channel < nchannels; curr_channel++) \
319 { \
320 if (curr_channel < curr_sound->channels) \
321 { \
322 SAMPLE_TO_NATIVE; \
323 } \
324 *buf++ += ((linear * new_volume) >> 7); \
325 } \
326 sample_count++; \
327 next_sample (sp, curr_sound); \
328 } \
329 else \
330 { \
331 check_buffers (sp); \
332 } \
333 } \
334 } \
335 \
336 return 1; \
337 }
338
339 /* ulaw */
340 x_to_native
341 (
342 ulaw_to_native,
343 linear = ulaw_to_linear(*(sp->ptr + curr_channel))
344 )
345
346 /* signed 8-bit */
347 x_to_native
348 (
349 s8_to_native,
350 linear = (char) *(sp->ptr + curr_channel);
351 linear <<= 8
352 )
353
354 /* unsigned 8-bit */
355 x_to_native
356 (
357 u8_to_native,
358 linear = (unsigned char) *(sp->ptr + curr_channel) ^ 0x80;
359 linear <<= 8
360 )
361
362 /* signed 16-bit big-endian */
363 x_to_native
364 (
365 big_s16_to_native,
366 linear = (short) (sp->ptr[curr_channel << 1] << 8 | sp->ptr[(curr_channel << 1) + 1])
367 )
368
369 /* signed 16-bit little-endian */
370 x_to_native
371 (
372 little_s16_to_native,
373 linear = (short) (sp->ptr[(curr_channel << 1) + 1] << 8 | sp->ptr[curr_channel << 1])
374 )
375
376 /* unsigned 16-bit big-endian */
377 x_to_native
378 (
379 big_u16_to_native,
380 linear = (short) (sp->ptr[curr_channel << 1] << 8 | sp->ptr[(curr_channel << 1) + 1]) ^ 0x8000;
381 )
382
383 /* unsigned 16-bit little-endian */
384 x_to_native
385 (
386 little_u16_to_native,
387 linear = (short) (sp->ptr[(curr_channel << 1) + 1] << 8 | sp->ptr[curr_channel << 1]) ^ 0x8000;
388 )
389
390 /* native to native */
391 x_to_native
392 (
393 native_to_native,
394 linear = (*((short *) sp->ptr + curr_channel))
395 )
396
397 static int
398 native_to_ulaw(native_buf, nsamples, nchannels)
399 char *native_buf;
400 int nsamples;
401 int nchannels;
402 {
403 short *from = (short *) native_buf;
404 char *to = (char *) native_buf;
405 int n = nsamples * nchannels;
406
407 while (n--)
408 {
409 *to++ = linear_to_ulaw(*from++);
410 }
411
412 return 0;
413 }
414
415 static int
416 native_to_s8(native_buf, nsamples, nchannels)
417 char *native_buf;
418 int nsamples;
419 int nchannels;
420 {
421 short *from = (short *) native_buf;
422 char *to = (char *) native_buf;
423 int n = nsamples * nchannels;
424
425 while (n--)
426 {
427 *to++ = (*from++ >> 8);
428 }
429
430 return 0;
431 }
432
433 static int
434 native_to_u8(native_buf, nsamples, nchannels)
435 char *native_buf;
436 int nsamples;
437 int nchannels;
438 {
439 short *from = (short *) native_buf;
440 char *to = (char *) native_buf;
441 int n = nsamples * nchannels;
442
443 while (n--)
444 {
445 *to++ = (*from++ >> 8) ^ 0x80;
446 }
447
448 return 0;
449 }
450
451 static int
452 native_to_s16(native_buf, nsamples, nchannels)
453 char *native_buf;
454 int nsamples;
455 int nchannels;
456 {
457 /* The native audio buffer is already signed 16-bit. */
458 return 0;
459 }
460
461 static int
462 native_to_u16(native_buf, nsamples, nchannels)
463 char *native_buf;
464 int nsamples;
465 int nchannels;
466 {
467 short *from = (short *) native_buf;
468 short *to = (short *) native_buf;
469 int n = nsamples * nchannels;
470
471 while (n--)
472 {
473 *to++ = *from++ ^ 0x8000;
474 }
475
476 return 0;
477 }
478
479 #ifdef HAVE_ADPCM
480 static int
481 native_to_g721(native_buf, nsamples, nchannels)
482 char *native_buf;
483 int nsamples;
484 int nchannels;
485 {
486 /* NOT SUPPORTED */
487 return 0;
488 }
489
490 static int
491 native_to_g723_3(native_buf, nsamples, nchannels)
492 char *native_buf;
493 int nsamples;
494 int nchannels;
495 {
496 /* NOT SUPPORTED */
497 return 0;
498 }
499
500 static int
501 native_to_g723_5(native_buf, nsamples, nchannels)
502 char *native_buf;
503 int nsamples;
504 int nchannels;
505 {
506 /* NOT SUPPORTED */
507 return 0;
508 }
509 #endif /* HAVE_ADPCM */
510
511 #ifdef HAVE_GSM
512 static int
513 native_to_gsm(native_buf, nsamples, nchannels)
514 char *native_buf;
515 int nsamples;
516 int nchannels;
517 {
518 /* NOT SUPPORTED */
519 return 0;
520 }
521 #endif /* HAVE_GSM */