]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
* string review
[vlc] / modules / gui / macosx / equalizer.m
1 /*****************************************************************************
2  * equalizer.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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[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, *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     if( p_object == NULL )
182         p_object = vlc_object_find( p_intf,
183                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
184     if( p_object == NULL )
185         return;
186
187     f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
188     psz_bands = var_GetString( p_object, "equalizer-bands" );
189     if( !psz_bands )
190         psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );
191
192     b_2p = var_GetBool( p_object, "equalizer-2pass" );
193
194     vlc_object_release( p_object );
195
196 /* Set the preamp slider */
197     [o_slider_preamp setFloatValue: f_preamp];
198
199 /* Set the bands slider */
200     for( i = 0; i < 10; i++ )
201     {
202         /* Read dB -20/20 */
203 #ifdef HAVE_STRTOF
204         f_band[i] = strtof( psz_bands, &p_next );
205 #else
206         f_band[i] = (float)strtod( psz_bands, &p_next );
207 #endif
208         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
209
210         if( !*psz_bands ) break; /* end of line */
211         psz_bands = p_next+1;
212     }
213     free( psz_bands );
214
215     [o_slider_band1 setFloatValue: f_band[0]];
216     [o_slider_band2 setFloatValue: f_band[1]];
217     [o_slider_band3 setFloatValue: f_band[2]];
218     [o_slider_band4 setFloatValue: f_band[3]];
219     [o_slider_band5 setFloatValue: f_band[4]];
220     [o_slider_band6 setFloatValue: f_band[5]];
221     [o_slider_band7 setFloatValue: f_band[6]];
222     [o_slider_band8 setFloatValue: f_band[7]];
223     [o_slider_band9 setFloatValue: f_band[8]];
224     [o_slider_band10 setFloatValue: f_band[9]];
225
226 /* Set the the checkboxes */
227     if( b_enabled == VLC_TRUE )
228         [o_ckb_enable setState:NSOnState];
229     else
230         [o_ckb_enable setState:NSOffState];
231
232     [o_ckb_2pass setState:( ( b_2p == VLC_TRUE ) ? NSOnState : NSOffState )];
233 }
234
235 - (IBAction)bandSliderUpdated:(id)sender
236 {
237     intf_thread_t *p_intf = VLCIntf;
238     vlc_object_t *p_object = vlc_object_find( p_intf,
239                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
240     char psz_values[102];
241     memset( psz_values, 0, 102 );
242
243     if( p_object == NULL )
244         p_object = vlc_object_find( p_intf,
245                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
246     if( p_object == NULL )
247         return;
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     vlc_object_release( p_object );
265 }
266
267 - (IBAction)changePreset:(id)sender
268 {
269     intf_thread_t *p_intf = VLCIntf;
270     int i;
271     vlc_object_t *p_object= vlc_object_find( p_intf,
272                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
273     char psz_values[102];
274     memset( psz_values, 0, 102 );
275
276     if( p_object == NULL )
277         p_object = vlc_object_find( p_intf,
278                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
279     if( p_object == NULL )
280         return;
281
282     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
283
284     for( i = 0; i < 10; i++ )
285         sprintf( psz_values, "%s %.1f", psz_values, eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] );
286     var_SetString( p_object, "equalizer-bands", psz_values );
287     var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
288
289     [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
290     [o_slider_band1 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[0]];
291     [o_slider_band2 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[1]];
292     [o_slider_band3 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[2]];
293     [o_slider_band4 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[3]];
294     [o_slider_band5 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[4]];
295     [o_slider_band6 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[5]];
296     [o_slider_band7 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[6]];
297     [o_slider_band8 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[7]];
298     [o_slider_band9 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[8]];
299     [o_slider_band10 setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[9]];
300
301     vlc_object_release( p_object );
302 }
303
304 - (IBAction)enable:(id)sender
305 {
306     ChangeFiltersString( VLCIntf, "equalizer", [sender state] );
307 }
308
309 - (IBAction)preampSliderUpdated:(id)sender
310 {
311     intf_thread_t *p_intf = VLCIntf;
312     float f_preamp = [sender floatValue] ;
313
314     vlc_object_t *p_object = vlc_object_find( p_intf,
315                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
316     if( p_object == NULL )
317         p_object = vlc_object_find( p_intf,
318                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
319     if( p_object == NULL )
320         return;
321
322     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
323
324     vlc_object_release( p_object );
325 }
326
327 - (IBAction)toggleWindow:(id)sender
328 {
329     if( [o_window isVisible] )
330     {
331         [o_window orderOut:sender];
332         [o_btn_equalizer setState:NSOffState];
333     }
334     else
335     {
336         [o_window makeKeyAndOrderFront:sender];
337         [o_btn_equalizer setState:NSOnState];
338     }
339 }
340
341 - (IBAction)twopass:(id)sender
342 {
343     intf_thread_t *p_intf = VLCIntf;
344     vlc_bool_t b_2p = [sender state] ? VLC_TRUE : VLC_FALSE;
345     vlc_object_t *p_object= vlc_object_find( p_intf,
346                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
347     aout_instance_t *p_aout = (aout_instance_t *)p_object;
348     if( p_object == NULL )
349         p_object = vlc_object_find( p_intf,
350                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
351     if( p_object == NULL )
352         return;
353
354     var_SetBool( p_object, "equalizer-2pass", b_2p );
355     if( ( [o_ckb_enable state] ) && ( p_aout != NULL ) )
356     {
357        int i;
358         for( i = 0; i < p_aout->i_nb_inputs; i++ )
359         {
360             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
361         }
362     }
363
364     vlc_object_release( p_object );
365 }
366
367 - (void)windowWillClose:(NSNotification *)aNotification
368 {
369     [o_btn_equalizer setState: NSOffState];
370 }
371
372 - (void)awakeFromNib
373 {
374     [o_window setExcludedFromWindowsMenu: TRUE];
375     
376     [self initStrings];
377     [self equalizerUpdated];
378 }
379
380 @end