equalizer.m 15.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * equalizer.m: MacOS X interface module
 *****************************************************************************
4
 * Copyright (C) 2004-2008 the VideoLAN team
5
 * $Id$
6
 *
7 8
 * Authors: Jérôme Decoodt <djc@videolan.org>
 *          Felix Paul Kühne <fkuehne -at- videolan -dot- org>
9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_aout.h>
34 35 36 37 38 39

#include "intf.h"

#include <math.h>

#include "equalizer.h"
Jérome Decoodt's avatar
Jérome Decoodt committed
40
#include "../../audio_filter/equalizer_presets.h"
41 42

/*****************************************************************************
43
 * VLCEqualizer implementation
44 45 46
 *****************************************************************************/
@implementation VLCEqualizer

Jérome Decoodt's avatar
Jérome Decoodt committed
47
static void ChangeFiltersString( intf_thread_t *p_intf,
48
                                 char *psz_name, bool b_add )
49 50
{
    char *psz_parser, *psz_string;
51
    int i;
Jérome Decoodt's avatar
Jérome Decoodt committed
52 53 54
    vlc_object_t *p_object = vlc_object_find( p_intf,
                                VLC_OBJECT_AOUT, FIND_ANYWHERE );
    aout_instance_t *p_aout = (aout_instance_t *)p_object;
55 56
    if( !p_object )
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
57
        p_object = (vlc_object_t *)pl_Hold( p_intf );
58
    }
59

60
    psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
61 62

    if( !psz_string ) psz_string = strdup( "" );
63 64 65 66 67 68 69 70

    psz_parser = strstr( psz_string, psz_name );

    if( b_add )
    {
        if( !psz_parser )
        {
            psz_parser = psz_string;
71
            asprintf( &psz_string, ( *psz_string ) ? "%s,%s" : "%s%s",
72 73 74 75 76
                            psz_string, psz_name );
            free( psz_parser );
        }
        else
        {
77
            vlc_object_release( p_object );
78 79 80 81 82 83 84
            return;
        }
    }
    else
    {
        if( psz_parser )
        {
85 86 87
            memmove( psz_parser, psz_parser + strlen( psz_name ) +
                            ( *( psz_parser + strlen( psz_name ) ) == ',' ? 1 : 0 ),
                            strlen( psz_parser + strlen( psz_name ) ) + 1 );
88

89
            if( *( psz_string+strlen( psz_string ) - 1 ) == ',' )
90
            {
91
                *( psz_string+strlen( psz_string ) - 1 ) = '\0';
92 93 94 95
            }
         }
         else
         {
96 97 98
            free( psz_string );
            vlc_object_release( p_object );
            return;
99 100 101
         }
    }

102
    aout_EnableFilter( p_object, psz_string, false);
103 104 105 106 107 108 109
    
    if( (BOOL)config_GetInt( p_object, "macosx-eq-keep" ) == YES )
    {
        /* save changed to config */
        config_PutPsz( p_object, "audio-filter", psz_string );
    }
    
110
    free( psz_string );
Jérome Decoodt's avatar
Jérome Decoodt committed
111
    vlc_object_release( p_object );
112 113
}

114
static bool GetFiltersStatus( intf_thread_t *p_intf,
115 116 117
                                 char *psz_name )
{
    char *psz_parser, *psz_string;
Jérome Decoodt's avatar
Jérome Decoodt committed
118 119 120
    vlc_object_t *p_object = vlc_object_find( p_intf,
                                VLC_OBJECT_AOUT, FIND_ANYWHERE );
    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
121
        p_object = (vlc_object_t *)pl_Hold( p_intf );
122

123 124 125 126 127
    if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
        psz_string = config_GetPsz( p_intf, "audio-filter" );

    if(! psz_string )
        psz_string = var_GetNonEmptyString( p_object, "audio-filter" );
128

129 130
    vlc_object_release( p_object );

131
    if( !psz_string ) return false;
132 133 134 135 136

    psz_parser = strstr( psz_string, psz_name );

    free( psz_string );

137
    if ( psz_parser )
138
        return true;
139
    else
140
        return false;
141 142
}

