File: ViewController.mm

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (101 lines) | stat: -rw-r--r-- 3,294 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  ViewController.m
//  iOSKeyboard
//
//  Created by Romain Michon on 6/6/16.
//  Copyright © 2016 CCRMA. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    DspFaust* faustDsp;
    NSInteger currentPreset;
    NSString *audioSettingsFile;
    NSDictionary *audioSettings;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    currentPreset = 0;
    
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    // if no presets in documents directory, then upload defaults if they exist    
    NSArray *resourceFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error:nil];
    NSMutableArray *presetFiles = [[NSMutableArray alloc] init];
    for(int i=0; i<[resourceFiles count]; i++){
        if([[resourceFiles objectAtIndex:i] rangeOfString:@"_keyb"].location != NSNotFound || [[resourceFiles objectAtIndex:i] rangeOfString:@"_dsp"].location != NSNotFound){
            [presetFiles addObject:[resourceFiles objectAtIndex:i]];
        }
    }
    
    if([[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil] count] == 0){
        for(int i=0; i<[presetFiles count]; i++){
            NSString *resourcePath = [[NSBundle mainBundle] pathForResource:[presetFiles objectAtIndex:i] ofType:nil];
            [[NSFileManager defaultManager] copyItemAtPath:resourcePath toPath:[documentsDirectory stringByAppendingPathComponent:[presetFiles objectAtIndex:i]] error:nil];
        }
    }
    
    // initializing audio settings
    audioSettingsFile = [documentsDirectory stringByAppendingPathComponent:@"audioSettings"];
    audioSettings = [[NSDictionary alloc] initWithContentsOfFile:audioSettingsFile];
    
    // if no saved settings then create defaults
    if([audioSettings count] == 0){
        [self createDefaultAudioSettings];
    }
    
    [self startFaustDsp];
    
    MultiKeyboard *multiKeyboard = [[MultiKeyboard alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height) withFaustDSP:faustDsp withPreset:nil];
    [self.view addSubview:multiKeyboard];
}

// creates default audio settings
- (void)createDefaultAudioSettings{
    audioSettings = [NSMutableDictionary dictionaryWithDictionary:
                     @{
                       @"SR":[NSNumber numberWithInt:44100],
                       @"bufferLength":[NSNumber numberWithInt:256]
                       }];
    [audioSettings writeToFile:audioSettingsFile atomically:YES];
}

// start the faust dsp object and its associated elements
- (void)startFaustDsp{
    if(!faustDsp){
        faustDsp = new DspFaust([audioSettings[@"SR"] intValue],[audioSettings[@"bufferLength"] intValue]);
        faustDsp->start();
    }
}

// stop the faust dsp object and its associated elements
- (void)stopFaustDsp{
    if(faustDsp){
        faustDsp->stop();
        delete faustDsp;
        faustDsp = NULL;
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self stopFaustDsp];
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (BOOL) shouldAutorotate{
    return NO;
}

@end