]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
String stuff
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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("Apply the"
143 /// \bug [String] missing space
144         "equalizer filter twice. The effect will be sharper.")];
145     [o_ckb_enable setTitle: _NS("Enable")];
146     [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
147         "manually or using a preset.")];
148     [o_fld_preamp setStringValue: _NS("Preamp")];
149
150     [o_popup_presets removeAllItems];
151     for( i = 0; i < 18 ; i++ )
152     {
153         [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
154     }
155     [o_window setTitle: _NS("Equalizer")];
156
157         /*
158     [o_slider_band1 setFloatValue: 0];
159     [o_slider_band2 setFloatValue: 0];
160     [o_slider_band3 setFloatValue: 0];
161     [o_slider_band4 setFloatValue: 0];
162     [o_slider_band5 setFloatValue: 0];
163     [o_slider_band6 setFloatValue: 0];
164     [o_slider_band7 setFloatValue: 0];
165     [o_slider_band8 setFloatValue: 0];
166     [o_slider_band9 setFloatValue: 0];
167     [o_slider_band10 setFloatValue: 0];
168         */
169         [self initBandSliders];
170     [o_ckb_enable setState: NSOffState];
171     [o_ckb_2pass setState: NSOffState];
172 }
173
174 - (void)equalizerUpdated
175 {
176     intf_thread_t *p_intf = VLCIntf;
177     float f_preamp, f_band[10];
178     char *psz_bands, *psz_bands_init, *p_next;
179     vlc_bool_t b_2p;
180     int i;
181     vlc_bool_t b_enabled = GetFiltersStatus( p_intf, "equalizer" );
182     vlc_object_t *p_object = vlc_object_find( p_intf,
183                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
184
185     if( p_object == NULL )
186         p_object = vlc_object_find( p_intf,
187                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
188     if( p_object == NULL )
189         return;
190
191     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
192                                                     VLC_VAR_DOINHERIT );
193     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
194                                                     VLC_VAR_DOINHERIT );
195
196     f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
197     psz_bands = var_GetString( p_object, "equalizer-bands" );
198
199     if( !strcmp( psz_bands, "" ) )
200         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
201
202     b_2p = var_GetBool( p_object, "equalizer-2pass" );
203
204     vlc_object_release( p_object );
205
206 /* Set the preamp slider */
207     [o_slider_preamp setFloatValue: f_preamp];
208
209 /* Set the bands slider */
210     psz_bands_init = psz_bands;
211
212     for( i = 0; i < 10; i++ )
213     {
214         /* Read dB -20/20 */
215 #ifdef HAVE_STRTOF
216         f_band[i] = strtof( psz_bands, &p_next );
217 #else
218         f_band[i] = (float)strtod( psz_bands, &p_next );
219 #endif
220         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
221
222         if( !*psz_bands ) break; /* end of line */
223         psz_bands = p_next+1;
224     }
225     free( psz_bands_init );
226         [self setBandSlidersValues:f_band];
227
228         /*
229     [o_slider_band1 setFloatValue: f_band[0]];
230     [o_slider_band2 setFloatValue: f_band[1]];
231     [o_slider_band3 setFloatValue: f_band[2]];
232     [o_slider_band4 setFloatValue: f_band[3]];
233     [o_slider_band5 setFloatValue: f_band[4]];
234     [o_slider_band6 setFloatValue: f_band[5]];
235     [o_slider_band7 setFloatValue: f_band[6]];
236     [o_slider_band8 setFloatValue: f_band[7]];
237     [o_slider_band9 setFloatValue: f_band[8]];
238     [o_slider_band10 setFloatValue: f_band[9]];
239         */
240         
241 /* Set the the checkboxes */
242     if( b_enabled == VLC_TRUE )
243         [o_ckb_enable setState:NSOnState];
244     else
245         [o_ckb_enable setState:NSOffState];
246
247     [o_ckb_2pass setState:( ( b_2p == VLC_TRUE ) ? NSOnState : NSOffState )];
248 }
249
250 - (IBAction)bandSliderUpdated:(id)sender
251 {
252     intf_thread_t *p_intf = VLCIntf;
253     vlc_object_t *p_object = vlc_object_find( p_intf,
254                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
255     char psz_values[102];
256     memset( psz_values, 0, 102 );
257
258     if( p_object == NULL )
259         p_object = vlc_object_find( p_intf,
260                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
261     if( p_object == NULL )
262         return;
263
264     /* Write the new bands values */
265 /* TODO: write a generic code instead of ten times the same thing */
266
267     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
268     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
269     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
270     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
271     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
272     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
273     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
274     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
275     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
276     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
277
278     var_SetString( p_object, "equalizer-bands", psz_values );
279     vlc_object_release( p_object );
280 }
281
282 - (IBAction)changePreset:(id)sender
283 {
284     intf_thread_t *p_intf = VLCIntf;
285     int i;
286     vlc_object_t *p_object= vlc_object_find( p_intf,
287                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
288     char psz_values[102];
289     memset( psz_values, 0, 102 );
290
291     if( p_object == NULL )
292         p_object = vlc_object_find( p_intf,
293                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
294     if( p_object == NULL )
295         return;
296
297     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
298
299     for( i = 0; i < 10; i++ )
300         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
301     var_SetString( p_object, "equalizer-bands", psz_values );
302     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
303
304     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
305         /*
306     [o_slider_band1 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[0]];
307     [o_slider_band2 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[1]];
308     [o_slider_band3 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[2]];
309     [o_slider_band4 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[3]];
310     [o_slider_band5 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[4]];
311     [o_slider_band6 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[5]];
312     [o_slider_band7 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[6]];
313     [o_slider_band8 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[7]];
314     [o_slider_band9 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[8]];
315     [o_slider_band10 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[9]];
316         */
317         [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
318         
319     vlc_object_release( p_object );
320 }
321
322 - (IBAction)enable:(id)sender
323 {
324     ChangeFiltersString( VLCIntf, "equalizer", [sender state] );
325 }
326
327 - (IBAction)preampSliderUpdated:(id)sender
328 {
329     intf_thread_t *p_intf = VLCIntf;
330     float f_preamp = [sender floatValue] ;
331
332     vlc_object_t *p_object = vlc_object_find( p_intf,
333                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
334     if( p_object == NULL )
335         p_object = vlc_object_find( p_intf,
336                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
337     if( p_object == NULL )
338         return;
339
340     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
341
342     vlc_object_release( p_object );
343 }
344
345 - (IBAction)toggleWindow:(id)sender
346 {
347     if( [o_window isVisible] )
348     {
349         [o_window orderOut:sender];
350         [o_btn_equalizer setState:NSOffState];
351     }
352     else
353     {
354         [o_window makeKeyAndOrderFront:sender];
355         [o_btn_equalizer setState:NSOnState];
356     }
357 }
358
359 - (IBAction)twopass:(id)sender
360 {
361     intf_thread_t *p_intf = VLCIntf;
362     vlc_bool_t b_2p = [sender state] ? VLC_TRUE : VLC_FALSE;
363     vlc_object_t *p_object= vlc_object_find( p_intf,
364                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
365     aout_instance_t *p_aout = (aout_instance_t *)p_object;
366     if( p_object == NULL )
367         p_object = vlc_object_find( p_intf,
368                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
369     if( p_object == NULL )
370         return;
371
372     var_SetBool( p_object, "equalizer-2pass", b_2p );
373     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
374     {
375        int i;
376         for( i = 0; i < p_aout->i_nb_inputs; i++ )
377         {
378             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
379         }
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_find( VLCIntf,
397                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
398
399     [o_window setExcludedFromWindowsMenu: TRUE];
400
401     [self initStrings];
402
403     if( p_object )
404     {
405         char *psz_preset;
406
407         var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
408                                                         VLC_VAR_DOINHERIT );
409         psz_preset = var_GetString( p_object, "equalizer-preset" );
410
411         for( i = 0 ; i < 18 ; i++ )
412         {
413             if( !strcmp( preset_list[i], psz_preset ) )
414             {
415                 [o_popup_presets selectItemAtIndex: i];
416
417                 [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
418                                 [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
419                                 
420                                 /*
421                 [o_slider_band1 setFloatValue: eqz_preset_10b[i]->f_amp[0]];
422                 [o_slider_band2 setFloatValue: eqz_preset_10b[i]->f_amp[1]];
423                 [o_slider_band3 setFloatValue: eqz_preset_10b[i]->f_amp[2]];
424                 [o_slider_band4 setFloatValue: eqz_preset_10b[i]->f_amp[3]];
425                 [o_slider_band5 setFloatValue: eqz_preset_10b[i]->f_amp[4]];
426                 [o_slider_band6 setFloatValue: eqz_preset_10b[i]->f_amp[5]];
427                 [o_slider_band7 setFloatValue: eqz_preset_10b[i]->f_amp[6]];
428                 [o_slider_band8 setFloatValue: eqz_preset_10b[i]->f_amp[7]];
429                 [o_slider_band9 setFloatValue: eqz_preset_10b[i]->f_amp[8]];
430                 [o_slider_band10 setFloatValue: eqz_preset_10b[i]->f_amp[9]];
431                                 */
432                                 
433                 if( strcmp( psz_preset, "flat" ) )
434                 {
435                     char psz_bands[100];
436                     memset( psz_bands, 0, 100 );
437
438                     sprintf( psz_bands, "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
439                                         "%.1f %.1f %.1f",
440                                         eqz_preset_10b[i]->f_amp[0],
441                                         eqz_preset_10b[i]->f_amp[1],
442                                         eqz_preset_10b[i]->f_amp[2],
443                                         eqz_preset_10b[i]->f_amp[3],
444                                         eqz_preset_10b[i]->f_amp[4],
445                                         eqz_preset_10b[i]->f_amp[5],
446                                         eqz_preset_10b[i]->f_amp[6],
447                                         eqz_preset_10b[i]->f_amp[7],
448                                         eqz_preset_10b[i]->f_amp[8],
449                                         eqz_preset_10b[i]->f_amp[9] );
450
451                     var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
452                                                             VLC_VAR_DOINHERIT );
453                     var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
454                                                             VLC_VAR_DOINHERIT );
455                     var_SetFloat( p_object, "equalizer-preamp",
456                                                 eqz_preset_10b[i]->f_preamp );
457                     var_SetString( p_object, "equalizer-bands", psz_bands );
458                 }
459             }
460         }
461         free( psz_preset );
462         vlc_object_release( p_object );
463     }
464
465     [self equalizerUpdated];
466
467 }
468
469
470 - (id)getSliderByIndex:(int)index
471 {
472         switch(index)
473         {
474                 case 0 : return o_slider_band1;
475                 case 1 : return o_slider_band2;
476                 case 2 : return o_slider_band3;
477                 case 3 : return o_slider_band4;
478                 case 4 : return o_slider_band5;
479                 case 5 : return o_slider_band6;
480                 case 6 : return o_slider_band7;
481                 case 7 : return o_slider_band8;
482                 case 8 : return o_slider_band9;
483                 case 9 : return o_slider_band10;
484                 default : return nil;
485         }
486 }
487
488 - (void)setBandSlidersValues:(float *)values
489 {
490         int i = 0;
491         for (i = 0 ; i<= 9 ; i++)
492         {
493                 [self setValue:values[i] forSlider:i];
494         }
495 }
496
497 - (void)initBandSliders
498 {
499         int i = 0;
500         for (i = 0 ; i< 9 ; i++)
501         {
502                 [self setValue:0.0 forSlider:i];
503         }
504 }
505
506 - (void)setValue:(float)value forSlider:(int)index
507 {
508         id slider = [self getSliderByIndex:index];
509         
510         if (slider != nil)
511         {
512                 [slider setFloatValue:value];
513         }
514 }
515
516 @end