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;
55 if( p_object == NULL )
56 p_object = vlc_object_find( p_intf,
57 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
58 if( p_object == NULL )
61 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
63 if( !psz_string ) psz_string = strdup( "" );
65 psz_parser = strstr( psz_string, psz_name );
71 psz_parser = psz_string;
72 asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
73 psz_string, psz_name );
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';
101 var_SetString( p_object, "audio-filter", psz_string );
104 for( i = 0; i < p_aout->i_nb_inputs; i++ )
106 p_aout->pp_inputs[i]->b_restart = true;
110 if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
112 /* save changed to config */
113 config_PutPsz( p_object, "audio-filter", psz_string );
116 config_SaveConfigFile( p_object, "main" );
120 vlc_object_release( p_object );
123 static bool GetFiltersStatus( intf_thread_t *p_intf,
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 )
135 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
136 psz_string = config_GetPsz( p_intf, "audio-filter" );
139 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
141 vlc_object_release( p_object );
143 if( !psz_string ) return false;
145 psz_parser = strstr( psz_string, psz_name );
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")];
167 [o_popup_presets removeAllItems];
168 for( i = 0; i < 18 ; i++ )
170 [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
172 [o_window setTitle: _NS("Equalizer")];
174 [self initBandSliders];
177 - (void)equalizerUpdated
179 intf_thread_t *p_intf = VLCIntf;
180 float f_preamp, f_band[10];
181 char *psz_bands, *psz_bands_init, *p_next;
184 bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
185 vlc_object_t *p_object = vlc_object_find( p_intf,
186 VLC_OBJECT_AOUT, FIND_ANYWHERE );
188 if( p_object == NULL )
189 p_object = vlc_object_find( p_intf,
190 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
191 if( p_object == NULL )
194 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
196 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
199 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
201 if( psz_bands == NULL )
202 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
204 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
206 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
207 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
211 b_2p = var_GetBool( p_object, "equalizer-2pass" );
212 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
215 vlc_object_release( p_object );
217 /* Set the preamp slider */
218 [o_slider_preamp setFloatValue: f_preamp];
220 /* Set the bands slider */
221 psz_bands_init = psz_bands;
223 for( i = 0; i < 10; i++ )
227 f_band[i] = strtof( psz_bands, &p_next );
229 f_band[i] = (float)strtod( psz_bands, &p_next );
231 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
233 if( !*psz_bands ) break; /* end of line */
234 psz_bands = p_next+1;
236 free( psz_bands_init );
237 [self setBandSlidersValues:f_band];
239 /* Set the the checkboxes */
240 [o_ckb_enable setState: b_enabled];
242 [o_ckb_2pass setState: b_2p];
245 - (IBAction)bandSliderUpdated:(id)sender
247 intf_thread_t *p_intf = VLCIntf;
248 vlc_object_t *p_object = vlc_object_find( p_intf,
249 VLC_OBJECT_AOUT, FIND_ANYWHERE );
251 if( p_object == NULL )
252 p_object = vlc_object_find( p_intf,
253 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
254 if( p_object == NULL )
257 char psz_values[102];
258 memset( psz_values, 0, 102 );
260 /* Write the new bands values */
261 /* TODO: write a generic code instead of ten times the same thing */
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] );
274 var_SetString( p_object, "equalizer-bands", psz_values );
276 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
278 /* save changed to config */
279 config_PutPsz( p_intf, "equalizer-bands", psz_values );
282 config_SaveConfigFile( p_intf, "equalizer" );
285 vlc_object_release( p_object );
288 - (IBAction)changePreset:(id)sender
290 intf_thread_t *p_intf = VLCIntf;
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 )
300 char psz_values[102];
301 memset( psz_values, 0, 102 );
303 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
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);
310 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
312 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
314 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
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]] );
322 config_SaveConfigFile( p_intf, "equalizer" );
325 vlc_object_release( p_object );
328 - (IBAction)enable:(id)sender
330 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
333 - (IBAction)preampSliderUpdated:(id)sender
335 intf_thread_t *p_intf = VLCIntf;
336 float f_preamp = [sender floatValue] ;
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 )
346 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
348 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
350 /* save changed to config */
351 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
354 config_SaveConfigFile( p_intf, "equalizer" );
357 vlc_object_release( p_object );
360 - (IBAction)toggleWindow:(id)sender
362 if( [o_window isVisible] )
364 [o_window orderOut:sender];
365 [o_btn_equalizer setState:NSOffState];
369 [o_window makeKeyAndOrderFront:sender];
370 [o_btn_equalizer setState:NSOnState];
374 - (IBAction)twopass:(id)sender
376 intf_thread_t *p_intf = VLCIntf;
377 bool b_2p = [sender state] ? true : 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 )
387 var_SetBool( p_object, "equalizer-2pass", b_2p );
388 if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
391 for( i = 0; i < p_aout->i_nb_inputs; i++ )
393 p_aout->pp_inputs[i]->b_restart = true;
397 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
399 /* save changed to config */
400 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
403 config_SaveConfigFile( p_intf, "equalizer" );
406 vlc_object_release( p_object );
409 - (void)windowWillClose:(NSNotification *)aNotification
411 [o_btn_equalizer setState: NSOffState];
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 );
423 [o_window setExcludedFromWindowsMenu: TRUE];
431 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
433 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
435 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
437 if( strcmp( preset_list[i], psz_preset ) )
440 [o_popup_presets selectItemAtIndex: i];
443 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
444 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
446 if( strcmp( psz_preset, "flat" ) )
450 snprintf( psz_bands, sizeof( psz_bands ),
451 "%.1f %.1f %.1f %.1f %.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] );
464 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
466 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
468 var_SetFloat( p_object, "equalizer-preamp",
469 eqz_preset_10b[i]->f_preamp );
470 var_SetString( p_object, "equalizer-bands", psz_bands );
474 vlc_object_release( p_object );
477 [self equalizerUpdated];
481 - (id)getSliderByIndex:(int)index
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;
499 - (void)setBandSlidersValues:(float *)values
502 for (i = 0 ; i<= 9 ; i++)
504 [self setValue:values[i] forSlider:i];
508 - (void)initBandSliders
511 for (i = 0 ; i< 9 ; i++)
513 [self setValue:0.0 forSlider:i];
517 - (void)setValue:(float)value forSlider:(int)index
519 id slider = [self getSliderByIndex:index];
523 [slider setFloatValue:value];