Debian Codegraph @

iOS/Csound iOS Obj-C Examples/Csound iOS Examples/ViewControllers/RecordTest/RecordTestViewController.m

raw
1 /*
2
3 RecordTestViewController.m:
4
5 Copyright (C) 2011 Thomas Hass
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 "RecordTestViewController.h"
28
29 @implementation RecordTestViewController
30
31 -(void)viewDidLoad {
32 self.title = @"13. Mic: Recording";
33 [super viewDidLoad];
34 self.mPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[self recordingURL] error:nil];
35 [self.mPlayer setDelegate:self];
36 }
37
38 -(IBAction) toggleOnOff:(id)component {
39 UISwitch *uiswitch = (UISwitch *)component;
40 NSLog(@"Status: %d", [uiswitch isOn]);
41
42 if(uiswitch.on) {
43
44 NSString *tempFile = [[NSBundle mainBundle] pathForResource:@"recordTest" ofType:@"csd"];
45
46 [self.csound stop];
47 self.csound = [[CsoundObj alloc] init];
48 self.csound.useAudioInput = YES;
49 [self.csound addListener:self];
50
51 CsoundUI *csoundUI = [[CsoundUI alloc] initWithCsoundObj:self.csound];
52 [csoundUI addSlider:_mGainSlider forChannelName:@"gain"];
53
54 [_mLevelMeter addToCsoundObj:self.csound forChannelName:@"meter"];
55 [self.csound play:tempFile];
56 } else {
57 [self.csound stopRecording];
58 [self.csound stop];
59 }
60 }
61
62 - (IBAction)changeGain:(UISlider *)sender
63 {
64 [_mGainLabel setText:[NSString stringWithFormat:@"%.2f", [sender value]]];
65 }
66
67 - (IBAction)play:(UIButton *)sender
68 {
69 [_mPlayer play];
70 [sender removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
71 [sender addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
72 [sender setTitle:@"Stop" forState:UIControlStateNormal];
73 }
74
75 - (IBAction)stop:(UIButton *)sender
76 {
77 [_mPlayer stop];
78 [_mPlayer setCurrentTime:0];
79 [sender removeTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
80 [sender addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
81 [sender setTitle:@"Play" forState:UIControlStateNormal];
82 }
83
84 - (IBAction)showInfo:(UIButton *)sender {
85 UIViewController *infoVC = [[UIViewController alloc] init];
86 infoVC.modalPresentationStyle = UIModalPresentationPopover;
87
88 UIPopoverPresentationController *popover = infoVC.popoverPresentationController;
89 popover.sourceView = sender;
90 popover.sourceRect = sender.bounds;
91 [infoVC setPreferredContentSize:CGSizeMake(300, 120)];
92
93 UITextView *infoText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, infoVC.preferredContentSize.width, infoVC.preferredContentSize.height)];
94 infoText.editable = NO;
95 infoText.selectable = NO;
96 NSString *description = @"This example uses a custom level-meter widget, and demonstrates efficient use of concurrency for an audio application.";
97 [infoText setAttributedText:[[NSAttributedString alloc] initWithString:description]];
98 infoText.font = [UIFont fontWithName:@"Menlo" size:16];
99 [infoVC.view addSubview:infoText];
100 popover.delegate = self;
101
102 [popover setPermittedArrowDirections:UIPopoverArrowDirectionUp];
103
104 [self presentViewController:infoVC animated:YES completion:nil];
105
106 }
107
108 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
109 {
110 [_mPlayer setCurrentTime:0];
111 [_mPlayButton removeTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
112 [_mPlayButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
113 [_mPlayButton setTitle:@"Play" forState:UIControlStateNormal];
114 }
115
116
117 - (NSURL *)recordingURL
118 {
119 NSURL *localDocDirURL = nil;
120 if (localDocDirURL == nil) {
121 NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
122 objectAtIndex:0];
123 localDocDirURL = [NSURL fileURLWithPath:docDirPath];
124 }
125 return [localDocDirURL URLByAppendingPathComponent:@"recording.wav"];
126 }
127
128 #pragma mark CsoundObjListener
129
130 -(void)csoundObjStarted:(CsoundObj *)csoundObj {
131 [self.csound recordToURL:[self recordingURL]];
132 }
133
134 -(void)csoundObjCompleted:(CsoundObj *)csoundObj {
135 [_mSwitch setOn:NO animated:YES];
136 _mPlayer = nil;
137 _mPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[self recordingURL] error:nil];
138 [_mPlayer setDelegate:self];
139 }
140
141 @end