2007-03-03 17:19:37 +00:00
|
|
|
//
|
|
|
|
// ConverterNode.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 8/2/05.
|
|
|
|
// Copyright 2005 Vincent Spader. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Converter.h"
|
2007-03-03 18:25:10 +00:00
|
|
|
#import "Node.h"
|
2007-03-03 17:19:37 +00:00
|
|
|
|
|
|
|
void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
|
|
|
|
{
|
|
|
|
if (!inDesc) {
|
|
|
|
printf ("Can't print a NULL desc!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf ("- - - - - - - - - - - - - - - - - - - -\n");
|
|
|
|
printf (" Sample Rate:%f\n", inDesc->mSampleRate);
|
|
|
|
printf (" Format ID:%s\n", (char*)&inDesc->mFormatID);
|
|
|
|
printf (" Format Flags:%lX\n", inDesc->mFormatFlags);
|
|
|
|
printf (" Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
|
|
|
|
printf (" Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
|
|
|
|
printf (" Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
|
|
|
|
printf (" Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
|
|
|
|
printf (" Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
|
|
|
|
printf ("- - - - - - - - - - - - - - - - - - - -\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation Converter
|
|
|
|
|
|
|
|
//called from the complexfill when the audio is converted...good clean fun
|
|
|
|
static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumberDataPackets, AudioBufferList* ioData, AudioStreamPacketDescription** outDataPacketDescription, void* inUserData)
|
|
|
|
{
|
|
|
|
Converter *converter = (Converter *)inUserData;
|
|
|
|
OSStatus err = noErr;
|
|
|
|
|
2007-03-03 21:13:25 +00:00
|
|
|
if (converter->inputBufferSize > 0) {
|
2007-05-10 23:19:56 +00:00
|
|
|
int amountConverted = *ioNumberDataPackets * converter->inputFormat.mBytesPerPacket;
|
|
|
|
if (amountConverted > converter->inputBufferSize) {
|
|
|
|
amountConverted = converter->inputBufferSize;
|
|
|
|
}
|
|
|
|
|
2007-03-03 21:13:25 +00:00
|
|
|
ioData->mBuffers[0].mData = converter->inputBuffer;
|
2007-05-10 23:19:56 +00:00
|
|
|
ioData->mBuffers[0].mDataByteSize = amountConverted;
|
2007-03-03 21:13:25 +00:00
|
|
|
ioData->mBuffers[0].mNumberChannels = (converter->inputFormat.mChannelsPerFrame);
|
|
|
|
ioData->mNumberBuffers = 1;
|
2007-05-10 23:19:56 +00:00
|
|
|
|
|
|
|
*ioNumberDataPackets = amountConverted / converter->inputFormat.mBytesPerPacket;
|
2007-03-03 21:13:25 +00:00
|
|
|
|
2007-05-10 23:19:56 +00:00
|
|
|
converter->inputBufferSize -= amountConverted;
|
|
|
|
converter->inputBuffer = ((char *)converter->inputBuffer) + amountConverted;
|
2007-03-03 21:13:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ioData->mBuffers[0].mData = NULL;
|
|
|
|
ioData->mBuffers[0].mDataByteSize = 0;
|
2007-03-03 22:55:26 +00:00
|
|
|
ioData->mNumberBuffers = 1;
|
2007-03-03 21:13:25 +00:00
|
|
|
*ioNumberDataPackets = 0;
|
2007-03-03 22:55:26 +00:00
|
|
|
|
|
|
|
//Reset the converter's internal bufferrs.
|
|
|
|
converter->needsReset = YES;
|
2007-03-03 21:13:25 +00:00
|
|
|
}
|
2007-03-03 17:19:37 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-03-03 21:13:25 +00:00
|
|
|
- (int)convert:(void *)input amount:(int)inputSize
|
2007-03-03 17:19:37 +00:00
|
|
|
{
|
|
|
|
AudioBufferList ioData;
|
|
|
|
UInt32 ioNumberFrames;
|
2007-03-03 18:25:10 +00:00
|
|
|
|
2007-03-04 00:59:40 +00:00
|
|
|
if (inputSize <= 0) {
|
|
|
|
return inputSize;
|
|
|
|
}
|
|
|
|
|
2007-03-03 17:19:37 +00:00
|
|
|
OSStatus err;
|
|
|
|
|
2007-03-03 22:55:26 +00:00
|
|
|
needsReset = NO;
|
|
|
|
|
2007-05-10 23:19:56 +00:00
|
|
|
ioNumberFrames = inputSize/inputFormat.mBytesPerFrame;
|
2007-03-03 18:25:10 +00:00
|
|
|
ioData.mBuffers[0].mData = outputBuffer;
|
2007-03-03 17:19:37 +00:00
|
|
|
ioData.mBuffers[0].mDataByteSize = outputSize;
|
|
|
|
ioData.mBuffers[0].mNumberChannels = outputFormat.mChannelsPerFrame;
|
|
|
|
ioData.mNumberBuffers = 1;
|
2007-03-03 21:13:25 +00:00
|
|
|
inputBuffer = input;
|
2007-03-03 18:25:10 +00:00
|
|
|
inputBufferSize = inputSize;
|
2007-03-03 17:19:37 +00:00
|
|
|
|
|
|
|
err = AudioConverterFillComplexBuffer(converter, ACInputProc, self, &ioNumberFrames, &ioData, NULL);
|
2007-03-03 22:55:26 +00:00
|
|
|
if (err != noErr || needsReset) //It returns insz at EOS at times...so run it again to make sure all data is converted
|
2007-03-03 17:19:37 +00:00
|
|
|
{
|
2007-03-03 22:55:26 +00:00
|
|
|
AudioConverterReset(converter);
|
2007-03-03 17:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ioData.mBuffers[0].mDataByteSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setupWithInputFormat:(AudioStreamBasicDescription)inf outputFormat:(AudioStreamBasicDescription)outf
|
|
|
|
{
|
|
|
|
NSLog(@"CREATING THE CONVERTER");
|
|
|
|
//Make the converter
|
|
|
|
OSStatus stat = noErr;
|
|
|
|
|
|
|
|
inputFormat = inf;
|
|
|
|
outputFormat = outf;
|
|
|
|
|
|
|
|
stat = AudioConverterNew ( &inputFormat, &outputFormat, &converter);
|
|
|
|
if (stat != noErr)
|
|
|
|
{
|
|
|
|
NSLog(@"Error creating converter %i", stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputFormat.mChannelsPerFrame == 1)
|
|
|
|
{
|
|
|
|
SInt32 channelMap[2] = { 0, 0 };
|
|
|
|
|
|
|
|
stat = AudioConverterSetProperty(converter,kAudioConverterChannelMap,sizeof(channelMap),channelMap);
|
|
|
|
if (stat != noErr)
|
|
|
|
{
|
|
|
|
NSLog(@"Error mapping channels %i", stat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 18:25:10 +00:00
|
|
|
outputSize = CHUNK_SIZE;
|
2007-03-03 21:13:25 +00:00
|
|
|
UInt32 dataSize = sizeof(outputSize);
|
2007-03-03 18:25:10 +00:00
|
|
|
AudioConverterGetProperty(converter,
|
2007-03-03 21:13:25 +00:00
|
|
|
kAudioConverterPropertyCalculateOutputBufferSize,
|
2007-03-03 18:25:10 +00:00
|
|
|
&dataSize,
|
2007-03-03 21:13:25 +00:00
|
|
|
(void*)&outputSize);
|
2007-05-10 23:19:56 +00:00
|
|
|
NSLog(@"Output size: %i %i", outputSize, CHUNK_SIZE);
|
2007-03-03 18:25:10 +00:00
|
|
|
if (outputBuffer)
|
2007-03-03 22:55:26 +00:00
|
|
|
{
|
|
|
|
NSLog(@"FREEING");
|
2007-03-03 18:25:10 +00:00
|
|
|
free(outputBuffer);
|
2007-03-03 22:55:26 +00:00
|
|
|
NSLog(@"FREED");
|
|
|
|
}
|
2007-03-03 18:25:10 +00:00
|
|
|
outputBuffer = malloc(outputSize);
|
|
|
|
|
|
|
|
|
2007-03-03 17:19:37 +00:00
|
|
|
NSLog(@"Converter setup!");
|
|
|
|
|
|
|
|
PrintStreamDesc(&inf);
|
|
|
|
PrintStreamDesc(&outf);
|
|
|
|
}
|
|
|
|
|
2007-03-03 18:25:10 +00:00
|
|
|
|
|
|
|
- (void *)outputBuffer
|
|
|
|
{
|
|
|
|
return outputBuffer;
|
|
|
|
}
|
|
|
|
|
2007-03-03 17:19:37 +00:00
|
|
|
- (void)cleanUp
|
|
|
|
{
|
2007-03-03 22:55:26 +00:00
|
|
|
if (outputBuffer) {
|
2007-03-03 18:25:10 +00:00
|
|
|
free(outputBuffer);
|
2007-03-03 22:55:26 +00:00
|
|
|
outputBuffer = NULL;
|
|
|
|
}
|
2007-03-03 17:19:37 +00:00
|
|
|
AudioConverterDispose(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|