source: roaraudio/libroardsp/convert.c @ 0:2a41d2f42394

Last change on this file since 0:2a41d2f42394 was 0:2a41d2f42394, checked in by phi, 16 years ago

Initial revision

File size: 4.3 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}
97int roar_conv_rate_16 (void * out, void * in, int samples, int from, int to, int channels) {
98 int16_t * ip = in, * op = out;
99 int16_t avg;
100 int i, j, c;
101 float scale = to/from;
102 int step;
103
104
105 if ( scale > 1 ) {
106  if ( scale - (int)scale != (float)0 )
107   return -1;
108
109  step = scale;
110/*
111  for (i = 0; i < samples/step; i++)
112   for (j = 0; j < step; j++)
113    for (c = 0; c < channels; c++)
114     op[i + j*channels + c] = ip[i + c];
115*/
116  if ( step == 2 ) {
117   for (i = 0; i < samples/step; i += channels) {
118    for (j = 0; j < step; j++) {
119     for (c = 0; c < channels; c++) {
120      op[i*step + j*channels + c] = ip[i+c];
121     }
122    }
123   }
124   return 0;
125  }
126  return -1;
127 } else {
128  scale = 1/scale;
129
130  if ( scale - (int)scale != (float)0 )
131   return -1;
132
133  step = scale;
134
135  for (i = 0; i < samples/step; i++) {
136   avg = 0;
137   for (j = 0; j < step; j++)
138    avg += ip[i*step + j];
139
140   op[i] = avg/step;
141  }
142  return 0;
143 }
144
145 return -1;
146}
147
148int raor_conv_codec (void * out, void * in, int samples, int from, int to, int bits) {
149 int inbo = ROAR_CODEC_BYTE_ORDER(from), outbo = ROAR_CODEC_BYTE_ORDER(to);
150 int ins  = ROAR_CODEC_IS_SIGNED(from),  outs  = ROAR_CODEC_IS_SIGNED(to);
151
152 if ( inbo != outbo )
153  return -1;
154
155 if ( ins != outs ) {
156  if ( ins && !outs ) {
157   if ( bits == 8 ) {
158    roar_conv_codec_s2u8(out, in, samples);
159   } else {
160    return -1;
161   }
162  } else {
163   return -1;
164  }
165 }
166
167 return 0;
168}
169
170int roar_conv_codec_s2u8 (void * out, void * in, int samples) {
171 char * ip = in;
172 unsigned char * op = out;
173 int i;
174
175 for(i = 0; i < samples; i++)
176  op[i] = ip[i] + 128;
177
178 return 0;
179}
180
181
182int roar_conv       (void * out, void * in, int samples, struct roar_audio_info * from, struct roar_audio_info * to) {
183 void * ip = in;
184
185 // TODO: decide how to work around both in and out beeing to small to hold all
186 //       data between the steps.
187 //       for the moment: guess out >= in
188
189 if ( from->bits != to->bits ) {
190  if ( roar_conv_bits(out, ip, samples, from->bits, to->bits) == -1 )
191   return -1;
192  else
193   ip = out;
194 }
195
196 if ( from->channels != to->channels ) {
197  if ( roar_conv_chans(out, ip, samples, from->channels, to->channels, to->bits) == -1 )
198   return -1;
199  else
200   ip = out;
201 }
202
203 if ( from->rate != to->rate ) {
204  if ( roar_conv_rate(out, ip, samples, from->rate, to->rate, to->bits, to->channels) == -1 )
205   return -1;
206  else
207   ip = out;
208 }
209
210 if ( from->codec != to->codec )
211  return -1;
212
213 return 0;
214}
215
216//ll
Note: See TracBrowser for help on using the repository browser.