Fixed up converter/inputnode relationship.

CQTexperiment
vspader 2007-03-03 18:25:10 +00:00
parent 1aead1bf0d
commit 1e16835c8c
3 changed files with 54 additions and 29 deletions

View File

@ -15,19 +15,28 @@
{ {
AudioConverterRef converter; AudioConverterRef converter;
void *buffer;
void *inputBuffer; void *inputBuffer;
void *outputBuffer;
//Temporary for callback use
int inputBufferSize; int inputBufferSize;
//end
int outputSize;
int maxInputSize;
AudioStreamBasicDescription inputFormat; AudioStreamBasicDescription inputFormat;
AudioStreamBasicDescription outputFormat; AudioStreamBasicDescription outputFormat;
} }
- (void *)buffer; - (void *)outputBuffer;
- (void *)inputBuffer;
- (void)setupWithInputFormat:(AudioStreamBasicDescription)inputFormat outputFormat:(AudioStreamBasicDescription)outputFormat; - (void)setupWithInputFormat:(AudioStreamBasicDescription)inputFormat outputFormat:(AudioStreamBasicDescription)outputFormat;
- (void)cleanUp; - (void)cleanUp;
- (int)convert:(void *)dest amount:(int)amount; - (int)convert:(int)amount;
- (int)maxInputSize;
@end @end

View File

@ -7,6 +7,7 @@
// //
#import "Converter.h" #import "Converter.h"
#import "Node.h"
void PrintStreamDesc (AudioStreamBasicDescription *inDesc) void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
{ {
@ -42,37 +43,25 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber
return err; return err;
} }
- (int)convert:(void *)input amount:(int)inputSize - (int)convert:(int)inputSize
{ {
AudioBufferList ioData; AudioBufferList ioData;
UInt32 ioNumberFrames; UInt32 ioNumberFrames;
int outputSize;
OSStatus err; OSStatus err;
outputSize = inputSize;
UInt32 dataSize = sizeof(outputSize);
err = AudioConverterGetProperty(converter,
kAudioConverterPropertyCalculateOutputBufferSize,
&dataSize,
(void*)&outputSize);
if (buffer != NULL)
free(buffer);
buffer = malloc(outputSize);
ioNumberFrames = outputSize/outputFormat.mBytesPerFrame; ioNumberFrames = outputSize/outputFormat.mBytesPerFrame;
ioData.mBuffers[0].mData = buffer; ioData.mBuffers[0].mData = outputBuffer;
ioData.mBuffers[0].mDataByteSize = outputSize; ioData.mBuffers[0].mDataByteSize = outputSize;
ioData.mBuffers[0].mNumberChannels = outputFormat.mChannelsPerFrame; ioData.mBuffers[0].mNumberChannels = outputFormat.mChannelsPerFrame;
ioData.mNumberBuffers = 1; ioData.mNumberBuffers = 1;
inputBuffer = input; inputBufferSize = inputSize;
inputBufferSize = inputSize;
err = AudioConverterFillComplexBuffer(converter, ACInputProc, self, &ioNumberFrames, &ioData, NULL); err = AudioConverterFillComplexBuffer(converter, ACInputProc, self, &ioNumberFrames, &ioData, NULL);
if (err == kAudioConverterErr_InvalidInputSize) //It returns insz at EOS at times...so run it again to make sure all data is converted if (err == kAudioConverterErr_InvalidInputSize) //It returns insz at EOS at times...so run it again to make sure all data is converted
{ {
return [self convert:input amount:inputSize]; return [self convert:inputSize];
} }
return ioData.mBuffers[0].mDataByteSize; return ioData.mBuffers[0].mDataByteSize;
@ -104,19 +93,50 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber
} }
} }
outputSize = CHUNK_SIZE;
maxInputSize = outputSize;
UInt32 dataSize = sizeof(maxInputSize);
AudioConverterGetProperty(converter,
kAudioConverterPropertyCalculateInputBufferSize,
&dataSize,
(void*)&maxInputSize);
if (outputBuffer)
free(outputBuffer);
outputBuffer = malloc(outputSize);
if (inputBuffer)
free(inputBuffer);
inputBuffer = malloc(maxInputSize);
NSLog(@"Converter setup!"); NSLog(@"Converter setup!");
PrintStreamDesc(&inf); PrintStreamDesc(&inf);
PrintStreamDesc(&outf); PrintStreamDesc(&outf);
} }
- (void *)buffer - (int)maxInputSize
{ {
return buffer; return maxInputSize;
}
- (void *)outputBuffer
{
return outputBuffer;
}
- (void *)inputBuffer
{
return inputBuffer;
} }
- (void)cleanUp - (void)cleanUp
{ {
if (inputBuffer)
free(inputBuffer);
if (outputBuffer)
free(outputBuffer);
AudioConverterDispose(converter); AudioConverterDispose(converter);
} }

View File

@ -80,12 +80,9 @@
- (void)process - (void)process
{ {
const int chunk_size = CHUNK_SIZE;
char *buf;
int amountRead, amountConverted; int amountRead, amountConverted;
NSLog(@"Playing file: %i", self); NSLog(@"Playing file: %i", self);
buf = malloc(chunk_size);
while ([self shouldContinue] == YES && [self endOfStream] == NO) while ([self shouldContinue] == YES && [self endOfStream] == NO)
{ {
@ -96,9 +93,9 @@
shouldSeek = NO; shouldSeek = NO;
} }
amountRead = [decoder fillBuffer:buf ofSize:chunk_size]; amountRead = [decoder fillBuffer:[converter inputBuffer] ofSize:[converter maxInputSize]];
amountConverted = [converter convert:buf amount:amountRead]; //Convert fills in converter buffer, til the next call amountConverted = [converter convert:amountRead]; //Convert fills in converter buffer, til the next call
if (amountConverted <= 0) if (amountConverted <= 0)
{ {
endOfStream = YES; endOfStream = YES;
@ -107,10 +104,9 @@
break; //eof break; //eof
} }
[self writeData:[converter buffer] amount:amountConverted]; [self writeData:[converter outputBuffer] amount:amountConverted];
} }
free(buf);
[decoder close]; [decoder close];
[converter cleanUp]; [converter cleanUp];