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 *****************************************************************************/
35 #include "equalizer.h"
36 #include "../../audio_filter/equalizer_presets.h"
38 /*****************************************************************************
39 * VLCEqualizer implementation
40 *****************************************************************************/
41 @implementation VLCEqualizer
43 static void ChangeFiltersString( intf_thread_t *p_intf,
44 char *psz_name, vlc_bool_t b_add )
46 char *psz_parser, *psz_string;
48 vlc_object_t *p_object = vlc_object_find( p_intf,
49 VLC_OBJECT_AOUT, FIND_ANYWHERE );
50 aout_instance_t *p_aout = (aout_instance_t *)p_object;
51 if( p_object == NULL )
52 p_object = vlc_object_find( p_intf,
53 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
54 if( p_object == NULL )
57 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
59 if( !psz_string ) psz_string = strdup( "" );
61 psz_parser = strstr( psz_string, psz_name );
67 psz_parser = psz_string;
68 asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
69 psz_string, psz_name );
81 memmove( psz_parser, psz_parser + strlen( psz_name ) +
82 ( *( psz_parser + strlen( psz_name ) ) == ',' ? 1 : 0 ),
83 strlen( psz_parser + strlen( psz_name ) ) + 1 );
85 if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
87 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
97 var_SetString( p_object, "audio-filter", psz_string );
100 for( i = 0; i < p_aout->i_nb_inputs; i++ )
102 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
106 if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
108 /* save changed to config */
109 config_PutPsz( p_object, "audio-filter", psz_string );
112 config_SaveConfigFile( p_object, "main" );
116 vlc_object_release( p_object );
119 static vlc_bool_t GetFiltersStatus( intf_thread_t *p_intf,
122 char *psz_parser, *psz_string;
123 vlc_object_t *p_object = vlc_object_find( p_intf,
124 VLC_OBJECT_AOUT, FIND_ANYWHERE );
125 if( p_object == NULL )
126 p_object = vlc_object_find( p_intf,
127 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
128 if( p_object == NULL )
131 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
132 psz_string = config_GetPsz( p_intf, "audio-filter" );
135 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
137 vlc_object_release( p_object );
139 if( !psz_string ) return VLC_FALSE;
141 psz_parser = strstr( psz_string, psz_name );
154 [o_btn_equalizer 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")];
163 [o_popup_presets removeAllItems];
164 for( i = 0; i < 18 ; i++ )
166 [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
168 [o_window setTitle: _NS("Equalizer")];
170 [self initBandSliders];
173 - (void)equalizerUpdated
175 intf_thread_t *p_intf = VLCIntf;
176 float f_preamp, f_band[10];
177 char *psz_bands, *psz_bands_init, *p_next;
180 vlc_bool_t b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
181 vlc_object_t *p_object = vlc_object_find( p_intf,
182 VLC_OBJECT_AOUT, FIND_ANYWHERE );
184 if( p_object == NULL )
185 p_object = vlc_object_find( p_intf,
186 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
187 if( p_object == NULL )
190 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
192 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
195 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
197 if( psz_bands == NULL )
198 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
200 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
202 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
203 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
207 b_2p = var_GetBool( p_object, "equalizer-2pass" );
208 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
211 vlc_object_release( p_object );
213 /* Set the preamp slider */
214 [o_slider_preamp setFloatValue: f_preamp];
216 /* Set the bands slider */
217 psz_bands_init = psz_bands;
219 for( i = 0; i < 10; i++ )
223 f_band[i] = strtof( psz_bands, &p_next );
225 f_band[i] = (float)strtod( psz_bands, &p_next );
227 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
229 if( !*psz_bands ) break; /* end of line */
230 psz_bands = p_next+1;
232 free( psz_bands_init );
233 [self setBandSlidersValues:f_band];
235 /* Set the the checkboxes */
236 [o_ckb_enable setState: b_enabled];
238 [o_ckb_2pass setState: b_2p];
241 - (IBAction)bandSliderUpdated:(id)sender
243 intf_thread_t *p_intf = VLCIntf;
244 vlc_object_t *p_object = vlc_object_find( p_intf,
245 VLC_OBJECT_AOUT, FIND_ANYWHERE );
247 if( p_object == NULL )
248 p_object = vlc_object_find( p_intf,
249 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
250 if( p_object == NULL )
253 char psz_values[102];
254 memset( psz_values, 0, 102 );
256 /* Write the new bands values */
257 /* TODO: write a generic code instead of ten times the same thing */
259 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
260 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
261 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
262 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
263 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
264 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
265 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
266 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
267 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
268 sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
270 var_SetString( p_object, "equalizer-bands", psz_values );
272 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
274 /* save changed to config */
275 config_PutPsz( p_intf, "equalizer-bands", psz_values );
278 config_SaveConfigFile( p_intf, "equalizer" );
281 vlc_object_release( p_object );
284 - (IBAction)changePreset:(id)sender
286 intf_thread_t *p_intf = VLCIntf;
288 vlc_object_t *p_object= vlc_object_find( p_intf,
289 VLC_OBJECT_AOUT, FIND_ANYWHERE );
290 if( p_object == NULL )
291 p_object = vlc_object_find( p_intf,
292 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
293 if( p_object == NULL )
296 char psz_values[102];
297 memset( psz_values, 0, 102 );
299 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
301 for( i = 0; i < 10; i++ )
302 sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
303 var_SetString( p_object, "equalizer-bands", psz_values );
304 var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
306 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
308 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
310 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
312 /* save changed to config */
313 config_PutPsz( p_intf, "equalizer-bands", psz_values );
314 config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
315 config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
318 config_SaveConfigFile( p_intf, "equalizer" );
321 vlc_object_release( p_object );
324 - (IBAction)enable:(id)sender
326 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
329 - (IBAction)preampSliderUpdated:(id)sender
331 intf_thread_t *p_intf = VLCIntf;
332 float f_preamp = [sender floatValue] ;
334 vlc_object_t *p_object = vlc_object_find( p_intf,
335 VLC_OBJECT_AOUT, FIND_ANYWHERE );
336 if( p_object == NULL )
337 p_object = vlc_object_find( p_intf,
338 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
339 if( p_object == NULL )
342 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
344 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
346 /* save changed to config */
347 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
350 config_SaveConfigFile( p_intf, "equalizer" );
353 vlc_object_release( p_object );
356 - (IBAction)toggleWindow:(id)sender
358 if( [o_window isVisible] )
360 [o_window orderOut:sender];
361 [o_btn_equalizer setState:NSOffState];
365 [o_window makeKeyAndOrderFront:sender];
366 [o_btn_equalizer setState:NSOnState];
370 - (IBAction)twopass:(id)sender
372 intf_thread_t *p_intf = VLCIntf;
373 vlc_bool_t b_2p = [sender state] ? VLC_TRUE : VLC_FALSE;
374 vlc_object_t *p_object= vlc_object_find( p_intf,
375 VLC_OBJECT_AOUT, FIND_ANYWHERE );
376 aout_instance_t *p_aout = (aout_instance_t *)p_object;
377 if( p_object == NULL )
378 p_object = vlc_object_find( p_intf,
379 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
380 if( p_object == NULL )
383 var_SetBool( p_object, "equalizer-2pass", b_2p );
384 if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
387 for( i = 0; i < p_aout->i_nb_inputs; i++ )
389 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
393 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
395 /* save changed to config */
396 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
399 config_SaveConfigFile( p_intf, "equalizer" );
402 vlc_object_release( p_object );
405 - (void)windowWillClose:(NSNotification *)aNotification
407 [o_btn_equalizer setState: NSOffState];
413 vlc_object_t *p_object= vlc_object_find( VLCIntf,
414 VLC_OBJECT_AOUT, FIND_ANYWHERE );
415 if( p_object == NULL )
416 p_object = vlc_object_find( VLCIntf,
417 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
419 [o_window setExcludedFromWindowsMenu: TRUE];
427 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
429 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
431 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
433 if( strcmp( preset_list[i], psz_preset ) )
436 [o_popup_presets selectItemAtIndex: i];
439 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
440 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
442 if( strcmp( psz_preset, "flat" ) )
446 snprintf( psz_bands, sizeof( psz_bands ),
447 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
449 eqz_preset_10b[i]->f_amp[0],
450 eqz_preset_10b[i]->f_amp[1],
451 eqz_preset_10b[i]->f_amp[2],
452 eqz_preset_10b[i]->f_amp[3],
453 eqz_preset_10b[i]->f_amp[4],
454 eqz_preset_10b[i]->f_amp[5],
455 eqz_preset_10b[i]->f_amp[6],
456 eqz_preset_10b[i]->f_amp[7],
457 eqz_preset_10b[i]->f_amp[8],
458 eqz_preset_10b[i]->f_amp[9] );
460 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
462 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
464 var_SetFloat( p_object, "equalizer-preamp",
465 eqz_preset_10b[i]->f_preamp );
466 var_SetString( p_object, "equalizer-bands", psz_bands );
470 vlc_object_release( p_object );
473 [self equalizerUpdated];
477 - (id)getSliderByIndex:(int)index
481 case 0 : return o_slider_band1;
482 case 1 : return o_slider_band2;
483 case 2 : return o_slider_band3;
484 case 3 : return o_slider_band4;
485 case 4 : return o_slider_band5;
486 case 5 : return o_slider_band6;
487 case 6 : return o_slider_band7;
488 case 7 : return o_slider_band8;
489 case 8 : return o_slider_band9;
490 case 9 : return o_slider_band10;
491 default : return nil;
495 - (void)setBandSlidersValues:(float *)values
498 for (i = 0 ; i<= 9 ; i++)
500 [self setValue:values[i] forSlider:i];
504 - (void)initBandSliders
507 for (i = 0 ; i< 9 ; i++)
509 [self setValue:0.0 forSlider:i];
513 - (void)setValue:(float)value forSlider:(int)index
515 id slider = [self getSliderByIndex:index];
519 [slider setFloatValue:value];