sync mga_vid.h to revision 265 from the mga_vid repo
[mplayer/glamo.git] / libaf / af_tools.c
blob784be31336feec52b56b87fcf02e3712ffff6345
1 #include <math.h>
2 #include <string.h>
3 #include "af.h"
5 /* Convert to gain value from dB. Returns AF_OK if of and AF_ERROR if
6 fail */
7 int af_from_dB(int n, float* in, float* out, float k, float mi, float ma)
9 int i = 0;
10 // Sanity check
11 if(!in || !out)
12 return AF_ERROR;
14 for(i=0;i<n;i++){
15 if(in[i]<=-200)
16 out[i]=0.0;
17 else
18 out[i]=pow(10.0,clamp(in[i],mi,ma)/k);
20 return AF_OK;
23 /* Convert from gain value to dB. Returns AF_OK if of and AF_ERROR if
24 fail */
25 int af_to_dB(int n, float* in, float* out, float k)
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] == 0.0)
34 out[i]=-200.0;
35 else
36 out[i]=k*log10(in[i]);
38 return AF_OK;
41 /* Convert from ms to sample time */
42 int af_from_ms(int n, float* in, int* out, int rate, float mi, float ma)
44 int i = 0;
45 // Sanity check
46 if(!in || !out)
47 return AF_ERROR;
49 for(i=0;i<n;i++)
50 out[i]=(int)((float)rate * clamp(in[i],mi,ma)/1000.0);
52 return AF_OK;
55 /* Convert from sample time to ms */
56 int af_to_ms(int n, int* in, float* out, int rate)
58 int i = 0;
59 // Sanity check
60 if(!in || !out || !rate)
61 return AF_ERROR;
63 for(i=0;i<n;i++)
64 out[i]=1000.0 * (float)in[i]/((float)rate);
66 return AF_OK;
69 /* Helper function for testing the output format */
70 int af_test_output(struct af_instance_s* af, af_data_t* out)
72 if((af->data->format != out->format) ||
73 (af->data->bps != out->bps) ||
74 (af->data->rate != out->rate) ||
75 (af->data->nch != out->nch)){
76 memcpy(out,af->data,sizeof(af_data_t));
77 return AF_FALSE;
79 return AF_OK;
82 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
83 post to Musicdsp.org */
84 float af_softclip(float a)
86 if (a >= M_PI/2)
87 return 1.0;
88 else if (a <= -M_PI/2)
89 return -1.0;
90 else
91 return sin(a);