VLCEqualizerView.m 16.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*****************************************************************************
 * VLCEqualizerView.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan dot org>
9
 *          Sylver Bruneau <sylver.bruneau # gmail dot com>
10 11 12 13 14
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCEqualizerView.h"
15 16
#import "VLCTrackSelectorTableViewCell.h"
#import "VLCTrackSelectorHeaderView.h"
17
#import "UIDevice+VLC.h"
18
#import "VLCSlider.h"
19 20 21

#define PROFILE_SELECTOR_TABLEVIEW_SECTIONHEADER @"profile selector table section header"
#define PROFILE_SELECTOR_TABLEVIEW_CELL @"profile selector table view cell"
22 23 24

@interface VLCEqualizerView ()
{
25 26 27 28 29 30 31 32 33 34 35
    VLCSlider *_preAmp_slider;
    VLCSlider *_60_slider;
    VLCSlider *_170_slider;
    VLCSlider *_310_slider;
    VLCSlider *_600_slider;
    VLCSlider *_1K_slider;
    VLCSlider *_3K_slider;
    VLCSlider *_6K_slider;
    VLCSlider *_12K_slider;
    VLCSlider *_14K_slider;
    VLCSlider *_16K_slider;
36 37 38 39 40 41 42 43 44 45 46 47 48 49
}

@end

@implementation VLCEqualizerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (!self)
        return self;

#define horizontal_padding 6.
50 51
#define vertical_padding_up 8.
#define vertical_padding_down 135.
52 53
#define spacer 8.

54 55 56 57
    UITextView *textView = nil;

    CGFloat sliderHeight = frame.size.height - (vertical_padding_up + vertical_padding_down);
    CGFloat sliderWidth = (frame.size.width - (spacer * 11.)) / 12.;
58 59
    CGFloat sliderY = (sliderHeight / 2.) - horizontal_padding;

60
    _preAmp_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(-sliderWidth + horizontal_padding, sliderY, sliderHeight, sliderWidth)];
61 62 63 64 65 66
    _preAmp_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _preAmp_slider.minimumValue = -20.;
    _preAmp_slider.maximumValue = 20.;
    [_preAmp_slider addTarget:self action:@selector(preampSliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_preAmp_slider];

67 68 69 70 71 72 73 74 75
    textView = [[UITextView alloc] initWithFrame:CGRectMake(0, frame.size.height - vertical_padding_down, 55, 25)];
    textView.text = NSLocalizedString(@"PREAMP", nil);
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    // Info
    textView = [[UITextView alloc] initWithFrame:CGRectMake(sliderWidth, vertical_padding_up - 10, 55, 20)];
76
    textView.text = [NSString stringWithFormat:NSLocalizedString(@"DB_FORMAT", nil), 20];
77 78 79 80 81 82 83
    textView.textAlignment = NSTextAlignmentRight;
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    textView = [[UITextView alloc] initWithFrame:CGRectMake(sliderWidth, vertical_padding_up + sliderHeight / 2 - 15, 55, 20)];
84
    textView.text = [NSString stringWithFormat:NSLocalizedString(@"DB_FORMAT", nil), 0];
85 86 87 88 89 90 91
    textView.textAlignment = NSTextAlignmentRight;
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    textView = [[UITextView alloc] initWithFrame:CGRectMake(sliderWidth, frame.size.height - vertical_padding_down - 20, 55, 20)];
92
    textView.text = [NSString stringWithFormat:NSLocalizedString(@"DB_FORMAT", nil), -20];
93 94 95 96 97 98 99 100
    textView.textAlignment = NSTextAlignmentRight;
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];


    _60_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 3. + sliderWidth, sliderY, sliderHeight, sliderWidth)];
101 102 103 104 105 106 107
    _60_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _60_slider.tag = 0;
    _60_slider.minimumValue = -20.;
    _60_slider.maximumValue = 20.;
    [_60_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_60_slider];

