]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
Qt: don't check if a variable is a command. This will show radio buttons for zzom...
[vlc] / modules / gui / macosx / equalizer.m
1 /*****************************************************************************
2  * equalizer.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jérôme Decoodt <djc@videolan.org>
8  *          Felix Paul Kühne <fkuehne -at- videolan -dot- org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_aout.h>
34
35 #include "intf.h"
36
37 #include <math.h>
38
39 #include "equalizer.h"
40 #include "../../audio_filter/equalizer_presets.h"
41
42 /*****************************************************************************
43  * VLCEqualizer implementation
44  *****************************************************************************/
45 @implementation VLCEqualizer
46
47 static void ChangeFiltersString( intf_thread_t *p_intf,
48                                  char *psz_name, bool b_add )
49 {
50     char *psz_parser, *psz_string;
51     int i;
52     vlc_object_t *p_object = vlc_object_find( p_intf,
53                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
54     aout_instance_t *p_aout = (aout_instance_t *)p_object;
55     if( !p_object )
56     {
57         p_object = (vlc_object_t *)pl_Yield( p_intf );
58     }
59
60     psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
61
62     if( !psz_string ) psz_string = strdup( "" );
63
64     psz_parser = strstr( psz_string, psz_name );
65
66     if( b_add )
67     {
68         if( !psz_parser )
69         {
70             psz_parser = psz_string;
71             asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
72                             psz_string, psz_name );
73             free( psz_parser );
74         }
75         else
76         {
77             vlc_object_release( p_object );
78             return;
79         }
80     }
81     else
82     {
83         if( psz_parser )
84         {
85             memmove( psz_parser, psz_parser + strlen( psz_name ) +
86                             ( *( psz_parser + strlen( psz_name ) ) == ',' ? 1 : 0 ),
87                             strlen( psz_parser + strlen( psz_name ) ) + 1 );
88
89             if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
90             {
91                 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
92             }
93          }
94          else
95          {
96             free( psz_string );
97             vlc_object_release( p_object );
98             return;
99          }
100     }
101
102     var_SetString( p_object, "audio-filter", psz_string );
103     if( p_aout )
104     {
105         for( i = 0; i < p_aout->i_nb_inputs; i++ )
106         {
107             p_aout->pp_inputs[i]->b_restart = true;
108         }
109     }
110     
111     if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
112     {
113         /* save changed to config */
114         config_PutPsz( p_object, "audio-filter", psz_string );
115
116         /* save to vlcrc */
117         config_SaveConfigFile( p_object, "main" );
118     }
119     
120     free( psz_string );
121     vlc_object_release( p_object );
122 }
123
124 static bool GetFiltersStatus( intf_thread_t *p_intf,
125                                  char *psz_name )
126 {
127     char *psz_parser, *psz_string;
128     vlc_object_t *p_object = vlc_object_find( p_intf,
129                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
130     if( p_object == NULL )
131         p_object = (vlc_object_t *)pl_Yield( p_intf );
132
133     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
134         psz_string = config_GetPsz( p_intf, "audio-filter" );
135
136     if(! psz_string )
137         psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
138
139     vlc_object_release( p_object );
140
141     if( !psz_string ) return false;
142
143     psz_parser = strstr( psz_string, psz_name );
144
145     free( psz_string );
146
147     if ( psz_parser )
148         return true;
149     else
150         return false;
151 }
152
153 - (void)initStrings
154 {
155     int i;
156     [o_btn_equalizer setToolTip: _NS("Equalizer")];
157     [o_ckb_2pass setTitle: _NS("2 Pass")];
158     [o_ckb_2pass setToolTip: _NS("Apply the "
159         "equalizer filter twice. The effect will be sharper.")];
160     [o_ckb_enable setTitle: _NS("Enable")];
161     [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
162         "manually or using a preset.")];
163     [o_fld_preamp setStringValue: _NS("Preamp")];
164
165     [o_popup_presets removeAllItems];
166     for( i = 0; i < 18 ; i++ )
167     {
168         [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
169     }
170     [o_window setTitle: _NS("Equalizer")];
171
172     [self initBandSliders];
173 }
174
175 - (void)equalizerUpdated
176 {
177     intf_thread_t *p_intf = VLCIntf;
178     float f_preamp, f_band[10];
179     char *psz_bands, *psz_bands_init, *p_next;
180     bool b_2p;
181     int i;
182     bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
183     vlc_object_t *p_object = vlc_object_find( p_intf,
184                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
185
186     if( p_object == NULL )
187         p_object = (vlc_object_t *)pl_Yield( p_intf );
188
189     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
190                 VLC_VAR_DOINHERIT );
191     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
192                 VLC_VAR_DOINHERIT );
193
194     psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
195
196     if( psz_bands == NULL )
197         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
198
199     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
200     {
201         b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
202         f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
203     }
204     else
205     {
206         b_2p = var_GetBool( p_object, "equalizer-2pass" );
207         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
208     }
209
210     vlc_object_release( p_object );
211     
212     /* Set the preamp slider */
213     [o_slider_preamp setFloatValue: f_preamp];
214
215     /* Set the bands slider */
216     psz_bands_init = psz_bands;
217
218     for( i = 0; i < 10; i++ )
219     {
220         /* Read dB -20/20 */
221 #ifdef HAVE_STRTOF
222         f_band[i] = strtof( psz_bands, &p_next );
223 #else
224         f_band[i] = (float)strtod( psz_bands, &p_next );
225 #endif
226         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
227     
228         if( !*psz_bands ) break; /* end of line */
229         psz_bands = p_next+1;
230     }
231     free( psz_bands_init );
232     [self setBandSlidersValues:f_band];
233
234     /* Set the the checkboxes */
235     [o_ckb_enable setState: b_enabled];
236
237     [o_ckb_2pass setState: b_2p];        
238 }
239
240 - (IBAction)bandSliderUpdated:(id)sender
241 {
242     intf_thread_t *p_intf = VLCIntf;
243     vlc_object_t *p_object = vlc_object_find( p_intf,
244                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
245
246     if( p_object == NULL )
247         p_object = (vlc_object_t *)pl_Yield( p_intf );
248
249     char psz_values[102];
250     memset( psz_values, 0, 102 );
251
252     /* Write the new bands values */
253     /* TODO: write a generic code instead of ten times the same thing */
254
255     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
256     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
257     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
258     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
259     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
260     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
261     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
262     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
263     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
264     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
265
266     var_SetString( p_object, "equalizer-bands", psz_values );
267
268     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
269     {
270         /* save changed to config */
271         config_PutPsz( p_intf, "equalizer-bands", psz_values );
272
273         /* save to vlcrc */
274         config_SaveConfigFile( p_intf, "equalizer" );
275     }
276
277     vlc_object_release( p_object );
278 }
279
280 - (IBAction)changePreset:(id)sender
281 {
282     intf_thread_t *p_intf = VLCIntf;
283     int i;
284     vlc_object_t *p_object= vlc_object_find( p_intf,
285                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
286     if( p_object == NULL )
287         p_object = (vlc_object_t *)pl_Yield( p_intf );
288
289     char psz_values[102];
290     memset( psz_values, 0, 102 );
291
292     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
293
294     for( i = 0; i < 10; i++ )
295         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
296     var_SetString( p_object, "equalizer-bands", psz_values );
297     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
298
299     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
300
301     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
302
303     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
304     {
305         /* save changed to config */
306         config_PutPsz( p_intf, "equalizer-bands", psz_values );
307         config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
308         config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
309
310         /* save to vlcrc */
311         config_SaveConfigFile( p_intf, "equalizer" );
312     }
313
314     vlc_object_release( p_object );
315 }
316
317 - (IBAction)enable:(id)sender
318 {
319     ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
320 }
321
322 - (IBAction)preampSliderUpdated:(id)sender
323 {
324     intf_thread_t *p_intf = VLCIntf;
325     float f_preamp = [sender floatValue] ;
326
327     vlc_object_t *p_object = vlc_object_find( p_intf,
328                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
329     if( p_object == NULL )
330         p_object = (vlc_object_t *)pl_Yield( p_intf );
331
332     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
333
334     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
335     {
336         /* save changed to config */
337         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
338
339         /* save to vlcrc */
340         config_SaveConfigFile( p_intf, "equalizer" );
341     }
342
343     vlc_object_release( p_object );
344 }
345
346 - (IBAction)toggleWindow:(id)sender
347 {
348     if( [o_window isVisible] )
349     {
350         [o_window orderOut:sender];
351         [o_btn_equalizer setState:NSOffState];
352     }
353     else
354     {
355         [o_window makeKeyAndOrderFront:sender];
356         [o_btn_equalizer setState:NSOnState];
357     }
358 }
359
360 - (IBAction)twopass:(id)sender
361 {
362     intf_thread_t *p_intf = VLCIntf;
363     bool b_2p = [sender state] ? true : false;
364     vlc_object_t *p_object= vlc_object_find( p_intf,
365                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
366     aout_instance_t *p_aout = (aout_instance_t *)p_object;
367     if( p_object == NULL )
368         p_object = (vlc_object_t *)pl_Yield( p_intf );
369
370     var_SetBool( p_object, "equalizer-2pass", b_2p );
371     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
372     {
373         int i;
374         for( i = 0; i < p_aout->i_nb_inputs; i++ )
375         {
376             p_aout->pp_inputs[i]->b_restart = true;
377         }
378     }
379
380     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
381     {
382         /* save changed to config */
383         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
384
385         /* save to vlcrc */
386         config_SaveConfigFile( p_intf, "equalizer" );
387     }
388
389     vlc_object_release( p_object );
390 }
391
392 - (void)windowWillClose:(NSNotification *)aNotification
393 {
394     [o_btn_equalizer setState: NSOffState];
395 }
396
397 - (void)awakeFromNib
398 {
399     int i;
400     vlc_object_t *p_object= vlc_object_find( VLCIntf,
401                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
402     if( p_object == NULL )
403         p_object = (vlc_object_t *)pl_Yield( VLCIntf );
404
405     [o_window setExcludedFromWindowsMenu: TRUE];
406
407     [self initStrings];
408
409     if( p_object )
410     {
411         char *psz_preset;
412
413         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
414                     VLC_VAR_DOINHERIT );
415         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
416
417         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
418         {
419             if( strcmp( preset_list[i], psz_preset ) )
420                 continue;
421     
422             [o_popup_presets selectItemAtIndex: i];
423         
424
425             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
426             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
427
428             if( strcmp( psz_preset, "flat" ) )
429             {
430                 char psz_bands[100];
431     
432                 snprintf( psz_bands, sizeof( psz_bands ),
433                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
434                           "%.1f %.1f %.1f",
435                           eqz_preset_10b[i]->f_amp[0],
436                           eqz_preset_10b[i]->f_amp[1],
437                           eqz_preset_10b[i]->f_amp[2],
438                           eqz_preset_10b[i]->f_amp[3],
439                           eqz_preset_10b[i]->f_amp[4],
440                           eqz_preset_10b[i]->f_amp[5],
441                           eqz_preset_10b[i]->f_amp[6],
442                           eqz_preset_10b[i]->f_amp[7],
443                           eqz_preset_10b[i]->f_amp[8],
444                           eqz_preset_10b[i]->f_amp[9] );
445     
446                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
447                             VLC_VAR_DOINHERIT );
448                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
449                             VLC_VAR_DOINHERIT );
450                 var_SetFloat( p_object, "equalizer-preamp",
451                               eqz_preset_10b[i]->f_preamp );
452                 var_SetString( p_object, "equalizer-bands", psz_bands );
453             }
454         }
455         free( psz_preset );
456         vlc_object_release( p_object );
457     }
458
459     [self equalizerUpdated];    
460 }
461
462
463 - (id)getSliderByIndex:(int)index
464 {
465     switch(index)
466     {
467         case 0 : return o_slider_band1;
468         case 1 : return o_slider_band2;
469         case 2 : return o_slider_band3;
470         case 3 : return o_slider_band4;
471         case 4 : return o_slider_band5;
472         case 5 : return o_slider_band6;
473         case 6 : return o_slider_band7;
474         case 7 : return o_slider_band8;
475         case 8 : return o_slider_band9;
476         case 9 : return o_slider_band10;
477         default : return nil;
478     }
479 }
480
481 - (void)setBandSlidersValues:(float *)values
482 {
483     int i = 0;
484     for (i = 0 ; i<= 9 ; i++)
485     {
486         [self setValue:values[i] forSlider:i];
487     }
488 }
489
490 - (void)initBandSliders
491 {
492     int i = 0;
493     for (i = 0 ; i< 9 ; i++)
494     {
495         [self setValue:0.0 forSlider:i];
496     }
497 }
498
499 - (void)setValue:(float)value forSlider:(int)index
500 {
501     id slider = [self getSliderByIndex:index];
502
503     if (slider != nil)
504     {
505         [slider setFloatValue:value];
506     }
507 }
508
509 @end