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