annotate libaf/af_format.c @ 9412:ed98b850668d

Fix for string handling bug from Alex Beregszasz
author eyck
date Thu, 13 Feb 2003 09:34:37 +0000
parents 06d7ef3c7b01
children be1e7cfddc08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
1 /* This audio output filter changes the format of a data block. Valid
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
2 formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
3 AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
4 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
5
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
6 #include <stdio.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
7 #include <stdlib.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
8 #include <string.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
9 #include <unistd.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
10 #include <inttypes.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
11 #include <limits.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
12
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
13 #include "af.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
14
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
15 // Integer to float conversion through lrintf()
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
16 #ifdef HAVE_LRINTF
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
17 #define __USE_ISOC99 1
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
18 #include <math.h>
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
19 #else
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
20 #define lrintf(x) ((int)(x))
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
21 #endif
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
22
e8832e66babd New features:
anders
parents: 7998
diff changeset
23 /* Functions used by play to convert the input audio to the correct
e8832e66babd New features:
anders
parents: 7998
diff changeset
24 format */
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
25
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
26 /* The below includes retrives functions for converting to and from
e8832e66babd New features:
anders
parents: 7998
diff changeset
27 ulaw and alaw */
e8832e66babd New features:
anders
parents: 7998
diff changeset
28 #include "af_format_ulaw.c"
e8832e66babd New features:
anders
parents: 7998
diff changeset
29 #include "af_format_alaw.c"
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
30
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
31 // Switch endianess
e8832e66babd New features:
anders
parents: 7998
diff changeset
32 static void endian(void* in, void* out, int len, int bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
33 // From singed to unsigned
e8832e66babd New features:
anders
parents: 7998
diff changeset
34 static void si2us(void* in, void* out, int len, int bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
35 // From unsinged to signed
e8832e66babd New features:
anders
parents: 7998
diff changeset
36 static void us2si(void* in, void* out, int len, int bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
37 // Change the number of bits per sample
e8832e66babd New features:
anders
parents: 7998
diff changeset
38 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
39 // From float to int signed
e8832e66babd New features:
anders
parents: 7998
diff changeset
40 static void float2int(void* in, void* out, int len, int bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
41 // From signed int to float
e8832e66babd New features:
anders
parents: 7998
diff changeset
42 static void int2float(void* in, void* out, int len, int bps);
7719
41e8d0916c60 Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents: 7711
diff changeset
43
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
44 // Convert from string to format
e8832e66babd New features:
anders
parents: 7998
diff changeset
45 static int str2fmt(char* str)
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
46 {
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
47 int format=0;
e8832e66babd New features:
anders
parents: 7998
diff changeset
48 // Scan for endianess
e8832e66babd New features:
anders
parents: 7998
diff changeset
49 if(strstr(str,"be") || strstr(str,"BE"))
e8832e66babd New features:
anders
parents: 7998
diff changeset
50 format |= AF_FORMAT_BE;
e8832e66babd New features:
anders
parents: 7998
diff changeset
51 else if(strstr(str,"le") || strstr(str,"LE"))
e8832e66babd New features:
anders
parents: 7998
diff changeset
52 format |= AF_FORMAT_LE;
e8832e66babd New features:
anders
parents: 7998
diff changeset
53 else
e8832e66babd New features:
anders
parents: 7998
diff changeset
54 format |= AF_FORMAT_NE;
e8832e66babd New features:
anders
parents: 7998
diff changeset
55
e8832e66babd New features:
anders
parents: 7998
diff changeset
56 // Scan for special formats
e8832e66babd New features:
anders
parents: 7998
diff changeset
57 if(strstr(str,"mulaw") || strstr(str,"MULAW")){
e8832e66babd New features:
anders
parents: 7998
diff changeset
58 format |= AF_FORMAT_MU_LAW; return format;
e8832e66babd New features:
anders
parents: 7998
diff changeset
59 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
60 if(strstr(str,"alaw") || strstr(str,"ALAW")){
e8832e66babd New features:
anders
parents: 7998
diff changeset
61 format |= AF_FORMAT_A_LAW; return format;
e8832e66babd New features:
anders
parents: 7998
diff changeset
62 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
63 if(strstr(str,"ac3") || strstr(str,"AC3")){
e8832e66babd New features:
anders
parents: 7998
diff changeset
64 format |= AF_FORMAT_AC3; return format;
e8832e66babd New features:
anders
parents: 7998
diff changeset
65 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
66 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){
e8832e66babd New features:
anders
parents: 7998
diff changeset
67 format |= AF_FORMAT_MPEG2; return format;
e8832e66babd New features:
anders
parents: 7998
diff changeset
68 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
69 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){
e8832e66babd New features:
anders
parents: 7998
diff changeset
70 format |= AF_FORMAT_IMA_ADPCM; return format;
e8832e66babd New features:
anders
parents: 7998
diff changeset
71 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
72
e8832e66babd New features:
anders
parents: 7998
diff changeset
73 // Scan for int/float
e8832e66babd New features:
anders
parents: 7998
diff changeset
74 if(strstr(str,"float") || strstr(str,"FLOAT")){
e8832e66babd New features:
anders
parents: 7998
diff changeset
75 format |= AF_FORMAT_F; return format;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
76 }
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
77 else
e8832e66babd New features:
anders
parents: 7998
diff changeset
78 format |= AF_FORMAT_I;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
79
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
80 // Scan for signed/unsigned
e8832e66babd New features:
anders
parents: 7998
diff changeset
81 if(strstr(str,"unsigned") || strstr(str,"UNSIGNED"))
e8832e66babd New features:
anders
parents: 7998
diff changeset
82 format |= AF_FORMAT_US;
e8832e66babd New features:
anders
parents: 7998
diff changeset
83 else
e8832e66babd New features:
anders
parents: 7998
diff changeset
84 format |= AF_FORMAT_SI;
e8832e66babd New features:
anders
parents: 7998
diff changeset
85
e8832e66babd New features:
anders
parents: 7998
diff changeset
86 return format;
e8832e66babd New features:
anders
parents: 7998
diff changeset
87 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
88
e8832e66babd New features:
anders
parents: 7998
diff changeset
89 /* Convert format to str input str is a buffer for the
e8832e66babd New features:
anders
parents: 7998
diff changeset
90 converted string, size is the size of the buffer */
8994
06d7ef3c7b01 export fmt2str()
arpi
parents: 8607
diff changeset
91 char* fmt2str(int format, char* str, size_t size)
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
92 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
93 int i=0;
e8832e66babd New features:
anders
parents: 7998
diff changeset
94 // Print endinaness
e8832e66babd New features:
anders
parents: 7998
diff changeset
95 if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK))
e8832e66babd New features:
anders
parents: 7998
diff changeset
96 i+=snprintf(str,size,"little endian ");
e8832e66babd New features:
anders
parents: 7998
diff changeset
97 else
e8832e66babd New features:
anders
parents: 7998
diff changeset
98 i+=snprintf(str,size,"big endian ");
e8832e66babd New features:
anders
parents: 7998
diff changeset
99
e8832e66babd New features:
anders
parents: 7998
diff changeset
100 if(format & AF_FORMAT_SPECIAL_MASK){
e8832e66babd New features:
anders
parents: 7998
diff changeset
101 switch(format & AF_FORMAT_SPECIAL_MASK){
e8832e66babd New features:
anders
parents: 7998
diff changeset
102 case(AF_FORMAT_MU_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
103 i+=snprintf(&str[i],size-i,"mu law "); break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
104 case(AF_FORMAT_A_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
105 i+=snprintf(&str[i],size-i,"A law "); break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
106 case(AF_FORMAT_MPEG2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
107 i+=snprintf(&str[i],size-i,"MPEG 2 "); break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
108 case(AF_FORMAT_AC3):
e8832e66babd New features:
anders
parents: 7998
diff changeset
109 i+=snprintf(&str[i],size-i,"AC3 "); break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
110 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
111 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
112 else{
e8832e66babd New features:
anders
parents: 7998
diff changeset
113 // Point
e8832e66babd New features:
anders
parents: 7998
diff changeset
114 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK))
e8832e66babd New features:
anders
parents: 7998
diff changeset
115 i+=snprintf(&str[i],size,"float ");
e8832e66babd New features:
anders
parents: 7998
diff changeset
116 else{
e8832e66babd New features:
anders
parents: 7998
diff changeset
117 // Sign
e8832e66babd New features:
anders
parents: 7998
diff changeset
118 if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK))
e8832e66babd New features:
anders
parents: 7998
diff changeset
119 i+=snprintf(&str[i],size-i,"unsigned ");
e8832e66babd New features:
anders
parents: 7998
diff changeset
120 else
e8832e66babd New features:
anders
parents: 7998
diff changeset
121 i+=snprintf(&str[i],size-i,"signed ");
e8832e66babd New features:
anders
parents: 7998
diff changeset
122
e8832e66babd New features:
anders
parents: 7998
diff changeset
123 i+=snprintf(&str[i],size,"int ");
e8832e66babd New features:
anders
parents: 7998
diff changeset
124 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
125 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
126 return str;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
127 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
128
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
129 // Helper functions to check sanity for input arguments
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
130
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
131 // Sanity check for bytes per sample
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
132 int check_bps(int bps)
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
133 {
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
134 if(bps != 4 && bps != 2 && bps != 1){
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
135 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample"
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
136 " must be 1, 2 or 4. Current value is %i \n",bps);
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
137 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
138 }
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
139 return AF_OK;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
140 }
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
141
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
142 // Check for unsupported formats
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
143 int check_format(int format)
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
144 {
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
145 char buf[256];
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
146 switch(format & AF_FORMAT_SPECIAL_MASK){
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
147 case(AF_FORMAT_MPEG2):
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
148 case(AF_FORMAT_AC3):
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
149 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n",
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
150 fmt2str(format,buf,255));
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
151 return AF_ERROR;
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
152 }
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
153 return AF_OK;
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
154 }
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
155
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
156 // Initialization and runtime control
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
157 static int control(struct af_instance_s* af, int cmd, void* arg)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
158 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
159 switch(cmd){
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
160 case AF_CONTROL_REINIT:{
e8832e66babd New features:
anders
parents: 7998
diff changeset
161 char buf1[256];
e8832e66babd New features:
anders
parents: 7998
diff changeset
162 char buf2[256];
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
163 // Make sure this filter isn't redundant
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
164 if(af->data->format == ((af_data_t*)arg)->format &&
e8832e66babd New features:
anders
parents: 7998
diff changeset
165 af->data->bps == ((af_data_t*)arg)->bps)
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
166 return AF_DETACH;
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
167
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
168 // Check for errors in configuraton
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
169 if((AF_OK != check_bps(((af_data_t*)arg)->bps)) ||
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
170 (AF_OK != check_format(((af_data_t*)arg)->format)) ||
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
171 (AF_OK != check_bps(af->data->bps)) ||
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
172 (AF_OK != check_format(af->data->format)))
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
173 return AF_ERROR;
e8832e66babd New features:
anders
parents: 7998
diff changeset
174
e8832e66babd New features:
anders
parents: 7998
diff changeset
175 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %ibit %sto %ibit %s \n",
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
176 ((af_data_t*)arg)->bps*8,fmt2str(((af_data_t*)arg)->format,buf1,255),
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
177 af->data->bps*8,fmt2str(af->data->format,buf2,255));
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
178
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
179 af->data->rate = ((af_data_t*)arg)->rate;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
180 af->data->nch = ((af_data_t*)arg)->nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
181 af->mul.n = af->data->bps;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
182 af->mul.d = ((af_data_t*)arg)->bps;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
183 return AF_OK;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
184 }
7998
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7719
diff changeset
185 case AF_CONTROL_COMMAND_LINE:{
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
186 int bps = 2;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
187 int format = AF_FORMAT_NE;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
188 char str[256];
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
189 str[0] = '\0';
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
190 sscanf((char*)arg,"%i:%s",&bps,str);
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
191 // Convert string to format
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
192 format = str2fmt(str);
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
193
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
194 // Automatic correction of errors
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
195 switch(format & AF_FORMAT_SPECIAL_MASK){
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
196 case(AF_FORMAT_A_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
197 case(AF_FORMAT_MU_LAW):
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
198 bps=1; break;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
199 case(AF_FORMAT_AC3):
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
200 bps=4; break; // I think
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
201 }
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
202 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK))
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
203 bps=4;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
204
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
205 if((AF_OK != af->control(af,AF_CONTROL_FORMAT_BPS | AF_CONTROL_SET,&bps)) ||
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
206 (AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)))
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
207 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
208 return AF_OK;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
209 }
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
210 case AF_CONTROL_FORMAT_BPS | AF_CONTROL_SET:
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
211 // Reinit must be called after this function has been called
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
212
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
213 // Check for errors in configuraton
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
214 if(AF_OK != check_bps(*(int*)arg))
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
215 return AF_ERROR;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
216
8607
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
217 af->data->bps = *(int*)arg;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
218 return AF_OK;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
219 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
220 // Reinit must be called after this function has been called
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
221
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
222 // Check for errors in configuraton
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
223 if(AF_OK != check_format(*(int*)arg))
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
224 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
225
d6f40a06867b Changes includes:
anders
parents: 8185
diff changeset
226 af->data->format = *(int*)arg;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
227 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
228 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
229 return AF_UNKNOWN;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
230 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
231
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
232 // Deallocate memory
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
233 static void uninit(struct af_instance_s* af)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
234 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
235 if(af->data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
236 free(af->data);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
237 (int)af->setup = 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
238 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
239
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
240 // Filter data through filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
241 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
242 {
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
243 af_data_t* l = af->data; // Local data
e8832e66babd New features:
anders
parents: 7998
diff changeset
244 af_data_t* c = data; // Current working data
e8832e66babd New features:
anders
parents: 7998
diff changeset
245 int len = c->len/c->bps; // Lenght in samples of current audio block
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
246
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
247 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
248 return NULL;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
249
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
250 // Change to cpu native endian format
e8832e66babd New features:
anders
parents: 7998
diff changeset
251 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
e8832e66babd New features:
anders
parents: 7998
diff changeset
252 endian(c->audio,c->audio,len,c->bps);
7719
41e8d0916c60 Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents: 7711
diff changeset
253
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
254 // Conversion table
e8832e66babd New features:
anders
parents: 7998
diff changeset
255 switch(c->format & ~AF_FORMAT_END_MASK){
e8832e66babd New features:
anders
parents: 7998
diff changeset
256 case(AF_FORMAT_MU_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
257 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
e8832e66babd New features:
anders
parents: 7998
diff changeset
258 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
e8832e66babd New features:
anders
parents: 7998
diff changeset
259 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
e8832e66babd New features:
anders
parents: 7998
diff changeset
260 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
e8832e66babd New features:
anders
parents: 7998
diff changeset
261 si2us(l->audio,l->audio,len,l->bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
262 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
263 case(AF_FORMAT_A_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
264 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
e8832e66babd New features:
anders
parents: 7998
diff changeset
265 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
e8832e66babd New features:
anders
parents: 7998
diff changeset
266 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
e8832e66babd New features:
anders
parents: 7998
diff changeset
267 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
e8832e66babd New features:
anders
parents: 7998
diff changeset
268 si2us(l->audio,l->audio,len,l->bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
269 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
270 case(AF_FORMAT_F):
e8832e66babd New features:
anders
parents: 7998
diff changeset
271 switch(l->format&AF_FORMAT_SPECIAL_MASK){
e8832e66babd New features:
anders
parents: 7998
diff changeset
272 case(AF_FORMAT_MU_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
273 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
e8832e66babd New features:
anders
parents: 7998
diff changeset
274 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
275 case(AF_FORMAT_A_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
276 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
277 break;
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
278 default:
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
279 float2int(c->audio, l->audio, len, l->bps);
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
280 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
281 si2us(l->audio,l->audio,len,l->bps);
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
282 break;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
283 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
284 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
285 default:
e8832e66babd New features:
anders
parents: 7998
diff changeset
286 // Input must be int
e8832e66babd New features:
anders
parents: 7998
diff changeset
287
e8832e66babd New features:
anders
parents: 7998
diff changeset
288 // Change signed/unsigned
e8832e66babd New features:
anders
parents: 7998
diff changeset
289 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
e8832e66babd New features:
anders
parents: 7998
diff changeset
290 if((c->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
e8832e66babd New features:
anders
parents: 7998
diff changeset
291 us2si(c->audio,c->audio,len,c->bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
292 else
e8832e66babd New features:
anders
parents: 7998
diff changeset
293 si2us(c->audio,c->audio,len,c->bps);
e8832e66babd New features:
anders
parents: 7998
diff changeset
294 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
295 // Convert to special formats
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
296 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
297 case(AF_FORMAT_MU_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
298 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
e8832e66babd New features:
anders
parents: 7998
diff changeset
299 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
300 case(AF_FORMAT_A_LAW):
e8832e66babd New features:
anders
parents: 7998
diff changeset
301 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
e8832e66babd New features:
anders
parents: 7998
diff changeset
302 break;
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
303 case(AF_FORMAT_F):
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
304 int2float(c->audio, l->audio, len, c->bps);
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
305 break;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
306 default:
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
307 // Change the number of bits
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
308 if(c->bps != l->bps)
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
309 change_bps(c->audio,l->audio,len,c->bps,l->bps);
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
310 else
8185
anders
parents: 8180
diff changeset
311 memcpy(l->audio,c->audio,len*c->bps);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
312 break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
313 }
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
314 break;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
315 }
7719
41e8d0916c60 Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents: 7711
diff changeset
316
41e8d0916c60 Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents: 7711
diff changeset
317 // Switch from cpu native endian to the correct endianess
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
318 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
e8832e66babd New features:
anders
parents: 7998
diff changeset
319 endian(l->audio,l->audio,len,l->bps);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
320
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
321 // Set output data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
322 c->audio = l->audio;
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
323 c->len = len*l->bps;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
324 c->bps = l->bps;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
325 c->format = l->format;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
326 return c;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
327 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
328
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
329 // Allocate memory and set function pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
330 static int open(af_instance_t* af){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
331 af->control=control;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
332 af->uninit=uninit;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
333 af->play=play;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
334 af->mul.n=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
335 af->mul.d=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
336 af->data=calloc(1,sizeof(af_data_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
337 if(af->data == NULL)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
338 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
339 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
340 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
341
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
342 // Description of this filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
343 af_info_t af_info_format = {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
344 "Sample format conversion",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
345 "format",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
346 "Anders",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
347 "",
7615
c67328dd459a Adding Support for non-reentrant audio filters
anders
parents: 7571
diff changeset
348 AF_FLAGS_REENTRANT,
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
349 open
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
350 };
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
351
e8832e66babd New features:
anders
parents: 7998
diff changeset
352 // Function implementations used by play
e8832e66babd New features:
anders
parents: 7998
diff changeset
353 static void endian(void* in, void* out, int len, int bps)
e8832e66babd New features:
anders
parents: 7998
diff changeset
354 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
355 register int i;
e8832e66babd New features:
anders
parents: 7998
diff changeset
356 switch(bps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
357 case(2):{
e8832e66babd New features:
anders
parents: 7998
diff changeset
358 register uint16_t s;
e8832e66babd New features:
anders
parents: 7998
diff changeset
359 for(i=0;i<len;i++){
e8832e66babd New features:
anders
parents: 7998
diff changeset
360 s=((uint16_t*)in)[i];
e8832e66babd New features:
anders
parents: 7998
diff changeset
361 ((uint16_t*)out)[i]=(uint16_t)(((s&0x00FF)<<8) | (s&0xFF00)>>8);
e8832e66babd New features:
anders
parents: 7998
diff changeset
362 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
363 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
364 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
365 case(4):{
e8832e66babd New features:
anders
parents: 7998
diff changeset
366 register uint32_t s;
e8832e66babd New features:
anders
parents: 7998
diff changeset
367 for(i=0;i<len;i++){
e8832e66babd New features:
anders
parents: 7998
diff changeset
368 s=((uint32_t*)in)[i];
e8832e66babd New features:
anders
parents: 7998
diff changeset
369 ((uint32_t*)out)[i]=(uint32_t)(((s&0x000000FF)<<24) |
e8832e66babd New features:
anders
parents: 7998
diff changeset
370 ((s&0x0000FF00)<<8) |
e8832e66babd New features:
anders
parents: 7998
diff changeset
371 ((s&0x00FF0000)>>8) |
e8832e66babd New features:
anders
parents: 7998
diff changeset
372 ((s&0xFF000000)>>24));
e8832e66babd New features:
anders
parents: 7998
diff changeset
373 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
374 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
375 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
376 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
377 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
378
e8832e66babd New features:
anders
parents: 7998
diff changeset
379 static void si2us(void* in, void* out, int len, int bps)
e8832e66babd New features:
anders
parents: 7998
diff changeset
380 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
381 register int i;
e8832e66babd New features:
anders
parents: 7998
diff changeset
382 switch(bps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
383 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
384 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
385 ((uint8_t*)out)[i]=(uint8_t)(SCHAR_MAX+((int)((int8_t*)in)[i]));
e8832e66babd New features:
anders
parents: 7998
diff changeset
386 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
387 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
388 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
389 ((uint16_t*)out)[i]=(uint16_t)(SHRT_MAX+((int)((int16_t*)in)[i]));
e8832e66babd New features:
anders
parents: 7998
diff changeset
390 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
391 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
392 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
393 ((uint32_t*)out)[i]=(uint32_t)(INT_MAX+((int32_t*)in)[i]);
e8832e66babd New features:
anders
parents: 7998
diff changeset
394 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
395 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
396 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
397
e8832e66babd New features:
anders
parents: 7998
diff changeset
398 static void us2si(void* in, void* out, int len, int bps)
e8832e66babd New features:
anders
parents: 7998
diff changeset
399 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
400 register int i;
e8832e66babd New features:
anders
parents: 7998
diff changeset
401 switch(bps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
402 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
403 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
404 ((int8_t*)out)[i]=(int8_t)(SCHAR_MIN+((int)((uint8_t*)in)[i]));
e8832e66babd New features:
anders
parents: 7998
diff changeset
405 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
406 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
407 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
408 ((int16_t*)out)[i]=(int16_t)(SHRT_MIN+((int)((uint16_t*)in)[i]));
e8832e66babd New features:
anders
parents: 7998
diff changeset
409 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
410 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
411 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
412 ((int32_t*)out)[i]=(int32_t)(INT_MIN+((uint32_t*)in)[i]);
e8832e66babd New features:
anders
parents: 7998
diff changeset
413 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
414 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
415 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
416
e8832e66babd New features:
anders
parents: 7998
diff changeset
417 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
e8832e66babd New features:
anders
parents: 7998
diff changeset
418 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
419 register int i;
e8832e66babd New features:
anders
parents: 7998
diff changeset
420 switch(inbps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
421 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
422 switch(outbps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
423 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
424 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
425 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
e8832e66babd New features:
anders
parents: 7998
diff changeset
426 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
427 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
428 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
429 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
e8832e66babd New features:
anders
parents: 7998
diff changeset
430 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
431 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
432 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
433 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
434 switch(outbps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
435 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
436 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
437 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
e8832e66babd New features:
anders
parents: 7998
diff changeset
438 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
439 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
440 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
441 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
e8832e66babd New features:
anders
parents: 7998
diff changeset
442 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
443 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
444 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
445 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
446 switch(outbps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
447 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
448 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
449 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
e8832e66babd New features:
anders
parents: 7998
diff changeset
450 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
451 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
452 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
453 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
e8832e66babd New features:
anders
parents: 7998
diff changeset
454 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
455 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
456 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
457 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
458 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
459
e8832e66babd New features:
anders
parents: 7998
diff changeset
460 static void float2int(void* in, void* out, int len, int bps)
e8832e66babd New features:
anders
parents: 7998
diff changeset
461 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
462 register int i;
e8832e66babd New features:
anders
parents: 7998
diff changeset
463 switch(bps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
464 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
465 for(i=0;i<len;i++)
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
466 ((int8_t*)out)[i]=(int8_t)lrintf(SCHAR_MAX*((float*)in)[i]);
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
467 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
468 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
469 for(i=0;i<len;i++)
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
470 ((int16_t*)out)[i]=(int16_t)lrintf(SHRT_MAX*((float*)in)[i]);
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
471 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
472 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
473 for(i=0;i<len;i++)
8180
4ba9aed295f2 Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents: 8167
diff changeset
474 ((int32_t*)out)[i]=(int32_t)lrintf(INT_MAX*((float*)in)[i]);
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
475 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
476 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
477 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
478
e8832e66babd New features:
anders
parents: 7998
diff changeset
479 static void int2float(void* in, void* out, int len, int bps)
e8832e66babd New features:
anders
parents: 7998
diff changeset
480 {
e8832e66babd New features:
anders
parents: 7998
diff changeset
481 register int i;
e8832e66babd New features:
anders
parents: 7998
diff changeset
482 switch(bps){
e8832e66babd New features:
anders
parents: 7998
diff changeset
483 case(1):
e8832e66babd New features:
anders
parents: 7998
diff changeset
484 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
485 ((float*)out)[i]=(1.0/SCHAR_MAX)*((float)((int8_t*)in)[i]);
e8832e66babd New features:
anders
parents: 7998
diff changeset
486 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
487 case(2):
e8832e66babd New features:
anders
parents: 7998
diff changeset
488 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
489 ((float*)out)[i]=(1.0/SHRT_MAX)*((float)((int16_t*)in)[i]);
e8832e66babd New features:
anders
parents: 7998
diff changeset
490 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
491 case(4):
e8832e66babd New features:
anders
parents: 7998
diff changeset
492 for(i=0;i<len;i++)
e8832e66babd New features:
anders
parents: 7998
diff changeset
493 ((float*)out)[i]=(1.0/INT_MAX)*((float)((int32_t*)in)[i]);
e8832e66babd New features:
anders
parents: 7998
diff changeset
494 break;
e8832e66babd New features:
anders
parents: 7998
diff changeset
495 }
e8832e66babd New features:
anders
parents: 7998
diff changeset
496 }