Jérome Decoodt's avatar
Jérome Decoodt committed
143
- (void)initStrings
144
{
Jérome Decoodt's avatar
Jérome Decoodt committed
145 146
    int i;
    [o_btn_equalizer setToolTip: _NS("Equalizer")];
147
    [o_btn_equalizer_embedded setToolTip: _NS("Equalizer")];
Jérome Decoodt's avatar
Jérome Decoodt committed
148
    [o_ckb_2pass setTitle: _NS("2 Pass")];
Christophe Mutricy's avatar
Christophe Mutricy committed
149
    [o_ckb_2pass setToolTip: _NS("Apply the "
150
        "equalizer filter twice. The effect will be sharper.")];
Jérome Decoodt's avatar
Jérome Decoodt committed
151
    [o_ckb_enable setTitle: _NS("Enable")];
152 153
    [o_ckb_enable setToolTip: _NS("Enable the equalizer. Bands can be set "
        "manually or using a preset.")];
Jérome Decoodt's avatar
Jérome Decoodt committed
154 155 156 157 158
    [o_fld_preamp setStringValue: _NS("Preamp")];

    [o_popup_presets removeAllItems];
    for( i = 0; i < 18 ; i++ )
    {
159
        [o_popup_presets insertItemWithTitle: _NS(preset_list_text[i]) atIndex: i];
Jérome Decoodt's avatar
Jérome Decoodt committed
160 161 162
    }
    [o_window setTitle: _NS("Equalizer")];

163
    [self initBandSliders];
164 165
}

Jérome Decoodt's avatar
Jérome Decoodt committed
166
- (void)equalizerUpdated
167
{
168 169
    intf_thread_t *p_intf = VLCIntf;
    float f_preamp, f_band[10];
170
    char *psz_bands, *psz_bands_init, *p_next;
171
    bool b_2p;
172
    int i;
173
    bool b_enabled = GetFiltersStatus( p_intf, (char *)"equalizer" );
Jérome Decoodt's avatar
Jérome Decoodt committed
174
    vlc_object_t *p_object = vlc_object_find( p_intf,
175
                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
176

Jérome Decoodt's avatar
Jérome Decoodt committed
177
    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
178
        p_object = (vlc_object_t *)pl_Hold( p_intf );
179

180
    var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
181
                VLC_VAR_DOINHERIT );
182
    var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
183
                VLC_VAR_DOINHERIT );
184

185
    psz_bands = var_GetNonEmptyString( p_object, "equalizer-bands" );
186

187
    if( psz_bands == NULL )
Jérome Decoodt's avatar
Jérome Decoodt committed
188 189
        psz_bands = strdup( "0 0 0 0 0 0 0 0 0 0" );

190 191 192 193 194 195 196 197 198 199
    if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
    {
        b_2p = (BOOL)config_GetInt( p_object, "equalizer-2pass" );
        f_preamp = config_GetFloat( p_object, "equalizer-preamp" );
    }
    else
    {
        b_2p = var_GetBool( p_object, "equalizer-2pass" );
        f_preamp = var_GetFloat( p_object, "equalizer-preamp" );
    }
Jérome Decoodt's avatar
Jérome Decoodt committed
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 208
    psz_bands_init = psz_bands;

209 210 211
    for( i = 0; i < 10; i++ )
    {
        /* Read dB -20/20 */
212 213
        f_band[i] = strtof( psz_bands, &p_next );
        if( !p_next || p_next == psz_bands ) break; /* strtof() failed */
214
    
215 216
        if( !*psz_bands ) break; /* end of line */
        psz_bands = p_next+1;
217
    }
218
    free( psz_bands_init );
219
    [self setBandSlidersValues:f_band];
Jérome Decoodt's avatar
Jérome Decoodt committed
220

221 222
    /* Set the the checkboxes */
    [o_ckb_enable setState: b_enabled];
223

224
    [o_ckb_2pass setState: b_2p];        
225 226
}

