@@ -22,6 +22,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
2222
2323#define STS_AUDSRC "STS_AUDSRC"
2424#define STS_MINLVL "STS_MIN_LVL"
25+ #define STS_MAXLVL "STS_MAXLVL"
2526#define STS_MINPER "STS_MINPER"
2627#define STS_MAXPER "STS_MAXPER"
2728#define STS_INVSCL "STS_INVSCL"
@@ -40,7 +41,10 @@ struct scale_to_sound_data {
4041 obs_source_t * target ;
4142
4243 obs_property_t * sources_list ;
44+ obs_source_t * audio_source ;
4345 double minimum_audio_level ;
46+ double maximum_audio_level ;
47+ double audio_range ;
4448 bool invert ;
4549 long long min ;
4650 long long max ;
@@ -59,7 +63,6 @@ struct scale_to_sound_data {
5963 double audio_level ;
6064
6165 gs_effect_t * mover ;
62- obs_source_t * audio_source ;
6366};
6467
6568static void calculate_audio_level (void * param , obs_source_t * source , const struct audio_data * data , bool muted )
@@ -90,13 +93,10 @@ static void calculate_audio_level(void *param, obs_source_t *source, const struc
9093 sum += sample * sample ;
9194 }
9295
93-
9496 double audio_level = (double )obs_mul_to_db (sqrtf (sum / nr_samples ));
9597
96- if (smooth ) {
97- smooth = 1 - smooth ;
98-
99- if (stsf -> audio_level < min_audio_level ) stsf -> audio_level = min_audio_level ;
98+ if (smooth < 1 ) {
99+ if (stsf -> audio_level < min_audio_level ) stsf -> audio_level = min_audio_level ;
100100
101101 if (stsf -> audio_level > audio_level ) stsf -> audio_level -= smooth ;
102102 else if (stsf -> audio_level < audio_level ) stsf -> audio_level += smooth ;
@@ -147,7 +147,7 @@ static void filter_update(void *data, obs_data_t *settings)
147147
148148 stsf -> invert = obs_data_get_bool (settings , STS_INVSCL );
149149
150- stsf -> smooth = obs_data_get_double (settings , STS_SMOOTH );
150+ stsf -> smooth = 1 - obs_data_get_double (settings , STS_SMOOTH );
151151
152152 stsf -> scale_w = obs_data_get_bool (settings , STS_SCALEW );
153153 stsf -> scale_h = obs_data_get_bool (settings , STS_SCALEH );
@@ -158,6 +158,21 @@ static void filter_update(void *data, obs_data_t *settings)
158158 stsf -> max_h = h * max / 100 ;
159159
160160 stsf -> minimum_audio_level = obs_data_get_double (settings , STS_MINLVL );
161+ double maximum_audio_level = obs_data_get_double (settings , STS_MAXLVL );
162+ if (maximum_audio_level > stsf -> minimum_audio_level ) {
163+ stsf -> maximum_audio_level = maximum_audio_level ;
164+
165+ double range = fabs (stsf -> maximum_audio_level - stsf -> minimum_audio_level );
166+ if (range == 0 ) {
167+ stsf -> audio_range = 0.5f ;
168+ }
169+ else stsf -> audio_range = range ;
170+ }
171+ else {
172+ obs_data_set_double (settings , STS_MAXLVL , stsf -> minimum_audio_level + 0.5f );
173+ stsf -> maximum_audio_level = stsf -> minimum_audio_level + 0.5f ;
174+ stsf -> audio_range = 0.5f ;
175+ }
161176
162177 obs_source_t * audio_source = obs_get_source_by_name (obs_data_get_string (settings , STS_AUDSRC ));
163178
@@ -204,6 +219,9 @@ static obs_properties_t *filter_properties(void *data)
204219 obs_property_t * minlvl = obs_properties_add_float_slider (p , STS_MINLVL , "Audio Threshold" , -100 , -0.5 , 0.5 );
205220 obs_property_float_set_suffix (minlvl , "dB" );
206221
222+ obs_property_t * maxlvl = obs_properties_add_float_slider (p , STS_MAXLVL , "Audio Ceiling" , -99.5 , 0 , 0.5 );
223+ obs_property_float_set_suffix (maxlvl , "dB" );
224+
207225 obs_property_t * minper = obs_properties_add_int_slider (p , STS_MINPER , "Minimum Size" , 0 , 99 , 1 );
208226 obs_property_int_set_suffix (minper , "%" );
209227
@@ -223,6 +241,7 @@ static obs_properties_t *filter_properties(void *data)
223241static void filter_defaults (obs_data_t * settings )
224242{
225243 obs_data_set_default_double (settings , STS_MINLVL , -40 );
244+ obs_data_set_default_double (settings , STS_MAXLVL , 0 );
226245
227246 obs_data_set_default_int (settings , STS_MINPER , 90 );
228247 obs_data_set_default_int (settings , STS_MAXPER , 100 );
@@ -252,10 +271,10 @@ static void filter_destroy(void *data)
252271static void target_update (void * data , float seconds )
253272{
254273 UNUSED_PARAMETER (seconds );
255-
256- //!This should really be done using a signal but I could not get those working so here we are...
274+
257275 struct scale_to_sound_data * stsf = data ;
258276
277+ //!This should really be done using a signal but I could not get those working so here we are...
259278 obs_source_t * target = stsf -> target ;
260279
261280 uint32_t w = stsf -> src_w ;
@@ -284,13 +303,17 @@ static void filter_render(void *data, gs_effect_t *effect)
284303 uint32_t max_scale_percent = stsf -> max ;
285304
286305 double min_audio_level = stsf -> minimum_audio_level ;
306+ double max_audio_level = stsf -> maximum_audio_level ;
307+
308+ double range = stsf -> audio_range ;
309+
287310 double audio_level = stsf -> audio_level ;
288311
289312 if (min_audio_level >= 0 ) min_audio_level = -0.5f ;
290313 double scale_percent = fabs (min_audio_level ) - fabs (audio_level );
291314
292315 //Scale the calculated from audio precentage down to the user-set range
293- scale_percent = (scale_percent * (max_scale_percent - min_scale_percent )) / fabs ( min_audio_level ) + min_scale_percent ;
316+ scale_percent = (scale_percent * (max_scale_percent - min_scale_percent )) / range + min_scale_percent ;
294317 if (scale_percent < min_scale_percent || audio_level >= 0 ) scale_percent = min_scale_percent ;
295318
296319 if (stsf -> invert ) scale_percent = min_scale_percent + max_scale_percent - scale_percent ;
0 commit comments