]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
1a217006b2604036c02b4d11cff018df93547ed9
[vlc] / modules / gui / macosx / equalizer.m
1 /*****************************************************************************
2  * equalizer.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jerome Decoodt <djc@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/aout.h>
29 #include <aout_internal.h>
30
31 #include "intf.h"
32
33 #include <math.h>
34
35 #include "equalizer.h"
36 #include "../../audio_filter/equalizer_presets.h"
37
38 /*****************************************************************************
39  * VLCEqualizer implementation 
40  *****************************************************************************/
41 @implementation VLCEqualizer
42
43 static void ChangeFiltersString( intf_thread_t *p_intf,
44                                  char *psz_name, vlc_bool_t b_add )
45 {
46     char *psz_parser, *psz_string;
47     int i;
48     vlc_object_t *p_object = vlc_object_find( p_intf,
49                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
50     aout_instance_t *p_aout = (aout_instance_t *)p_object;
51     if( p_object == NULL )
52         p_object = vlc_object_find( p_intf,
53                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
54     if( p_object == NULL )
55         return;
56
57     psz_string = var_GetString( p_object, "audio-filter" );
58
59     if( !psz_string ) psz_string = strdup( "" );
60
61     psz_parser = strstr( psz_string, psz_name );
62
63     if( b_add )
64     {
65         if( !psz_parser )
66         {
67             psz_parser = psz_string;
68             asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
69                             psz_string, psz_name );
70             free( psz_parser );
71         }
72         else
73         {
74             return;
75         }
76     }
77     else
78     {
79         if( psz_parser )
80         {
81             memmove( psz_parser, psz_parser + strlen( psz_name ) +
82                             ( *( psz_parser + strlen( psz_name ) ) == ',' ? 1 : 0 ),
83                             strlen( psz_parser + strlen( psz_name ) ) + 1 );
84
85             if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
86             {
87                 *( psz_string+strlen( psz_string ) - 1 ) = '\0';
88             }
89          }
90          else
91          {
92              free( psz_string );
93              return;
94          }
95     }
96
97     var_SetString( p_object, "audio-filter", psz_string );
98     if( p_aout )
99     {
100         for( i = 0; i < p_aout->i_nb_inputs; i++ )
101         {
102             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
103         }
104     }
105     free( psz_string );
106     vlc_object_release( p_object );
107 }
108
109 static vlc_bool_t GetFiltersStatus( intf_thread_t *p_intf,
110                                  char *psz_name )
111 {
112     char *psz_parser, *psz_string;
113     vlc_object_t *p_object = vlc_object_find( p_intf,
114                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
115     if( p_object == NULL )
116         p_object = vlc_object_find( p_intf,
117                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
118     if( p_object == NULL )
119         return VLC_FALSE;
120
121     psz_string = var_GetString( p_object, "audio-filter" );
122
123     vlc_object_release( p_object );
124
125     if( !psz_string ) return VLC_FALSE;
126
127     psz_parser = strstr( psz_string, psz_name );
128
129     free( psz_string );
130
131     if ( psz_parser )
132         return VLC_TRUE;
133     else
134         return VLC_FALSE;
135 }
136
137 - (void)initStrings
138 {
139     int i;
140     [o_btn_equalizer setToolTip: _NS("Equalizer")];
141     [o_ckb_2pass setTitle: _NS("2 Pass")];
142     [o_ckb_2pass setToolTip: _NS("If you enable this settting, the "
143         "equalizer filter will be applied twice. The effect will be sharper.")];
144     [o_ckb_enable setTitle: _NS("Enable")];
145     [o_ckb_enable setToolTip: _NS("Enable the equalizer. You can either "
146         "manually change the bands or use a preset.")];
147     [o_fld_preamp setStringValue: _NS("Preamp")];
148
149     [o_popup_presets removeAllItems];
150     for( i = 0; i < 18 ; i++ )
151     {
152         [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
153     }
154     [o_window setTitle: _NS("Equalizer")];
155
156     [o_slider_band1 setFloatValue: 0];
157     [o_slider_band2 setFloatValue: 0];
158     [o_slider_band3 setFloatValue: 0];
159     [o_slider_band4 setFloatValue: 0];
160     [o_slider_band5 setFloatValue: 0];
161     [o_slider_band6 setFloatValue: 0];
162     [o_slider_band7 setFloatValue: 0];
163     [o_slider_band8 setFloatValue: 0];
164     [o_slider_band9 setFloatValue: 0];
165     [o_slider_band10 setFloatValue: 0];
166
167     [o_ckb_enable setState: NSOffState];
168     [o_ckb_2pass setState: NSOffState];
169 }
170
171 - (void)equalizerUpdated
172 {
173     intf_thread_t *p_intf = VLCIntf;
174     float f_preamp, f_band[10];
175     char *psz_bands, *psz_bands_init, *p_next;
176     vlc_bool_t b_2p;
177     int i;
178     vlc_bool_t b_enabled = GetFiltersStatus( p_intf, "equalizer" );
179     vlc_object_t *p_object = vlc_object_find( p_intf,
180                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
181
182     if( p_object == NULL )
183         p_object = vlc_object_find( p_intf,
184                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
185     if( p_object == NULL )
186         return;
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
193     f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
194     psz_bands = var_GetString( p_object, "equalizer-bands" );
195
196     if( !strcmp( psz_bands, "" ) )
197         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
198
199     b_2p = var_GetBool( p_object, "equalizer-2pass" );
200
201     vlc_object_release( p_object );
202
203 /* Set the preamp slider */
204     [o_slider_preamp setFloatValue: f_preamp];
205
206 /* Set the bands slider */
207     psz_bands_init = psz_bands;
208
209     for( i = 0; i < 10; i++ )
210     {
211         /* Read dB -20/20 */
212 #ifdef HAVE_STRTOF
213         f_band[i] = strtof( psz_bands, &p_next );
214 #else
215         f_band[i] = (float)strtod( psz_bands, &p_next );
216 #endif
217         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
218
219         if( !*psz_bands ) break; /* end of line */
220         psz_bands = p_next+1;
221     }
222     free( psz_bands_init );
223
224     [o_slider_band1 setFloatValue: f_band[0]];
225     [o_slider_band2 setFloatValue: f_band[1]];
226     [o_slider_band3 setFloatValue: f_band[2]];
227     [o_slider_band4 setFloatValue: f_band[3]];
228     [o_slider_band5 setFloatValue: f_band[4]];
229     [o_slider_band6 setFloatValue: f_band[5]];
230     [o_slider_band7 setFloatValue: f_band[6]];
231     [o_slider_band8 setFloatValue: f_band[7]];
232     [o_slider_band9 setFloatValue: f_band[8]];
233     [o_slider_band10 setFloatValue: f_band[9]];
234
235 /* Set the the checkboxes */
236     if( b_enabled == VLC_TRUE )
237         [o_ckb_enable setState:NSOnState];
238     else
239         [o_ckb_enable setState:NSOffState];
240
241     [o_ckb_2pass setState:( ( b_2p == VLC_TRUE ) ? NSOnState : NSOffState )];
242 }
243
244 - (IBAction)bandSliderUpdated:(id)sender
245 {
246     intf_thread_t *p_intf = VLCIntf;
247     vlc_object_t *p_object = vlc_object_find( p_intf,
248                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
249     char psz_values[102];
250     memset( psz_values, 0, 102 );
251
252     if( p_object == NULL )
253         p_object = vlc_object_find( p_intf,
254                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
255     if( p_object == NULL )
256         return;
257
258     /* Write the new bands values */
259 /* TODO: write a generic code instead of ten times the same thing */
260
261     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
262     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
263     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
264     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
265     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
266     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
267     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
268     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
269     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
270     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
271
272     var_SetString( p_object, "equalizer-bands", psz_values );
273     vlc_object_release( p_object );
274 }
275
276 - (IBAction)changePreset:(id)sender
277 {
278     intf_thread_t *p_intf = VLCIntf;
279     int i;
280     vlc_object_t *p_object= vlc_object_find( p_intf,
281                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
282     char psz_values[102];
283     memset( psz_values, 0, 102 );
284
285     if( p_object == NULL )
286         p_object = vlc_object_find( p_intf,
287                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
288     if( p_object == NULL )
289         return;
290
291     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
292
293     for( i = 0; i < 10; i++ )
294         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
295     var_SetString( p_object, "equalizer-bands", psz_values );
296     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
297
298     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
299     [o_slider_band1 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[0]];
300     [o_slider_band2 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[1]];
301     [o_slider_band3 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[2]];
302     [o_slider_band4 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[3]];
303     [o_slider_band5 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[4]];
304     [o_slider_band6 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[5]];
305     [o_slider_band7 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[6]];
306     [o_slider_band8 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[7]];
307     [o_slider_band9 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[8]];
308     [o_slider_band10 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[9]];
309
310     vlc_object_release( p_object );
311 }
312
313 - (IBAction)enable:(id)sender
314 {
315     ChangeFiltersString( VLCIntf, "equalizer", [sender state] );
316 }
317
318 - (IBAction)preampSliderUpdated:(id)sender
319 {
320     intf_thread_t *p_intf = VLCIntf;
321     float f_preamp = [sender floatValue] ;
322
323     vlc_object_t *p_object = vlc_object_find( p_intf,
324                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
325     if( p_object == NULL )
326         p_object = vlc_object_find( p_intf,
327                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
328     if( p_object == NULL )
329         return;
330
331     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
332
333     vlc_object_release( p_object );
334 }
335
336 - (IBAction)toggleWindow:(id)sender
337 {
338     if( [o_window isVisible] )
339     {
340         [o_window orderOut:sender];
341         [o_btn_equalizer setState:NSOffState];
342     }
343     else
344     {
345         [o_window makeKeyAndOrderFront:sender];
346         [o_btn_equalizer setState:NSOnState];
347     }
348 }
349
350 - (IBAction)twopass:(id)sender
351 {
352     intf_thread_t *p_intf = VLCIntf;
353     vlc_bool_t b_2p = [sender state] ? VLC_TRUE : VLC_FALSE;
354     vlc_object_t *p_object= vlc_object_find( p_intf,
355                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
356     aout_instance_t *p_aout = (aout_instance_t *)p_object;
357     if( p_object == NULL )
358         p_object = vlc_object_find( p_intf,
359                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
360     if( p_object == NULL )
361         return;
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 = VLC_TRUE;
370         }
371     }
372
373     vlc_object_release( p_object );
374 }
375
376 - (void)windowWillClose:(NSNotification *)aNotification
377 {
378     [o_btn_equalizer setState: NSOffState];
379 }
380
381 - (void)awakeFromNib
382 {
383     int i;
384     vlc_object_t *p_object= vlc_object_find( VLCIntf,
385                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
386     if( p_object == NULL )
387                 p_object = vlc_object_find( VLCIntf,
388                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
389
390     [o_window setExcludedFromWindowsMenu: TRUE];
391
392     [self initStrings];
393
394     if( p_object )
395     {
396         char *psz_preset;
397
398         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
399                                                         VLC_VAR_DOINHERIT );
400         psz_preset = var_GetString( p_object, "equalizer-preset" );
401
402         for( i = 0 ; i < 18 ; i++ )
403         {
404             if( !strcmp( preset_list[i], psz_preset ) )
405             {
406                 [o_popup_presets selectItemAtIndex: i];
407
408                 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
409                 [o_slider_band1 setFloatValue: eqz_preset_10b[i]->f_amp[0]];
410                 [o_slider_band2 setFloatValue: eqz_preset_10b[i]->f_amp[1]];
411                 [o_slider_band3 setFloatValue: eqz_preset_10b[i]->f_amp[2]];
412                 [o_slider_band4 setFloatValue: eqz_preset_10b[i]->f_amp[3]];
413                 [o_slider_band5 setFloatValue: eqz_preset_10b[i]->f_amp[4]];
414                 [o_slider_band6 setFloatValue: eqz_preset_10b[i]->f_amp[5]];
415                 [o_slider_band7 setFloatValue: eqz_preset_10b[i]->f_amp[6]];
416                 [o_slider_band8 setFloatValue: eqz_preset_10b[i]->f_amp[7]];
417                 [o_slider_band9 setFloatValue: eqz_preset_10b[i]->f_amp[8]];
418                 [o_slider_band10 setFloatValue: eqz_preset_10b[i]->f_amp[9]];
419
420                 if( strcmp( psz_preset, "flat" ) )
421                 {
422                     char psz_bands[100];
423                     memset( psz_bands, 0, 100 );
424
425                     sprintf( psz_bands, "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
426                                         "%.1f %.1f %.1f",
427                                         eqz_preset_10b[i]->f_amp[0],
428                                         eqz_preset_10b[i]->f_amp[1],
429                                         eqz_preset_10b[i]->f_amp[2],
430                                         eqz_preset_10b[i]->f_amp[3],
431                                         eqz_preset_10b[i]->f_amp[4],
432                                         eqz_preset_10b[i]->f_amp[5],
433                                         eqz_preset_10b[i]->f_amp[6],
434                                         eqz_preset_10b[i]->f_amp[7],
435                                         eqz_preset_10b[i]->f_amp[8],
436                                         eqz_preset_10b[i]->f_amp[9] );
437
438                     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
439                                                             VLC_VAR_DOINHERIT );
440                     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
441                                                             VLC_VAR_DOINHERIT );
442                     var_SetFloat( p_object, "equalizer-preamp",
443                                                 eqz_preset_10b[i]->f_preamp );
444                     var_SetString( p_object, "equalizer-bands", psz_bands );
445                 }
446             }
447         }
448         free( psz_preset );
449         vlc_object_release( p_object );
450     }
451
452     [self equalizerUpdated];
453
454 }
455
456 @end