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_Hold( 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 aout_EnableFilter( p_object, psz_string, false);
104 if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
106 /* save changed to config */
107 config_PutPsz( p_object, "audio-filter", psz_string );
111 vlc_object_release( p_object );
114 static bool GetFiltersStatus( intf_thread_t *p_intf,
117 char *psz_parser, *psz_string;
118 vlc_object_t *p_object = vlc_object_find( p_intf,
119 VLC_OBJECT_AOUT, FIND_ANYWHERE );
120 if( p_object == NULL )
121 p_object = (vlc_object_t *)pl_Hold( p_intf );
123 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
124 psz_string = config_GetPsz( p_intf, "audio-filter" );
127 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
129 vlc_object_release( p_object );
131 if( !psz_string ) return false;
133 psz_parser = strstr( psz_string, psz_name );
146 [o_btn_equalizer setToolTip: _NS("Equalizer")];
147 [o_btn_equalizer_embedded setToolTip: _NS("Equalizer")];
148 [o_ckb_2pass setTitle: _NS("2 Pass")];
149 [o_ckb_2pass setToolTip: _NS("Apply the "
150 "equalizer filter twice. The effect will be sharper.")];
151 [o_ckb_enable setTitle: _NS("Enable")];
152 [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
153 "manually or using a preset.")];
154 [o_fld_preamp setStringValue: _NS("Preamp")];
156 [o_popup_presets removeAllItems];
157 for( i = 0; i < 18 ; i++ )
159 [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
161 [o_window setTitle: _NS("Equalizer")];
163 [self initBandSliders];
166 - (void)equalizerUpdated
168 intf_thread_t *p_intf = VLCIntf;
169 float f_preamp, f_band[10];
170 char *psz_bands, *psz_bands_init, *p_next;
173 bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
174 vlc_object_t *p_object = vlc_object_find( p_intf,
175 VLC_OBJECT_AOUT, FIND_ANYWHERE );
177 if( p_object == NULL )
178 p_object = (vlc_object_t *)pl_Hold( p_intf );
180 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
182 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
185 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
187 if( psz_bands == NULL )
188 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
190 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
192 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
193 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
197 b_2p = var_GetBool( p_object, "equalizer-2pass" );
198 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
201 vlc_object_release( p_object );
203 /* Set the preamp slider */
204 [o_slider_preamp setFloatValue: f_preamp];
206 /* Set the bands slider */
207 psz_bands_init = psz_bands;
209 for( i = 0; i < 10; i++ )
212 f_band[i] = strtof( psz_bands, &p_next );
213 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
215 if( !*psz_bands ) break; /* end of line */
216 psz_bands = p_next+1;
218 free( psz_bands_init );
219 [self setBandSlidersValues:f_band];
221 /* Set the the checkboxes */
222 [o_ckb_enable setState: b_enabled];
224 [o_ckb_2pass setState: b_2p];
227 - (IBAction)bandSliderUpdated:(id)sender
229 intf_thread_t *p_intf = VLCIntf;
230 vlc_object_t *p_object = vlc_object_find( p_intf,
231 VLC_OBJECT_AOUT, FIND_ANYWHERE );
233 if( p_object == NULL )
234 p_object = (vlc_object_t *)pl_Hold( p_intf );
236 const char *psz_values;
237 NSString *preset = [NSString stringWithFormat:@"%.1f", [o_slider_band1 floatValue] ];
238 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band2 floatValue] ];
239 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band3 floatValue] ];
240 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band4 floatValue] ];
241 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band5 floatValue] ];
242 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band6 floatValue] ];
243 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band7 floatValue] ];
244 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band8 floatValue] ];
245 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band9 floatValue] ];
246 preset = [preset stringByAppendingFormat:@"%.1f", [o_slider_band10 floatValue] ];
248 psz_values = [preset UTF8String];
249 var_SetString( p_object, "equalizer-bands", psz_values );
251 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
253 /* save changed to config */
254 config_PutPsz( p_intf, "equalizer-bands", psz_values );
257 config_SaveConfigFile( p_intf, "equalizer" );
260 vlc_object_release( p_object );
263 - (IBAction)changePreset:(id)sender
265 intf_thread_t *p_intf = VLCIntf;
267 vlc_object_t *p_object= vlc_object_find( p_intf,
268 VLC_OBJECT_AOUT, FIND_ANYWHERE );
269 if( p_object == NULL )
270 p_object = (vlc_object_t *)pl_Hold( p_intf );
272 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
274 NSString *preset = @"";
275 const char *psz_values;
276 for( i = 0; i < 10; i++ )
278 preset = [preset stringByAppendingFormat:@"%.1f ", eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] ];
280 psz_values = [preset UTF8String];
281 var_SetString( p_object, "equalizer-bands", psz_values );
282 var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
284 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
286 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
288 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
290 /* save changed to config */
291 config_PutPsz( p_intf, "equalizer-bands", psz_values );
292 config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
293 config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
296 config_SaveConfigFile( p_intf, "equalizer" );
299 vlc_object_release( p_object );
302 - (IBAction)enable:(id)sender
304 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
307 - (IBAction)preampSliderUpdated:(id)sender
309 intf_thread_t *p_intf = VLCIntf;
310 float f_preamp = [sender floatValue] ;
312 vlc_object_t *p_object = vlc_object_find( p_intf,
313 VLC_OBJECT_AOUT, FIND_ANYWHERE );
314 if( p_object == NULL )
315 p_object = (vlc_object_t *)pl_Hold( p_intf );
317 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
319 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
321 /* save changed to config */
322 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
325 config_SaveConfigFile( p_intf, "equalizer" );
328 vlc_object_release( p_object );
331 - (IBAction)toggleWindow:(id)sender
333 if( [o_window isVisible] )
335 [o_window orderOut:sender];
336 [o_btn_equalizer setState:NSOffState];
337 [o_btn_equalizer_embedded setState:NSOffState];
341 [o_window makeKeyAndOrderFront:sender];
342 [o_btn_equalizer setState:NSOnState];
343 [o_btn_equalizer_embedded setState:NSOnState];
347 - (IBAction)twopass:(id)sender
349 intf_thread_t *p_intf = VLCIntf;
350 bool b_2p = [sender state] ? true : false;
351 vlc_object_t *p_object= vlc_object_find( p_intf,
352 VLC_OBJECT_AOUT, FIND_ANYWHERE );
353 aout_instance_t *p_aout = (aout_instance_t *)p_object;
354 if( p_object == NULL )
355 p_object = (vlc_object_t *)pl_Hold( p_intf );
357 var_SetBool( p_object, "equalizer-2pass", b_2p );
358 if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
361 for( i = 0; i < p_aout->i_nb_inputs; i++ )
363 p_aout->pp_inputs[i]->b_restart = true;
367 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
369 /* save changed to config */
370 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
373 config_SaveConfigFile( p_intf, "equalizer" );
376 vlc_object_release( p_object );
379 - (void)windowWillClose:(NSNotification *)aNotification
381 [o_btn_equalizer setState: NSOffState];
387 vlc_object_t *p_object= vlc_object_find( VLCIntf,
388 VLC_OBJECT_AOUT, FIND_ANYWHERE );
389 if( p_object == NULL )
390 p_object = (vlc_object_t *)pl_Hold( VLCIntf );
392 [o_window setExcludedFromWindowsMenu: TRUE];
400 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
402 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
404 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
406 if( strcmp( preset_list[i], psz_preset ) )
409 [o_popup_presets selectItemAtIndex: i];
412 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
413 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
415 if( strcmp( psz_preset, "flat" ) )
419 snprintf( psz_bands, sizeof( psz_bands ),
420 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
422 eqz_preset_10b[i]->f_amp[0],
423 eqz_preset_10b[i]->f_amp[1],
424 eqz_preset_10b[i]->f_amp[2],
425 eqz_preset_10b[i]->f_amp[3],
426 eqz_preset_10b[i]->f_amp[4],
427 eqz_preset_10b[i]->f_amp[5],
428 eqz_preset_10b[i]->f_amp[6],
429 eqz_preset_10b[i]->f_amp[7],
430 eqz_preset_10b[i]->f_amp[8],
431 eqz_preset_10b[i]->f_amp[9] );
433 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
435 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
437 var_SetFloat( p_object, "equalizer-preamp",
438 eqz_preset_10b[i]->f_preamp );
439 var_SetString( p_object, "equalizer-bands", psz_bands );
443 vlc_object_release( p_object );
446 [self equalizerUpdated];
450 - (id)sliderByIndex:(int)index
454 case 0 : return o_slider_band1;
455 case 1 : return o_slider_band2;
456 case 2 : return o_slider_band3;
457 case 3 : return o_slider_band4;
458 case 4 : return o_slider_band5;
459 case 5 : return o_slider_band6;
460 case 6 : return o_slider_band7;
461 case 7 : return o_slider_band8;
462 case 8 : return o_slider_band9;
463 case 9 : return o_slider_band10;
464 default : return nil;
468 - (void)setBandSlidersValues:(float *)values
471 for (i = 0 ; i<= 9 ; i++)
473 [self setValue:values[i] forSlider:i];
477 - (void)initBandSliders
480 for (i = 0 ; i< 9 ; i++)
482 [self setValue:0.0 forSlider:i];
486 - (void)setValue:(float)value forSlider:(int)index
488 id slider = [self sliderByIndex:index];
492 [slider setFloatValue:value];