I am trying to take audio from the mic in and apply a 180 phase shift to that input stream and output it.
Here is the code I'm using to init the session and capture the audio (sample rate is set to 44.1 KHz)
OSStatus status = noErr;
status = AudioSessionSetActive(true);
assert(status == noErr);
UInt32 category = kAudioSessionCategory_PlayAndRecord;
status = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &category);
assert(status == noErr);
float aBufferLength = 0.002902; // In seconds
status = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration,
sizeof(aBufferLength), &aBufferLength);
assert(status == noErr);
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
// get AU component
AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);
// create audio unit by component
status = AudioComponentInstanceNew(inputComponent, &_audioState->audioUnit);
assert(status == noErr);
// record io on the input bus
UInt32 flag = 1;
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
1, /*input*/
&flag,
sizeof(flag));
assert(status == noErr);
// play on io on the output bus
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0, /*output*/
&flag,
sizeof(flag));
assert(status == noErr);
// Fetch sample rate, in case we didn't get quite what we requested
Float64 achievedSampleRate;
UInt32 size = sizeof(achievedSampleRate);
status = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate, &size, &achievedSampleRate);
if ( achievedSampleRate != SAMPLE_RATE ) {
NSLog(@"Hardware sample rate is %f", achievedSampleRate);
} else {
achievedSampleRate = SAMPLE_RATE;
NSLog(@"Hardware sample rate is %f", achievedSampleRate);
}
// specify stream format for recording
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = achievedSampleRate;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 2;
audioFormat.mBytesPerFrame = 2;
// set the format on the output stream
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputBus,
&audioFormat,
sizeof(audioFormat));
assert(status == noErr);
// set the format on the input stream
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
kOutputBus,
&audioFormat,
sizeof(audioFormat));
assert(status == noErr);
AURenderCallbackStruct callbackStruct;
memset(&callbackStruct, 0, sizeof(AURenderCallbackStruct));
callbackStruct.inputProc = RenderCallback;
callbackStruct.inputProcRefCon = _audioState;
// set input callback
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global,
kInputBus,
&callbackStruct,
sizeof(callbackStruct));
assert(status == noErr);
callbackStruct.inputProc = PlaybackCallback;
callbackStruct.inputProcRefCon = _audioState;
// set Render callback for output
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
kOutputBus,
&callbackStruct,
sizeof(callbackStruct));
assert(status == noErr);
flag = 0;
// allocate render buffer
status = AudioUnitSetProperty(_audioState->audioUnit,
kAudioUnitProperty_ShouldAllocateBuffer,
kAudioUnitScope_Output,
kInputBus,
&flag,
sizeof(flag));
assert(status == noErr);
_audioState->audioBuffer.mNumberChannels = 1;
_audioState->audioBuffer.mDataByteSize = 256 * 2;
_audioState->audioBuffer.mData = malloc(256 * 2);
// initialize the audio unit
status = AudioUnitInitialize(_audioState->audioUnit);
assert(status == noErr);
}
Does anyone know a way to shift the phase to create a destructive sine wave? I've heard talk of using vDSP to do band pass filtering but i'm unsure...