ao: fix crash after ao init failure (from recent 3a5fd15fa2)
[mplayer/greg.git] / libaf / af_tools.c
blob82e0940fb2209c134e8523ffdb233fa601914eba
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <math.h>
20 #include <string.h>
21 #include "af.h"
23 /* Convert to gain value from dB. Returns AF_OK if of and AF_ERROR if
24 fail */
25 int af_from_dB(int n, float* in, float* out, float k, float mi, float ma)
27 int i = 0;
28 // Sanity check
29 if(!in || !out)
30 return AF_ERROR;
32 for(i=0;i<n;i++){
33 if(in[i]<=-200)
34 out[i]=0.0;
35 else
36 out[i]=pow(10.0,clamp(in[i],mi,ma)/k);
38 return AF_OK;
41 /* Convert from gain value to dB. Returns AF_OK if of and AF_ERROR if
42 fail */
43 int af_to_dB(int n, float* in, float* out, float k)
45 int i = 0;
46 // Sanity check
47 if(!in || !out)
48 return AF_ERROR;
50 for(i=0;i<n;i++){
51 if(in[i] == 0.0)
52 out[i]=-200.0;
53 else
54 out[i]=k*log10(in[i]);
56 return AF_OK;
59 /* Convert from ms to sample time */
60 int af_from_ms(int n, float* in, int* out, int rate, float mi, float ma)
62 int i = 0;
63 // Sanity check
64 if(!in || !out)
65 return AF_ERROR;
67 for(i=0;i<n;i++)
68 out[i]=(int)((float)rate * clamp(in[i],mi,ma)/1000.0);
70 return AF_OK;
73 /* Convert from sample time to ms */
74 int af_to_ms(int n, int* in, float* out, int rate)
76 int i = 0;
77 // Sanity check
78 if(!in || !out || !rate)
79 return AF_ERROR;
81 for(i=0;i<n;i++)
82 out[i]=1000.0 * (float)in[i]/((float)rate);
84 return AF_OK;
87 /* Helper function for testing the output format */
88 int af_test_output(struct af_instance_s* af, af_data_t* out)
90 if((af->data->format != out->format) ||
91 (af->data->bps != out->bps) ||
92 (af->data->rate != out->rate) ||
93 (af->data->nch != out->nch)){
94 memcpy(out,af->data,sizeof(af_data_t));
95 return AF_FALSE;
97 return AF_OK;
100 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
101 post to Musicdsp.org */
102 float af_softclip(float a)
104 if (a >= M_PI/2)
105 return 1.0;
106 else if (a <= -M_PI/2)
107 return -1.0;
108 else
109 return sin(a);