source: roaraudio/libroardsp/convert.c @ 33:55fb857975c3

Last change on this file since 33:55fb857975c3 was 33:55fb857975c3, checked in by phi, 16 years ago

changed minor formating

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