]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
macosx: sync option strings with 1.0-bugfix
[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_Hold( 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     
117     free( psz_string );
118     vlc_object_release( p_object );
119 }
120
121 static bool GetFiltersStatus( intf_thread_t *p_intf,
122                                  char *psz_name )
123 {
124     char *psz_parser, *psz_string;
125     vlc_object_t *p_object = vlc_object_find( p_intf,
126                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
127     if( p_object == NULL )
128         p_object = (vlc_object_t *)pl_Hold( p_intf );
129
130     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
131         psz_string = config_GetPsz( p_intf, "audio-filter" );
132
133     if(! psz_string )
134         psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
135
136     vlc_object_release( p_object );
137
138     if( !psz_string ) return false;
139
140     psz_parser = strstr( psz_string, psz_name );
141
142     free( psz_string );
143
144     if ( psz_parser )
145         return true;
146     else
147         return false;
148 }
149
150 - (void)initStrings
151 {
152     int i;
153     [o_btn_equalizer setToolTip: _NS("Equalizer")];
154     [o_btn_equalizer_embedded 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     bool b_2p;
179     int i;
180     bool 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_t *)pl_Hold( p_intf );
186
187     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
188                 VLC_VAR_DOINHERIT );
189     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
190                 VLC_VAR_DOINHERIT );
191
192     psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
193
194     if( psz_bands == NULL )
195         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
196
197     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
198     {
199         b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
200         f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
201     }
202     else
203     {
204         b_2p = var_GetBool( p_object, "equalizer-2pass" );
205         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
206     }
207
208     vlc_object_release( p_object );
209     
210     /* Set the preamp slider */
211     [o_slider_preamp setFloatValue: f_preamp];
212
213     /* Set the bands slider */
214     psz_bands_init = psz_bands;
215
216     for( i = 0; i < 10; i++ )
217     {
218         /* Read dB -20/20 */
219         f_band[i] = strtof( psz_bands, &p_next );
220         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
221     
222         if( !*psz_bands ) break; /* end of line */
223         psz_bands = p_next+1;
224     }
225     free( psz_bands_init );
226     [self setBandSlidersValues:f_band];
227
228     /* Set the the checkboxes */
229     [o_ckb_enable setState: b_enabled];
230
231     [o_ckb_2pass setState: b_2p];        
232 }
233
234 - (IBAction)bandSliderUpdated:(id)sender
235 {
236     intf_thread_t *p_intf = VLCIntf;
237     vlc_object_t *p_object = vlc_object_find( p_intf,
238                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
239
240     if( p_object == NULL )
241         p_object = (vlc_object_t *)pl_Hold( p_intf );
242
243     const char *psz_values;
244     NSString *preset = [NSString stringWithFormat:@"%.1f", [o_slider_band1 floatValue] ];
245     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band2 floatValue] ];
246     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band3 floatValue] ];
247     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band4 floatValue] ];
248     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band5 floatValue] ];
249     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band6 floatValue] ];
250     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band7 floatValue] ];
251     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band8 floatValue] ];
252     preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band9 floatValue] ];
253     preset = [preset stringByAppendingFormat:@"%.1f", [o_slider_band10 floatValue] ];
254
255     psz_values = [preset UTF8String];
256     var_SetString( p_object, "equalizer-bands", psz_values );
257
258     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
259     {
260         /* save changed to config */
261         config_PutPsz( p_intf, "equalizer-bands", psz_values );
262
263         /* save to vlcrc */
264         config_SaveConfigFile( p_intf, "equalizer" );
265     }
266
267     vlc_object_release( p_object );
268 }
269
270 - (IBAction)changePreset:(id)sender
271 {
272     intf_thread_t *p_intf = VLCIntf;
273     int i;
274     vlc_object_t *p_object= vlc_object_find( p_intf,
275                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
276     if( p_object == NULL )
277         p_object = (vlc_object_t *)pl_Hold( p_intf );
278
279     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
280
281     NSString *preset = @"";
282     const char *psz_values;
283     for( i = 0; i < 10; i++ )
284     {
285         preset = [preset stringByAppendingFormat:@"%.1f ", eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] ];
286     }
287     psz_values = [preset UTF8String];
288     var_SetString( p_object, "equalizer-bands", psz_values );
289     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
290
291     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
292
293     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
294
295     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
296     {
297         /* save changed to config */
298         config_PutPsz( p_intf, "equalizer-bands", psz_values );
299         config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
300         config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
301
302         /* save to vlcrc */
303         config_SaveConfigFile( p_intf, "equalizer" );
304     }
305
306     vlc_object_release( p_object );
307 }
308
309 - (IBAction)enable:(id)sender
310 {
311     ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
312 }
313
314 - (IBAction)preampSliderUpdated:(id)sender
315 {
316     intf_thread_t *p_intf = VLCIntf;
317     float f_preamp = [sender floatValue] ;
318
319     vlc_object_t *p_object = vlc_object_find( p_intf,
320                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
321     if( p_object == NULL )
322         p_object = (vlc_object_t *)pl_Hold( p_intf );
323
324     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
325
326     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
327     {
328         /* save changed to config */
329         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
330
331         /* save to vlcrc */
332         config_SaveConfigFile( p_intf, "equalizer" );
333     }
334
335     vlc_object_release( p_object );
336 }
337
338 - (IBAction)toggleWindow:(id)sender
339 {
340     if( [o_window isVisible] )
341     {
342         [o_window orderOut:sender];
343         [o_btn_equalizer setState:NSOffState];
344         [o_btn_equalizer_embedded setState:NSOffState];
345     }
346     else
347     {
348         [o_window makeKeyAndOrderFront:sender];
349         [o_btn_equalizer setState:NSOnState];
350         [o_btn_equalizer_embedded setState:NSOnState];
351     }
352 }
353
354 - (IBAction)twopass:(id)sender
355 {
356     intf_thread_t *p_intf = VLCIntf;
357     bool b_2p = [sender state] ? true : false;
358     vlc_object_t *p_object= vlc_object_find( p_intf,
359                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
360     aout_instance_t *p_aout = (aout_instance_t *)p_object;
361     if( p_object == NULL )
362         p_object = (vlc_object_t *)pl_Hold( p_intf );
363
364     var_SetBool( p_object, "equalizer-2pass", b_2p );
365     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
366     {
367         int i;
368         for( i = 0; i < p_aout->i_nb_inputs; i++ )
369         {
370             p_aout->pp_inputs[i]->b_restart = true;
371         }
372     }
373
374     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
375     {
376         /* save changed to config */
377         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
378
379         /* save to vlcrc */
380         config_SaveConfigFile( p_intf, "equalizer" );
381     }
382
383     vlc_object_release( p_object );
384 }
385
386 - (void)windowWillClose:(NSNotification *)aNotification
387 {
388     [o_btn_equalizer setState: NSOffState];
389 }
390
391 - (void)awakeFromNib
392 {
393     int i;
394     vlc_object_t *p_object= vlc_object_find( VLCIntf,
395                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
396     if( p_object == NULL )
397         p_object = (vlc_object_t *)pl_Hold( VLCIntf );
398
399     [o_window setExcludedFromWindowsMenu: TRUE];
400
401     [self initStrings];
402
403     if( p_object )
404     {
405         char *psz_preset;
406
407         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
408                     VLC_VAR_DOINHERIT );
409         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
410
411         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
412         {
413             if( strcmp( preset_list[i], psz_preset ) )
414                 continue;
415     
416             [o_popup_presets selectItemAtIndex: i];
417         
418
419             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
420             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
421
422             if( strcmp( psz_preset, "flat" ) )
423             {
424                 char psz_bands[100];
425     
426                 snprintf( psz_bands, sizeof( psz_bands ),
427                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
428                           "%.1f %.1f %.1f",
429                           eqz_preset_10b[i]->f_amp[0],
430                           eqz_preset_10b[i]->f_amp[1],
431                           eqz_preset_10b[i]->f_amp[2],
432                           eqz_preset_10b[i]->f_amp[3],
433                           eqz_preset_10b[i]->f_amp[4],
434                           eqz_preset_10b[i]->f_amp[5],
435                           eqz_preset_10b[i]->f_amp[6],
436                           eqz_preset_10b[i]->f_amp[7],
437                           eqz_preset_10b[i]->f_amp[8],
438                           eqz_preset_10b[i]->f_amp[9] );
439     
440                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
441                             VLC_VAR_DOINHERIT );
442                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
443                             VLC_VAR_DOINHERIT );
444                 var_SetFloat( p_object, "equalizer-preamp",
445                               eqz_preset_10b[i]->f_preamp );
446                 var_SetString( p_object, "equalizer-bands", psz_bands );
447             }
448         }
449         free( psz_preset );
450         vlc_object_release( p_object );
451     }
452
453     [self equalizerUpdated];    
454 }
455
456
457 - (id)sliderByIndex:(int)index
458 {
459     switch(index)
460     {
461         case 0 : return o_slider_band1;
462         case 1 : return o_slider_band2;
463         case 2 : return o_slider_band3;
464         case 3 : return o_slider_band4;
465         case 4 : return o_slider_band5;
466         case 5 : return o_slider_band6;
467         case 6 : return o_slider_band7;
468         case 7 : return o_slider_band8;
469         case 8 : return o_slider_band9;
470         case 9 : return o_slider_band10;
471         default : return nil;
472     }
473 }
474
475 - (void)setBandSlidersValues:(float *)values
476 {
477     int i = 0;
478     for (i = 0 ; i<= 9 ; i++)
479     {
480         [self setValue:values[i] forSlider:i];
481     }
482 }
483
484 - (void)initBandSliders
485 {
486     int i = 0;
487     for (i = 0 ; i< 9 ; i++)
488     {
489         [self setValue:0.0 forSlider:i];
490     }
491 }
492
493 - (void)setValue:(float)value forSlider:(int)index
494 {
495     id slider = [self sliderByIndex:index];
496
497     if (slider != nil)
498     {
499         [slider setFloatValue:value];
500     }
501 }
502
503 @end