]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
26c366039e6f3372db3091ada6e1ee54db8cc1ad
[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( p_object, psz_string, false);
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;
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 }
301
302 - (IBAction)preampSliderUpdated:(id)sender
303 {
304     intf_thread_t *p_intf = VLCIntf;
305     float f_preamp = [sender floatValue] ;
306
307     vlc_object_t *p_object = VLC_OBJECT(getAout());
308     if( p_object == NULL )
309         p_object = vlc_object_hold(pl_Get( p_intf ));
310
311     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
312
313     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
314     {
315         /* save changed to config */
316         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
317
318         /* save to vlcrc */
319         config_SaveConfigFile( p_intf, "equalizer" );
320     }
321
322     vlc_object_release( p_object );
323 }
324
325 - (IBAction)toggleWindow:(id)sender
326 {
327     if( [o_window isVisible] )
328     {
329         [o_window orderOut:sender];
330         [o_btn_equalizer setState:NSOffState];
331         [o_btn_equalizer_embedded setState:NSOffState];
332     }
333     else
334     {
335         [o_window makeKeyAndOrderFront:sender];
336         [o_btn_equalizer setState:NSOnState];
337         [o_btn_equalizer_embedded setState:NSOnState];
338     }
339 }
340
341 - (IBAction)twopass:(id)sender
342 {
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 = vl_object_hold(pl_Get( p_intf ));
349
350     var_SetBool( p_object, "equalizer-2pass", b_2p );
351
352     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
353     {
354         /* save changed to config */
355         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
356
357         /* save to vlcrc */
358         config_SaveConfigFile( p_intf, "equalizer" );
359     }
360
361     vlc_object_release( p_object );
362 }
363
364 - (void)windowWillClose:(NSNotification *)aNotification
365 {
366     [o_btn_equalizer setState: NSOffState];
367 }
368
369 - (void)awakeFromNib
370 {
371     int i;
372     vlc_object_t *p_object= VLC_OBJECT(getAout());
373     if( p_object == NULL )
374         p_object = vlc_object_hold(pl_Get( VLCIntf ));
375
376     [o_window setExcludedFromWindowsMenu: TRUE];
377
378     [self initStrings];
379
380     if( p_object )
381     {
382         char *psz_preset;
383
384         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
385                     VLC_VAR_DOINHERIT );
386         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
387
388         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
389         {
390             if( strcmp( preset_list[i], psz_preset ) )
391                 continue;
392     
393             [o_popup_presets selectItemAtIndex: i];
394         
395
396             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
397             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
398
399             if( strcmp( psz_preset, "flat" ) )
400             {
401                 char psz_bands[100];
402     
403                 snprintf( psz_bands, sizeof( psz_bands ),
404                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
405                           "%.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] );
416     
417                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
418                             VLC_VAR_DOINHERIT );
419                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
420                             VLC_VAR_DOINHERIT );
421                 var_SetFloat( p_object, "equalizer-preamp",
422                               eqz_preset_10b[i]->f_preamp );
423                 var_SetString( p_object, "equalizer-bands", psz_bands );
424             }
425         }
426         free( psz_preset );
427         vlc_object_release( p_object );
428     }
429
430     [self equalizerUpdated];    
431 }
432
433
434 - (id)sliderByIndex:(int)index
435 {
436     switch(index)
437     {
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;
449     }
450 }
451
452 - (void)setBandSlidersValues:(float *)values
453 {
454     int i = 0;
455     for (i = 0 ; i<= 9 ; i++)
456     {
457         [self setValue:values[i] forSlider:i];
458     }
459 }
460
461 - (void)initBandSliders
462 {
463     int i = 0;
464     for (i = 0 ; i< 9 ; i++)
465     {
466         [self setValue:0.0 forSlider:i];
467     }
468 }
469
470 - (void)setValue:(float)value forSlider:(int)index
471 {
472     id slider = [self sliderByIndex:index];
473
474     if (slider != nil)
475     {
476         [slider setFloatValue:value];
477     }
478 }
479
480 @end