]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
macosx: Fix controller playlist toggling to use the contentRect and not the window...
[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 == NULL )
56         p_object = pl_Yield( p_intf );
57     if( p_object == NULL )
58         return;
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             return;
78         }
79     }
80     else
81     {
82         if( psz_parser )
83         {
84             memmove( psz_parser, psz_parser + strlen( psz_name ) +
85                             ( *( psz_parser + strlen( psz_name ) ) == ',' ? 1 : 0 ),
86                             strlen( psz_parser + strlen( psz_name ) ) + 1 );
87
88             if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
89             {
90                 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
91             }
92          }
93          else
94          {
95              free( psz_string );
96              return;
97          }
98     }
99
100     var_SetString( p_object, "audio-filter", psz_string );
101     if( p_aout )
102     {
103         for( i = 0; i < p_aout->i_nb_inputs; i++ )
104         {
105             p_aout->pp_inputs[i]->b_restart = true;
106         }
107     }
108     
109     if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
110     {
111         /* save changed to config */
112         config_PutPsz( p_object, "audio-filter", psz_string );
113
114         /* save to vlcrc */
115         config_SaveConfigFile( p_object, "main" );
116     }
117     
118     free( psz_string );
119     vlc_object_release( p_object );
120 }
121
122 static bool GetFiltersStatus( intf_thread_t *p_intf,
123                                  char *psz_name )
124 {
125     char *psz_parser, *psz_string;
126     vlc_object_t *p_object = vlc_object_find( p_intf,
127                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
128     if( p_object == NULL )
129         p_object = pl_Yield( p_intf );
130     if( p_object == NULL )
131         return false;
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 = pl_Yield( p_intf );
188     if( p_object == NULL )
189         return;
190
191     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
192                 VLC_VAR_DOINHERIT );
193     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
194                 VLC_VAR_DOINHERIT );
195
196     psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
197
198     if( psz_bands == NULL )
199         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
200
201     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
202     {
203         b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
204         f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
205     }
206     else
207     {
208         b_2p = var_GetBool( p_object, "equalizer-2pass" );
209         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
210     }
211
212     vlc_object_release( p_object );
213     
214     /* Set the preamp slider */
215     [o_slider_preamp setFloatValue: f_preamp];
216
217     /* Set the bands slider */
218     psz_bands_init = psz_bands;
219
220     for( i = 0; i < 10; i++ )
221     {
222         /* Read dB -20/20 */
223 #ifdef HAVE_STRTOF
224         f_band[i] = strtof( psz_bands, &p_next );
225 #else
226         f_band[i] = (float)strtod( psz_bands, &p_next );
227 #endif
228         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
229     
230         if( !*psz_bands ) break; /* end of line */
231         psz_bands = p_next+1;
232     }
233     free( psz_bands_init );
234     [self setBandSlidersValues:f_band];
235
236     /* Set the the checkboxes */
237     [o_ckb_enable setState: b_enabled];
238
239     [o_ckb_2pass setState: b_2p];        
240 }
241
242 - (IBAction)bandSliderUpdated:(id)sender
243 {
244     intf_thread_t *p_intf = VLCIntf;
245     vlc_object_t *p_object = vlc_object_find( p_intf,
246                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
247
248     if( p_object == NULL )
249         p_object = pl_Yield( p_intf );
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 = pl_Yield( p_intf );
292     if( p_object == NULL )
293         return;
294
295     char psz_values[102];
296     memset( psz_values, 0, 102 );
297
298     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
299
300     for( i = 0; i < 10; i++ )
301         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
302     var_SetString( p_object, "equalizer-bands", psz_values );
303     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
304
305     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
306
307     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
308
309     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
310     {
311         /* save changed to config */
312         config_PutPsz( p_intf, "equalizer-bands", psz_values );
313         config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
314         config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
315
316         /* save to vlcrc */
317         config_SaveConfigFile( p_intf, "equalizer" );
318     }
319
320     vlc_object_release( p_object );
321 }
322
323 - (IBAction)enable:(id)sender
324 {
325     ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
326 }
327
328 - (IBAction)preampSliderUpdated:(id)sender
329 {
330     intf_thread_t *p_intf = VLCIntf;
331     float f_preamp = [sender floatValue] ;
332
333     vlc_object_t *p_object = vlc_object_find( p_intf,
334                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
335     if( p_object == NULL )
336         p_object = pl_Yield( p_intf );
337     if( p_object == NULL )
338         return;
339
340     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
341
342     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
343     {
344         /* save changed to config */
345         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
346
347         /* save to vlcrc */
348         config_SaveConfigFile( p_intf, "equalizer" );
349     }
350
351     vlc_object_release( p_object );
352 }
353
354 - (IBAction)toggleWindow:(id)sender
355 {
356     if( [o_window isVisible] )
357     {
358         [o_window orderOut:sender];
359         [o_btn_equalizer setState:NSOffState];
360     }
361     else
362     {
363         [o_window makeKeyAndOrderFront:sender];
364         [o_btn_equalizer setState:NSOnState];
365     }
366 }
367
368 - (IBAction)twopass:(id)sender
369 {
370     intf_thread_t *p_intf = VLCIntf;
371     bool b_2p = [sender state] ? true : false;
372     vlc_object_t *p_object= vlc_object_find( p_intf,
373                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
374     aout_instance_t *p_aout = (aout_instance_t *)p_object;
375     if( p_object == NULL )
376         p_object = pl_Yield( p_intf );
377     if( p_object == NULL )
378         return;
379
380     var_SetBool( p_object, "equalizer-2pass", b_2p );
381     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
382     {
383         int i;
384         for( i = 0; i < p_aout->i_nb_inputs; i++ )
385         {
386             p_aout->pp_inputs[i]->b_restart = true;
387         }
388     }
389
390     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
391     {
392         /* save changed to config */
393         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
394
395         /* save to vlcrc */
396         config_SaveConfigFile( p_intf, "equalizer" );
397     }
398
399     vlc_object_release( p_object );
400 }
401
402 - (void)windowWillClose:(NSNotification *)aNotification
403 {
404     [o_btn_equalizer setState: NSOffState];
405 }
406
407 - (void)awakeFromNib
408 {
409     int i;
410     vlc_object_t *p_object= vlc_object_find( VLCIntf,
411                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
412     if( p_object == NULL )
413         p_object = pl_Yield( VLCIntf );
414
415     [o_window setExcludedFromWindowsMenu: TRUE];
416
417     [self initStrings];
418
419     if( p_object )
420     {
421         char *psz_preset;
422
423         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
424                     VLC_VAR_DOINHERIT );
425         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
426
427         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
428         {
429             if( strcmp( preset_list[i], psz_preset ) )
430                 continue;
431     
432             [o_popup_presets selectItemAtIndex: i];
433         
434
435             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
436             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
437
438             if( strcmp( psz_preset, "flat" ) )
439             {
440                 char psz_bands[100];
441     
442                 snprintf( psz_bands, sizeof( psz_bands ),
443                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
444                           "%.1f %.1f %.1f",
445                           eqz_preset_10b[i]->f_amp[0],
446                           eqz_preset_10b[i]->f_amp[1],
447                           eqz_preset_10b[i]->f_amp[2],
448                           eqz_preset_10b[i]->f_amp[3],
449                           eqz_preset_10b[i]->f_amp[4],
450                           eqz_preset_10b[i]->f_amp[5],
451                           eqz_preset_10b[i]->f_amp[6],
452                           eqz_preset_10b[i]->f_amp[7],
453                           eqz_preset_10b[i]->f_amp[8],
454                           eqz_preset_10b[i]->f_amp[9] );
455     
456                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
457                             VLC_VAR_DOINHERIT );
458                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
459                             VLC_VAR_DOINHERIT );
460                 var_SetFloat( p_object, "equalizer-preamp",
461                               eqz_preset_10b[i]->f_preamp );
462                 var_SetString( p_object, "equalizer-bands", psz_bands );
463             }
464         }
465         free( psz_preset );
466         vlc_object_release( p_object );
467     }
468
469     [self equalizerUpdated];    
470 }
471
472
473 - (id)getSliderByIndex:(int)index
474 {
475     switch(index)
476     {
477         case 0 : return o_slider_band1;
478         case 1 : return o_slider_band2;
479         case 2 : return o_slider_band3;
480         case 3 : return o_slider_band4;
481         case 4 : return o_slider_band5;
482         case 5 : return o_slider_band6;
483         case 6 : return o_slider_band7;
484         case 7 : return o_slider_band8;
485         case 8 : return o_slider_band9;
486         case 9 : return o_slider_band10;
487         default : return nil;
488     }
489 }
490
491 - (void)setBandSlidersValues:(float *)values
492 {
493     int i = 0;
494     for (i = 0 ; i<= 9 ; i++)
495     {
496         [self setValue:values[i] forSlider:i];
497     }
498 }
499
500 - (void)initBandSliders
501 {
502     int i = 0;
503     for (i = 0 ; i< 9 ; i++)
504     {
505         [self setValue:0.0 forSlider:i];
506     }
507 }
508
509 - (void)setValue:(float)value forSlider:(int)index
510 {
511     id slider = [self getSliderByIndex:index];
512
513     if (slider != nil)
514     {
515         [slider setFloatValue:value];
516     }
517 }
518
519 @end