source: roaraudio/libroardsp/convert.c @ 59:db97b72a53ca

Last change on this file since 59:db97b72a53ca was 59:db97b72a53ca, checked in by phi, 16 years ago

added another non-working resampling code

File size: 4.5 KB
Line 
1//convert.c:
2
3#include "libroar.h"
4
5int roar_conv_bits (void * out, void * in, int samples, int from, int to) {
6 if ( from == to ) {
7  if ( in == out )
8   return 0;
9
10  memcpy(out, in, samples * from / 8);
11  return 0;
12 }
13
14 if ( from ==  8 && to == 16 )
15  return roar_conv_bits_8to16(out, in, samples);
16
17 if ( from == 16 && to ==  8 )
18  return roar_conv_bits_16to8(out, in, samples);
19
20 return -1;
21}
22
23int roar_conv_bits_8to16 (void * out, void * in, int samples) {
24 char    * ip = (char   *)in;
25 int16_t * op = (int16_t*)out;
26 int i;
27
28 for (i = samples - 1; i >= 0; i--)
29  op[i] = ip[i] << 8;
30
31 return 0;
32}
33
34int roar_conv_bits_16to8 (void * out, void * in, int samples) {
35 int16_t * ip = (int16_t*)in;
36 char    * op = (char   *)out;
37 int i;
38
39 for (i = 0; i < samples; i++)
40  op[i] = ip[i] >> 8;
41
42 return 0;
43}
44
45
46int roar_conv_chans (void * out, void * in, int samples, int from, int to, int bits) {
47 if ( from == 1 ) {
48  if ( bits == 8 ) {
49   return roar_conv_chans_1ton8(out, in, samples, to);
50  } else if ( bits == 16 ) {
51   return roar_conv_chans_1ton16(out, in, samples, to);
52  } else {
53   return -1;
54  }
55 }
56
57 return -1;
58}
59
60int roar_conv_chans_1ton8  (void * out, void * in, int samples, int to) {
61 char * ip = (char*) in, * op = (char*) out;
62 int i;
63 int c;
64
65 for (i = samples - 1; i >= 0; i--)
66  for (c = to - 1; c >= 0; c--)
67   op[i*to + c] = ip[i];
68
69 return 0;
70}
71
72int roar_conv_chans_1ton16 (void * out, void * in, int samples, int to) {
73 int16_t * ip = (int16_t*) in, * op = (int16_t*) out;
74 int i;
75 int c;
76
77 for (i = samples - 1; i >= 0; i--)
78  for (c = to - 1; c >= 0; c--)
79   op[i*to + c] = ip[i];
80
81 return 0;
82}
83
84int roar_conv_rate (void * out, void * in, int samples, int from, int to, int bits, int channels) {
85 if ( bits == 8  )
86  return roar_conv_rate_8(out, in, samples, from, to, channels);
87
88 if ( bits == 16 )
89  return roar_conv_rate_16(out, in, samples, from, to, channels);
90
91 return -1;
92}
93
94int roar_conv_rate_8  (void * out, void * in, int samples, int from, int to, int channels) {
95 return -1;
96}
97
98int roar_conv_rate_16 (void * out, void * in, int samples, int from, int to, int channels) {
99 int16_t * ip = (int16_t*) in, * op = (int16_t*) out;
100 int i, j;
101 int max;
102 int teiler = -1;
103
104
105 if ( from > to ) {
106  return -1;
107 } else {
108  for (i = 8; i > 0; i--) {
109   if ( from * i == to ) {
110    teiler = i;
111    break;
112   }
113  }
114
115  if ( teiler == -1 )
116   return -1;
117
118  if ( channels == 2 ) {
119   max = (samples-1)/(2*teiler);
120
121   for (i = 0; i < max; i++) {
122    for (j = 0; j < teiler; j++) {
123     op[teiler*i*2     + j] = ip[i];
124     op[teiler*i*2 + 2 + j] = ip[i+1];
125    }
126   }
127
128  } else if ( channels == 1 ) {
129   max = (samples-1)/teiler;
130
131   for (i = 0; i < max; i++) {
132    for (j = 0; j < teiler; j++) {
133     op[teiler*i + j] = ip[i];
134    }
135   }
136
137  } else {
138   return -1;
139   max = (samples-1)/teiler;
140
141   for (i = 0; i < max; i++) {
142    for (j = 0; j < teiler; j++) {
143//      printf("op[teiler*i + j = %i] = ip[i=%i]; // samples=%i, teiler=%i, channels=%i\n",
144//                teiler*i + j, i + ((i+j) % channels), samples, teiler, channels);
145      op[teiler*i + j] = ip[i + ((i+j) % channels)];
146    }
147   }
148  }
149
150  return 0;
151 }
152
153 return -1;
154}
155
156int raor_conv_codec (void * out, void * in, int samples, int from, int to, int bits) {
157 int inbo = ROAR_CODEC_BYTE_ORDER(from), outbo = ROAR_CODEC_BYTE_ORDER(to);
158 int ins  = ROAR_CODEC_IS_SIGNED(from),  outs  = ROAR_CODEC_IS_SIGNED(to);
159
160 if ( inbo != outbo )
161  return -1;
162
163 if ( ins != outs ) {
164  if ( ins && !outs ) {
165   if ( bits == 8 ) {
166    roar_conv_codec_s2u8(out, in, samples);
167   } else {
168    return -1;
169   }
170  } else {
171   return -1;
172  }
173 }
174
175 return 0;
176}
177
178int roar_conv_codec_s2u8 (void * out, void * in, int samples) {
179 char * ip = in;
180 unsigned char * op = out;
181 int i;
182
183 for(i = 0; i < samples; i++)
184  op[i] = ip[i] + 128;
185
186 return 0;
187}
188
189
190int roar_conv       (void * out, void * in, int samples, struct roar_audio_info * from, struct roar_audio_info * to) {
191 void * ip = in;
192
193 // TODO: decide how to work around both in and out beeing to small to hold all
194 //       data between the steps.
195 //       for the moment: guess out >= in
196
197 if ( from->bits != to->bits ) {
198  if ( roar_conv_bits(out, ip, samples, from->bits, to->bits) == -1 )
199   return -1;
200  else
201   ip = out;
202 }
203
204 if ( from->channels != to->channels ) {
205  if ( roar_conv_chans(out, ip, samples, from->channels, to->channels, to->bits) == -1 )
206   return -1;
207  else
208   ip = out;
209 }
210
211 if ( from->rate != to->rate ) {
212  if ( roar_conv_rate(out, ip, samples, from->rate, to->rate, to->bits, to->channels) == -1 )
213   return -1;
214  else
215   ip = out;
216 }
217
218 if ( from->codec != to->codec )
219  return -1;
220
221 return 0;
222}
223
224//ll
Note: See TracBrowser for help on using the repository browser.