Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed memory exception when playing a file more than once. Added call…

… to AudioFileReader::stop() when the end of the audio file is reached. Will still give -50 error due to inNumberFrames being 471 instead of 470.
  • Loading branch information...
commit a4febacb6782f206aa245907446b4ea0d6a146dd 1 parent f95d007
Corey casbreuk authored
74 Novocaine iOS Example/ViewController.mm
View
@@ -167,47 +167,47 @@ - (void)viewWillAppear:(BOOL)animated
// AUDIO FILE READING OHHH YEAHHHH
// ========================================
- NSURL *inputFileURL = [[NSBundle mainBundle] URLForResource:@"TLC" withExtension:@"mp3"];
-
- fileReader = [[AudioFileReader alloc]
- initWithAudioFileURL:inputFileURL
- samplingRate:audioManager.samplingRate
- numChannels:audioManager.numOutputChannels];
-
- [fileReader play];
- fileReader.currentTime = 30.0;
-
- [audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
- {
- [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];
- NSLog(@"Time: %f", fileReader.currentTime);
- }];
+// NSURL *inputFileURL = [[NSBundle mainBundle] URLForResource:@"TLC" withExtension:@"mp3"];
+//
+// fileReader = [[AudioFileReader alloc]
+// initWithAudioFileURL:inputFileURL
+// samplingRate:audioManager.samplingRate
+// numChannels:audioManager.numOutputChannels];
+//
+// [fileReader play];
+// fileReader.currentTime = 30.0;
+//
+// [audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
+// {
+// [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];
+// NSLog(@"Time: %f", fileReader.currentTime);
+// }];
// AUDIO FILE WRITING YEAH!
// ========================================
-// NSArray *pathComponents = [NSArray arrayWithObjects:
-// [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
-// @"My Recording.m4a",
-// nil];
-// NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
-// NSLog(@"URL: %@", outputFileURL);
-//
-// fileWriter = [[AudioFileWriter alloc]
-// initWithAudioFileURL:outputFileURL
-// samplingRate:audioManager.samplingRate
-// numChannels:audioManager.numInputChannels];
-//
-//
-// __block int counter = 0;
-// audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) {
-// [fileWriter writeNewAudio:data numFrames:numFrames numChannels:numChannels];
-// counter += 1;
-// if (counter > 400) { // roughly 5 seconds of audio
-// audioManager.inputBlock = nil;
-// [fileWriter release];
-// }
-// };
+ NSArray *pathComponents = [NSArray arrayWithObjects:
+ [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
+ @"My Recording.m4a",
+ nil];
+ NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
+ NSLog(@"URL: %@", outputFileURL);
+
+ fileWriter = [[AudioFileWriter alloc]
+ initWithAudioFileURL:outputFileURL
+ samplingRate:audioManager.samplingRate
+ numChannels:audioManager.numInputChannels];
+
+
+ __block int counter = 0;
+ audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) {
+ [fileWriter writeNewAudio:data numFrames:numFrames numChannels:numChannels];
+ counter += 1;
+ if (counter > 400) { // roughly 5 seconds of audio
+ audioManager.inputBlock = nil;
+ [fileWriter release];
+ }
+ };
}
9 Novocaine/AudioFileReader.mm
View
@@ -181,8 +181,11 @@ - (void)bufferNewAudio
ringBuffer->AddNewInterleavedFloatData(self.outputBuffer, framesRead, self.numChannels);
if ((self.currentFileTime - self.duration) < 0.01 && framesRead == 0) {
- self.currentTime = 0.0f;
- [self pause];
+ // modified to allow for auto-stopping. //
+ // Need to change your output block to check for [fileReader playing] and nuke your fileReader if it is //
+ // not playing and not paused, on the next frame. Otherwise, the sound clip's final buffer is not played. //
+// self.currentTime = 0.0f;
+ [self stop];
ringBuffer->Clear();
}
@@ -285,8 +288,6 @@ - (void)stop
// Release the dispatch timer because it holds a reference to this class instance
[self pause];
if (self.callbackTimer) {
- dispatch_suspend(self.callbackTimer);
- dispatch_suspend(self.callbackTimer);
dispatch_release(self.callbackTimer);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.