| 1 |
/* |
| 2 |
|
| 3 |
ConsoleOutputViewController.m: |
| 4 |
|
| 5 |
Copyright (C) 2014 Thomas Hass, 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 "ConsoleOutputViewController.h" |
| 28 |
|
| 29 |
@interface ConsoleOutputViewController() { |
| 30 |
NSArray *csdArray; |
| 31 |
NSString *csdPath; |
| 32 |
NSString *folderPath; |
| 33 |
|
| 34 |
UIViewController *csdListVC; |
| 35 |
} |
| 36 |
|
| 37 |
@end |
| 38 |
|
| 39 |
@implementation ConsoleOutputViewController |
| 40 |
|
| 41 |
- (void)viewDidLoad { |
| 42 |
NSError *error; |
| 43 |
folderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"csdStorage/"]; |
| 44 |
csdArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]; |
| 45 |
|
| 46 |
if(error != nil) NSLog(@"%@", [error localizedDescription]); |
| 47 |
|
| 48 |
csdPath = [[NSBundle mainBundle] pathForResource:@"Yi - Countdown" ofType:@"csd" inDirectory:@"csdStorage"]; |
| 49 |
NSLog(@"%@", csdArray); |
| 50 |
} |
| 51 |
|
| 52 |
#pragma mark UITableView Methods |
| 53 |
|
| 54 |
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 55 |
csdPath = [folderPath stringByAppendingPathComponent:[tableView cellForRowAtIndexPath:indexPath].textLabel.text]; |
| 56 |
[csdListVC dismissViewControllerAnimated:YES completion:nil]; |
| 57 |
} |
| 58 |
|
| 59 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 60 |
return csdArray.count; |
| 61 |
} |
| 62 |
|
| 63 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 64 |
UITableViewCell *cell = [[UITableViewCell alloc] init]; |
| 65 |
cell.textLabel.text = [csdArray objectAtIndex:indexPath.row]; |
| 66 |
return cell; |
| 67 |
} |
| 68 |
|
| 69 |
#pragma mark IBActions |
| 70 |
|
| 71 |
- (IBAction)run:(UIButton *)sender |
| 72 |
{ |
| 73 |
if(sender.isSelected == NO) { |
| 74 |
mTextView.text = @""; |
| 75 |
[sender setSelected:YES]; |
| 76 |
|
| 77 |
[self.csound stop]; |
| 78 |
self.csound = [[CsoundObj alloc] init]; |
| 79 |
[self.csound setMessageCallback:@selector(messageCallback:) withListener:self]; |
| 80 |
[self.csound addListener:self]; |
| 81 |
[self.csound play:csdPath]; |
| 82 |
} else { |
| 83 |
[self.csound stop]; |
| 84 |
[sender setSelected:NO]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
- (IBAction)showCSDs:(UIButton *)sender { |
| 89 |
csdListVC = [[UIViewController alloc] init]; |
| 90 |
csdListVC.modalPresentationStyle = UIModalPresentationPopover; |
| 91 |
|
| 92 |
UIPopoverPresentationController *popover = csdListVC.popoverPresentationController; |
| 93 |
popover.sourceView = sender; |
| 94 |
popover.sourceRect = sender.bounds; |
| 95 |
[csdListVC setPreferredContentSize:CGSizeMake(300, 600)]; |
| 96 |
|
| 97 |
self.csdTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, csdListVC.preferredContentSize.width, csdListVC.preferredContentSize.height)]; |
| 98 |
self.csdTable.delegate = self; |
| 99 |
self.csdTable.dataSource = self; |
| 100 |
[csdListVC.view addSubview:self.csdTable]; |
| 101 |
|
| 102 |
popover.delegate = self; |
| 103 |
[popover setPermittedArrowDirections:UIPopoverArrowDirectionUp]; |
| 104 |
|
| 105 |
[self presentViewController:csdListVC animated:YES completion:nil]; |
| 106 |
} |
| 107 |
|
| 108 |
- (IBAction)showInfo:(UIButton *)sender { |
| 109 |
UIViewController *infoVC = [[UIViewController alloc] init]; |
| 110 |
infoVC.modalPresentationStyle = UIModalPresentationPopover; |
| 111 |
|
| 112 |
UIPopoverPresentationController *popover = infoVC.popoverPresentationController; |
| 113 |
popover.sourceView = sender; |
| 114 |
popover.sourceRect = sender.bounds; |
| 115 |
[infoVC setPreferredContentSize:CGSizeMake(300, 160)]; |
| 116 |
|
| 117 |
UITextView *infoText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, infoVC.preferredContentSize.width, infoVC.preferredContentSize.height)]; |
| 118 |
infoText.editable = NO; |
| 119 |
infoText.selectable = NO; |
| 120 |
NSString *description = @"Renders, by default, a simple 5-second 'countdown' csd and publishes information to a virtual Csound console every second. Click the CSD button on the right to select a different csd file."; |
| 121 |
[infoText setAttributedText:[[NSAttributedString alloc] initWithString:description]]; |
| 122 |
infoText.font = [UIFont fontWithName:@"Menlo" size:16]; |
| 123 |
[infoVC.view addSubview:infoText]; |
| 124 |
popover.delegate = self; |
| 125 |
|
| 126 |
[popover setPermittedArrowDirections:UIPopoverArrowDirectionUp]; |
| 127 |
|
| 128 |
[self presentViewController:infoVC animated:YES completion:nil]; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
-(void)csoundObjCompleted:(CsoundObj *)csoundObj |
| 133 |
{ |
| 134 |
dispatch_async(dispatch_get_main_queue(), ^(void) { |
| 135 |
[self.renderButton setSelected:NO]; |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
- (void)updateUIWithNewMessage:(NSString *)newMessage |
| 140 |
{ |
| 141 |
NSString *oldText = mTextView.text; |
| 142 |
NSString *fullText = [oldText stringByAppendingString:newMessage]; |
| 143 |
mTextView.text = fullText; |
| 144 |
} |
| 145 |
|
| 146 |
- (void)messageCallback:(NSValue *)infoObj |
| 147 |
{ |
| 148 |
@autoreleasepool { |
| 149 |
|
| 150 |
Message info; |
| 151 |
[infoObj getValue:&info]; |
| 152 |
char message[1024]; |
| 153 |
vsnprintf(message, 1024, info.format, info.valist); |
| 154 |
NSString *messageStr = [NSString stringWithFormat:@"%s", message]; |
| 155 |
[self performSelectorOnMainThread:@selector(updateUIWithNewMessage:) |
| 156 |
withObject:messageStr |
| 157 |
waitUntilDone:NO]; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
| 162 |
{ |
| 163 |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 164 |
if (self) { |
| 165 |
self.title = @"07. Console Output"; |
| 166 |
} |
| 167 |
return self; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
@end |