annotate libaf/af_tools.c @ 11401:c17fc395142a

now distclean will run on systems which don't have 'readlink' anyway dunno why it was used in this target
author gabucino
date Thu, 06 Nov 2003 20:32:47 +0000
parents 93212da0032e
children 0293cab15c03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
1 #include <math.h>
8623
440301fef3fe Added/reordered #includes to silence warnings about "implicit declaration".
rathann
parents: 8607
diff changeset
2 #include <string.h>
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
3 #include <af.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
4
d6f40a06867b Changes includes:
anders
parents:
diff changeset
5 /* Convert to gain value from dB. Returns AF_OK if of and AF_ERROR if
d6f40a06867b Changes includes:
anders
parents:
diff changeset
6 fail */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
7 inline int af_from_dB(int n, float* in, float* out, float k, float mi, float ma)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
8 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
9 int i = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
10 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
11 if(!in || !out)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
12 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
13
d6f40a06867b Changes includes:
anders
parents:
diff changeset
14 for(i=0;i<n;i++){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
15 if(in[i]<=-200)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
16 out[i]=0.0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
17 else
d6f40a06867b Changes includes:
anders
parents:
diff changeset
18 out[i]=pow(10.0,clamp(in[i],mi,ma)/k);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
19 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
20 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
21 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
22
d6f40a06867b Changes includes:
anders
parents:
diff changeset
23 /* Convert from gain value to dB. Returns AF_OK if of and AF_ERROR if
d6f40a06867b Changes includes:
anders
parents:
diff changeset
24 fail */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
25 inline int af_to_dB(int n, float* in, float* out, float k)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
26 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
27 int i = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
28 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
29 if(!in || !out)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
30 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
31
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
32 for(i=0;i<n;i++){
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
33 if(in[i] == 0.0)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
34 out[i]=-200.0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
35 else
d6f40a06867b Changes includes:
anders
parents:
diff changeset
36 out[i]=k*log10(in[i]);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
37 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
38 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
39 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
40
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
41 /* Convert from ms to sample time */
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
42 inline int af_from_ms(int n, float* in, int* out, int rate, float mi, float ma)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
43 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
44 int i = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
45 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
46 if(!in || !out)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
47 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
48
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
49 for(i=0;i<n;i++)
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
50 out[i]=(int)((float)rate * clamp(in[i],mi,ma)/1000.0);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
51
d6f40a06867b Changes includes:
anders
parents:
diff changeset
52 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
53 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
54
d6f40a06867b Changes includes:
anders
parents:
diff changeset
55 /* Convert from sample time to ms */
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
56 inline int af_to_ms(int n, int* in, float* out, int rate)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
57 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
58 int i = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
59 // Sanity check
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
60 if(!in || !out || !rate)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
61 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
62
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
63 for(i=0;i<n;i++)
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
64 out[i]=1000.0 * (float)in[i]/((float)rate);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
65
d6f40a06867b Changes includes:
anders
parents:
diff changeset
66 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
67 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
68
d6f40a06867b Changes includes:
anders
parents:
diff changeset
69 /* Helper function for testing the output format */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
70 inline int af_test_output(struct af_instance_s* af, af_data_t* out)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
71 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
72 if((af->data->format != out->format) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
73 (af->data->bps != out->bps) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
74 (af->data->rate != out->rate) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
75 (af->data->nch != out->nch)){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
76 memcpy(out,af->data,sizeof(af_data_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
77 return AF_FALSE;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
78 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
79 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
80 }