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