]> git.sesse.net Git - vlc/blob - modules/gui/macosx/equalizer.m
All: equalizer window for MacOS. Need a bit of corrections for the presets part.
[vlc] / modules / gui / macosx / equalizer.m
1 /*****************************************************************************
2  * equalizer.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: equalizer.m 1 2004-08-07 23:50:00Z djc $
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( intf_thread_t *p_intf,
43                                  aout_instance_t * p_aout,
44                                  char *psz_name, vlc_bool_t b_add )
45 {
46     char *psz_parser, *psz_string;
47
48     if( p_aout )
49     {
50         psz_string = var_GetString( p_aout, "audio-filter" );
51     }
52     else
53     {
54         psz_string = config_GetPsz( p_intf, "audio-filter" );
55     }
56
57     if( !psz_string ) psz_string = strdup("");
58
59     psz_parser = strstr( psz_string, psz_name );
60
61     if( b_add )
62     {
63         if( !psz_parser )
64         {
65             psz_parser = psz_string;
66             asprintf( &psz_string, (*psz_string) ? "%s,%s" : "%s%s",
67                             psz_string, psz_name );
68             free( psz_parser );
69         }
70         else
71         {
72             return;
73         }
74     }
75     else
76     {
77         if( psz_parser )
78         {
79             memmove( psz_parser, psz_parser + strlen(psz_name) +
80                             (*(psz_parser + strlen(psz_name)) == ',' ? 1 : 0 ),
81                             strlen(psz_parser + strlen(psz_name)) + 1 );
82
83             if( *(psz_string+strlen(psz_string ) -1 ) == ',' )
84             {
85                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
86             }
87          }
88          else
89          {
90              free( psz_string );
91              return;
92          }
93     }
94
95     if( p_aout == NULL )
96     {
97         config_PutPsz( p_intf, "audio-filter", psz_string );
98     }
99     else
100     {
101         int i;
102         var_SetString( p_aout, "audio-filter", psz_string );
103         for( i = 0; i < p_aout->i_nb_inputs; i++ )
104         {
105             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
106         }
107     }
108     free( psz_string );
109 }
110
111 static vlc_bool_t GetFiltersStatus( intf_thread_t *p_intf,
112                                  aout_instance_t * p_aout,
113                                  char *psz_name )
114 {
115     char *psz_parser, *psz_string;
116
117     if( p_aout )
118     {
119         psz_string = var_GetString( p_aout, "audio-filter" );
120     }
121     else
122     {
123         psz_string = config_GetPsz( p_intf, "audio-filter" );
124     }
125
126     if( !psz_string ) psz_string = strdup("");
127
128     psz_parser = strstr( psz_string, psz_name );
129
130     free( psz_string );
131
132     if (psz_parser)
133         return VLC_TRUE;
134     else
135         return VLC_FALSE;
136 }
137
138 - (IBAction)bandSliderUpdated:(id)sender
139 {
140     intf_thread_t * p_intf = VLCIntf;
141     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
142                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
143     char psz_values[102];
144     memset( psz_values, 0, 102 );
145
146     /* Write the new bands values */
147 /* TODO: write a generic code instead of ten times the same thing */
148
149     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band1 floatValue] );
150     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band2 floatValue] );
151     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band3 floatValue] );
152     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band4 floatValue] );
153     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band5 floatValue] );
154     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band6 floatValue] );
155     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band7 floatValue] );
156     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band8 floatValue] );
157     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band9 floatValue] );
158     sprintf( psz_values, "%s %.1f", psz_values, [o_slider_band10 floatValue] );
159
160     if( p_aout == NULL )
161     {
162         config_PutPsz( p_intf, "equalizer-bands", psz_values );
163         msg_Dbg( p_intf, "equalizer settings set to %s", psz_values);
164     }
165     else
166     {
167         var_SetString( p_aout, "equalizer-bands", psz_values );
168         msg_Dbg( p_intf, "equalizer settings changed to %s", psz_values);
169         vlc_object_release( p_aout );
170     }
171 }
172
173 - (IBAction)changePreset:(id)sender
174 {
175     intf_thread_t * p_intf = VLCIntf;
176     float preamp, band[10];
177     char *p, *p_next;
178     int b_2p;
179     int i;
180     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
181                                 VLC_OBJECT_AOUT, FIND_ANYWHERE);
182
183     vlc_bool_t enabled = GetFiltersStatus( p_intf, p_aout, "equalizer" );
184
185 static char *preset_list[] = {
186     "flat", "classical", "club", "dance", "fullbass", "fullbasstreeble",
187     "fulltreeble", "headphones","largehall", "live", "party", "pop", "reggae",
188     "rock", "ska", "soft", "softrock", "techno"
189 };
190
191     if( p_aout == NULL )
192     {
193         config_PutPsz( p_intf, "equalizer-preset",preset_list[[sender indexOfSelectedItem]] );
194     }
195     else
196     {
197         var_SetString( p_aout , "equalizer-preset" ,preset_list[[sender indexOfSelectedItem]] );
198         vlc_object_release( p_aout );
199     }
200
201         
202     if( ( p_aout == NULL ) || ( enabled == VLC_FALSE ) )
203     {
204         preamp = config_GetFloat( p_intf, "equalizer-preamp" );
205         p = config_GetPsz( p_intf, "equalizer-bands");
206         b_2p = config_GetInt( p_intf, "equalizer-2pass" );
207     }
208     else
209     {
210         preamp = var_GetFloat( p_aout, "equalizer-preamp" );
211         p = var_GetString( p_aout, "equalizer-bands" );
212         b_2p = var_GetBool( p_aout, "equalizer-2pass" );
213         vlc_object_release( p_aout );
214     }
215
216 /* Set the preamp slider */
217     [o_slider_preamp setFloatValue: preamp];
218
219 /* Set the bands slider */
220     for( i = 0; i < 10; i++ )
221     {
222         /* Read dB -20/20 */
223 #ifdef HAVE_STRTOF
224         band[i] = strtof( p, &p_next );
225 #else
226         band[i] = (float) strtod( p, &p_next );
227 #endif
228         if( !p_next || p_next == p ) break; /* strtof() failed */
229
230         if( !*p ) break; /* end of line */
231         p=p_next+1;
232     }
233     [o_slider_band1 setFloatValue: band[0]];
234     [o_slider_band2 setFloatValue: band[1]];
235     [o_slider_band3 setFloatValue: band[2]];
236     [o_slider_band4 setFloatValue: band[3]];
237     [o_slider_band5 setFloatValue: band[4]];
238     [o_slider_band6 setFloatValue: band[5]];
239     [o_slider_band7 setFloatValue: band[6]];
240     [o_slider_band8 setFloatValue: band[7]];
241     [o_slider_band9 setFloatValue: band[8]];
242     [o_slider_band10 setFloatValue: band[9]];
243                                  
244     if( enabled == VLC_TRUE )
245         [o_btn_enable setState:NSOnState];
246     else
247         [o_btn_enable setState:NSOffState];
248
249     [o_btn_2pass setState:( ( b_2p == VLC_TRUE )?NSOnState:NSOffState )];
250 }
251
252 - (IBAction)enable:(id)sender
253 {
254     intf_thread_t * p_intf = VLCIntf;
255     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
256                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
257     ChangeFiltersString( p_intf,p_aout, "equalizer", [sender state]);
258
259     if( p_aout != NULL )
260         vlc_object_release( p_aout );
261 }
262
263 - (IBAction)preampSliderUpdated:(id)sender
264 {
265     intf_thread_t * p_intf = VLCIntf;
266     float f= [sender floatValue] ;
267
268     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
269                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
270
271     if( p_aout == NULL )
272     {
273         config_PutFloat( p_intf, "equalizer-preamp", f );
274     }
275     else
276     {
277         var_SetFloat( p_aout, "equalizer-preamp", f );
278         vlc_object_release( p_aout );
279     }
280 }
281
282 - (IBAction)toggleWindow:(id)sender
283 {
284     if( [o_window isVisible] )
285     {
286         [o_window orderOut:sender];
287         [o_btn_equalizer setState:NSOffState];
288     }
289     else
290     {
291         intf_thread_t * p_intf = VLCIntf;
292         float preamp, band[10];
293         char *p, *p_next;
294         int b_2p;
295         int i;
296         aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
297                                     VLC_OBJECT_AOUT, FIND_ANYWHERE);
298
299         vlc_bool_t enabled = GetFiltersStatus( p_intf, p_aout, "equalizer" );
300         
301         if( ( p_aout == NULL ) || ( enabled == VLC_FALSE ) )
302         {
303             preamp = config_GetFloat( p_intf, "equalizer-preamp" );
304             p = config_GetPsz( p_intf, "equalizer-bands");
305             b_2p = config_GetInt( p_intf, "equalizer-2pass" );
306         }
307         else
308         {
309             preamp = var_GetFloat( p_aout, "equalizer-preamp" );
310             p = var_GetString( p_aout, "equalizer-bands" );
311             b_2p = var_GetBool( p_aout, "equalizer-2pass" );
312             vlc_object_release( p_aout );
313         }
314
315 /* Set the preamp slider */
316         [o_slider_preamp setFloatValue: preamp];
317
318 /* Set the bands slider */
319         for( i = 0; i < 10; i++ )
320         {
321             /* Read dB -20/20 */
322 #ifdef HAVE_STRTOF
323             band[i] = strtof( p, &p_next );
324 #else
325             band[i] = (float) strtod( p, &p_next );
326 #endif
327             if( !p_next || p_next == p ) break; /* strtof() failed */
328
329             if( !*p ) break; /* end of line */
330             p=p_next+1;
331         }
332         [o_slider_band1 setFloatValue: band[0]];
333         [o_slider_band2 setFloatValue: band[1]];
334         [o_slider_band3 setFloatValue: band[2]];
335         [o_slider_band4 setFloatValue: band[3]];
336         [o_slider_band5 setFloatValue: band[4]];
337         [o_slider_band6 setFloatValue: band[5]];
338         [o_slider_band7 setFloatValue: band[6]];
339         [o_slider_band8 setFloatValue: band[7]];
340         [o_slider_band9 setFloatValue: band[8]];
341         [o_slider_band10 setFloatValue: band[9]];
342
343         if( enabled == VLC_TRUE )
344             [o_btn_enable setState:NSOnState];
345         else
346             [o_btn_enable setState:NSOffState];
347
348         [o_btn_2pass setState:( ( b_2p == VLC_TRUE )?NSOnState:NSOffState )];
349         
350         [o_window makeKeyAndOrderFront:sender];
351         [o_btn_equalizer setState:NSOnState];
352     }
353 }
354
355 - (IBAction)twopass:(id)sender
356 {
357     intf_thread_t * p_intf = VLCIntf;
358     aout_instance_t *p_aout= (aout_instance_t *)vlc_object_find(p_intf,
359                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
360
361     vlc_bool_t b_2p = [sender state] ? VLC_TRUE : VLC_FALSE;
362
363     if( p_aout == NULL )
364     {
365         config_PutInt( p_intf, "equalizer-2pass", b_2p );
366     }
367     else
368     {
369         var_SetBool( p_aout, "equalizer-2pass", b_2p );
370         if( [o_btn_enable state] )
371         {
372             int i;
373             for( i = 0; i < p_aout->i_nb_inputs; i++ )
374             {
375                 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
376             }
377         }
378         vlc_object_release( p_aout );
379     }
380
381 }
382
383 @end