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