1 | //mixer.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009 |
---|
5 | * |
---|
6 | * This file is part of libroardsp a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroardsp is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "libroardsp.h" |
---|
26 | |
---|
27 | int roar_mix_pcm (void * output, int bits, void ** input, int samples) { |
---|
28 | switch (bits) { |
---|
29 | case 8: return roar_mix_pcm_8bit (output, (int8_t **)input, samples); break; |
---|
30 | case 16: return roar_mix_pcm_16bit(output, (int16_t **)input, samples); break; |
---|
31 | case 24: return roar_mix_pcm_24bit(output, input, samples); break; |
---|
32 | case 32: return roar_mix_pcm_32bit(output, (int32_t **)input, samples); break; |
---|
33 | default: return -1; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | int roar_mix_pcm_8bit (int8_t * output, int8_t ** input, int samples) { |
---|
38 | int i, s; |
---|
39 | register int c; |
---|
40 | |
---|
41 | for (s = 0; s < samples; s++) { |
---|
42 | c = 0; |
---|
43 | |
---|
44 | for (i = 0; input[i] != NULL; i++) |
---|
45 | c += input[i][s]; |
---|
46 | |
---|
47 | if ( c > 127 ) |
---|
48 | c = 127; |
---|
49 | else if ( c < -128 ) |
---|
50 | c = -128; |
---|
51 | output[s] = (char)c; |
---|
52 | } |
---|
53 | |
---|
54 | return 0; |
---|
55 | } |
---|
56 | |
---|
57 | int roar_mix_pcm_16bit (int16_t * output, int16_t ** input, int samples) { |
---|
58 | int i, s; |
---|
59 | register int_least32_t c; |
---|
60 | |
---|
61 | #ifdef DEBUG |
---|
62 | for (i = 0; input[i] != NULL; i++) |
---|
63 | ROAR_DBG("mix_clients_16bit(*): input[%i] = %p", i, input[i]); |
---|
64 | #endif |
---|
65 | |
---|
66 | for (s = 0; s < samples; s++) { |
---|
67 | c = 0; |
---|
68 | for (i = 0; input[i] != NULL; i++) { |
---|
69 | c += input[i][s]; |
---|
70 | } |
---|
71 | |
---|
72 | if ( c > 32767 ) |
---|
73 | c = 32767; |
---|
74 | else if ( c < -32768 ) |
---|
75 | c = -32768; |
---|
76 | |
---|
77 | output[s] = c; |
---|
78 | } |
---|
79 | |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | int roar_mix_pcm_24bit (void * output, void ** input, int samples) { |
---|
84 | return -1; |
---|
85 | } |
---|
86 | |
---|
87 | int roar_mix_pcm_32bit (int32_t * output, int32_t ** input, int samples) { |
---|
88 | #ifdef ROAR_NATIVE_INT64 |
---|
89 | int i, s; |
---|
90 | ROAR_NATIVE_INT64 c; |
---|
91 | |
---|
92 | for (s = 0; s < samples; s++) { |
---|
93 | c = 0; |
---|
94 | |
---|
95 | for (i = 0; input[i]; i++) |
---|
96 | c += input[i][s]; |
---|
97 | |
---|
98 | if ( c > 21474836487LL ) |
---|
99 | c = 2147483647LL; |
---|
100 | else if ( c < -2147483648LL ) |
---|
101 | c = -2147483648LL; |
---|
102 | output[s] = (int32_t)c; |
---|
103 | } |
---|
104 | |
---|
105 | return 0; |
---|
106 | #else |
---|
107 | return -1; |
---|
108 | #endif |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | //ll |
---|