Skip to content
Browse files

Seeking functional in AudioFileReader

  • Loading branch information...
1 parent cf17f7f commit 1a93ed14ce48cf3fa70ab7294a5631fcb795cd85 Alex Wiltschko committed Apr 27, 2012
View
80 Novocaine Mac Example/AppDelegate.mm
@@ -36,9 +36,10 @@ - (void)dealloc
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
- ringBuffer = new RingBuffer(32768, 2);
- audioManager = [Novocaine audioManager];
+ audioManager = [Novocaine audioManager];
+// ringBuffer = new RingBuffer(32768, 2);
+
// A simple delay that's hard to express without ring buffers
// ========================================
@@ -70,44 +71,53 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// AUDIO FILE READING COOL!
// ========================================
-// NSURL *inputFileURL = [[NSBundle mainBundle] URLForResource:@"TLC" withExtension:@"mp3"];
-//
-// fileReader = [[AudioFileReader alloc]
-// initWithAudioFileURL:inputFileURL
-// samplingRate:audioManager.samplingRate
-// numChannels:audioManager.numOutputChannels];
-//
-// [fileReader play];
-//
-// [audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
-// {
-// [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];
-// }];
-
+ NSURL *inputFileURL = [[NSBundle mainBundle] URLForResource:@"TLC" withExtension:@"mp3"];
- // AUDIO FILE WRITING YEAH!
- // ========================================
- NSArray *pathComponents = [NSArray arrayWithObjects:
- [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
- @"My Recording.m4a",
- nil];
- NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
+ fileReader = [[AudioFileReader alloc]
+ initWithAudioFileURL:inputFileURL
+ samplingRate:audioManager.samplingRate
+ numChannels:audioManager.numOutputChannels];
- fileWriter = [[AudioFileWriter alloc]
- initWithAudioFileURL:outputFileURL
- samplingRate:audioManager.samplingRate
- numChannels:audioManager.numInputChannels];
+ fileReader.currentTime = 5;
+ [fileReader play];
__block int counter = 0;
- audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) {
- [fileWriter writeNewAudio:data numFrames:numFrames numChannels:numChannels];
- counter += 1;
- if (counter > 10 * audioManager.samplingRate / numChannels) { // 10 seconds of recording
- audioManager.inputBlock = nil;
- [fileWriter release];
- }
- };
+ [audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
+ {
+ [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];
+ counter++;
+ if (counter % 80 == 0)
+ 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];
+//
+// 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 > 10 * audioManager.samplingRate / numChannels) { // 10 seconds of recording
+// audioManager.inputBlock = nil;
+// [fileWriter release];
+// }
+// };
}
View
72 Novocaine iOS Example/ViewController.mm
@@ -167,45 +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];
-//
-// [audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
-// {
-// [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];
-// }];
+ 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];
+// }
+// };
}
View
4 Novocaine/AudioFileReader.h
@@ -40,7 +40,7 @@
BOOL playing;
}
-@property float currentTime;
+@property (getter=getCurrentTime, setter=setCurrentTime:) float currentTime;
@property float duration;
@property float samplingRate;
@property UInt32 numChannels;
@@ -55,7 +55,7 @@
// You use this method to grab audio if you have your own callback.
// The buffer'll fill at the speed the audio is normally being played.
- (void)retrieveFreshAudio:(float *)buffer numFrames:(UInt32)thisNumFrames numChannels:(UInt32)thisNumChannels;
-
+//- (float)getCurrentTime;
- (void)play;
- (void)pause;
View
28 Novocaine/AudioFileReader.mm
@@ -32,10 +32,10 @@ @interface AudioFileReader ()
RingBuffer *ringBuffer;
}
+@property RingBuffer *ringBuffer;
@property AudioStreamBasicDescription outputFormat;
@property ExtAudioFileRef inputFile;
@property UInt32 outputBufferSize;
-//@property RingBuffer *ringBuffer;
@property float *outputBuffer;
@property float *holdingBuffer;
@property UInt32 numSamplesReadPerPacket;
@@ -52,9 +52,9 @@ - (void)bufferNewAudio;
@implementation AudioFileReader
+@synthesize ringBuffer = _ringBuffer;
@synthesize outputFormat = _outputFormat;
@synthesize inputFile = _inputFile;
-//@synthesize ringBuffer = _ringBuffer;
@synthesize outputBuffer = _outputBuffer;
@synthesize holdingBuffer = _holdingBuffer;
@synthesize outputBufferSize = _outputBufferSize;
@@ -110,7 +110,6 @@ - (id)initWithAudioFileURL:(NSURL *)urlToAudioFile samplingRate:(float)thisSampl
// Set a few defaults and presets
self.samplingRate = thisSamplingRate;
self.numChannels = thisNumChannels;
- self.currentTime = 0.0;
self.latency = .011609977; // 512 samples / ( 44100 samples / sec ) default
@@ -125,8 +124,6 @@ - (id)initWithAudioFileURL:(NSURL *)urlToAudioFile samplingRate:(float)thisSampl
_outputFormat.mChannelsPerFrame = self.numChannels;
_outputFormat.mBitsPerChannel = 32;
-// _outputFormat = thisAudioFormat;
-
// Apply the format to our file
ExtAudioFileSetProperty(_inputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &_outputFormat);
@@ -150,7 +147,10 @@ - (id)initWithAudioFileURL:(NSURL *)urlToAudioFile samplingRate:(float)thisSampl
return self;
}
-
+- (void)clearBuffer
+{
+ ringBuffer->Clear();
+}
- (void)bufferNewAudio
{
@@ -184,6 +184,22 @@ - (void)bufferNewAudio
}
+- (float)getCurrentTime
+{
+ return self.currentFileTime - ringBuffer->NumUnreadFrames()/self.samplingRate;
+}
+
+- (void)setCurrentTime:(float)thisCurrentTime
+{
+ [self pause];
+ ExtAudioFileSeek(self.inputFile, thisCurrentTime*self.samplingRate);
+
+ [self clearBuffer];
+ [self bufferNewAudio];
+
+ [self play];
+}
+
- (float)getDuration
{
// We're going to directly calculate the duration of the audio file (in seconds)

0 comments on commit 1a93ed1

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