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_hold(pl_Get( 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( pl_Get( p_intf ), psz_string, b_add);
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 = NULL;
117 vlc_object_t *p_object = VLC_OBJECT(getAout());
118 if( p_object == NULL )
119 p_object = vlc_object_hold(pl_Get( 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_hold(pl_Get( 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_hold(pl_Get( 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_hold(pl_Get( 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] );
301 aout_EnableFilter( pl_Get( VLCIntf ), (char *)"equalizer", [sender state]);
304 - (IBAction)preampSliderUpdated:(id)sender
306 intf_thread_t *p_intf = VLCIntf;
307 float f_preamp = [sender floatValue] ;
309 vlc_object_t *p_object = VLC_OBJECT(getAout());
310 if( p_object == NULL )
311 p_object = vlc_object_hold(pl_Get( p_intf ));
313 var_SetFloat( p_object, "equalizer-preamp", f_preamp );
315 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
317 /* save changed to config */
318 config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
321 config_SaveConfigFile( p_intf, "equalizer" );
324 vlc_object_release( p_object );
327 - (IBAction)toggleWindow:(id)sender
329 if( [o_window isVisible] )
331 [o_window orderOut:sender];
332 [o_btn_equalizer setState:NSOffState];
333 [o_btn_equalizer_embedded setState:NSOffState];
337 [o_window makeKeyAndOrderFront:sender];
338 [o_btn_equalizer setState:NSOnState];
339 [o_btn_equalizer_embedded setState:NSOnState];
343 - (IBAction)twopass:(id)sender
345 intf_thread_t *p_intf = VLCIntf;
346 bool b_2p = [sender state] ? true : false;
347 aout_instance_t *p_aout = getAout();
348 vlc_object_t *p_object= VLC_OBJECT(p_aout);
349 if( p_object == NULL )
350 p_object = vlc_object_hold(pl_Get( p_intf ));
352 var_SetBool( p_object, "equalizer-2pass", b_2p );
354 if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
356 /* save changed to config */
357 config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
360 config_SaveConfigFile( p_intf, "equalizer" );
363 vlc_object_release( p_object );
366 - (void)windowWillClose:(NSNotification *)aNotification
368 [o_btn_equalizer setState: NSOffState];
374 vlc_object_t *p_object= VLC_OBJECT(getAout());
375 if( p_object == NULL )
376 p_object = vlc_object_hold(pl_Get( VLCIntf ));
378 [o_window setExcludedFromWindowsMenu: TRUE];
386 var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
388 psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
390 for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
392 if( strcmp( preset_list[i], psz_preset ) )
395 [o_popup_presets selectItemAtIndex: i];
398 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
399 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
401 if( strcmp( psz_preset, "flat" ) )
405 snprintf( psz_bands, sizeof( psz_bands ),
406 "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
408 eqz_preset_10b[i]->f_amp[0],
409 eqz_preset_10b[i]->f_amp[1],
410 eqz_preset_10b[i]->f_amp[2],
411 eqz_preset_10b[i]->f_amp[3],
412 eqz_preset_10b[i]->f_amp[4],
413 eqz_preset_10b[i]->f_amp[5],
414 eqz_preset_10b[i]->f_amp[6],
415 eqz_preset_10b[i]->f_amp[7],
416 eqz_preset_10b[i]->f_amp[8],
417 eqz_preset_10b[i]->f_amp[9] );
419 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
421 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
423 var_SetFloat( p_object, "equalizer-preamp",
424 eqz_preset_10b[i]->f_preamp );
425 var_SetString( p_object, "equalizer-bands", psz_bands );
429 vlc_object_release( p_object );
432 [self equalizerUpdated];
436 - (id)sliderByIndex:(int)index
440 case 0 : return o_slider_band1;
441 case 1 : return o_slider_band2;
442 case 2 : return o_slider_band3;
443 case 3 : return o_slider_band4;
444 case 4 : return o_slider_band5;
445 case 5 : return o_slider_band6;
446 case 6 : return o_slider_band7;
447 case 7 : return o_slider_band8;
448 case 8 : return o_slider_band9;
449 case 9 : return o_slider_band10;
450 default : return nil;
454 - (void)setBandSlidersValues:(float *)values
457 for (i = 0 ; i<= 9 ; i++)
459 [self setValue:values[i] forSlider:i];
463 - (void)initBandSliders
466 for (i = 0 ; i< 9 ; i++)
468 [self setValue:0.0 forSlider:i];
472 - (void)setValue:(float)value forSlider:(int)index
474 id slider = [self sliderByIndex:index];
478 [slider setFloatValue:value];