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 = pl_Yield( p_intf );
57 if( p_object == NULL )
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 );
84 memmove( psz_parser, psz_parser + strlen( psz_name ) +
85 ( *( psz_parser + strlen( psz_name ) ) == ',' ? 1 : 0 ),
86 strlen( psz_parser + strlen( psz_name ) ) + 1 );
88 if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
90 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
100 var_SetString( p_object, "audio-filter", psz_string );
103 for( i = 0; i < p_aout->i_nb_inputs; i++ )
105 p_aout->pp_inputs[i]->b_restart = true;
109 if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
111 /* save changed to config */
112 config_PutPsz( p_object, "audio-filter", psz_string );
115 config_SaveConfigFile( p_object, "main" );
119 vlc_object_release( p_object );
122 static bool GetFiltersStatus( intf_thread_t *p_intf,
125 char *psz_parser, *psz_string;
126 vlc_object_t *p_object = vlc_object_find( p_intf,
127 VLC_OBJECT_AOUT, FIND_ANYWHERE );
128 if( p_object == NULL )
129 p_object = pl_Yield( p_intf );
130 if( p_object == NULL )
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 = pl_Yield( p_intf );
188 if( p_object == NULL )
191 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
193 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
196 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
198 if( psz_bands == NULL )
199 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
201 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
203 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
204 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
208 b_2p = var_GetBool( p_object, "equalizer-2pass" );
209 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
212 vlc_object_release( p_object );
214 /* Set the preamp slider */
215 [o_slider_preamp setFloatValue: f_preamp];
217 /* Set the bands slider */
218 psz_bands_init = psz_bands;
220 for( i = 0; i < 10; i++ )
224 f_band[i] = strtof( psz_bands, &p_next );
226 f_band[i] = (float)strtod( psz_bands, &p_next );
228 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
230 if( !*psz_bands ) break; /* end of line */
231 psz_bands = p_next+1;
233 free( psz_bands_init );
234 [self setBandSlidersValues:f_band];
236 /* Set the the checkboxes */
237 [o_ckb_enable setState: b_enabled];
239 [o_ckb_2pass setState: b_2p];
242 - (IBAction)bandSliderUpdated:(id)sender
244 intf_thread_t *p_intf = VLCIntf;
245 vlc_object_t *p_object = vlc_object_find( p_intf,
246 VLC_OBJECT_AOUT, FIND_ANYWHERE );
248 if( p_object == NULL )
249 p_object = pl_Yield( p_intf );
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 = pl_Yield( p_intf );
292 if( p_object == NULL )
295 char psz_values[102];
296 memset( psz_values, 0, 102 );
298 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
300 for( i = 0; i < 10; i++ )
301 sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
302 var_SetString( p_object, "equalizer-bands", psz_values );
303 var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
305 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
307 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
309 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
311 /* save changed to config */
312 config_PutPsz( p_intf, "equalizer-bands", psz_values );
313 config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
314 config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
317 config_SaveConfigFile( p_intf, "equalizer" );
320 vlc_object_release( p_object );
323 - (IBAction)enable:(id)sender
325 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
328 - (IBAction)preampSliderUpdated:(id)sender
330 intf_thread_t *p_intf = VLCIntf;
331 float f_preamp = [sender floatValue] ;
333 vlc_object_t *p_object = vlc_object_find( p_intf,
334 VLC_OBJECT_AOUT, FIND_ANYWHERE );
335 if( p_object == NULL )
336 p_object = pl_Yield( p_intf );
337 if( p_object == NULL )
340 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
342 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
344 /* save changed to config */
345 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
348 config_SaveConfigFile( p_intf, "equalizer" );
351 vlc_object_release( p_object );
354 - (IBAction)toggleWindow:(id)sender
356 if( [o_window isVisible] )
358 [o_window orderOut:sender];
359 [o_btn_equalizer setState:NSOffState];
363 [o_window makeKeyAndOrderFront:sender];
364 [o_btn_equalizer setState:NSOnState];
368 - (IBAction)twopass:(id)sender
370 intf_thread_t *p_intf = VLCIntf;
371 bool b_2p = [sender state] ? true : false;
372 vlc_object_t *p_object= vlc_object_find( p_intf,
373 VLC_OBJECT_AOUT, FIND_ANYWHERE );
374 aout_instance_t *p_aout = (aout_instance_t *)p_object;
375 if( p_object == NULL )
376 p_object = pl_Yield( p_intf );
377 if( p_object == NULL )
380 var_SetBool( p_object, "equalizer-2pass", b_2p );
381 if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
384 for( i = 0; i < p_aout->i_nb_inputs; i++ )
386 p_aout->pp_inputs[i]->b_restart = true;
390 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
392 /* save changed to config */
393 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
396 config_SaveConfigFile( p_intf, "equalizer" );
399 vlc_object_release( p_object );
402 - (void)windowWillClose:(NSNotification *)aNotification
404 [o_btn_equalizer setState: NSOffState];
410 vlc_object_t *p_object= vlc_object_find( VLCIntf,
411 VLC_OBJECT_AOUT, FIND_ANYWHERE );
412 if( p_object == NULL )
413 p_object = pl_Yield( VLCIntf );
415 [o_window setExcludedFromWindowsMenu: TRUE];
423 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
425 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
427 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
429 if( strcmp( preset_list[i], psz_preset ) )
432 [o_popup_presets selectItemAtIndex: i];
435 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
436 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
438 if( strcmp( psz_preset, "flat" ) )
442 snprintf( psz_bands, sizeof( psz_bands ),
443 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
445 eqz_preset_10b[i]->f_amp[0],
446 eqz_preset_10b[i]->f_amp[1],
447 eqz_preset_10b[i]->f_amp[2],
448 eqz_preset_10b[i]->f_amp[3],
449 eqz_preset_10b[i]->f_amp[4],
450 eqz_preset_10b[i]->f_amp[5],
451 eqz_preset_10b[i]->f_amp[6],
452 eqz_preset_10b[i]->f_amp[7],
453 eqz_preset_10b[i]->f_amp[8],
454 eqz_preset_10b[i]->f_amp[9] );
456 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
458 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
460 var_SetFloat( p_object, "equalizer-preamp",
461 eqz_preset_10b[i]->f_preamp );
462 var_SetString( p_object, "equalizer-bands", psz_bands );
466 vlc_object_release( p_object );
469 [self equalizerUpdated];
473 - (id)getSliderByIndex:(int)index
477 case 0 : return o_slider_band1;
478 case 1 : return o_slider_band2;
479 case 2 : return o_slider_band3;
480 case 3 : return o_slider_band4;
481 case 4 : return o_slider_band5;
482 case 5 : return o_slider_band6;
483 case 6 : return o_slider_band7;
484 case 7 : return o_slider_band8;
485 case 8 : return o_slider_band9;
486 case 9 : return o_slider_band10;
487 default : return nil;
491 - (void)setBandSlidersValues:(float *)values
494 for (i = 0 ; i<= 9 ; i++)
496 [self setValue:values[i] forSlider:i];
500 - (void)initBandSliders
503 for (i = 0 ; i< 9 ; i++)
505 [self setValue:0.0 forSlider:i];
509 - (void)setValue:(float)value forSlider:(int)index
511 id slider = [self getSliderByIndex:index];
515 [slider setFloatValue:value];