108 109 110 111 112 113 114 115
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 2. + sliderWidth * 2.5, frame.size.height - vertical_padding_down, 25, 25)];
    textView.text = @"60";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _170_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 4. + sliderWidth * 2, sliderY, sliderHeight, sliderWidth)];
116 117 118 119 120 121 122
    _170_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _170_slider.tag = 1;
    _170_slider.minimumValue = -20.;
    _170_slider.maximumValue = 20.;
    [_170_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_170_slider];

123 124 125 126 127 128 129 130
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 3. + sliderWidth * 3.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"170";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _310_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 5. + sliderWidth * 3., sliderY, sliderHeight, sliderWidth)];
131 132 133 134 135 136 137
    _310_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _310_slider.tag = 2;
    _310_slider.minimumValue = -20.;
    _310_slider.maximumValue = 20.;
    [_310_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_310_slider];

138 139 140 141 142 143 144 145
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 4. + sliderWidth * 4.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"310";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _600_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 6. + sliderWidth * 4., sliderY, sliderHeight, sliderWidth)];
146 147 148 149 150 151 152
    _600_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _600_slider.tag = 3;
    _600_slider.minimumValue = -20.;
    _600_slider.maximumValue = 20.;
    [_600_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_600_slider];

153 154 155 156 157 158 159 160
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 5. + sliderWidth * 5.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"600";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _1K_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 7. + sliderWidth * 5., sliderY, sliderHeight, sliderWidth)];
161 162 163 164 165 166 167
    _1K_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _1K_slider.tag = 4;
    _1K_slider.minimumValue = -20.;
    _1K_slider.maximumValue = 20.;
    [_1K_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_1K_slider];

168 169 170 171 172 173 174 175
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 6. + sliderWidth * 6.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"1K";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _3K_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 8. + sliderWidth * 6., sliderY, sliderHeight, sliderWidth)];
176 177 178 179 180 181 182
    _3K_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _3K_slider.tag = 5;
    _3K_slider.minimumValue = -20.;
    _3K_slider.maximumValue = 20.;
    [_3K_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_3K_slider];

183 184 185 186 187 188 189 190
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 7. + sliderWidth * 7.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"3K";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _6K_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 9. + sliderWidth * 7., sliderY, sliderHeight, sliderWidth)];
191 192 193 194 195 196 197
    _6K_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _6K_slider.tag = 6;
    _6K_slider.minimumValue = -20.;
    _6K_slider.maximumValue = 20.;
    [_6K_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_6K_slider];

198 199 200 201 202 203 204 205
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 8. + sliderWidth * 8.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"6K";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _12K_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 10. + sliderWidth * 8., sliderY, sliderHeight, sliderWidth)];
206 207 208 209 210 211 212
    _12K_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _12K_slider.tag = 7;
    _12K_slider.minimumValue = -20.;
    _12K_slider.maximumValue = 20.;
    [_12K_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_12K_slider],

213 214 215 216 217 218 219 220
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 9. + sliderWidth * 9.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"12K";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _14K_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 11. + sliderWidth * 9., sliderY, sliderHeight, sliderWidth)];
221 222 223 224 225 226 227
    _14K_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _14K_slider.tag = 8;
    _14K_slider.minimumValue = -20.;
    _14K_slider.maximumValue = 20.;
    [_14K_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_14K_slider];

228 229 230 231 232 233 234 235
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 10. + sliderWidth * 10.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"14K";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    _16K_slider = [[VLCSlider alloc] initWithFrame:CGRectMake(horizontal_padding * 12. + sliderWidth * 10., sliderY, sliderHeight, sliderWidth)];
236 237 238 239 240 241 242
    _16K_slider.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _16K_slider.tag = 9;
    _16K_slider.minimumValue = -20.;
    _16K_slider.maximumValue = 20.;
    [_16K_slider addTarget:self action:@selector(sliderChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_16K_slider];

243 244 245 246 247 248 249 250
    textView = [[UITextView alloc] initWithFrame:CGRectMake(horizontal_padding * 11. + sliderWidth * 11.5, frame.size.height - vertical_padding_down, 35, 25)];
    textView.text = @"16K";
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self addSubview:textView];

    // TableView
