annotate libaf/af_format.c @ 14156:c30e193ac112

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