]> git.sesse.net Git - vlc/blob - modules/gui/macosx/AudioEffects.m
macosx: added Audio Effects panel to demonstrate BGHUDAppKit
[vlc] / modules / gui / macosx / AudioEffects.m
1 /*****************************************************************************
2  * AudioEffects.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2004-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *          Jérôme Decoodt <djc@videolan.org>
9  *          
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # import "config.h"
28 #endif
29
30 #import "AudioEffects.h"
31 #import "intf.h"
32 #import "../../audio_filter/equalizer_presets.h"
33
34 #import <vlc_common.h>
35 #import <vlc_aout.h>
36
37 #import <math.h>
38
39 #pragma mark -
40 #pragma mark Initialization & Generic code
41
42 @implementation VLCAudioEffects
43 static VLCAudioEffects *_o_sharedInstance = nil;
44
45 + (VLCAudioEffects *)sharedInstance
46 {
47     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
48 }
49
50 - (id)init
51 {
52     if (_o_sharedInstance) {
53         [self dealloc];
54     } else {
55         _o_sharedInstance = [super init];
56     }
57     
58     return _o_sharedInstance;
59 }
60
61 - (void)awakeFromNib
62 {
63     /* setup the user's language */
64     /* Equalizer */
65     [o_eq_enable_ckb setTitle:_NS("Enable")];
66     [o_eq_twopass_ckb setTitle:_NS("2 Pass")];
67     [o_eq_preamp_lbl setStringValue:_NS("Preamp")];
68     [o_eq_presets_popup removeAllItems];
69     for( int i = 0; i < 18 ; i++ )
70         [o_eq_presets_popup insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
71     
72     /* Compressor */
73     [o_comp_enable_ckb setTitle:_NS("Enable dynamic range compressor")];
74     [o_comp_band1_lbl setStringValue:_NS("RMS/peak")];;
75     [o_comp_band2_lbl setStringValue:_NS("Attack")];
76     [o_comp_band3_lbl setStringValue:_NS("Release")];
77     [o_comp_band4_lbl setStringValue:_NS("Threshold")];
78     [o_comp_band5_lbl setStringValue:_NS("Ratio")];
79     [o_comp_band6_lbl setStringValue:_NS("Knee radius")];
80     [o_comp_band7_lbl setStringValue:_NS("Makeup gain")];
81     
82     /* Spatializer */
83     [o_spat_enable_ckb setTitle:_NS("Enable Spatializer")];
84     [o_spat_band1_lbl setStringValue:_NS("Size")];
85     [o_spat_band2_lbl setStringValue:_NS("Width")];
86     [o_spat_band3_lbl setStringValue:_NS("Wet")];
87     [o_spat_band4_lbl setStringValue:_NS("Dry")];
88     [o_spat_band5_lbl setStringValue:_NS("Dump")];
89     
90     /* Filter */
91     [o_filter_headPhone_ckb setTitle:_NS("Headphone virtualization")];
92     [o_filter_normLevel_ckb setTitle:_NS("Volume normalization")];
93     [o_filter_normLevel_lbl setStringValue:_NS("Maximum level")];
94     
95     /* generic */
96     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"equalizer"]] setLabel:_NS("Equalizer")];
97     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"compressor"]] setLabel:_NS("Compressor")];
98     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"spatializer"]] setLabel:_NS("Spatializer")];
99     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"filter"]] setLabel:_NS("Filter")];
100     [o_window setTitle:_NS("Audio Effects")];
101     [o_window setExcludedFromWindowsMenu:YES];
102
103     [self setupEqualizer];
104 }
105
106 - (IBAction)toggleWindow:(id)sender
107 {
108     if( [o_window isVisible] )
109         [o_window orderOut:sender];
110     else
111         [o_window makeKeyAndOrderFront:sender];
112 }
113
114 #pragma mark -
115 #pragma mark Equalizer
116 static bool GetFiltersStatus( intf_thread_t *p_intf,
117                              char *psz_name )
118 {
119     char *psz_parser, *psz_string = NULL;
120     vlc_object_t *p_object = VLC_OBJECT(getAout());
121     if( p_object == NULL )
122         p_object = vlc_object_hold(pl_Get( p_intf ));
123     
124     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
125         psz_string = config_GetPsz( p_intf, "audio-filter" );
126     
127     if(! psz_string )
128         psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
129     
130     vlc_object_release( p_object );
131     
132     if( !psz_string ) return false;
133     
134     psz_parser = strstr( psz_string, psz_name );
135     
136     free( psz_string );
137     
138     if ( psz_parser )
139         return true;
140     else
141         return false;
142 }
143
144 - (void)setupEqualizer
145 {
146     int i;
147     vlc_object_t *p_object= VLC_OBJECT(getAout());
148     if( p_object == NULL )
149         p_object = vlc_object_hold(pl_Get( VLCIntf ));
150     
151     if( p_object )
152     {
153         char *psz_preset;
154         
155         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
156                    VLC_VAR_DOINHERIT );
157         psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
158         
159         for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
160         {
161             if( strcmp( preset_list[i], psz_preset ) )
162                 continue;
163             
164             [o_eq_presets_popup selectItemAtIndex: i];
165             
166             
167             [o_eq_preamp_sld setFloatValue: eqz_preset_10b[i]->f_preamp];
168             [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
169             
170             if( strcmp( psz_preset, "flat" ) )
171             {
172                 char psz_bands[100];
173                 
174                 snprintf( psz_bands, sizeof( psz_bands ),
175                          "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
176                          "%.1f %.1f %.1f",
177                          eqz_preset_10b[i]->f_amp[0],
178                          eqz_preset_10b[i]->f_amp[1],
179                          eqz_preset_10b[i]->f_amp[2],
180                          eqz_preset_10b[i]->f_amp[3],
181                          eqz_preset_10b[i]->f_amp[4],
182                          eqz_preset_10b[i]->f_amp[5],
183                          eqz_preset_10b[i]->f_amp[6],
184                          eqz_preset_10b[i]->f_amp[7],
185                          eqz_preset_10b[i]->f_amp[8],
186                          eqz_preset_10b[i]->f_amp[9] );
187                 
188                 var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
189                            VLC_VAR_DOINHERIT );
190                 var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
191                            VLC_VAR_DOINHERIT );
192                 var_SetFloat( p_object, "equalizer-preamp",
193                              eqz_preset_10b[i]->f_preamp );
194                 var_SetString( p_object, "equalizer-bands", psz_bands );
195             }
196         }
197         free( psz_preset );
198         vlc_object_release( p_object );
199     }
200     
201     [self equalizerUpdated];
202 }
203
204 - (void)equalizerUpdated
205 {
206     intf_thread_t *p_intf = VLCIntf;
207     float f_preamp, f_band[10];
208     char *psz_bands, *psz_bands_init, *p_next;
209     bool b_2p;
210     int i;
211     bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
212     vlc_object_t *p_object = VLC_OBJECT(getAout());
213     
214     if( p_object == NULL )
215         p_object = vlc_object_hold(pl_Get( p_intf ));
216     
217     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
218                VLC_VAR_DOINHERIT );
219     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
220                VLC_VAR_DOINHERIT );
221     
222     psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
223     
224     if( psz_bands == NULL )
225         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
226     
227     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
228     {
229         b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
230         f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
231     }
232     else
233     {
234         b_2p = var_GetBool( p_object, "equalizer-2pass" );
235         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
236     }
237     
238     vlc_object_release( p_object );
239     
240     /* Set the preamp slider */
241     [o_eq_preamp_sld setFloatValue: f_preamp];
242
243     /* Set the bands slider */
244     psz_bands_init = psz_bands;
245     
246     for( i = 0; i < 10; i++ )
247     {
248         /* Read dB -20/20 */
249         f_band[i] = strtof( psz_bands, &p_next );
250         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
251         
252         if( !*psz_bands ) break; /* end of line */
253         psz_bands = p_next+1;
254     }
255     free( psz_bands_init );
256     [self setBandSlidersValues:f_band];
257     
258     /* Set the the checkboxes */
259     [o_eq_enable_ckb setState: b_enabled];
260     [o_eq_twopass_ckb setState: b_2p];
261 }
262
263 - (id)sliderByIndex:(int)index
264 {
265     switch(index)
266     {
267         case 0 : return o_eq_band1_sld;
268         case 1 : return o_eq_band2_sld;
269         case 2 : return o_eq_band3_sld;
270         case 3 : return o_eq_band4_sld;
271         case 4 : return o_eq_band5_sld;
272         case 5 : return o_eq_band6_sld;
273         case 6 : return o_eq_band7_sld;
274         case 7 : return o_eq_band8_sld;
275         case 8 : return o_eq_band9_sld;
276         case 9 : return o_eq_band10_sld;
277         default : return nil;
278     }
279 }
280
281 - (void)setBandSlidersValues:(float *)values
282 {
283     for (int i = 0 ; i<= 9 ; i++)
284         [self setValue:values[i] forSlider:i];
285 }
286
287 - (void)initBandSliders
288 {
289     for (int i = 0 ; i< 9 ; i++)
290         [self setValue:0.0 forSlider:i];
291 }
292
293 - (void)setValue:(float)value forSlider:(int)index
294 {
295     id slider = [self sliderByIndex:index];
296     
297     if (slider != nil)
298         [slider setFloatValue:value];
299 }
300
301 - (IBAction)eq_enable:(id)sender
302 {
303     aout_EnableFilter( pl_Get( VLCIntf ), (char *)"equalizer", [sender state]);
304 }
305
306 - (IBAction)eq_bandSliderUpdated:(id)sender
307 {
308     intf_thread_t *p_intf = VLCIntf;
309     vlc_object_t *p_object = VLC_OBJECT(getAout());
310     
311     if( p_object == NULL )
312         p_object = vlc_object_hold(pl_Get( p_intf ));
313     
314     const char *psz_values;
315     NSString *preset = [NSString stringWithFormat:@"%.1f ", [o_eq_band1_sld floatValue] ];
316     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band2_sld floatValue] ];
317     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band3_sld floatValue] ];
318     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band4_sld floatValue] ];
319     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band5_sld floatValue] ];
320     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band6_sld floatValue] ];
321     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band7_sld floatValue] ];
322     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band8_sld floatValue] ];
323     preset = [preset stringByAppendingFormat:@"%.1f ", [o_eq_band9_sld floatValue] ];
324     preset = [preset stringByAppendingFormat:@"%.1f", [o_eq_band10_sld floatValue] ];
325
326     psz_values = [preset UTF8String];
327     var_SetString( p_object, "equalizer-bands", psz_values );
328     
329     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
330     {
331         /* save changed to config */
332         config_PutPsz( p_intf, "equalizer-bands", psz_values );
333         
334         /* save to vlcrc */
335         config_SaveConfigFile( p_intf, "equalizer" );
336     }
337     
338     vlc_object_release( p_object );
339 }
340 - (IBAction)eq_changePreset:(id)sender
341 {
342     intf_thread_t *p_intf = VLCIntf;
343     int i;
344     vlc_object_t *p_object= VLC_OBJECT(getAout());
345     if( p_object == NULL )
346         p_object = vlc_object_hold(pl_Get( p_intf ));
347     
348     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
349     
350     NSString *preset = @"";
351     const char *psz_values;
352     for( i = 0; i < 10; i++ )
353     {
354         preset = [preset stringByAppendingFormat:@"%.1f ", eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] ];
355     }
356     psz_values = [preset UTF8String];
357     var_SetString( p_object, "equalizer-bands", psz_values );
358     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
359     
360     [o_eq_preamp_sld setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
361     
362     [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
363     
364     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
365     {
366         /* save changed to config */
367         config_PutPsz( p_intf, "equalizer-bands", psz_values );
368         config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
369         config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );
370         
371         /* save to vlcrc */
372         config_SaveConfigFile( p_intf, "equalizer" );
373     }
374     
375     vlc_object_release( p_object );
376 }
377 - (IBAction)eq_preampSliderUpdated:(id)sender
378 {
379     intf_thread_t *p_intf = VLCIntf;
380     float f_preamp = [sender floatValue] ;
381
382     vlc_object_t *p_object = VLC_OBJECT(getAout());
383     if( p_object == NULL )
384         p_object = vlc_object_hold(pl_Get( p_intf ));
385
386     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
387
388     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
389     {
390         /* save changed to config */
391         config_PutFloat( p_intf, "equalizer-preamp", f_preamp );
392
393         /* save to vlcrc */
394         config_SaveConfigFile( p_intf, "equalizer" );
395     }
396
397     vlc_object_release( p_object );
398 }
399 - (IBAction)eq_twopass:(id)sender
400 {
401     intf_thread_t *p_intf = VLCIntf;
402     bool b_2p = [sender state] ? true : false;
403     aout_instance_t *p_aout = getAout();
404     vlc_object_t *p_object= VLC_OBJECT(p_aout);
405     if( p_object == NULL )
406         p_object = vlc_object_hold(pl_Get( p_intf ));
407     
408     var_SetBool( p_object, "equalizer-2pass", b_2p );
409     
410     if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
411     {
412         /* save changed to config */
413         config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );
414         
415         /* save to vlcrc */
416         config_SaveConfigFile( p_intf, "equalizer" );
417     }
418     
419     vlc_object_release( p_object );
420 }
421
422 #pragma mark -
423 #pragma mark Compressor
424
425
426 #pragma mark -
427 #pragma mark Spatializer
428
429
430 #pragma mark -
431 #pragma mark Filter
432
433 @end