Debian Codegraph @

iOS/Csound iOS Obj-C Examples/Csound iOS Examples/ViewControllers/TrappedGenerator/TrappedGeneratorViewController.m

raw
1 /*
2
3 TrappedGeneratorViewController.m:
4
5 Copyright (C) 2014 Steven Yi, Aurelius Prochazka
6 Updated in 2017 by Dr. Richard Boulanger, Nikhil Singh
7
8 This file is part of Csound iOS Examples.
9
10 The Csound for iOS Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
14
15 Csound is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with Csound; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA
24
25 */
26
27 #import "TrappedGeneratorViewController.h"
28
29 @interface TrappedGeneratorViewController () {
30 BOOL hasRendered;
31 NSString *localFilePath;
32 AVAudioPlayer *player;
33 }
34
35 @end
36
37 @implementation TrappedGeneratorViewController
38
39 - (void)viewDidLoad {
40 self.title = @"06. Render: Trapped in Convert";
41 [super viewDidLoad];
42 hasRendered = NO;
43 }
44
45 - (IBAction)generateTrappedToDocumentsFolder:(id)sender {
46 NSString *csdPath = [[NSBundle mainBundle] pathForResource:@"trapped" ofType:@"csd"];
47
48 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
49 NSString *documentsDirectory = [paths objectAtIndex:0];
50 localFilePath = [documentsDirectory stringByAppendingPathComponent:@"trapped.wav"];
51 NSLog(@"OUTPUT: %@", localFilePath);
52
53 [self.csound stop];
54
55 self.csound = [[CsoundObj alloc] init];
56
57 [self.csound addListener:self];
58 [self.csound record:csdPath toFile:localFilePath];
59 }
60
61 - (IBAction)play:(id)sender {
62 if(hasRendered) {
63 NSError *error;
64 player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:localFilePath] error:&error];
65
66 if(error != nil) NSLog(@"%@", [error localizedDescription]);
67
68 [player prepareToPlay];
69 [player play];
70 } else {
71 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Not Rendered" message:@"Please render the file before playing it." preferredStyle:UIAlertControllerStyleAlert];
72 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
73
74 [alert addAction:defaultAction];
75 [self presentViewController:alert animated:YES completion:nil];
76 }
77 }
78
79 - (IBAction)stop:(id)sender {
80 [player stop];
81 }
82
83 - (IBAction)showInfo:(UIButton *)sender {
84 UIViewController *infoVC = [[UIViewController alloc] init];
85 infoVC.modalPresentationStyle = UIModalPresentationPopover;
86
87 UIPopoverPresentationController *popover = infoVC.popoverPresentationController;
88 popover.sourceView = sender;
89 popover.sourceRect = sender.bounds;
90 [infoVC setPreferredContentSize:CGSizeMake(300, 140)];
91
92 UITextView *infoText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, infoVC.preferredContentSize.width, infoVC.preferredContentSize.height)];
93 infoText.editable = NO;
94 infoText.selectable = NO;
95 NSString *description = @"This example demonstrates creating a virtual Csound console, and implementing a method to update this virtual console with information accessed from CsoundObj.";
96 [infoText setAttributedText:[[NSAttributedString alloc] initWithString:description]];
97 infoText.font = [UIFont fontWithName:@"Menlo" size:16];
98 [infoVC.view addSubview:infoText];
99 popover.delegate = self;
100
101 [popover setPermittedArrowDirections:UIPopoverArrowDirectionUp];
102
103 [self presentViewController:infoVC animated:YES completion:nil];
104
105 }
106
107 #pragma mark CsoundObjListener
108
109 - (void)csoundObjCompleted:(CsoundObj *)csoundObj {
110
111 NSString *title = @"Render Complete";
112 NSString *message = @"File generated as trapped.wav in application Documents Folder.";
113
114 UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
115 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
116 [alert addAction:defaultAction];
117 [self presentViewController:alert animated:YES completion:nil];
118
119 hasRendered = YES;
120 }
121
122
123 @end