1 /*****************************************************************************
2 * equalizer.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
7 * Authors: Jérôme Decoodt <djc@videolan.org>
8 * Felix Paul Kühne <fkuehne -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
39 #include "equalizer.h"
40 #include "../../audio_filter/equalizer_presets.h"
42 /*****************************************************************************
43 * VLCEqualizer implementation
44 *****************************************************************************/
45 @implementation VLCEqualizer
47 static void ChangeFiltersString( intf_thread_t *p_intf,
48 char *psz_name, bool b_add )
50 char *psz_parser, *psz_string;
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;
57 p_object = (vlc_object_t *)pl_Yield( p_intf );
60 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
62 if( !psz_string ) psz_string = strdup( "" );
64 psz_parser = strstr( psz_string, psz_name );
70 psz_parser = psz_string;
71 asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
72 psz_string, psz_name );
77 vlc_object_release( p_object );
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 );
89 if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
91 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
97 vlc_object_release( p_object );
102 var_SetString( p_object, "audio-filter", psz_string );
105 for( i = 0; i < p_aout->i_nb_inputs; i++ )
107 p_aout->pp_inputs[i]->b_restart = true;
111 if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
113 /* save changed to config */
114 config_PutPsz( p_object, "audio-filter", psz_string );
117 config_SaveConfigFile( p_object, "main" );
121 vlc_object_release( p_object );
124 static bool GetFiltersStatus( intf_thread_t *p_intf,
127 char *psz_parser, *psz_string;
128 vlc_object_t *p_object = vlc_object_find( p_intf,
129 VLC_OBJECT_AOUT, FIND_ANYWHERE );
130 if( p_object == NULL )
131 p_object = (vlc_object_t *)pl_Yield( p_intf );
133 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
134 psz_string = config_GetPsz( p_intf, "audio-filter" );
137 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
139 vlc_object_release( p_object );
141 if( !psz_string ) return false;
143 psz_parser = strstr( psz_string, psz_name );
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")];
165 [o_popup_presets removeAllItems];
166 for( i = 0; i < 18 ; i++ )
168 [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
170 [o_window setTitle: _NS("Equalizer")];
172 [self initBandSliders];
175 - (void)equalizerUpdated
177 intf_thread_t *p_intf = VLCIntf;
178 float f_preamp, f_band[10];
179 char *psz_bands, *psz_bands_init, *p_next;
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 );
186 if( p_object == NULL )
187 p_object = (vlc_object_t *)pl_Yield( p_intf );
189 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
191 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
194 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
196 if( psz_bands == NULL )
197 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
199 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
201 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
202 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
206 b_2p = var_GetBool( p_object, "equalizer-2pass" );
207 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
210 vlc_object_release( p_object );
212 /* Set the preamp slider */
213 [o_slider_preamp setFloatValue: f_preamp];
215 /* Set the bands slider */
216 psz_bands_init = psz_bands;
218 for( i = 0; i < 10; i++ )
222 f_band[i] = strtof( psz_bands, &p_next );
224 f_band[i] = (float)strtod( psz_bands, &p_next );
226 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
228 if( !*psz_bands ) break; /* end of line */
229 psz_bands = p_next+1;
231 free( psz_bands_init );
232 [self setBandSlidersValues:f_band];
234 /* Set the the checkboxes */
235 [o_ckb_enable setState: b_enabled];
237 [o_ckb_2pass setState: b_2p];
240 - (IBAction)bandSliderUpdated:(id)sender
242 intf_thread_t *p_intf = VLCIntf;
243 vlc_object_t *p_object = vlc_object_find( p_intf,
244 VLC_OBJECT_AOUT, FIND_ANYWHERE );
246 if( p_object == NULL )
247 p_object = (vlc_object_t *)pl_Yield( p_intf );
249 char psz_values[102];
250 memset( psz_values, 0, 102 );
252 /* Write the new bands values */
253 /* TODO: write a generic code instead of ten times the same thing */
255 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
256 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
257 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
258 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
259 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
260 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
261 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
262 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
263 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
264 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
266 var_SetString( p_object, "equalizer-bands", psz_values );
268 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
270 /* save changed to config */
271 config_PutPsz( p_intf, "equalizer-bands", psz_values );
274 config_SaveConfigFile( p_intf, "equalizer" );
277 vlc_object_release( p_object );
280 - (IBAction)changePreset:(id)sender
282 intf_thread_t *p_intf = VLCIntf;
284 vlc_object_t *p_object= vlc_object_find( p_intf,
285 VLC_OBJECT_AOUT, FIND_ANYWHERE );
286 if( p_object == NULL )
287 p_object = (vlc_object_t *)pl_Yield( p_intf );
289 char psz_values[102];
290 memset( psz_values, 0, 102 );
292 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
294 for( i = 0; i < 10; i++ )
295 sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
296 var_SetString( p_object, "equalizer-bands", psz_values );
297 var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
299 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
301 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
303 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
305 /* save changed to config */
306 config_PutPsz( p_intf, "equalizer-bands", psz_values );
307 config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
308 config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
311 config_SaveConfigFile( p_intf, "equalizer" );
314 vlc_object_release( p_object );
317 - (IBAction)enable:(id)sender
319 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
322 - (IBAction)preampSliderUpdated:(id)sender
324 intf_thread_t *p_intf = VLCIntf;
325 float f_preamp = [sender floatValue] ;
327 vlc_object_t *p_object = vlc_object_find( p_intf,
328 VLC_OBJECT_AOUT, FIND_ANYWHERE );
329 if( p_object == NULL )
330 p_object = (vlc_object_t *)pl_Yield( p_intf );
332 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
334 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
336 /* save changed to config */
337 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
340 config_SaveConfigFile( p_intf, "equalizer" );
343 vlc_object_release( p_object );
346 - (IBAction)toggleWindow:(id)sender
348 if( [o_window isVisible] )
350 [o_window orderOut:sender];
351 [o_btn_equalizer setState:NSOffState];
355 [o_window makeKeyAndOrderFront:sender];
356 [o_btn_equalizer setState:NSOnState];
360 - (IBAction)twopass:(id)sender
362 intf_thread_t *p_intf = VLCIntf;
363 bool b_2p = [sender state] ? true : false;
364 vlc_object_t *p_object= vlc_object_find( p_intf,
365 VLC_OBJECT_AOUT, FIND_ANYWHERE );
366 aout_instance_t *p_aout = (aout_instance_t *)p_object;
367 if( p_object == NULL )
368 p_object = (vlc_object_t *)pl_Yield( p_intf );
370 var_SetBool( p_object, "equalizer-2pass", b_2p );
371 if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
374 for( i = 0; i < p_aout->i_nb_inputs; i++ )
376 p_aout->pp_inputs[i]->b_restart = true;
380 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
382 /* save changed to config */
383 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
386 config_SaveConfigFile( p_intf, "equalizer" );
389 vlc_object_release( p_object );
392 - (void)windowWillClose:(NSNotification *)aNotification
394 [o_btn_equalizer setState: NSOffState];
400 vlc_object_t *p_object= vlc_object_find( VLCIntf,
401 VLC_OBJECT_AOUT, FIND_ANYWHERE );
402 if( p_object == NULL )
403 p_object = (vlc_object_t *)pl_Yield( VLCIntf );
405 [o_window setExcludedFromWindowsMenu: TRUE];
413 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
415 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
417 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
419 if( strcmp( preset_list[i], psz_preset ) )
422 [o_popup_presets selectItemAtIndex: i];
425 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
426 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
428 if( strcmp( psz_preset, "flat" ) )
432 snprintf( psz_bands, sizeof( psz_bands ),
433 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
435 eqz_preset_10b[i]->f_amp[0],
436 eqz_preset_10b[i]->f_amp[1],
437 eqz_preset_10b[i]->f_amp[2],
438 eqz_preset_10b[i]->f_amp[3],
439 eqz_preset_10b[i]->f_amp[4],
440 eqz_preset_10b[i]->f_amp[5],
441 eqz_preset_10b[i]->f_amp[6],
442 eqz_preset_10b[i]->f_amp[7],
443 eqz_preset_10b[i]->f_amp[8],
444 eqz_preset_10b[i]->f_amp[9] );
446 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
448 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
450 var_SetFloat( p_object, "equalizer-preamp",
451 eqz_preset_10b[i]->f_preamp );
452 var_SetString( p_object, "equalizer-bands", psz_bands );
456 vlc_object_release( p_object );
459 [self equalizerUpdated];
463 - (id)getSliderByIndex:(int)index
467 case 0 : return o_slider_band1;
468 case 1 : return o_slider_band2;
469 case 2 : return o_slider_band3;
470 case 3 : return o_slider_band4;
471 case 4 : return o_slider_band5;
472 case 5 : return o_slider_band6;
473 case 6 : return o_slider_band7;
474 case 7 : return o_slider_band8;
475 case 8 : return o_slider_band9;
476 case 9 : return o_slider_band10;
477 default : return nil;
481 - (void)setBandSlidersValues:(float *)values
484 for (i = 0 ; i<= 9 ; i++)
486 [self setValue:values[i] forSlider:i];
490 - (void)initBandSliders
493 for (i = 0 ; i< 9 ; i++)
495 [self setValue:0.0 forSlider:i];
499 - (void)setValue:(float)value forSlider:(int)index
501 id slider = [self getSliderByIndex:index];
505 [slider setFloatValue:value];