diff --git a/EZAudioExamples/OSX/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m b/EZAudioExamples/OSX/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m index f33d90a..4f97e10 100644 --- a/EZAudioExamples/OSX/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m +++ b/EZAudioExamples/OSX/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m @@ -181,13 +181,13 @@ #pragma mark - EZAudioFileDelegate -(void)audioFile:(EZAudioFile *)audioFile readAudio:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels { if( [EZOutput sharedOutput].isPlaying ){ - if( self.audioPlot.plotType == EZPlotTypeBuffer && - self.audioPlot.shouldFill == YES && - self.audioPlot.shouldMirror == YES ){ - self.audioPlot.shouldFill = NO; - self.audioPlot.shouldMirror = NO; - } dispatch_async(dispatch_get_main_queue(), ^{ + if( self.audioPlot.plotType == EZPlotTypeBuffer && + self.audioPlot.shouldFill == YES && + self.audioPlot.shouldMirror == YES ){ + self.audioPlot.shouldFill = NO; + self.audioPlot.shouldMirror = NO; + } [self.audioPlot updateBuffer:buffer[0] withBufferSize:bufferSize]; }); } diff --git a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample.xcodeproj/project.pbxproj b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample.xcodeproj/project.pbxproj index 74e1751..046655e 100644 --- a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample.xcodeproj/project.pbxproj +++ b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ 94056E70185BB42100EB94BA /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 94056E6F185BB42100EB94BA /* QuartzCore.framework */; }; 94056E74185BB44200EB94BA /* RecordViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 94056E72185BB44200EB94BA /* RecordViewController.m */; }; 94056E75185BB44200EB94BA /* RecordViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94056E73185BB44200EB94BA /* RecordViewController.xib */; }; + 941D71AA186298AA007D52D8 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 941D71A9186298AA007D52D8 /* AVFoundation.framework */; }; 944D07B5186049E80076EF7A /* AEFloatConverter.m in Sources */ = {isa = PBXBuildFile; fileRef = 944D0699186049E70076EF7A /* AEFloatConverter.m */; }; 944D07B6186049E80076EF7A /* TPCircularBuffer.c in Sources */ = {isa = PBXBuildFile; fileRef = 944D069A186049E70076EF7A /* TPCircularBuffer.c */; }; 944D07B7186049E80076EF7A /* EZAudioFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 944D069E186049E70076EF7A /* EZAudioFile.m */; }; @@ -79,6 +80,7 @@ 94056E71185BB44200EB94BA /* RecordViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecordViewController.h; sourceTree = ""; }; 94056E72185BB44200EB94BA /* RecordViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RecordViewController.m; sourceTree = ""; }; 94056E73185BB44200EB94BA /* RecordViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RecordViewController.xib; sourceTree = ""; }; + 941D71A9186298AA007D52D8 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; 944D0698186049E70076EF7A /* AEFloatConverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AEFloatConverter.h; sourceTree = ""; }; 944D0699186049E70076EF7A /* AEFloatConverter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AEFloatConverter.m; sourceTree = ""; }; 944D069A186049E70076EF7A /* TPCircularBuffer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = TPCircularBuffer.c; sourceTree = ""; }; @@ -109,6 +111,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 941D71AA186298AA007D52D8 /* AVFoundation.framework in Frameworks */, 94056E70185BB42100EB94BA /* QuartzCore.framework in Frameworks */, 94056E6E185BB41900EB94BA /* CoreAudio.framework in Frameworks */, 94056E6B185BB40F00EB94BA /* AudioToolbox.framework in Frameworks */, @@ -153,6 +156,7 @@ 94056E0B185BB3D800EB94BA /* Frameworks */ = { isa = PBXGroup; children = ( + 941D71A9186298AA007D52D8 /* AVFoundation.framework */, 94056E6F185BB42100EB94BA /* QuartzCore.framework */, 94056E6D185BB41900EB94BA /* CoreAudio.framework */, 94056E69185BB40F00EB94BA /* AudioToolbox.framework */, diff --git a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.h b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.h index 4d43357..d033d3e 100644 --- a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.h +++ b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.h @@ -28,13 +28,16 @@ // Import EZAudio header #import "EZAudio.h" +// Import AVFoundation to play the file (will save EZAudioFile and EZOutput for separate example) +#import + // By default this will record a file to /Users/YOUR_USERNAME/Documents/test.caf #define kAudioFilePath [NSString stringWithFormat:@"%@%@",NSHomeDirectory(),@"/Documents/test.caf"] /** We will allow this view controller to act as an EZMicrophoneDelegate. This is how we listen for the microphone callback. */ -@interface RecordViewController : NSViewController +@interface RecordViewController : NSViewController /** Use a OpenGL based plot to visualize the data coming in @@ -57,6 +60,11 @@ @property (nonatomic,strong) EZRecorder *recorder; #pragma mark - Actions +/** + Stops the recorder and starts playing whatever has been recorded. + */ +-(IBAction)playFile:(id)sender; + /** Toggles the microphone on and off. When the microphone is on it will send its delegate (aka this view controller) the audio data in various ways (check out the EZMicrophoneDelegate documentation for more details); */ diff --git a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m index fcab673..b103e7f 100644 --- a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m +++ b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m @@ -25,15 +25,23 @@ #import "RecordViewController.h" -@interface RecordViewController () - +@interface RecordViewController (){ + BOOL _hasSomethingToPlay; +} +@property (nonatomic,strong) AVAudioPlayer *audioPlayer; +@property (nonatomic,weak) IBOutlet NSButton *microphoneToggle; +@property (nonatomic,weak) IBOutlet NSButton *playButton; +@property (nonatomic,weak) IBOutlet NSButton *recordingToggle; @end @implementation RecordViewController +@synthesize audioPlayer; @synthesize audioPlot; @synthesize isRecording; @synthesize microphone; +@synthesize microphoneToggle; @synthesize recorder; +@synthesize recordingToggle; #pragma mark - Initialization -(id)init { @@ -77,7 +85,14 @@ // Waveform color self.audioPlot.color = [NSColor colorWithCalibratedRed: 0.114 green: 0.346 blue: 0.685 alpha: 1]; // Plot type - self.audioPlot.plotType = EZPlotTypeBuffer; + self.audioPlot.plotType = EZPlotTypeRolling; + // Fill + self.audioPlot.shouldFill = YES; + // Mirror + self.audioPlot.shouldMirror = YES; + + // Configure the play button + [self.playButton setHidden:YES]; /* Start the microphone @@ -87,6 +102,32 @@ } #pragma mark - Actions +-(void)playFile:(id)sender { + + // Update microphone state + [self.microphone stopFetchingAudio]; + self.microphoneToggle.state = NSOffState; + [self.microphoneToggle setEnabled:NO]; + + // Update recording state + self.isRecording = NO; + self.recordingToggle.state = NSOffState; + [self.recordingToggle setEnabled:NO]; + + // Create Audio Player + if( self.audioPlayer ){ + if( self.audioPlayer.playing ) [self.audioPlayer stop]; + self.audioPlayer = nil; + } + NSError *err; + self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:kAudioFilePath] + error:&err]; + + [self.audioPlayer play]; + self.audioPlayer.delegate = self; + +} + -(void)toggleMicrophone:(id)sender { switch([sender state]){ case NSOffState: @@ -101,6 +142,7 @@ } -(void)toggleRecording:(id)sender { + [self.playButton setHidden:NO]; self.isRecording = (BOOL)[sender state]; } @@ -145,4 +187,20 @@ withNumberOfChannels:(UInt32)numberOfChannels { } +#pragma mark - AVAudioPlayerDelegate +/* + Occurs when the audio player instance completes playback + */ +-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { + // Update microphone state + self.microphoneToggle.state = NSOnState; + [self.microphoneToggle setEnabled:YES]; + [self.microphone startFetchingAudio]; + + // Update recording state + self.isRecording = NO; + self.recordingToggle.state = NSOffState; + [self.recordingToggle setEnabled:YES]; +} + @end diff --git a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.xib b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.xib index 4f3351a..c01a642 100644 --- a/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.xib +++ b/EZAudioExamples/OSX/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.xib @@ -8,6 +8,9 @@ + + + @@ -46,15 +49,29 @@ + + + + diff --git a/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/Base.lproj/Main_iPad.storyboard b/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/Base.lproj/Main_iPad.storyboard index 3f774a7..c6fb4b6 100644 --- a/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/Base.lproj/Main_iPad.storyboard +++ b/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/Base.lproj/Main_iPad.storyboard @@ -15,8 +15,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m b/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m index a93ea3b..f1d799c 100644 --- a/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m +++ b/EZAudioExamples/iOS/EZAudioPlayFileExample/EZAudioPlayFileExample/PlayFileViewController.m @@ -154,9 +154,17 @@ readAudio:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels { - dispatch_async(dispatch_get_main_queue(), ^{ - [self.audioPlot updateBuffer:buffer[0] withBufferSize:bufferSize]; - }); + if( [EZOutput sharedOutput].isPlaying ){ + dispatch_async(dispatch_get_main_queue(), ^{ + if( self.audioPlot.plotType == EZPlotTypeBuffer && + self.audioPlot.shouldFill == YES && + self.audioPlot.shouldMirror == YES ){ + self.audioPlot.shouldFill = NO; + self.audioPlot.shouldMirror = NO; + } + [self.audioPlot updateBuffer:buffer[0] withBufferSize:bufferSize]; + }); + } } -(void)audioFile:(EZAudioFile *)audioFile diff --git a/EZAudioExamples/iOS/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m b/EZAudioExamples/iOS/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m index 19565fb..f244183 100644 --- a/EZAudioExamples/iOS/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m +++ b/EZAudioExamples/iOS/EZAudioRecordExample/EZAudioRecordExample/RecordViewController.m @@ -80,6 +80,9 @@ self.recordingTextField.text = @"Not Recording"; self.playingTextField.text = @"Not Playing"; + // Hide the play button + self.playButton.hidden = YES; + /* Log out where the file is being written to within the app's documents directory */ @@ -139,6 +142,7 @@ if( self.audioPlayer.playing ) [self.audioPlayer stop]; self.audioPlayer = nil; } + self.playButton.hidden = NO; self.isRecording = (BOOL)[sender isOn]; self.recordingTextField.text = self.isRecording ? @"Recording" : @"Not Recording";