bool ofxAudioUnitFilePlayer::setFile(const std::string &filePath) { CFURLRef fileURL; fileURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)filePath.c_str(), filePath.length(), NULL); if(_fileID[0]) { AudioFileClose(_fileID[0]); _fileID[0] = NULL; } OSStatus s = AudioFileOpenURL(fileURL, kAudioFileReadPermission, 0, _fileID); CFRelease(fileURL); _primed = false; if(s != noErr) { cout << "Error " << s << " while opening file at " << filePath << endl; return false; } else { // setting the file ID now since it seems to have some overhead. // Doing it now ensures you'll get sound pretty much instantly after // calling play() (subsequent calls don't have the overhead) OFXAU_RET_BOOL(AudioUnitSetProperty(*_unit, kAudioUnitProperty_ScheduledFileIDs, kAudioUnitScope_Global, 0, _fileID, sizeof(_fileID)), "setting file player's file ID"); } }
// ---------------------------------------------------------- bool ofxAudioUnitInput::start() // ---------------------------------------------------------- { if(!_impl->isReady) _impl->isReady = configureInputDevice(); if(!_impl->isReady) return false; OFXAU_RET_BOOL(AudioOutputUnitStart(*_unit), "starting hardware input unit"); }
// ---------------------------------------------------------- bool ofxAudioUnitOutput::start(int deviceID) // ---------------------------------------------------------- { cout<<"ofxAudioUnitOutput::start() "<<deviceID<<endl; if(!_isReady) _isReady = configureOutputDevice(deviceID); if(!_isReady) return false; OFXAU_RET_BOOL(AudioOutputUnitStart(*_unit), "starting output unit"); }
// ---------------------------------------------------------- bool ofxAudioUnitInput::stop() // ---------------------------------------------------------- { if(_unit) { OFXAU_RET_BOOL(AudioOutputUnitStop(*_unit), "stopping hardware input unit"); } return false; }
// ---------------------------------------------------------- bool ofxAudioUnitFilePlayer::setFile(const std::string &filePath) // ---------------------------------------------------------- { CFURLRef fileURL; fileURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)filePath.c_str(), filePath.length(), NULL); if(_fileID[0]) AudioFileClose(_fileID[0]); OSStatus s = AudioFileOpenURL(fileURL, kAudioFileReadPermission, 0, _fileID); CFRelease(fileURL); if(s != noErr) { cout << "Error " << s << " while opening file at " << filePath << endl; return false; } UInt64 numPackets = 0; UInt32 dataSize = sizeof(numPackets); AudioFileGetProperty(_fileID[0], kAudioFilePropertyAudioDataPacketCount, &dataSize, &numPackets); AudioStreamBasicDescription asbd = {0}; dataSize = sizeof(asbd); AudioFileGetProperty(_fileID[0], kAudioFilePropertyDataFormat, &dataSize, &asbd); // defining a region which basically says "play the whole file" memset(&_region, 0, sizeof(_region)); _region.mTimeStamp.mFlags = kAudioTimeStampHostTimeValid; _region.mTimeStamp.mSampleTime = 0; _region.mCompletionProc = NULL; _region.mCompletionProcUserData = NULL; _region.mAudioFile = _fileID[0]; _region.mLoopCount = 0; _region.mStartFrame = 0; _region.mFramesToPlay = numPackets * asbd.mFramesPerPacket; // setting the file ID now since it seems to have some overhead. // Doing it now ensures you'll get sound pretty much instantly after // calling play() (subsequent calls don't have the overhead) OFXAU_RET_BOOL(AudioUnitSetProperty(*_unit, kAudioUnitProperty_ScheduledFileIDs, kAudioUnitScope_Global, 0, _fileID, sizeof(_fileID)), "setting file player's file ID"); }
// ---------------------------------------------------------- bool ofxAudioUnitInput::setDevice(AudioDeviceID deviceID) // ---------------------------------------------------------- { _impl->inputDeviceID = deviceID; UInt32 deviceIDSize = sizeof(deviceID); OFXAU_RET_BOOL(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceID, deviceIDSize), "setting input unit's device ID"); }
// ---------------------------------------------------------- bool ofxAudioUnitMidiReceiver::connectToMidiSource(unsigned long midiSourceIndex) // ---------------------------------------------------------- { if(!_port) { OSStatus s = MIDIInputPortCreate(_client, CFSTR("oF MIDI Input Port"), ofxAudioUnitMidiReadProc, &_unit, &_port); if(s != noErr) return false; } OFXAU_RET_BOOL(MIDIPortConnectSource(_port, MIDIGetSource(midiSourceIndex), NULL), "connecting MIDI receiver to source"); }
// ---------------------------------------------------------- bool ofxAudioUnitInput::setDevice(AudioDeviceID deviceID) // ---------------------------------------------------------- { _impl->inputDeviceID = deviceID; // Only actively set the device if it's already been configured. If it's not // yet configured, it'll be handled when configureInputDevice() is called. if(_impl->isReady) { UInt32 deviceIDSize = sizeof(deviceID); OFXAU_RET_BOOL(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceID, deviceIDSize), "setting input unit's device ID"); } }
// ---------------------------------------------------------- bool ofxAudioUnitOutput::configureOutputDevice(int deviceID) // ---------------------------------------------------------- { OSStatus err = noErr; UInt32 outSize; Boolean outWritable; err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &outSize, &outWritable); if ( err != noErr ) cout<<"err 1"<<endl; UInt16 devicesAvailable = outSize / sizeof(AudioDeviceID); if ( devicesAvailable < 1 ) { fprintf(stderr, "No devices\n" ); //return err; }else{ cout<<"*devicesAvailable "<<devicesAvailable<<endl; } UInt32 on = 1; UInt32 off = 0; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &off, sizeof(off)), "disabling input on HAL unit"); OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &on, sizeof(on)), "enabling output on HAL unit"); AudioDeviceID outputDeviceID = kAudioObjectUnknown; UInt32 deviceIDSize = sizeof( AudioDeviceID ); AudioObjectPropertyAddress prop_addr = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; OFXAU_RET_FALSE(AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop_addr, 0, NULL, &deviceIDSize, &outputDeviceID), "getting device ID for default input"); if(deviceID != -1) outputDeviceID = deviceID; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &outputDeviceID, deviceIDSize), "setting HAL unit's device ID"); cout<<"outputDeviceID "<<outputDeviceID<<endl; OFXAU_RET_BOOL(AudioUnitInitialize(*_unit), "initializing hardware input unit after setting it to input mode"); }
// ---------------------------------------------------------- bool ofxAudioUnitOutput::stop() // ---------------------------------------------------------- { OFXAU_RET_BOOL(AudioOutputUnitStop(*_unit), "stopping output unit"); }
// ---------------------------------------------------------- bool ofxAudioUnitInput::configureInputDevice() // ---------------------------------------------------------- { UInt32 on = 1; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &on, sizeof(on)), "enabling input on HAL unit"); UInt32 off = 0; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &off, sizeof(off)), "disabling output on HAL unit"); UInt32 deviceIDSize = sizeof(AudioDeviceID); OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &_impl->inputDeviceID, deviceIDSize), "setting HAL unit's device ID"); AudioStreamBasicDescription deviceASBD = {0}; UInt32 ASBDSize = sizeof(deviceASBD); OFXAU_RET_FALSE(AudioUnitGetProperty(*_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &deviceASBD, &ASBDSize), "getting hardware stream format"); deviceASBD.mSampleRate = 44100; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &deviceASBD, sizeof(deviceASBD)), "setting input sample rate to 44100"); AURenderCallbackStruct inputCallback = {RenderCallback, &_impl->ctx}; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &inputCallback, sizeof(inputCallback)), "setting hardware input callback"); OFXAU_RET_BOOL(AudioUnitInitialize(*_unit), "initializing hardware input unit after setting it to input mode"); }
// ---------------------------------------------------------- bool ofxAudioUnitInput::configureInputDevice() // ---------------------------------------------------------- { UInt32 on = 1; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &on, sizeof(on)), "enabling input on HAL unit"); UInt32 off = 0; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &off, sizeof(off)), "disabling output on HAL unit"); AudioDeviceID inputDeviceID = kAudioObjectUnknown; UInt32 deviceIDSize = sizeof( AudioDeviceID ); AudioObjectPropertyAddress prop_addr = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; OFXAU_RET_FALSE(AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop_addr, 0, NULL, &deviceIDSize, &inputDeviceID), "getting device ID for default input"); OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &inputDeviceID, deviceIDSize), "setting HAL unit's device ID"); AudioStreamBasicDescription deviceASBD = {0}; UInt32 ASBDSize = sizeof(deviceASBD); OFXAU_RET_FALSE(AudioUnitGetProperty(*_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &deviceASBD, &ASBDSize), "getting hardware stream format"); deviceASBD.mSampleRate = 44100; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &deviceASBD, sizeof(deviceASBD)), "setting input sample rate to 44100"); AURenderCallbackStruct inputCallback; inputCallback.inputProc = ofxAudioUnitInput::renderCallback; inputCallback.inputProcRefCon = &_renderContext; OFXAU_RET_FALSE(AudioUnitSetProperty(*_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &inputCallback, sizeof(inputCallback)), "setting hardware input callback"); OFXAU_RET_BOOL(AudioUnitInitialize(*_unit), "initializing hardware input unit after setting it to input mode"); }