Debian Codegraph @

rplayd/native.c

raw symbols ◀ ▶ SCIP
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 #include <string.h>
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(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
47 static int native_to_s8(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
48 static int native_to_u8(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
49 static int native_to_s16(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
50 static int native_to_u16(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
51 static int native_to_g721(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
52 static int native_to_g723_3(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
53 static int native_to_g723_5(SPOOL *sp, char *native_buf, int nsamples, int nchannels);
54 static int native_to_gsm(SPOOL *sp, 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(char *native_buf, int nsamples, int nchannels)
98 {
99 memset(native_buf, 0, nsamples * nchannels * 2); /* 2 == 16-bit */
100 }
101
102
103 void
•104 level(char *native_buf, int nsamples, int nchannels)
105 {
106 short *p = (short *) native_buf, sample;
107 int n = nsamples * nchannels;
108 long left_max = 0, right_max = 0;
109
110 /* Optimize the cases for stereo and mono output. */
111
112 if (nchannels == 2) /* stereo */
113 {
114 while (n--)
115 {
116 #ifdef FAKE_VOLUME
117 sample = (*p++ >> 7);
118 #else
119 sample = (*p++ * rplay_audio_volume) >> 14;
120 #endif
121 if (n & 1)
122 {
123 if (sample > right_max)
124 {
125 right_max = sample;
126 }
127 }
128 else
129 {
130 if (sample > left_max)
131 {
132 left_max = sample;
133 }
134 }
135 }
136 }
137 else
138 /* mono */
139 {
140 while (n--)
141 {
142 #ifdef FAKE_VOLUME
143 sample = (*p++ >> 7);
144 #else
145 sample = (*p++ * rplay_audio_volume) >> 14;
146 #endif
147 if (sample > left_max)
148 {
149 left_max = sample;
150 }
151 }
152 right_max = left_max;
153 }
154
155 rplay_audio_left_level = left_max;
156 rplay_audio_right_level = right_max;
157 }
158
159
160 #ifdef FAKE_VOLUME
161 /* Simulate hardware volume control. */
162 void
•163 fake_volume(char *native_buf, int nsamples, int nchannels)
164 {
165 short *p = (short *) native_buf;
166 int n = nsamples * nchannels;
167
168 while (n--)
169 {
170 *p++ = (*p * rplay_audio_volume) >> 7;
171 }
172 }
173 #endif /* FAKE_VOLUME */
174
175
176 /* Move `sp' to the next sample. */
177 #define next_sample(sp, s) \
178 { \
179 int sample_delta; \
180 \
181 sample_delta = sp->sample_index; \
182 sp->sample_index += sp->sample_factor; \
183 sample_delta = sp->sample_index - sample_delta; \
184 sp->offset = sample_delta * s->output_sample_size; \
185 if (s->samples > 0 \
186 && sp->sample_index >= s->samples \
187 && (s->type == SOUND_FILE || sp->si->eof)) \
188 { \
189 return 0; \
190 } \
191 }
192
193 #define check_buffers(sp) \
194 { \
195 /* Compute the next offset. */ \
196 sp->offset = sp->ptr - sp->ptr_end; \
197 \
198 /* Try to use curr_buffer's next buffer. */ \
199 if (sp->curr_buffer->next && sp->curr_buffer->next->nbytes > 0) \
200 { \
201 BUFFER *b = sp->curr_buffer; \
202 \
203 /* report (REPORT_DEBUG, "* using curr_buffer->next\n"); */ \
204 \
205 sp->curr_buffer = sp->curr_buffer->next; \
206 sp->ptr = (unsigned char *)sp->curr_buffer->buf; \
207 sp->ptr_end = (unsigned char *)sp->curr_buffer->buf \
208 + sp->curr_buffer->nbytes; \
209 \
210 buffer_destroy (b); \
211 } \
212 /* Try to use next_buffer. */ \
213 else if (sp->next_buffer) \
214 { \
215 BUFFER *b = sp->curr_buffer; \
216 \
217 /* report (REPORT_DEBUG, "* using next_buffer\n"); */ \
218 \
219 sp->curr_buffer = sp->next_buffer; \
220 sp->ptr = (unsigned char *)sp->curr_buffer->buf; \
221 sp->ptr_end = (unsigned char *)sp->curr_buffer->buf \
222 + sp->curr_buffer->nbytes; \
223 \
224 sp->next_buffer = NULL; \
225 \
226 buffer_dealloc (b, 0); \
227 } \
228 /* No more buffers are available. */ \
229 else \
230 { \
231 /* report (REPORT_DEBUG, "* no buffers!\n"); */ \
232 \
233 buffer_dealloc (sp->curr_buffer, 0); \
234 sp->curr_buffer = NULL; \
235 \
236 break; \
237 } \
238 }
239
•240
241 //XXX org: FUNC (SPOOL *sp, short *native_buf, int nsamples, int nchannels)
242
243 /* Return 0 when the sound is over, otherwise return 1. */
244 #define x_to_native(FUNC, SAMPLE_TO_NATIVE) \
245 static int \
•246 FUNC (SPOOL *sp, char *native_buf, int nsamples, int nchannels) \
247 { \
248 SOUND *curr_sound; \
249 short new_volume; \
250 int sample_count = 0; \
251 int curr_channel; \
252 short linear; \
253 short orig_linear; \
254 short *buf = (short *)native_buf; \
255 \
256 /* Find the sound that being played. */ \
257 curr_sound = sp->sound[sp->curr_sound]; \
258 \
259 /* Compute the new volume for the sound using priorities. */ \
260 new_volume = sp->curr_attrs->volume; \
261 if (spool_prio && spool_nplaying > 1) \
262 { \
263 new_volume -= (spool_prio - sp->rp->priority) >> 1; \
264 new_volume = MAX(new_volume, RPLAY_MIN_VOLUME); \
265 } \
266 \
267 /* do oversampling */ \
268 if (sp->sample_factor < 1) \
269 { \
270 while (sp->curr_buffer && sample_count < nsamples) \
271 { \
272 sp->ptr += sp->offset; \
273 if (sp->ptr < sp->ptr_end) \
274 { \
275 for (curr_channel = 0; curr_channel < nchannels; curr_channel++) \
276 { \
277 if (curr_channel < curr_sound->channels) \
278 { \
279 SAMPLE_TO_NATIVE; \
280 orig_linear = linear; \
281 linear = sp->oversample[curr_channel]; \
282 sp->oversample[curr_channel] += sp->oversample_inc[curr_channel]; \
283 if (sp->sample_index + sp->sample_factor > sp->offset + 1) \
284 { \
285 sp->oversample_inc[curr_channel] = (orig_linear - \
286 sp->oversample[curr_channel]) * sp->sample_factor; \
287 } \
288 } \
289 *buf++ += ((linear * new_volume) >> 7); \
290 } \
291 sample_count++; \
292 next_sample (sp, curr_sound); \
293 } \
294 else \
295 { \
296 check_buffers (sp); \
297 } \
298 } \
299 } \
300 /* read the sound as-is */ \
301 else \
302 { \
303 while (sp->curr_buffer && sample_count < nsamples) \
304 { \
305 sp->ptr += sp->offset; \
306 if (sp->ptr < sp->ptr_end) \
307 { \
308 for (curr_channel = 0; curr_channel < nchannels; curr_channel++) \
309 { \
310 if (curr_channel < curr_sound->channels) \
311 { \
312 SAMPLE_TO_NATIVE; \
313 } \
314 *buf++ += ((linear * new_volume) >> 7); \
315 } \
316 sample_count++; \
317 next_sample (sp, curr_sound); \
318 } \
319 else \
320 { \
321 check_buffers (sp); \
322 } \
323 } \
324 } \
325 \
326 return 1; \
327 }
328
329 /* ulaw */
330 x_to_native
331 (
332 ulaw_to_native,
333 linear = ulaw_to_linear(*(sp->ptr + curr_channel))
334 )
335
336 /* signed 8-bit */
337 x_to_native
338 (
339 s8_to_native,
340 linear = (char) *(sp->ptr + curr_channel);
341 linear <<= 8
342 )
343
344 /* unsigned 8-bit */
345 x_to_native
346 (
347 u8_to_native,
348 linear = (unsigned char) *(sp->ptr + curr_channel) ^ 0x80;
349 linear <<= 8
350 )
351
352 /* signed 16-bit big-endian */
353 x_to_native
354 (
355 big_s16_to_native,
356 linear = (short) (sp->ptr[curr_channel << 1] << 8 | sp->ptr[(curr_channel << 1) + 1])
357 )
358
359 /* signed 16-bit little-endian */
360 x_to_native
361 (
362 little_s16_to_native,
363 linear = (short) (sp->ptr[(curr_channel << 1) + 1] << 8 | sp->ptr[curr_channel << 1])
364 )
365
366 /* unsigned 16-bit big-endian */
367 x_to_native
368 (
369 big_u16_to_native,
370 linear = (short) (sp->ptr[curr_channel << 1] << 8 | sp->ptr[(curr_channel << 1) + 1]) ^ 0x8000;
371 )
372
373 /* unsigned 16-bit little-endian */
374 x_to_native
375 (
376 little_u16_to_native,
377 linear = (short) (sp->ptr[(curr_channel << 1) + 1] << 8 | sp->ptr[curr_channel << 1]) ^ 0x8000;
378 )
379
380 /* native to native */
381 x_to_native
382 (
383 native_to_native,
384 linear = (*((short *) sp->ptr + curr_channel))
385 )
386
•387 static int
388 native_to_ulaw(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
389 {
390 short *from = (short *) native_buf;
391 char *to = (char *) native_buf;
392 int n = nsamples * nchannels;
393
394 while (n--)
395 {
396 *to++ = linear_to_ulaw(*from++);
397 }
398
399 return 0;
400 }
401
402 static int
•403 native_to_s8(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
404 {
405 short *from = (short *) native_buf;
406 char *to = (char *) native_buf;
407 int n = nsamples * nchannels;
408
409 while (n--)
410 {
411 *to++ = (*from++ >> 8);
412 }
413
414 return 0;
415 }
416
417 static int
•418 native_to_u8(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
419 {
420 short *from = (short *) native_buf;
421 char *to = (char *) native_buf;
422 int n = nsamples * nchannels;
423
424 while (n--)
425 {
426 *to++ = (*from++ >> 8) ^ 0x80;
427 }
428
429 return 0;
430 }
431
432 static int
•433 native_to_s16(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
434 {
435 /* The native audio buffer is already signed 16-bit. */
436 return 0;
437 }
438
439 static int
•440 native_to_u16(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
441 {
442 short *from = (short *) native_buf;
443 short *to = (short *) native_buf;
444 int n = nsamples * nchannels;
445
446 while (n--)
447 {
448 *to++ = *from++ ^ 0x8000;
449 }
450
451 return 0;
452 }
453
454 #ifdef HAVE_ADPCM
455 static int
•456 native_to_g721(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
457 {
458 /* NOT SUPPORTED */
459 return 0;
460 }
461
462 static int
•463 native_to_g723_3(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
464 {
465 /* NOT SUPPORTED */
466 return 0;
467 }
468
469 static int
•470 native_to_g723_5(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
471 {
472 /* NOT SUPPORTED */
473 return 0;
474 }
475 #endif /* HAVE_ADPCM */
476
477 #ifdef HAVE_GSM
478 static int
•479 native_to_gsm(SPOOL *sp, char *native_buf, int nsamples, int nchannels)
480 {
481 /* NOT SUPPORTED */
482 return 0;
483 }
484 #endif /* HAVE_GSM */