Jérome Decoodt's avatar
Jérome Decoodt committed
227
- (IBAction)bandSliderUpdated:(id)sender
228
{
229
    intf_thread_t *p_intf = VLCIntf;
Jérome Decoodt's avatar
Jérome Decoodt committed
230
    vlc_object_t *p_object = vlc_object_find( p_intf,
231
                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
Jérome Decoodt's avatar
Jérome Decoodt committed
232 233

    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
234
        p_object = (vlc_object_t *)pl_Hold( p_intf );
235

236
    const char *psz_values;
237 238 239 240 241 242 243 244 245 246
    NSString *preset = [NSString stringWithFormat:@"%.1f", [o_slider_band1 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band2 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band3 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band4 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band5 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band6 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band7 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band8 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f ", [o_slider_band9 floatValue] ];
    preset = [preset stringByAppendingFormat:@"%.1f", [o_slider_band10 floatValue] ];
247 248

    psz_values = [preset UTF8String];
Jérome Decoodt's avatar
Jérome Decoodt committed
249
    var_SetString( p_object, "equalizer-bands", psz_values );
250 251 252 253 254 255 256 257 258 259

    if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
    {
        /* save changed to config */
        config_PutPsz( p_intf, "equalizer-bands", psz_values );

        /* save to vlcrc */
        config_SaveConfigFile( p_intf, "equalizer" );
    }

Jérome Decoodt's avatar
Jérome Decoodt committed
260
    vlc_object_release( p_object );
261 262
}

Jérome Decoodt's avatar
Jérome Decoodt committed
263
- (IBAction)changePreset:(id)sender
264
{
265
    intf_thread_t *p_intf = VLCIntf;
Jérome Decoodt's avatar
Jérome Decoodt committed
266 267
    int i;
    vlc_object_t *p_object= vlc_object_find( p_intf,
268
                                             VLC_OBJECT_AOUT, FIND_ANYWHERE );
Jérome Decoodt's avatar
Jérome Decoodt committed
269
    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
270
        p_object = (vlc_object_t *)pl_Hold( p_intf );
Jérome Decoodt's avatar
Jérome Decoodt committed
271 272 273

    var_SetString( p_object , "equalizer-preset" , preset_list[[sender indexOfSelectedItem]] );

274
    NSString *preset = @"";
275
    const char *psz_values;
Jérome Decoodt's avatar
Jérome Decoodt committed
276
    for( i = 0; i < 10; i++ )
277
    {
278
        preset = [preset stringByAppendingFormat:@"%.1f ", eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp[i] ];
279 280
    }
    psz_values = [preset UTF8String];
Jérome Decoodt's avatar
Jérome Decoodt committed
281
    var_SetString( p_object, "equalizer-bands", psz_values );
282
    var_SetFloat( p_object, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp);
283

Jérome Decoodt's avatar
Jérome Decoodt committed
284
    [o_slider_preamp setFloatValue: eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp];
285

286
    [self setBandSlidersValues:(float *)eqz_preset_10b[[sender indexOfSelectedItem]]->f_amp];
287 288 289 290 291 292 293 294 295 296 297 298

    if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
    {
        /* save changed to config */
        config_PutPsz( p_intf, "equalizer-bands", psz_values );
        config_PutFloat( p_intf, "equalizer-preamp", eqz_preset_10b[[sender indexOfSelectedItem]]->f_preamp );
        config_PutPsz( p_intf, "equalizer-preset", preset_list[[sender indexOfSelectedItem]] );

        /* save to vlcrc */
        config_SaveConfigFile( p_intf, "equalizer" );
    }

Jérome Decoodt's avatar
Jérome Decoodt committed
299 300 301 302 303
    vlc_object_release( p_object );
}

- (IBAction)enable:(id)sender
{
304
    ChangeFiltersString( VLCIntf, (char *)"equalizer", [sender state] );
Jérome Decoodt's avatar
Jérome Decoodt committed
305 306 307 308 309 310 311 312
}

- (IBAction)preampSliderUpdated:(id)sender
{
    intf_thread_t *p_intf = VLCIntf;
    float f_preamp = [sender floatValue] ;

    vlc_object_t *p_object = vlc_object_find( p_intf,
313
                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
Jérome Decoodt's avatar
Jérome Decoodt committed
314
    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
315
        p_object = (vlc_object_t *)pl_Hold( p_intf );
Jérome Decoodt's avatar
Jérome Decoodt committed
316

317 318
    var_SetFloat( p_object, "equalizer-preamp", f_preamp );

319 320 321 322 323 324 325 326 327
    if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
    {
        /* save changed to config */
        config_PutFloat( p_intf, "equalizer-preamp", f_preamp );

        /* save to vlcrc */
        config_SaveConfigFile( p_intf, "equalizer" );
    }

Jérome Decoodt's avatar
Jérome Decoodt committed
328
    vlc_object_release( p_object );
329 330 331 332 333 334 335 336
}

- (IBAction)toggleWindow:(id)sender
{
    if( [o_window isVisible] )
    {
        [o_window orderOut:sender];
        [o_btn_equalizer setState:NSOffState];
337
        [o_btn_equalizer_embedded setState:NSOffState];
338 339 340 341 342
    }
    else
    {
        [o_window makeKeyAndOrderFront:sender];
        [o_btn_equalizer setState:NSOnState];
343
        [o_btn_equalizer_embedded setState:NSOnState];
344 345 346 347 348
    }
}

- (IBAction)twopass:(id)sender
{
349
    intf_thread_t *p_intf = VLCIntf;
350
    bool b_2p = [sender state] ? true : false;
Jérome Decoodt's avatar
Jérome Decoodt committed
351
    vlc_object_t *p_object= vlc_object_find( p_intf,
352
                                             VLC_OBJECT_AOUT, FIND_ANYWHERE );
Jérome Decoodt's avatar
Jérome Decoodt committed
353 354
    aout_instance_t *p_aout = (aout_instance_t *)p_object;
    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
355
        p_object = (vlc_object_t *)pl_Hold( p_intf );
356 357

    var_SetBool( p_object, "equalizer-2pass", b_2p );
358

359 360 361 362 363 364 365 366 367
    if( (BOOL)config_GetInt( p_intf, "macosx-eq-keep" ) == YES )
    {
        /* save changed to config */
        config_PutInt( p_intf, "equalizer-2pass", (int)b_2p );

        /* save to vlcrc */
        config_SaveConfigFile( p_intf, "equalizer" );
    }

Jérome Decoodt's avatar
Jérome Decoodt committed
368 369 370 371 372 373 374 375 376 377
    vlc_object_release( p_object );
}

- (void)windowWillClose:(NSNotification *)aNotification
{
    [o_btn_equalizer setState: NSOffState];
}

- (void)awakeFromNib
{
378 379
    int i;
    vlc_object_t *p_object= vlc_object_find( VLCIntf,
380
                                             VLC_OBJECT_AOUT, FIND_ANYWHERE );
381
    if( p_object == NULL )
Antoine Cellerier's avatar
Antoine Cellerier committed
382
        p_object = (vlc_object_t *)pl_Hold( VLCIntf );
383

Jérome Decoodt's avatar
Jérome Decoodt committed
384
    [o_window setExcludedFromWindowsMenu: TRUE];
385

Jérome Decoodt's avatar
Jérome Decoodt committed
386
    [self initStrings];
387 388 389 390 391 392

    if( p_object )
    {
        char *psz_preset;

        var_Create( p_object, "equalizer-preset", VLC_VAR_STRING |
393
                    VLC_VAR_DOINHERIT );
394
        psz_preset = var_GetNonEmptyString( p_object, "equalizer-preset" );
395

396
        for( i = 0 ; (psz_preset != NULL) && (i < 18) ; i++ )
397
        {
398 399
            if( strcmp( preset_list[i], psz_preset ) )
                continue;
400
    
401
            [o_popup_presets selectItemAtIndex: i];
402
        
403

404 405
            [o_slider_preamp setFloatValue: eqz_preset_10b[i]->f_preamp];
            [self setBandSlidersValues: (float *)eqz_preset_10b[i]->f_amp];
406

407 408 409
            if( strcmp( psz_preset, "flat" ) )
            {
                char psz_bands[100];
410
    
411 412 413 414 415 416 417 418 419 420 421 422 423
                snprintf( psz_bands, sizeof( psz_bands ),
                          "%.1f %.1f %.1f %.1f %.1f %.1f %.1f "
                          "%.1f %.1f %.1f",
                          eqz_preset_10b[i]->f_amp[0],
                          eqz_preset_10b[i]->f_amp[1],
                          eqz_preset_10b[i]->f_amp[2],
                          eqz_preset_10b[i]->f_amp[3],
                          eqz_preset_10b[i]->f_amp[4],
                          eqz_preset_10b[i]->f_amp[5],
                          eqz_preset_10b[i]->f_amp[6],
                          eqz_preset_10b[i]->f_amp[7],
                          eqz_preset_10b[i]->f_amp[8],
                          eqz_preset_10b[i]->f_amp[9] );
424
    
425
                var_Create( p_object, "equalizer-preamp", VLC_VAR_FLOAT |
426
                            VLC_VAR_DOINHERIT );
427
                var_Create( p_object, "equalizer-bands", VLC_VAR_STRING |
428
                            VLC_VAR_DOINHERIT );
429
                var_SetFloat( p_object, "equalizer-preamp",
430
                              eqz_preset_10b[i]->f_preamp );
431
                var_SetString( p_object, "equalizer-bands", psz_bands );
432 433 434 435 436 437
            }
        }
        free( psz_preset );
        vlc_object_release( p_object );
    }

438
    [self equalizerUpdated];    
439 440
}

441

442
- (id)sliderByIndex:(int)index
443
{
444 445 446 447 448 449 450 451 452 453 454 455 456 457
    switch(index)
    {
        case 0 : return o_slider_band1;
        case 1 : return o_slider_band2;
        case 2 : return o_slider_band3;
        case 3 : return o_slider_band4;
        case 4 : return o_slider_band5;
        case 5 : return o_slider_band6;
        case 6 : return o_slider_band7;
        case 7 : return o_slider_band8;
        case 8 : return o_slider_band9;
        case 9 : return o_slider_band10;
        default : return nil;
    }
458 459 460 461
}

- (void)setBandSlidersValues:(float *)values
{
462 463 464 465 466
    int i = 0;
    for (i = 0 ; i<= 9 ; i++)
    {
        [self setValue:values[i] forSlider:i];
    }
467 468 469 470
}

- (void)initBandSliders
{
471 472 473 474 475
    int i = 0;
    for (i = 0 ; i< 9 ; i++)
    {
        [self setValue:0.0 forSlider:i];
    }
476 477 478 479
}

- (void)setValue:(float)value forSlider:(int)index
{
480
    id slider = [self sliderByIndex:index];
481

482 483 484 485
    if (slider != nil)
    {
        [slider setFloatValue:value];
    }
486 487
}

488
@end