annotate libaf/af_tools.c @ 34582:d0617776d506

Check for allocation error.
author ib
date Thu, 09 Feb 2012 14:37:06 +0000
parents 0f1b5b68af32
children 2b9bc3c2933d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28229
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
1 /*
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
2 * This file is part of MPlayer.
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
3 *
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
5 * it under the terms of the GNU General Public License as published by
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
7 * (at your option) any later version.
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
8 *
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
12 * GNU General Public License for more details.
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
13 *
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
14 * You should have received a copy of the GNU General Public License along
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
17 */
72d0b1444141 Replace informal license notices by standard license header
diego
parents: 26342
diff changeset
18
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
19 #include <math.h>
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
20 #include <string.h>
26342
327b7955381f Use quotes instead of angular brackets for local includes.
diego
parents: 24890
diff changeset
21 #include "af.h"
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
22
d6f40a06867b Changes includes:
anders
parents:
diff changeset
23 /* Convert to gain value from dB. Returns AF_OK if of and AF_ERROR if
d6f40a06867b Changes includes:
anders
parents:
diff changeset
24 fail */
24890
a54a25221b79 Remove some pointless 'inline' qualifiers
uau
parents: 14621
diff changeset
25 int af_from_dB(int n, float* in, float* out, float k, float mi, float ma)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
26 {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
27 int i = 0;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
28 // Sanity check
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
29 if(!in || !out)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
30 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
31
d6f40a06867b Changes includes:
anders
parents:
diff changeset
32 for(i=0;i<n;i++){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
33 if(in[i]<=-200)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
34 out[i]=0.0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
35 else
d6f40a06867b Changes includes:
anders
parents:
diff changeset
36 out[i]=pow(10.0,clamp(in[i],mi,ma)/k);
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
d6f40a06867b Changes includes:
anders
parents:
diff changeset
41 /* Convert from gain value to dB. Returns AF_OK if of and AF_ERROR if
d6f40a06867b Changes includes:
anders
parents:
diff changeset
42 fail */
24890
a54a25221b79 Remove some pointless 'inline' qualifiers
uau
parents: 14621
diff changeset
43 int af_to_dB(int n, float* in, float* out, float k)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
44 {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
45 int i = 0;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
46 // Sanity check
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
47 if(!in || !out)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
48 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
49
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
50 for(i=0;i<n;i++){
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
51 if(in[i] == 0.0)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
52 out[i]=-200.0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
53 else
d6f40a06867b Changes includes:
anders
parents:
diff changeset
54 out[i]=k*log10(in[i]);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
55 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
56 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
57 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
58
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
59 /* Convert from ms to sample time */
24890
a54a25221b79 Remove some pointless 'inline' qualifiers
uau
parents: 14621
diff changeset
60 int af_from_ms(int n, float* in, int* out, int rate, float mi, float ma)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
61 {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
62 int i = 0;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
63 // Sanity check
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
64 if(!in || !out)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
65 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
66
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
67 for(i=0;i<n;i++)
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
68 out[i]=(int)((float)rate * clamp(in[i],mi,ma)/1000.0);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
69
d6f40a06867b Changes includes:
anders
parents:
diff changeset
70 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
71 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
72
d6f40a06867b Changes includes:
anders
parents:
diff changeset
73 /* Convert from sample time to ms */
24890
a54a25221b79 Remove some pointless 'inline' qualifiers
uau
parents: 14621
diff changeset
74 int af_to_ms(int n, int* in, float* out, int rate)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
75 {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
76 int i = 0;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
77 // Sanity check
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
78 if(!in || !out || !rate)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
79 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
80
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
81 for(i=0;i<n;i++)
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8623
diff changeset
82 out[i]=1000.0 * (float)in[i]/((float)rate);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
83
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
84 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
85 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
86
d6f40a06867b Changes includes:
anders
parents:
diff changeset
87 /* Helper function for testing the output format */
24890
a54a25221b79 Remove some pointless 'inline' qualifiers
uau
parents: 14621
diff changeset
88 int af_test_output(struct af_instance_s* af, af_data_t* out)
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
89 {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28229
diff changeset
90 if((af->data->format != out->format) ||
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
91 (af->data->bps != out->bps) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
92 (af->data->rate != out->rate) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
93 (af->data->nch != out->nch)){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
94 memcpy(out,af->data,sizeof(af_data_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
95 return AF_FALSE;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
96 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
97 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
98 }
14621
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
99
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
100 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
101 post to Musicdsp.org */
24890
a54a25221b79 Remove some pointless 'inline' qualifiers
uau
parents: 14621
diff changeset
102 float af_softclip(float a)
14621
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
103 {
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
104 if (a >= M_PI/2)
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
105 return 1.0;
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
106 else if (a <= -M_PI/2)
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
107 return -1.0;
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
108 else
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
109 return sin(a);
0293cab15c03 af_softclip
alex
parents: 8674
diff changeset
110 }