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