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 );
118 vlc_object_release( p_object );
121 static bool GetFiltersStatus( intf_thread_t *p_intf,
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_Yield( p_intf );
130 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
131 psz_string = config_GetPsz( p_intf, "audio-filter" );
134 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
136 vlc_object_release( p_object );
138 if( !psz_string ) return false;
140 psz_parser = strstr( psz_string, psz_name );
153 [o_btn_equalizer setToolTip: _NS("Equalizer")];
154 [o_ckb_2pass setTitle: _NS("2 Pass")];
155 [o_ckb_2pass setToolTip: _NS("Apply the "
156 "equalizer filter twice. The effect will be sharper.")];
157 [o_ckb_enable setTitle: _NS("Enable")];
158 [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
159 "manually or using a preset.")];
160 [o_fld_preamp setStringValue: _NS("Preamp")];
162 [o_popup_presets removeAllItems];
163 for( i = 0; i < 18 ; i++ )
165 [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
167 [o_window setTitle: _NS("Equalizer")];
169 [self initBandSliders];
172 - (void)equalizerUpdated
174 intf_thread_t *p_intf = VLCIntf;
175 float f_preamp, f_band[10];
176 char *psz_bands, *psz_bands_init, *p_next;
179 bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
180 vlc_object_t *p_object = vlc_object_find( p_intf,
181 VLC_OBJECT_AOUT, FIND_ANYWHERE );
183 if( p_object == NULL )
184 p_object = (vlc_object_t *)pl_Yield( p_intf );
186 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
188 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
191 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
193 if( psz_bands == NULL )
194 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
196 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
198 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
199 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
203 b_2p = var_GetBool( p_object, "equalizer-2pass" );
204 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
207 vlc_object_release( p_object );
209 /* Set the preamp slider */
210 [o_slider_preamp setFloatValue: f_preamp];
212 /* Set the bands slider */
213 psz_bands_init = psz_bands;
215 for( i = 0; i < 10; i++ )
219 f_band[i] = strtof( psz_bands, &p_next );
221 f_band[i] = (float)strtod( psz_bands, &p_next );
223 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
225 if( !*psz_bands ) break; /* end of line */
226 psz_bands = p_next+1;
228 free( psz_bands_init );
229 [self setBandSlidersValues:f_band];
231 /* Set the the checkboxes */
232 [o_ckb_enable setState: b_enabled];
234 [o_ckb_2pass setState: b_2p];
237 - (IBAction)bandSliderUpdated:(id)sender
239 intf_thread_t *p_intf = VLCIntf;
240 vlc_object_t *p_object = vlc_object_find( p_intf,
241 VLC_OBJECT_AOUT, FIND_ANYWHERE );
243 if( p_object == NULL )
244 p_object = (vlc_object_t *)pl_Yield( p_intf );
246 char psz_values[102];
247 memset( psz_values, 0, 102 );
249 /* Write the new bands values */
250 /* TODO: write a generic code instead of ten times the same thing */
252 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
253 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
254 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
255 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
256 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
257 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
258 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
259 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
260 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
261 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
263 var_SetString( p_object, "equalizer-bands", psz_values );
265 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
267 /* save changed to config */
268 config_PutPsz( p_intf, "equalizer-bands", psz_values );
271 config_SaveConfigFile( p_intf, "equalizer" );
274 vlc_object_release( p_object );
277 - (IBAction)changePreset:(id)sender
279 intf_thread_t *p_intf = VLCIntf;
281 vlc_object_t *p_object= vlc_object_find( p_intf,
282 VLC_OBJECT_AOUT, FIND_ANYWHERE );
283 if( p_object == NULL )
284 p_object = (vlc_object_t *)pl_Yield( p_intf );
286 char psz_values[102];
287 memset( psz_values, 0, 102 );
289 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
291 for( i = 0; i < 10; i++ )
292 sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
293 var_SetString( p_object, "equalizer-bands", psz_values );
294 var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
296 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
298 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
300 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
302 /* save changed to config */
303 config_PutPsz( p_intf, "equalizer-bands", psz_values );
304 config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
305 config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
308 config_SaveConfigFile( p_intf, "equalizer" );
311 vlc_object_release( p_object );
314 - (IBAction)enable:(id)sender
316 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
319 - (IBAction)preampSliderUpdated:(id)sender
321 intf_thread_t *p_intf = VLCIntf;
322 float f_preamp = [sender floatValue] ;
324 vlc_object_t *p_object = vlc_object_find( p_intf,
325 VLC_OBJECT_AOUT, FIND_ANYWHERE );
326 if( p_object == NULL )
327 p_object = (vlc_object_t *)pl_Yield( p_intf );
329 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
331 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
333 /* save changed to config */
334 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
337 config_SaveConfigFile( p_intf, "equalizer" );
340 vlc_object_release( p_object );
343 - (IBAction)toggleWindow:(id)sender
345 if( [o_window isVisible] )
347 [o_window orderOut:sender];
348 [o_btn_equalizer setState:NSOffState];
352 [o_window makeKeyAndOrderFront:sender];
353 [o_btn_equalizer setState:NSOnState];
357 - (IBAction)twopass:(id)sender
359 intf_thread_t *p_intf = VLCIntf;
360 bool b_2p = [sender state] ? true : false;
361 vlc_object_t *p_object= vlc_object_find( p_intf,
362 VLC_OBJECT_AOUT, FIND_ANYWHERE );
363 aout_instance_t *p_aout = (aout_instance_t *)p_object;
364 if( p_object == NULL )
365 p_object = (vlc_object_t *)pl_Yield( p_intf );
367 var_SetBool( p_object, "equalizer-2pass", b_2p );
368 if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
371 for( i = 0; i < p_aout->i_nb_inputs; i++ )
373 p_aout->pp_inputs[i]->b_restart = true;
377 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
379 /* save changed to config */
380 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
383 config_SaveConfigFile( p_intf, "equalizer" );
386 vlc_object_release( p_object );
389 - (void)windowWillClose:(NSNotification *)aNotification
391 [o_btn_equalizer setState: NSOffState];
397 vlc_object_t *p_object= vlc_object_find( VLCIntf,
398 VLC_OBJECT_AOUT, FIND_ANYWHERE );
399 if( p_object == NULL )
400 p_object = (vlc_object_t *)pl_Yield( VLCIntf );
402 [o_window setExcludedFromWindowsMenu: TRUE];
410 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
412 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
414 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
416 if( strcmp( preset_list[i], psz_preset ) )
419 [o_popup_presets selectItemAtIndex: i];
422 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
423 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
425 if( strcmp( psz_preset, "flat" ) )
429 snprintf( psz_bands, sizeof( psz_bands ),
430 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
432 eqz_preset_10b[i]->f_amp[0],
433 eqz_preset_10b[i]->f_amp[1],
434 eqz_preset_10b[i]->f_amp[2],
435 eqz_preset_10b[i]->f_amp[3],
436 eqz_preset_10b[i]->f_amp[4],
437 eqz_preset_10b[i]->f_amp[5],
438 eqz_preset_10b[i]->f_amp[6],
439 eqz_preset_10b[i]->f_amp[7],
440 eqz_preset_10b[i]->f_amp[8],
441 eqz_preset_10b[i]->f_amp[9] );
443 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
445 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
447 var_SetFloat( p_object, "equalizer-preamp",
448 eqz_preset_10b[i]->f_preamp );
449 var_SetString( p_object, "equalizer-bands", psz_bands );
453 vlc_object_release( p_object );
456 [self equalizerUpdated];
460 - (id)getSliderByIndex:(int)index
464 case 0 : return o_slider_band1;
465 case 1 : return o_slider_band2;
466 case 2 : return o_slider_band3;
467 case 3 : return o_slider_band4;
468 case 4 : return o_slider_band5;
469 case 5 : return o_slider_band6;
470 case 6 : return o_slider_band7;
471 case 7 : return o_slider_band8;
472 case 8 : return o_slider_band9;
473 case 9 : return o_slider_band10;
474 default : return nil;
478 - (void)setBandSlidersValues:(float *)values
481 for (i = 0 ; i<= 9 ; i++)
483 [self setValue:values[i] forSlider:i];
487 - (void)initBandSliders
490 for (i = 0 ; i< 9 ; i++)
492 [self setValue:0.0 forSlider:i];
496 - (void)setValue:(float)value forSlider:(int)index
498 id slider = [self getSliderByIndex:index];
502 [slider setFloatValue:value];