]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
project.pbxproj: correct to relatives paths
[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: J\8er\99me 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
37 /*****************************************************************************
38  * VLCEqualizer implementation 
39  *****************************************************************************/
40 @implementation VLCEqualizer
41
42 static void ChangeFiltersString( playlist_t *p_playlist,
43                                  aout_instance_t *p_aout,
44                                  char *psz_name, vlc_bool_t b_add )
45 {
46     char *psz_parser, *psz_string;
47     vlc_object_t *p_object = NULL;
48     int i;
49
50     if( p_playlist != NULL )
51         p_object = ( vlc_object_t* )p_playlist;
52     if( p_aout != NULL )
53         p_object = ( vlc_object_t* )p_aout;
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 }
107
108 static vlc_bool_t GetFiltersStatus( playlist_t *p_playlist,
109                                  aout_instance_t *p_aout,
110                                  char *psz_name )
111 {
112     char *psz_parser, *psz_string;
113     vlc_object_t *p_object = NULL;
114
115     if( p_playlist != NULL )
116         p_object = ( vlc_object_t* )p_playlist;
117     if( p_aout != NULL )
118         p_object = ( vlc_object_t* )p_aout;
119     if( p_object == NULL )
120         return VLC_FALSE;
121
122     psz_string = var_GetString( p_object, "audio-filter" );
123
124     if( !psz_string ) psz_string = strdup("");
125
126     psz_parser = strstr( psz_string, psz_name );
127
128     free( psz_string );
129
130     if ( psz_parser )
131         return VLC_TRUE;
132     else
133         return VLC_FALSE;
134 }
135
136 - (IBAction)bandSliderUpdated:(id)sender
137 {
138     intf_thread_t *p_intf = VLCIntf;
139     aout_instance_t *p_aout = ( aout_instance_t * )vlc_object_find( p_intf,
140                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
141     playlist_t *p_playlist = ( playlist_t * )vlc_object_find( p_intf,
142                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
143     vlc_object_t *p_object = NULL;
144
145     char psz_values[102];
146     memset( psz_values, 0, 102 );
147
148     if( p_playlist != NULL )
149         p_object = ( vlc_object_t* )p_playlist;
150     if( p_aout != NULL )
151         p_object = ( vlc_object_t* )p_aout;
152     if( p_object == NULL )
153         return;
154
155     /* Write the new bands values */
156 /* TODO: write a generic code instead of ten times the same thing */
157
158     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
159     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
160     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
161     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
162     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
163     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
164     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
165     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
166     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
167     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
168
169     var_SetString( p_object, "equalizer-bands", psz_values );
170     if( p_aout )
171         vlc_object_release( p_aout );
172     if( p_playlist )
173         vlc_object_release( p_playlist );
174 }
175
176 - (IBAction)changePreset:(id)sender
177 {
178     intf_thread_t *p_intf = VLCIntf;
179     float f_preamp, f_band[10];
180     char *psz_bands, *p_next;
181     vlc_bool_t b_2p;
182     int i;
183     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
184                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
185     playlist_t *p_playlist = ( playlist_t * )vlc_object_find( p_intf,
186                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
187     vlc_object_t *p_object = NULL;
188     vlc_bool_t b_enabled = GetFiltersStatus( p_playlist, p_aout, "equalizer" );
189
190     static char *preset_list[] = {
191         "flat", "classical", "club", "dance", "fullbass", "fullbasstreble",
192         "fulltreble", "headphones","largehall", "live", "party", "pop", "reggae",
193         "rock", "ska", "soft", "softrock", "techno"
194     };
195
196     if( p_playlist != NULL )
197         p_object = ( vlc_object_t* )p_playlist;
198     if( p_aout != NULL )
199         p_object = ( vlc_object_t* )p_aout;
200     if( p_object == NULL )
201     {
202         msg_Dbg( p_intf, "equalizer settings not set: no playlist nor aout found");
203         return;
204     }
205     
206     var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );
207
208     f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
209     psz_bands = var_GetString( p_object, "equalizer-bands" );
210     b_2p = var_GetBool( p_object, "equalizer-2pass" );
211     vlc_object_release( p_object );
212
213 /* Set the preamp slider */
214     [o_slider_preamp setFloatValue: f_preamp];
215
216 /* Set the bands slider */
217     for( i = 0; i < 10; i++ )
218     {
219         /* Read dB -20/20 */
220 #ifdef HAVE_STRTOF
221         f_band[i] = strtof( psz_bands, &p_next );
222 #else
223         f_band[i] = (float)strtod( psz_bands, &p_next );
224 #endif
225         if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
226
227         if( !*psz_bands ) break; /* end of line */
228         psz_bands = p_next+1;
229     }
230     [o_slider_band1 setFloatValue: f_band[0]];
231     [o_slider_band2 setFloatValue: f_band[1]];
232     [o_slider_band3 setFloatValue: f_band[2]];
233     [o_slider_band4 setFloatValue: f_band[3]];
234     [o_slider_band5 setFloatValue: f_band[4]];
235     [o_slider_band6 setFloatValue: f_band[5]];
236     [o_slider_band7 setFloatValue: f_band[6]];
237     [o_slider_band8 setFloatValue: f_band[7]];
238     [o_slider_band9 setFloatValue: f_band[8]];
239     [o_slider_band10 setFloatValue: f_band[9]];
240                                  
241     if( b_enabled == VLC_TRUE )
242         [o_btn_enable setState:NSOnState];
243     else
244         [o_btn_enable setState:NSOffState];
245
246     [o_btn_2pass setState:( ( b_2p == VLC_TRUE ) ? NSOnState : NSOffState )];
247 }
248
249 - (IBAction)enable:(id)sender
250 {
251     intf_thread_t *p_intf = VLCIntf;
252     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find( p_intf,
253                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
254     playlist_t *p_playlist = ( playlist_t * )vlc_object_find( p_intf,
255                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
256     ChangeFiltersString( p_playlist, p_aout, "equalizer", [sender state] );
257
258     [o_popup_presets setEnabled: [sender state]];
259     if( p_aout != NULL )
260         vlc_object_release( p_aout );
261     if( p_playlist != NULL )
262         vlc_object_release( p_playlist );
263 }
264
265 - (IBAction)preampSliderUpdated:(id)sender
266 {
267     intf_thread_t *p_intf = VLCIntf;
268     float f_preamp= [sender floatValue] ;
269
270     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
271                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
272     playlist_t *p_playlist = ( playlist_t * )vlc_object_find( p_intf,
273                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
274
275     vlc_object_t *p_object = NULL;
276
277     if( p_playlist != NULL )
278         p_object = ( vlc_object_t* )p_playlist;
279     if( p_aout != NULL )
280         p_object = ( vlc_object_t* )p_aout;
281     if( p_object == NULL )
282     {
283         msg_Dbg( p_intf, "equalizer settings not set: no playlist nor aout found");
284         return;
285     }
286     
287     var_SetFloat( p_object, "equalizer-preamp", f_preamp );
288
289     if( p_aout != NULL )
290         vlc_object_release( p_aout );
291     if( p_playlist != NULL )
292         vlc_object_release( p_playlist );
293 }
294
295 - (IBAction)toggleWindow:(id)sender
296 {
297     if( [o_window isVisible] )
298     {
299         [o_window orderOut:sender];
300         [o_btn_equalizer setState:NSOffState];
301     }
302     else
303     {
304         intf_thread_t *p_intf = VLCIntf;
305         float f_preamp, f_band[10];
306         char *psz_bands, *p_next;
307         vlc_bool_t b_2p;
308         int i;
309         aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
310                                     VLC_OBJECT_AOUT, FIND_ANYWHERE );
311         playlist_t *p_playlist = ( playlist_t * )vlc_object_find( p_intf,
312                                     VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
313
314         vlc_object_t *p_object = NULL;
315
316         vlc_bool_t b_enabled = GetFiltersStatus( p_playlist, p_aout, "equalizer" );
317
318         if( p_playlist != NULL )
319             p_object = ( vlc_object_t* )p_playlist;
320         if( p_aout != NULL )
321             p_object = ( vlc_object_t* )p_aout;
322         if( p_object == NULL )
323         {
324             msg_Dbg( p_intf, "equalizer settings not set: no playlist nor aout found");
325             return;
326         }
327     
328         f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
329         psz_bands = var_GetString( p_object, "equalizer-bands" );
330         b_2p = var_GetBool( p_object, "equalizer-2pass" );
331
332         if( p_aout != NULL )
333             vlc_object_release( p_aout );
334         if( p_playlist != NULL )
335             vlc_object_release( p_playlist );
336
337         if( !psz_bands )
338             psz_bands = "0 0 0 0 0 0 0 0 0 0";
339
340 /* Set the preamp slider */
341         [o_slider_preamp setFloatValue: f_preamp];
342
343 /* Set the bands slider */
344         for( i = 0; i < 10; i++ )
345         {
346             /* Read dB -20/20 */
347 #ifdef HAVE_STRTOF
348             f_band[i] = strtof( psz_bands, &p_next );
349 #else
350             f_band[i] = (float)strtod( psz_bands, &p_next );
351 #endif
352             if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
353
354             if( !*psz_bands ) break; /* end of line */
355             psz_bands = p_next+1;
356         }
357         [o_slider_band1 setFloatValue: f_band[0]];
358         [o_slider_band2 setFloatValue: f_band[1]];
359         [o_slider_band3 setFloatValue: f_band[2]];
360         [o_slider_band4 setFloatValue: f_band[3]];
361         [o_slider_band5 setFloatValue: f_band[4]];
362         [o_slider_band6 setFloatValue: f_band[5]];
363         [o_slider_band7 setFloatValue: f_band[6]];
364         [o_slider_band8 setFloatValue: f_band[7]];
365         [o_slider_band9 setFloatValue: f_band[8]];
366         [o_slider_band10 setFloatValue: f_band[9]];
367
368         if( b_enabled == VLC_TRUE )
369             [o_btn_enable setState:NSOnState];
370         else
371             [o_btn_enable setState:NSOffState];
372
373         [o_btn_2pass setState:( ( b_2p == VLC_TRUE ) ? NSOnState : NSOffState )];
374         
375         [o_window makeKeyAndOrderFront:sender];
376         [o_btn_equalizer setState:NSOnState];
377     }
378 }
379
380 - (IBAction)twopass:(id)sender
381 {
382     intf_thread_t *p_intf = VLCIntf;
383     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
384                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
385     playlist_t *p_playlist = ( playlist_t * )vlc_object_find( p_intf,
386                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
387     vlc_object_t *p_object = NULL;
388
389     vlc_bool_t b_2p = [sender state] ? VLC_TRUE : VLC_FALSE;
390
391     if( p_playlist != NULL )
392         p_object = ( vlc_object_t* )p_playlist;
393     if( p_aout != NULL )
394         p_object = ( vlc_object_t* )p_aout;
395     if( p_object == NULL )
396     {
397         msg_Dbg( p_intf, "equalizer settings not set: no playlist nor aout found");
398         return;
399     }
400
401     var_SetBool( p_object, "equalizer-2pass", b_2p );
402     if( ( [o_btn_enable state] ) && ( p_aout != NULL ) )
403     {
404        int i;
405         for( i = 0; i < p_aout->i_nb_inputs; i++ )
406         {
407             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
408         }
409     }
410
411     if( p_aout != NULL )
412         vlc_object_release( p_aout );
413     if( p_playlist != NULL )
414         vlc_object_release( p_playlist );
415 }
416
417 @end