annotate libaf/af_tools.c @ 19995:9cd5e242121e

in case of errors after decoding quit the main decoding loop before memmove() when sh_audio->in_buffer_len <= 0; patch by Chandan Pitta chandan.pitta gmail com
author nicodvb
date Wed, 27 Sep 2006 21:45:15 +0000
parents 0293cab15c03
children a54a25221b79
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 }
14621
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
81
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
82 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
83 post to Musicdsp.org */
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
84 inline float af_softclip(float a)
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
85 {
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
86 if (a >= M_PI/2)
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
87 return 1.0;
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
88 else if (a <= -M_PI/2)
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
89 return -1.0;
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
90 else
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
91 return sin(a);
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
92 }