Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / goom / sound_tester.c
1 /* Goom Project
2  * Copyright (C) <2003> iOS-Software
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "sound_tester.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 /* some constants */
25 #define BIG_GOOM_DURATION 100
26 #define BIG_GOOM_SPEED_LIMIT 0.1f
27
28 #define ACCEL_MULT 0.95f
29 #define SPEED_MULT 0.99f
30
31
32 void
33 evaluate_sound (gint16 data[2][512], SoundInfo * info)
34 {
35
36   int i;
37   float difaccel;
38   float prevspeed;
39
40   /* find the max */
41   int incvar = 0;
42
43   for (i = 0; i < 512; i += 2) {
44     if (incvar < data[0][i])
45       incvar = data[0][i];
46   }
47
48   if (incvar > info->allTimesMax)
49     info->allTimesMax = incvar;
50
51   /* volume sonore */
52   info->volume = (float) incvar / (float) info->allTimesMax;
53   memcpy (info->samples[0], data[0], 512 * sizeof (short));
54   memcpy (info->samples[1], data[1], 512 * sizeof (short));
55
56   difaccel = info->accelvar;
57   info->accelvar = info->volume;        /* accel entre 0 et 1 */
58
59   /* transformations sur la vitesse du son */
60   if (info->speedvar > 1.0f)
61     info->speedvar = 1.0f;
62
63   if (info->speedvar < 0.1f)
64     info->accelvar *= (1.0f - (float) info->speedvar);
65   else if (info->speedvar < 0.3f)
66     info->accelvar *= (0.9f - (float) (info->speedvar - 0.1f) / 2.0f);
67   else
68     info->accelvar *= (0.8f - (float) (info->speedvar - 0.3f) / 4.0f);
69
70   /* adoucissement de l'acceleration */
71   info->accelvar *= ACCEL_MULT;
72   if (info->accelvar < 0)
73     info->accelvar = 0;
74
75   difaccel = info->accelvar - difaccel;
76   if (difaccel < 0)
77     difaccel = -difaccel;
78
79   /* mise a jour de la vitesse */
80   prevspeed = info->speedvar;
81   info->speedvar = (info->speedvar + difaccel * 0.5f) / 2;
82   info->speedvar *= SPEED_MULT;
83   info->speedvar = (info->speedvar + 3.0f * prevspeed) / 4.0f;
84   if (info->speedvar < 0)
85     info->speedvar = 0;
86   if (info->speedvar > 1)
87     info->speedvar = 1;
88
89   /* temps du goom */
90   info->timeSinceLastGoom++;
91   info->timeSinceLastBigGoom++;
92   info->cycle++;
93
94   /* detection des nouveaux gooms */
95   if ((info->speedvar > (float) IVAL (info->biggoom_speed_limit_p) / 100.0f)
96       && (info->accelvar > info->bigGoomLimit)
97       && (info->timeSinceLastBigGoom > BIG_GOOM_DURATION)) {
98     info->timeSinceLastBigGoom = 0;
99   }
100
101   if (info->accelvar > info->goom_limit) {
102     /* TODO: tester && (info->timeSinceLastGoom > 20)) { */
103     info->totalgoom++;
104     info->timeSinceLastGoom = 0;
105     info->goomPower = info->accelvar - info->goom_limit;
106   }
107
108   if (info->accelvar > info->prov_max)
109     info->prov_max = info->accelvar;
110
111   if (info->goom_limit > 1)
112     info->goom_limit = 1;
113
114   /* toute les 2 secondes : v�rifier si le taux de goom est correct
115    * et le modifier sinon.. */
116   if (info->cycle % 64 == 0) {
117     if (info->speedvar < 0.01f)
118       info->goom_limit *= 0.91;
119     if (info->totalgoom > 4) {
120       info->goom_limit += 0.02;
121     }
122     if (info->totalgoom > 7) {
123       info->goom_limit *= 1.03f;
124       info->goom_limit += 0.03;
125     }
126     if (info->totalgoom > 16) {
127       info->goom_limit *= 1.05f;
128       info->goom_limit += 0.04;
129     }
130     if (info->totalgoom == 0) {
131       info->goom_limit = info->prov_max - 0.02;
132     }
133     if ((info->totalgoom == 1) && (info->goom_limit > 0.02))
134       info->goom_limit -= 0.01;
135     info->totalgoom = 0;
136     info->bigGoomLimit =
137         info->goom_limit * (1.0f +
138         (float) IVAL (info->biggoom_factor_p) / 500.0f);
139     info->prov_max = 0;
140   }
141
142   /* mise a jour des parametres pour la GUI */
143   FVAL (info->volume_p) = info->volume;
144   info->volume_p.change_listener (&info->volume_p);
145   FVAL (info->speed_p) = info->speedvar * 4;
146   info->speed_p.change_listener (&info->speed_p);
147   FVAL (info->accel_p) = info->accelvar;
148   info->accel_p.change_listener (&info->accel_p);
149
150   FVAL (info->goom_limit_p) = info->goom_limit;
151   info->goom_limit_p.change_listener (&info->goom_limit_p);
152   FVAL (info->goom_power_p) = info->goomPower;
153   info->goom_power_p.change_listener (&info->goom_power_p);
154   FVAL (info->last_goom_p) = 1.0 - ((float) info->timeSinceLastGoom / 20.0f);
155   info->last_goom_p.change_listener (&info->last_goom_p);
156   FVAL (info->last_biggoom_p) =
157       1.0 - ((float) info->timeSinceLastBigGoom / 40.0f);
158   info->last_biggoom_p.change_listener (&info->last_biggoom_p);
159
160   /* bigGoomLimit ==goomLimit*9/8+7 ? */
161 }