]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
s/pl_Yield/pl_Hold/
[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 #ifdef HAVE_STRTOF
219         f_band[i] = strtof( psz_bands, &p_next );
220 #else
221         f_band[i] = (float)strtod( psz_bands, &p_next );
222 #endif
223         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
224     
225         if( !*psz_bands ) break; /* end of line */
226         psz_bands = p_next+1;
227     }
228     free( psz_bands_init );
229     [self setBandSlidersValues:f_band];
230
231     /* Set the the checkboxes */
232     [o_ckb_enable setState: b_enabled];
233
234     [o_ckb_2pass setState: b_2p];        
235 }
236
237 - (IBAction)bandSliderUpdated:(id)sender
238 {
239     intf_thread_t *p_intf = VLCIntf;
240     vlc_object_t *p_object = vlc_object_find( p_intf,
241                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
242
243     if( p_object == NULL )
244         p_object = (vlc_object_t *)pl_Hold( p_intf );
245
246     char psz_values[102];
247     memset( psz_values, 0, 102 );
248
249     /* Write the new bands values */
250     /* TODO: write a generic code instead of ten times the same thing */
251
252     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
253     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
254     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
255     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
256     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
257     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
258     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
259     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
260     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
261     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
262
263     var_SetString( p_object, "equalizer-bands", psz_values );
264
265     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
266     {
267         /* save changed to config */
268         config_PutPsz( p_intf, "equalizer-bands", psz_values );
269
270         /* save to vlcrc */
271         config_SaveConfigFile( p_intf, "equalizer" );
272     }
273
274     vlc_object_release( p_object );
275 }
276
277 - (IBAction)changePreset:(id)sender
278 {
279     intf_thread_t *p_intf = VLCIntf;
280     int i;
281     vlc_object_t *p_object= vlc_object_find( p_intf,
282                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
283     if( p_object == NULL )
284         p_object = (vlc_object_t *)pl_Hold( p_intf );
285
286     char psz_values[102];
287     memset( psz_values, 0, 102 );
288
289     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
290
291     for( i = 0; i < 10; i++ )
292         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
293     var_SetString( p_object, "equalizer-bands", psz_values );
294     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
295
296     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
297
298     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
299
300     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
301     {
302         /* save changed to config */
303         config_PutPsz( p_intf, "equalizer-bands", psz_values );
304         config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
305         config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
306
307         /* save to vlcrc */
308         config_SaveConfigFile( p_intf, "equalizer" );
309     }
310
311     vlc_object_release( p_object );
312 }
313
314 - (IBAction)enable:(id)sender
315 {
316     ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
317 }
318
319 - (IBAction)preampSliderUpdated:(id)sender
320 {
321     intf_thread_t *p_intf = VLCIntf;
322     float f_preamp = [sender floatValue] ;
323
324     vlc_object_t *p_object = vlc_object_find( p_intf,
325                                               VLC_OBJECT_AOUT, FIND_ANYWHERE );
326     if( p_object == NULL )
327         p_object = (vlc_object_t *)pl_Hold( p_intf );
328
329     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
330
331     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
332     {
333         /* save changed to config */
334         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
335
336         /* save to vlcrc */
337         config_SaveConfigFile( p_intf, "equalizer" );
338     }
339
340     vlc_object_release( p_object );
341 }
342
343 - (IBAction)toggleWindow:(id)sender
344 {
345     if( [o_window isVisible] )
346     {
347         [o_window orderOut:sender];
348         [o_btn_equalizer setState:NSOffState];
349     }
350     else
351     {
352         [o_window makeKeyAndOrderFront:sender];
353         [o_btn_equalizer setState:NSOnState];
354     }
355 }
356
357 - (IBAction)twopass:(id)sender
358 {
359     intf_thread_t *p_intf = VLCIntf;
360     bool b_2p = [sender state] ? true : false;
361     vlc_object_t *p_object= vlc_object_find( p_intf,
362                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
363     aout_instance_t *p_aout = (aout_instance_t *)p_object;
364     if( p_object == NULL )
365         p_object = (vlc_object_t *)pl_Hold( p_intf );
366
367     var_SetBool( p_object, "equalizer-2pass", b_2p );
368     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
369     {
370         int i;
371         for( i = 0; i < p_aout->i_nb_inputs; i++ )
372         {
373             p_aout->pp_inputs[i]->b_restart = true;
374         }
375     }
376
377     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
378     {
379         /* save changed to config */
380         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
381
382         /* save to vlcrc */
383         config_SaveConfigFile( p_intf, "equalizer" );
384     }
385
386     vlc_object_release( p_object );
387 }
388
389 - (void)windowWillClose:(NSNotification *)aNotification
390 {
391     [o_btn_equalizer setState: NSOffState];
392 }
393
394 - (void)awakeFromNib
395 {
396     int i;
397     vlc_object_t *p_object= vlc_object_find( VLCIntf,
398                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
399     if( p_object == NULL )
400         p_object = (vlc_object_t *)pl_Hold( VLCIntf );
401
402     [o_window setExcludedFromWindowsMenu: TRUE];
403
404     [self initStrings];
405
406     if( p_object )
407     {
408         char *psz_preset;
409
410         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
411                     VLC_VAR_DOINHERIT );
412         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
413
414         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
415         {
416             if( strcmp( preset_list[i], psz_preset ) )
417                 continue;
418     
419             [o_popup_presets selectItemAtIndex: i];
420         
421
422             [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
423             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
424
425             if( strcmp( psz_preset, "flat" ) )
426             {
427                 char psz_bands[100];
428     
429                 snprintf( psz_bands, sizeof( psz_bands ),
430                           "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
431                           "%.1f %.1f %.1f",
432                           eqz_preset_10b[i]->f_amp[0],
433                           eqz_preset_10b[i]->f_amp[1],
434                           eqz_preset_10b[i]->f_amp[2],
435                           eqz_preset_10b[i]->f_amp[3],
436                           eqz_preset_10b[i]->f_amp[4],
437                           eqz_preset_10b[i]->f_amp[5],
438                           eqz_preset_10b[i]->f_amp[6],
439                           eqz_preset_10b[i]->f_amp[7],
440                           eqz_preset_10b[i]->f_amp[8],
441                           eqz_preset_10b[i]->f_amp[9] );
442     
443                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
444                             VLC_VAR_DOINHERIT );
445                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
446                             VLC_VAR_DOINHERIT );
447                 var_SetFloat( p_object, "equalizer-preamp",
448                               eqz_preset_10b[i]->f_preamp );
449                 var_SetString( p_object, "equalizer-bands", psz_bands );
450             }
451         }
452         free( psz_preset );
453         vlc_object_release( p_object );
454     }
455
456     [self equalizerUpdated];    
457 }
458
459
460 - (id)getSliderByIndex:(int)index
461 {
462     switch(index)
463     {
464         case 0 : return o_slider_band1;
465         case 1 : return o_slider_band2;
466         case 2 : return o_slider_band3;
467         case 3 : return o_slider_band4;
468         case 4 : return o_slider_band5;
469         case 5 : return o_slider_band6;
470         case 6 : return o_slider_band7;
471         case 7 : return o_slider_band8;
472         case 8 : return o_slider_band9;
473         case 9 : return o_slider_band10;
474         default : return nil;
475     }
476 }
477
478 - (void)setBandSlidersValues:(float *)values
479 {
480     int i = 0;
481     for (i = 0 ; i<= 9 ; i++)
482     {
483         [self setValue:values[i] forSlider:i];
484     }
485 }
486
487 - (void)initBandSliders
488 {
489     int i = 0;
490     for (i = 0 ; i< 9 ; i++)
491     {
492         [self setValue:0.0 forSlider:i];
493     }
494 }
495
496 - (void)setValue:(float)value forSlider:(int)index
497 {
498     id slider = [self getSliderByIndex:index];
499
500     if (slider != nil)
501     {
502         [slider setFloatValue:value];
503     }
504 }
505
506 @end