]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
4f78770d3fa6e233f7bca6f43515daa9aa29101b
[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     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;
55     if( !p_object )
56     {
57         p_object = (vlc_object_t *)pl_Hold( p_intf );
58     }
59
60     psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
61
62     if( !psz_string ) psz_string = strdup( "" );
63
64     psz_parser = strstr( psz_string, psz_name );
65
66     if( b_add )
67     {
68         if( !psz_parser )
69         {
70             psz_parser = psz_string;
71             asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
72                             psz_string, psz_name );
73             free( psz_parser );
74         }
75         else
76         {
77             vlc_object_release( p_object );
78             return;
79         }
80     }
81     else
82     {
83         if( psz_parser )
84         {
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 );
88
89             if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
90             {
91                 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
92             }
93          }
94          else
95          {
96             free( psz_string );
97             vlc_object_release( p_object );
98             return;
99          }
100     }
101
102     var_SetString( p_object, "audio-filter", psz_string );
103     if( p_aout )
104     {
105         for( i = 0; i < p_aout->i_nb_inputs; i++ )
106         {
107             p_aout->pp_inputs[i]->b_restart = true;
108         }
109     }
110     
111     if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
112     {
113         /* save changed to config */
114         config_PutPsz( p_object, "audio-filter", psz_string );
115     }
116     
117     free( psz_string );
118     vlc_object_release( p_object );
119 }
120
121 static bool GetFiltersStatus( intf_thread_t *p_intf,
122                                  char *psz_name )
123 {
124     char *psz_parser, *psz_string;
125     vlc_object_t *p_object = vlc_object_find( p_intf,
126                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
127     if( p_object == NULL )
128         p_object = (vlc_object_t *)pl_Hold( p_intf );
129
130     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
131         psz_string = config_GetPsz( p_intf, "audio-filter" );
132
133     if(! psz_string )
134         psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
135
136     vlc_object_release( p_object );
137
138     if( !psz_string ) return false;
139
140     psz_parser = strstr( psz_string, psz_name );
141
142     free( psz_string );
143
144     if ( psz_parser )
145         return true;
146     else
147         return false;
148 }
149
150 - (void)initStrings
151 {
152     int i;
153     [o_btn_equalizer setToolTip: _NS("Equalizer")];
154     [o_ckb_2pass setTitle: _NS("2 Pass")];
155     [o_ckb_2pass setToolTip: _NS("Apply the "
156         "equalizer filter twice. The effect will be sharper.")];
157     [o_ckb_enable setTitle: _NS("Enable")];
158     [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
159         "manually or using a preset.")];
160     [o_fld_preamp setStringValue: _NS("Preamp")];
161
162     [o_popup_presets removeAllItems];
163     for( i = 0; i < 18 ; i++ )
164     {
165         [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
166     }
167     [o_window setTitle: _NS("Equalizer")];
168
169     [self initBandSliders];
170 }
171
172 - (void)equalizerUpdated
173 {
174     intf_thread_t *p_intf = VLCIntf;
175     float f_preamp, f_band[10];
176     char *psz_bands, *psz_bands_init, *p_next;
177     bool b_2p;
178     int i;
179     bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
180     vlc_object_t *p_object = vlc_object_find( p_intf,
181                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
182
183     if( p_object == NULL )
184         p_object = (vlc_object_t *)pl_Hold( p_intf );
185
186     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
187                 VLC_VAR_DOINHERIT );
188     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
189                 VLC_VAR_DOINHERIT );
190
191     psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
192
193     if( psz_bands == NULL )
194         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
195
196     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
197     {
198         b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
199         f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
200     }
201     else
202     {
203         b_2p = var_GetBool( p_object, "equalizer-2pass" );
204         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
205     }
206
207     vlc_object_release( p_object );
208     
209     /* Set the preamp slider */
210     [o_slider_preamp setFloatValue: f_preamp];
211
212     /* Set the bands slider */
213     psz_bands_init = psz_bands;
214
215     for( i = 0; i < 10; i++ )
216     {
217         /* Read dB -20/20 */
218         f_band[i] = strtof( psz_bands, &p_next );
219         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
220     
221         if( !*psz_bands ) break; /* end of line */
222         psz_bands = p_next+1;
223     }
224     free( psz_bands_init );
225     [self setBandSlidersValues:f_band];
226
227     /* Set the the checkboxes */
228     [o_ckb_enable setState: b_enabled];
229
230     [o_ckb_2pass setState: b_2p];        
231 }
232
233 - (IBAction)bandSliderUpdated:(id)sender
234 {
235     intf_thread_t *p_intf = VLCIntf;
236     vlc_object_t *p_object = vlc_object_find( p_intf,
237                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
238
239     if( p_object == NULL )
240         p_object = (vlc_object_t *)pl_Hold( p_intf );
241
242     char psz_values[102];
243     memset( psz_values, 0, 102 );
244
245     /* Write the new bands values */
246     /* TODO: write a generic code instead of ten times the same thing */
247
248     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
249     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
250     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
251     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
252     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
253     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
254     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
255     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
256     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
257     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
258
259     var_SetString( p_object, "equalizer-bands", psz_values );
260
261     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
262     {
263         /* save changed to config */
264         config_PutPsz( p_intf, "equalizer-bands", psz_values );
265
266         /* save to vlcrc */
267         config_SaveConfigFile( p_intf, "equalizer" );
268     }
269
270     vlc_object_release( p_object );
271 }
272
273 - (IBAction)changePreset:(id)sender
274 {
275     intf_thread_t *p_intf = VLCIntf;
276     int i;
277     vlc_object_t *p_object= vlc_object_find( p_intf,
278                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
279     if( p_object == NULL )
280         p_object = (vlc_object_t *)pl_Hold( p_intf );
281
282     char psz_values[102];
283     memset( psz_values, 0, 102 );
284
285     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
286
287     for( i = 0; i < 10; i++ )
288         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
289     var_SetString( p_object, "equalizer-bands", psz_values );
290     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
291
292     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
293
294     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
295
296     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
297     {
298         /* save changed to config */
299         config_PutPsz( p_intf, "equalizer-bands", psz_values );
300         config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
301         config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
302
303         /* save to vlcrc */
304         config_SaveConfigFile( p_intf, "equalizer" );
305     }
306
307     vlc_object_release( p_object );
308 }
309
310 - (IBAction)enable:(id)sender
311 {
312     ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
313 }
314
315 - (IBAction)preampSliderUpdated:(id)sender
316 {
317     intf_thread_t *p_intf = VLCIntf;
318     float f_preamp = [sender floatValue] ;
319
320     vlc_object_t *p_object = vlc_object_find( p_intf,
321                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
322     if( p_object == NULL )
323         p_object = (vlc_object_t *)pl_Hold( p_intf );
324
325     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
326
327     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
328     {
329         /* save changed to config */
330         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
331
332         /* save to vlcrc */
333         config_SaveConfigFile( p_intf, "equalizer" );
334     }
335
336     vlc_object_release( p_object );
337 }
338
339 - (IBAction)toggleWindow:(id)sender
340 {
341     if( [o_window isVisible] )
342     {
343         [o_window orderOut:sender];
344         [o_btn_equalizer setState:NSOffState];
345     }
346     else
347     {
348         [o_window makeKeyAndOrderFront:sender];
349         [o_btn_equalizer setState:NSOnState];
350     }
351 }
352
353 - (IBAction)twopass:(id)sender
354 {
355     intf_thread_t *p_intf = VLCIntf;
356     bool b_2p = [sender state] ? true : false;
357     vlc_object_t *p_object= vlc_object_find( p_intf,
358                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
359     aout_instance_t *p_aout = (aout_instance_t *)p_object;
360     if( p_object == NULL )
361         p_object = (vlc_object_t *)pl_Hold( p_intf );
362
363     var_SetBool( p_object, "equalizer-2pass", b_2p );
364     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
365     {
366         int i;
367         for( i = 0; i < p_aout->i_nb_inputs; i++ )
368         {
369             p_aout->pp_inputs[i]->b_restart = true;
370         }
371     }
372
373     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
374     {
375         /* save changed to config */
376         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
377
378         /* save to vlcrc */
379         config_SaveConfigFile( p_intf, "equalizer" );
380     }
381
382     vlc_object_release( p_object );
383 }
384
385 - (void)windowWillClose:(NSNotification *)aNotification
386 {
387     [o_btn_equalizer setState: NSOffState];
388 }
389
390 - (void)awakeFromNib
391 {
392     int i;
393     vlc_object_t *p_object= vlc_object_find( VLCIntf,
394                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
395     if( p_object == NULL )
396         p_object = (vlc_object_t *)pl_Hold( VLCIntf );
397
398     [o_window setExcludedFromWindowsMenu: TRUE];
399
400     [self initStrings];
401
402     if( p_object )
403     {
404         char *psz_preset;
405
406         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
407                     VLC_VAR_DOINHERIT );
408         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
409
410         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
411         {
412             if( strcmp( preset_list[i], psz_preset ) )
413                 continue;
414     
415             [o_popup_presets selectItemAtIndex: i];
416         
417
418             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
419             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
420
421             if( strcmp( psz_preset, "flat" ) )
422             {
423                 char psz_bands[100];
424     
425                 snprintf( psz_bands, sizeof( psz_bands ),
426                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
427                           "%.1f %.1f %.1f",
428                           eqz_preset_10b[i]->f_amp[0],
429                           eqz_preset_10b[i]->f_amp[1],
430                           eqz_preset_10b[i]->f_amp[2],
431                           eqz_preset_10b[i]->f_amp[3],
432                           eqz_preset_10b[i]->f_amp[4],
433                           eqz_preset_10b[i]->f_amp[5],
434                           eqz_preset_10b[i]->f_amp[6],
435                           eqz_preset_10b[i]->f_amp[7],
436                           eqz_preset_10b[i]->f_amp[8],
437                           eqz_preset_10b[i]->f_amp[9] );
438     
439                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
440                             VLC_VAR_DOINHERIT );
441                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
442                             VLC_VAR_DOINHERIT );
443                 var_SetFloat( p_object, "equalizer-preamp",
444                               eqz_preset_10b[i]->f_preamp );
445                 var_SetString( p_object, "equalizer-bands", psz_bands );
446             }
447         }
448         free( psz_preset );
449         vlc_object_release( p_object );
450     }
451
452     [self equalizerUpdated];    
453 }
454
455
456 - (id)sliderByIndex:(int)index
457 {
458     switch(index)
459     {
460         case 0 : return o_slider_band1;
461         case 1 : return o_slider_band2;
462         case 2 : return o_slider_band3;
463         case 3 : return o_slider_band4;
464         case 4 : return o_slider_band5;
465         case 5 : return o_slider_band6;
466         case 6 : return o_slider_band7;
467         case 7 : return o_slider_band8;
468         case 8 : return o_slider_band9;
469         case 9 : return o_slider_band10;
470         default : return nil;
471     }
472 }
473
474 - (void)setBandSlidersValues:(float *)values
475 {
476     int i = 0;
477     for (i = 0 ; i<= 9 ; i++)
478     {
479         [self setValue:values[i] forSlider:i];
480     }
481 }
482
483 - (void)initBandSliders
484 {
485     int i = 0;
486     for (i = 0 ; i< 9 ; i++)
487     {
488         [self setValue:0.0 forSlider:i];
489     }
490 }
491
492 - (void)setValue:(float)value forSlider:(int)index
493 {
494     id slider = [self sliderByIndex:index];
495
496     if (slider != nil)
497     {
498         [slider setFloatValue:value];
499     }
500 }
501
502 @end