]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
MacOS: fix equalizer 170Hz issue
[vlc] / modules / gui / macosx / equalizer.m
1 /*****************************************************************************
2  * equalizer.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jérôme Decoodt <djc@videolan.org>
8  *          Felix Paul Kühne <fkuehne -at- videolan -dot- org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_aout.h>
34
35 #include "intf.h"
36
37 #include <math.h>
38
39 #include "equalizer.h"
40 #include "../../audio_filter/equalizer_presets.h"
41
42 /*****************************************************************************
43  * VLCEqualizer implementation
44  *****************************************************************************/
45 @implementation VLCEqualizer
46
47 static void ChangeFiltersString( intf_thread_t *p_intf,
48                                  char *psz_name, bool b_add )
49 {
50     char *psz_parser, *psz_string;
51     int i;
52     aout_instance_t *p_aout = getAout();
53     vlc_object_t *p_object = VLC_OBJECT(p_aout);
54     if( !p_object )
55     {
56         p_object = vlc_object_hold(pl_Get( p_intf ));
57     }
58
59     psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
60
61     if( !psz_string ) psz_string = strdup( "" );
62
63     psz_parser = strstr( psz_string, psz_name );
64
65     if( b_add )
66     {
67         if( !psz_parser )
68         {
69             psz_parser = psz_string;
70             asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
71                             psz_string, psz_name );
72             free( psz_parser );
73         }
74         else
75         {
76             vlc_object_release( p_object );
77             return;
78         }
79     }
80     else
81     {
82         if( psz_parser )
83         {
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 );
87
88             if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
89             {
90                 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
91             }
92          }
93          else
94          {
95             free( psz_string );
96             vlc_object_release( p_object );
97             return;
98          }
99     }
100
101     aout_EnableFilter( pl_Get( p_intf ), psz_string, b_add);
102
103     if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
104     {
105         /* save changed to config */
106         config_PutPsz( p_object, "audio-filter", psz_string );
107     }
108
109     free( psz_string );
110     vlc_object_release( p_object );
111 }
112
113 static bool GetFiltersStatus( intf_thread_t *p_intf,
114                                  char *psz_name )
115 {
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 ));
120
121     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
122         psz_string = config_GetPsz( p_intf, "audio-filter" );
123
124     if(! psz_string )
125         psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
126
127     vlc_object_release( p_object );
128
129     if( !psz_string ) return false;
130
131     psz_parser = strstr( psz_string, psz_name );
132
133     free( psz_string );
134
135     if ( psz_parser )
136         return true;
137     else
138         return false;
139 }
140
141 - (void)initStrings
142 {
143     int i;
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")];
153
154     [o_popup_presets removeAllItems];
155     for( i = 0; i < 18 ; i++ )
156     {
157         [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
158     }
159     [o_window setTitle: _NS("Equalizer")];
160
161     [self initBandSliders];
162 }
163
164 - (void)equalizerUpdated
165 {
166     intf_thread_t *p_intf = VLCIntf;
167     float f_preamp, f_band[10];
168     char *psz_bands, *psz_bands_init, *p_next;
169     bool b_2p;
170     int i;
171     bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
172     vlc_object_t *p_object = VLC_OBJECT(getAout());
173
174     if( p_object == NULL )
175         p_object = vlc_object_hold(pl_Get( p_intf ));
176
177     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
178                 VLC_VAR_DOINHERIT );
179     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
180                 VLC_VAR_DOINHERIT );
181
182     psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
183
184     if( psz_bands == NULL )
185         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
186
187     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
188     {
189         b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
190         f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
191     }
192     else
193     {
194         b_2p = var_GetBool( p_object, "equalizer-2pass" );
195         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
196     }
197
198     vlc_object_release( p_object );
199
200     /* Set the preamp slider */
201     [o_slider_preamp setFloatValue: f_preamp];
202
203     /* Set the bands slider */
204     psz_bands_init = psz_bands;
205
206     for( i = 0; i < 10; i++ )
207     {
208         /* Read dB -20/20 */
209         f_band[i] = strtof( psz_bands, &p_next );
210         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
211
212         if( !*psz_bands ) break; /* end of line */
213         psz_bands = p_next+1;
214     }
215     free( psz_bands_init );
216     [self setBandSlidersValues:f_band];
217
218     /* Set the the checkboxes */
219     [o_ckb_enable setState: b_enabled];
220
221     [o_ckb_2pass setState: b_2p];
222 }
223
224 - (IBAction)bandSliderUpdated:(id)sender
225 {
226     intf_thread_t *p_intf = VLCIntf;
227     vlc_object_t *p_object = VLC_OBJECT(getAout());
228
229     if( p_object == NULL )
230         p_object = vlc_object_hold(pl_Get( p_intf ));
231
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] ];
243
244     psz_values = [preset UTF8String];
245     var_SetString( p_object, "equalizer-bands", psz_values );
246
247     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
248     {
249         /* save changed to config */
250         config_PutPsz( p_intf, "equalizer-bands", psz_values );
251
252         /* save to vlcrc */
253         config_SaveConfigFile( p_intf, "equalizer" );
254     }
255
256     vlc_object_release( p_object );
257 }
258
259 - (IBAction)changePreset:(id)sender
260 {
261     intf_thread_t *p_intf = VLCIntf;
262     int i;
263     vlc_object_t *p_object= VLC_OBJECT(getAout());
264     if( p_object == NULL )
265         p_object = vlc_object_hold(pl_Get( p_intf ));
266
267     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
268
269     NSString *preset = @"";
270     const char *psz_values;
271     for( i = 0; i < 10; i++ )
272     {
273         preset = [preset stringByAppendingFormat:@"%.1f ", eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] ];
274     }
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);
278
279     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
280
281     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
282
283     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
284     {
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]] );
289
290         /* save to vlcrc */
291         config_SaveConfigFile( p_intf, "equalizer" );
292     }
293
294     vlc_object_release( p_object );
295 }
296
297 - (IBAction)enable:(id)sender
298 {
299 //    ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
300     // to fix #3718
301     aout_EnableFilter( pl_Get( VLCIntf ), (char *)"equalizer", [sender state]);
302 }
303
304 - (IBAction)preampSliderUpdated:(id)sender
305 {
306     intf_thread_t *p_intf = VLCIntf;
307     float f_preamp = [sender floatValue] ;
308
309     vlc_object_t *p_object = VLC_OBJECT(getAout());
310     if( p_object == NULL )
311         p_object = vlc_object_hold(pl_Get( p_intf ));
312
313     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
314
315     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
316     {
317         /* save changed to config */
318         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
319
320         /* save to vlcrc */
321         config_SaveConfigFile( p_intf, "equalizer" );
322     }
323
324     vlc_object_release( p_object );
325 }
326
327 - (IBAction)toggleWindow:(id)sender
328 {
329     if( [o_window isVisible] )
330     {
331         [o_window orderOut:sender];
332         [o_btn_equalizer setState:NSOffState];
333         [o_btn_equalizer_embedded setState:NSOffState];
334     }
335     else
336     {
337         [o_window makeKeyAndOrderFront:sender];
338         [o_btn_equalizer setState:NSOnState];
339         [o_btn_equalizer_embedded setState:NSOnState];
340     }
341 }
342
343 - (IBAction)twopass:(id)sender
344 {
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 ));
351
352     var_SetBool( p_object, "equalizer-2pass", b_2p );
353
354     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
355     {
356         /* save changed to config */
357         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
358
359         /* save to vlcrc */
360         config_SaveConfigFile( p_intf, "equalizer" );
361     }
362
363     vlc_object_release( p_object );
364 }
365
366 - (void)windowWillClose:(NSNotification *)aNotification
367 {
368     [o_btn_equalizer setState: NSOffState];
369 }
370
371 - (void)awakeFromNib
372 {
373     int i;
374     vlc_object_t *p_object= VLC_OBJECT(getAout());
375     if( p_object == NULL )
376         p_object = vlc_object_hold(pl_Get( VLCIntf ));
377
378     [o_window setExcludedFromWindowsMenu: TRUE];
379
380     [self initStrings];
381
382     if( p_object )
383     {
384         char *psz_preset;
385
386         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
387                     VLC_VAR_DOINHERIT );
388         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
389
390         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
391         {
392             if( strcmp( preset_list[i], psz_preset ) )
393                 continue;
394
395             [o_popup_presets selectItemAtIndex: i];
396
397
398             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
399             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
400
401             if( strcmp( psz_preset, "flat" ) )
402             {
403                 char psz_bands[100];
404
405                 snprintf( psz_bands, sizeof( psz_bands ),
406                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
407                           "%.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] );
418
419                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
420                             VLC_VAR_DOINHERIT );
421                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
422                             VLC_VAR_DOINHERIT );
423                 var_SetFloat( p_object, "equalizer-preamp",
424                               eqz_preset_10b[i]->f_preamp );
425                 var_SetString( p_object, "equalizer-bands", psz_bands );
426             }
427         }
428         free( psz_preset );
429         vlc_object_release( p_object );
430     }
431
432     [self equalizerUpdated];
433 }
434
435
436 - (id)sliderByIndex:(int)index
437 {
438     switch(index)
439     {
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;
451     }
452 }
453
454 - (void)setBandSlidersValues:(float *)values
455 {
456     int i = 0;
457     for (i = 0 ; i<= 9 ; i++)
458     {
459         [self setValue:values[i] forSlider:i];
460     }
461 }
462
463 - (void)initBandSliders
464 {
465     int i = 0;
466     for (i = 0 ; i< 9 ; i++)
467     {
468         [self setValue:0.0 forSlider:i];
469     }
470 }
471
472 - (void)setValue:(float)value forSlider:(int)index
473 {
474     id slider = [self sliderByIndex:index];
475
476     if (slider != nil)
477     {
478         [slider setFloatValue:value];
479     }
480 }
481
482 @end