251 252
    CGFloat tableInset = frame.size.height - vertical_padding_down + 25.;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,tableInset,frame.size.width, frame.size.height - tableInset)
253 254 255 256 257
                                              style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor clearColor];
    _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
258 259 260
    _tableView.rowHeight = 44.;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.sectionHeaderHeight = 28.;
261 262
    [_tableView registerClass:[VLCTrackSelectorHeaderView class] forHeaderFooterViewReuseIdentifier:PROFILE_SELECTOR_TABLEVIEW_SECTIONHEADER];
    [_tableView registerClass:[VLCTrackSelectorTableViewCell class] forCellReuseIdentifier:PROFILE_SELECTOR_TABLEVIEW_CELL];
263
    if ([[UIDevice currentDevice] vlcSpeedCategory] >= VLCSpeedCategoryThreeDevices) {
264 265 266 267 268
        _tableView.opaque = NO;
        _tableView.backgroundColor = [UIColor clearColor];
    } else
        _tableView.backgroundColor = [UIColor blackColor];
    [self addSubview:_tableView];
269 270 271
    return self;
}

272
- (IBAction)sliderChangedValue:(VLCSlider *)sender
273 274
{
    if (self.delegate)
275
        [self.delegate setAmplification:[sender value] forBand:(unsigned)[sender tag]];
276 277
    if ([self.UIdelegate respondsToSelector:@selector(equalizerViewReceivedUserInput)])
        [self.UIdelegate equalizerViewReceivedUserInput];
278 279
}

280
- (IBAction)preampSliderChangedValue:(VLCSlider *)sender
281 282 283
{
    if (self.delegate)
        [self.delegate setPreAmplification:sender.value];
284 285
    if ([self.UIdelegate respondsToSelector:@selector(equalizerViewReceivedUserInput)])
        [self.UIdelegate equalizerViewReceivedUserInput];
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

- (void)reloadData
{
    if (self.delegate) {
        _preAmp_slider.value = [self.delegate preAmplification];
        _60_slider.value = [self.delegate amplificationOfBand:0];
        _170_slider.value = [self.delegate amplificationOfBand:1];
        _310_slider.value = [self.delegate amplificationOfBand:2];
        _600_slider.value = [self.delegate amplificationOfBand:3];
        _1K_slider.value = [self.delegate amplificationOfBand:4];
        _3K_slider.value = [self.delegate amplificationOfBand:5];
        _6K_slider.value = [self.delegate amplificationOfBand:6];
        _12K_slider.value = [self.delegate amplificationOfBand:7];
        _14K_slider.value = [self.delegate amplificationOfBand:8];
        _16K_slider.value = [self.delegate amplificationOfBand:9];
    }
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
#pragma mark - track selector table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:PROFILE_SELECTOR_TABLEVIEW_SECTIONHEADER];
    if (!view)
        view = [[VLCTrackSelectorHeaderView alloc] initWithReuseIdentifier:PROFILE_SELECTOR_TABLEVIEW_SECTIONHEADER];

    return view;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return NSLocalizedString(@"CHOOSE_EQUALIZER_PROFILES", nil);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VLCTrackSelectorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PROFILE_SELECTOR_TABLEVIEW_CELL];

    if (!cell)
        cell = [[VLCTrackSelectorTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PROFILE_SELECTOR_TABLEVIEW_CELL];

    NSInteger row = indexPath.row;

    cell.textLabel.text = [[self.delegate equalizerProfiles] objectAtIndex:row];
    unsigned int profile = (unsigned int)[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingEqualizerProfile] integerValue];

    if (profile == row)
        [cell setShowsCurrentTrack:YES];
    else
        [cell setShowsCurrentTrack:NO];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.delegate equalizerProfiles].count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger index = indexPath.row;

    [self.delegate resetEqualizerFromProfile:(unsigned)index];
    [self reloadData];
    [self.tableView reloadData];
}

361
@end