Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

2012/03/27

  • Loading branch information...
commit c1cb467eec26ca6dd8db1c3377c3a5f9431242c3 1 parent ede5d91
Yukio MURAKAMI authored
1  DemoAudio/AudioUnitViewController.h
@@ -13,6 +13,7 @@
13 13 @interface AudioUnitViewController : UIViewController
14 14
15 15 @property (nonatomic, assign) AUGraph auGraph;
  16 +@property (nonatomic, assign) BOOL isRecording;
16 17 @property (nonatomic, assign) AudioStreamBasicDescription audioUnitOutputFormat;
17 18
18 19 - (IBAction)record:(id)sender;
50 DemoAudio/AudioUnitViewController.m
@@ -10,14 +10,20 @@
10 10
11 11 static AudioStreamBasicDescription AUCanonicalASBD(Float64 sampleRate, UInt32 channel);
12 12 static AudioStreamBasicDescription CanonicalASBD(Float64 sampleRate, UInt32 channel);
  13 +static OSStatus MyAURenderCallack(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags,
  14 + const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber,
  15 + UInt32 inNumberFrames, AudioBufferList *ioData);
13 16
14 17 @interface AudioUnitViewController ()
15 18 - (void)prepareAUGraph;
  19 +- (AudioStreamBasicDescription)auCanonicalASBDSampleRate:(Float64)sampleRate channel:(UInt32)channel;
  20 +- (AudioStreamBasicDescription)canonicalASBDSampleRate:(Float64)sampleRate channel:(UInt32)channel;
16 21 @end
17 22
18 23 @implementation AudioUnitViewController
19 24
20 25 @synthesize auGraph = __auGraph;
  26 +@synthesize isRecording = __isRecording;
21 27 @synthesize audioUnitOutputFormat = __audioUnitOutputFormat;
22 28
23 29 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
@@ -51,13 +57,20 @@ - (void)viewDidLoad
51 57 DBGMSG(@"%s", __func__);
52 58 [super viewDidLoad];
53 59 // Do any additional setup after loading the view from its nib.
54   -
  60 +
  61 + self.isRecording = NO;
55 62 [self prepareAUGraph];
56 63 }
57 64
58 65 - (void)viewDidUnload
59 66 {
60 67 DBGMSG(@"%s", __func__);
  68 +
  69 + if (self.isRecording) [self stop:nil];
  70 + AUGraphUninitialize(self.auGraph);
  71 + AUGraphClose(self.auGraph);
  72 + DisposeAUGraph(self.auGraph);
  73 +
61 74 [super viewDidUnload];
62 75 // Release any retained subviews of the main view.
63 76 // e.g. self.myOutlet = nil;
@@ -72,6 +85,11 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
72 85 - (IBAction)record:(id)sender
73 86 {
74 87 DBGMSG(@"%s", __func__);
  88 + if (self.isRecording) return;
  89 +
  90 + AUGraphStart(self.auGraph);
  91 + AUGraphAddRenderNotify(self.auGraph, MyAURenderCallack, NULL);
  92 + self.isRecording = YES;
75 93 }
76 94
77 95 - (IBAction)play:(id)sender
@@ -82,6 +100,11 @@ - (IBAction)play:(id)sender
82 100 - (IBAction)stop:(id)sender
83 101 {
84 102 DBGMSG(@"%s", __func__);
  103 + if (! self.isRecording) return;
  104 +
  105 + AUGraphRemoveRenderNotify(self.auGraph, MyAURenderCallack, NULL);
  106 + AUGraphStop(self.auGraph);
  107 + self.isRecording = NO;
85 108 }
86 109
87 110 - (void)prepareAUGraph
@@ -106,14 +129,24 @@ - (void)prepareAUGraph
106 129 UInt32 flag = 1;
107 130 AudioUnitSetProperty(remoteIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &flag, sizeof(flag));
108 131
109   - AudioStreamBasicDescription audioFormat = CanonicalASBD(44100.0, 1);
  132 + AudioStreamBasicDescription audioFormat = [self canonicalASBDSampleRate:44100.0 channel:1];
110 133 AudioUnitSetProperty(remoteIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &audioFormat, sizeof(AudioStreamBasicDescription));
111 134 AudioUnitSetProperty(remoteIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &audioFormat, sizeof(AudioStreamBasicDescription));
112 135
113   - AUGraphConnectNodeInput(self.auGraph, remoteIONode, 1, remoteIONode, 0);
  136 + //AUGraphConnectNodeInput(self.auGraph, remoteIONode, 1, remoteIONode, 0);
114 137 AUGraphInitialize(self.auGraph);
115 138 }
116 139
  140 +- (AudioStreamBasicDescription)auCanonicalASBDSampleRate:(Float64)sampleRate channel:(UInt32)channel
  141 +{
  142 + return AUCanonicalASBD(sampleRate, channel);
  143 +}
  144 +
  145 +- (AudioStreamBasicDescription)canonicalASBDSampleRate:(Float64)sampleRate channel:(UInt32)channel
  146 +{
  147 + return CanonicalASBD(sampleRate, channel);
  148 +}
  149 +
117 150 @end
118 151
119 152 static AudioStreamBasicDescription AUCanonicalASBD(Float64 sampleRate, UInt32 channel)
@@ -146,4 +179,15 @@ static AudioStreamBasicDescription CanonicalASBD(Float64 sampleRate, UInt32 chan
146 179 return audioFormat;
147 180 }
148 181
  182 +static OSStatus MyAURenderCallack(void *inRefCon,
  183 + AudioUnitRenderActionFlags *ioActionFlags,
  184 + const AudioTimeStamp *inTimeStamp,
  185 + UInt32 inBusNumber,
  186 + UInt32 inNumberFrames,
  187 + AudioBufferList *ioData)
  188 +{
  189 + DBGMSG(@"%s, inNumberFrames:%u", __func__, (unsigned int)inNumberFrames);
  190 + return noErr;
  191 +}
  192 +
149 193 /* End Of File */

0 comments on commit c1cb467

Please sign in to comment.
Something went wrong with that request. Please try again.