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 aout_instance_t *p_aout = getAout();
53 vlc_object_t *p_object = VLC_OBJECT(p_aout);
56 p_object = (vlc_object_t *)pl_Hold( p_intf );
59 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
61 if( !psz_string ) psz_string = strdup( "" );
63 psz_parser = strstr( psz_string, psz_name );
69 psz_parser = psz_string;
70 asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
71 psz_string, psz_name );
76 vlc_object_release( p_object );
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';
96 vlc_object_release( p_object );
101 aout_EnableFilter( p_object, psz_string, false);
103 if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
105 /* save changed to config */
106 config_PutPsz( p_object, "audio-filter", psz_string );
110 vlc_object_release( p_object );
113 static bool GetFiltersStatus( intf_thread_t *p_intf,
116 char *psz_parser, *psz_string;
117 vlc_object_t *p_object = VLC_OBJECT(getAout());
118 if( p_object == NULL )
119 p_object = (vlc_object_t *)pl_Hold( p_intf );
121 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
122 psz_string = config_GetPsz( p_intf, "audio-filter" );
125 psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
127 vlc_object_release( p_object );
129 if( !psz_string ) return false;
131 psz_parser = strstr( psz_string, psz_name );
144 [o_btn_equalizer setToolTip: _NS("Equalizer")];
145 [o_btn_equalizer_embedded setToolTip: _NS("Equalizer")];
146 [o_ckb_2pass setTitle: _NS("2 Pass")];
147 [o_ckb_2pass setToolTip: _NS("Apply the "
148 "equalizer filter twice. The effect will be sharper.")];
149 [o_ckb_enable setTitle: _NS("Enable")];
150 [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
151 "manually or using a preset.")];
152 [o_fld_preamp setStringValue: _NS("Preamp")];
154 [o_popup_presets removeAllItems];
155 for( i = 0; i < 18 ; i++ )
157 [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
159 [o_window setTitle: _NS("Equalizer")];
161 [self initBandSliders];
164 - (void)equalizerUpdated
166 intf_thread_t *p_intf = VLCIntf;
167 float f_preamp, f_band[10];
168 char *psz_bands, *psz_bands_init, *p_next;
171 bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
172 vlc_object_t *p_object = VLC_OBJECT(getAout());
174 if( p_object == NULL )
175 p_object = (vlc_object_t *)pl_Hold( p_intf );
177 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
179 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
182 psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
184 if( psz_bands == NULL )
185 psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
187 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
189 b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
190 f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
194 b_2p = var_GetBool( p_object, "equalizer-2pass" );
195 f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
198 vlc_object_release( p_object );
200 /* Set the preamp slider */
201 [o_slider_preamp setFloatValue: f_preamp];
203 /* Set the bands slider */
204 psz_bands_init = psz_bands;
206 for( i = 0; i < 10; i++ )
209 f_band[i] = strtof( psz_bands, &p_next );
210 if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
212 if( !*psz_bands ) break; /* end of line */
213 psz_bands = p_next+1;
215 free( psz_bands_init );
216 [self setBandSlidersValues:f_band];
218 /* Set the the checkboxes */
219 [o_ckb_enable setState: b_enabled];
221 [o_ckb_2pass setState: b_2p];
224 - (IBAction)bandSliderUpdated:(id)sender
226 intf_thread_t *p_intf = VLCIntf;
227 vlc_object_t *p_object = VLC_OBJECT(getAout());
229 if( p_object == NULL )
230 p_object = (vlc_object_t *)pl_Hold( p_intf );
232 const char *psz_values;
233 NSString *preset = [NSString stringWithFormat:@"%.1f", [o_slider_band1 floatValue] ];
234 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band2 floatValue] ];
235 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band3 floatValue] ];
236 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band4 floatValue] ];
237 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band5 floatValue] ];
238 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band6 floatValue] ];
239 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band7 floatValue] ];
240 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band8 floatValue] ];
241 preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band9 floatValue] ];
242 preset = [preset stringByAppendingFormat:@"%.1f", [o_slider_band10 floatValue] ];
244 psz_values = [preset UTF8String];
245 var_SetString( p_object, "equalizer-bands", psz_values );
247 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
249 /* save changed to config */
250 config_PutPsz( p_intf, "equalizer-bands", psz_values );
253 config_SaveConfigFile( p_intf, "equalizer" );
256 vlc_object_release( p_object );
259 - (IBAction)changePreset:(id)sender
261 intf_thread_t *p_intf = VLCIntf;
263 vlc_object_t *p_object= VLC_OBJECT(getAout());
264 if( p_object == NULL )
265 p_object = (vlc_object_t *)pl_Hold( p_intf );
267 var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
269 NSString *preset = @"";
270 const char *psz_values;
271 for( i = 0; i < 10; i++ )
273 preset = [preset stringByAppendingFormat:@"%.1f ", eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] ];
275 psz_values = [preset UTF8String];
276 var_SetString( p_object, "equalizer-bands", psz_values );
277 var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
279 [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
281 [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
283 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
285 /* save changed to config */
286 config_PutPsz( p_intf, "equalizer-bands", psz_values );
287 config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
288 config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
291 config_SaveConfigFile( p_intf, "equalizer" );
294 vlc_object_release( p_object );
297 - (IBAction)enable:(id)sender
299 ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
302 - (IBAction)preampSliderUpdated:(id)sender
304 intf_thread_t *p_intf = VLCIntf;
305 float f_preamp = [sender floatValue] ;
307 vlc_object_t *p_object = VLC_OBJECT(getAout());
308 if( p_object == NULL )
309 p_object = (vlc_object_t *)pl_Hold( p_intf );
311 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
313 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
315 /* save changed to config */
316 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
319 config_SaveConfigFile( p_intf, "equalizer" );
322 vlc_object_release( p_object );
325 - (IBAction)toggleWindow:(id)sender
327 if( [o_window isVisible] )
329 [o_window orderOut:sender];
330 [o_btn_equalizer setState:NSOffState];
331 [o_btn_equalizer_embedded setState:NSOffState];
335 [o_window makeKeyAndOrderFront:sender];
336 [o_btn_equalizer setState:NSOnState];
337 [o_btn_equalizer_embedded setState:NSOnState];
341 - (IBAction)twopass:(id)sender
343 intf_thread_t *p_intf = VLCIntf;
344 bool b_2p = [sender state] ? true : false;
345 aout_instance_t *p_aout = getAout();
346 vlc_object_t *p_object= VLC_OBJECT(p_aout);
347 if( p_object == NULL )
348 p_object = (vlc_object_t *)pl_Hold( p_intf );
350 var_SetBool( p_object, "equalizer-2pass", b_2p );
352 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
354 /* save changed to config */
355 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
358 config_SaveConfigFile( p_intf, "equalizer" );
361 vlc_object_release( p_object );
364 - (void)windowWillClose:(NSNotification *)aNotification
366 [o_btn_equalizer setState: NSOffState];
372 vlc_object_t *p_object= VLC_OBJECT(getAout());
373 if( p_object == NULL )
374 p_object = (vlc_object_t *)pl_Hold( VLCIntf );
376 [o_window setExcludedFromWindowsMenu: TRUE];
384 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
386 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
388 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
390 if( strcmp( preset_list[i], psz_preset ) )
393 [o_popup_presets selectItemAtIndex: i];
396 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
397 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
399 if( strcmp( psz_preset, "flat" ) )
403 snprintf( psz_bands, sizeof( psz_bands ),
404 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
406 eqz_preset_10b[i]->f_amp[0],
407 eqz_preset_10b[i]->f_amp[1],
408 eqz_preset_10b[i]->f_amp[2],
409 eqz_preset_10b[i]->f_amp[3],
410 eqz_preset_10b[i]->f_amp[4],
411 eqz_preset_10b[i]->f_amp[5],
412 eqz_preset_10b[i]->f_amp[6],
413 eqz_preset_10b[i]->f_amp[7],
414 eqz_preset_10b[i]->f_amp[8],
415 eqz_preset_10b[i]->f_amp[9] );
417 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
419 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
421 var_SetFloat( p_object, "equalizer-preamp",
422 eqz_preset_10b[i]->f_preamp );
423 var_SetString( p_object, "equalizer-bands", psz_bands );
427 vlc_object_release( p_object );
430 [self equalizerUpdated];
434 - (id)sliderByIndex:(int)index
438 case 0 : return o_slider_band1;
439 case 1 : return o_slider_band2;
440 case 2 : return o_slider_band3;
441 case 3 : return o_slider_band4;
442 case 4 : return o_slider_band5;
443 case 5 : return o_slider_band6;
444 case 6 : return o_slider_band7;
445 case 7 : return o_slider_band8;
446 case 8 : return o_slider_band9;
447 case 9 : return o_slider_band10;
448 default : return nil;
452 - (void)setBandSlidersValues:(float *)values
455 for (i = 0 ; i<= 9 ; i++)
457 [self setValue:values[i] forSlider:i];
461 - (void)initBandSliders
464 for (i = 0 ; i< 9 ; i++)
466 [self setValue:0.0 forSlider:i];
470 - (void)setValue:(float)value forSlider:(int)index
472 id slider = [self sliderByIndex:index];
476 [slider setFloatValue:value];