Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new target "nuiPhone-ub" to compile a universal library for iPhone device and simulator #5

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion include/nglPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum nglPathBase
ePathUserAppSettings, ///< Use a user-related ('unix sense') location as root for application settings(may not be visible by default)
ePathUserDocuments, ///< Use a user-related ('unix sense') location as root for application documents
ePathUserPreferences, ///< Use a user-related ('unix sense') location as root for preferences
ePathUserDesktop ///< Use the desktop folder as root
ePathUserDesktop ///< Use the desktop folder as root,
};


Expand Down
7 changes: 7 additions & 0 deletions include/nuiAudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class nuiAudioEngine : public nuiObject
void UnsetInputProcessDelegate();
void UnsetOutputProcessDelegate();

void DeactivateOutputDevice();
bool ActivateOutputDevice();
void DeactivateInputDevice();
bool ActivateInputDevice(ChannelConfig inputConfig);



nuiVoice* PlaySound(const nglPath& path, nuiSound::Type type = nuiSound::eStream);
nuiVoice* PlaySound(nuiSound* pSound);
void StopSound(nuiVoice* pnuiVoice);
Expand Down
3 changes: 1 addition & 2 deletions include/nuiMemorySound.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ class nuiMemorySound : public nuiSound
std::vector<float*> mSamples;
int64 mLength;
nglPath mPath;
nglIStream* mpStream;
};
};
3,142 changes: 3,100 additions & 42 deletions nui3.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Application/Cocoa/nglApplication_Cocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- (void) applicationDidFinishLaunching: (NSApplication*) pUIApp;
- (BOOL) application:(NSApplication *)pNSApplication didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (void) applicationDidBecomeActive: (NSApplication*) pUIApp;
- (void) applicationWillResignActive: (NSApplication*) pUIApp;
- (void) applicationDidEnterBackground: (NSApplication*) pUIApp;
- (void) applicationDidReceiveMemoryWarning: (NSApplication*) pUIApp;
- (void) applicationSignificantTimeChange: (NSApplication*) pUIApp;
- (void) applicationWillTerminate: (NSApplication*) pUIApp;
Expand Down
4 changes: 2 additions & 2 deletions src/Application/Cocoa/nglApplication_Cocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ - (void) applicationDidBecomeActive: (NSApplication*) pNSApplication
}
}

- (void) applicationWillResignActive: (NSApplication*) pNSApplication
- (void) applicationDidEnterBackground: (NSApplication*) pNSApplication
{
NGL_DEBUG( NGL_OUT(_T("[nglNSApplicationDelegate applicationWillResignActive]\n")); )
NGL_DEBUG( NGL_OUT(_T("[nglNSApplicationDelegate applicationDidEnterBackground]\n")); )
NGL_ASSERT(App);


Expand Down
2 changes: 1 addition & 1 deletion src/Application/UIKit/nglApplication_UIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- (void) applicationDidFinishLaunching: (UIApplication*) pUIApp;
- (BOOL) application:(UIApplication *)pUIApplication didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (void) applicationDidBecomeActive: (UIApplication*) pUIApp;
- (void) applicationWillResignActive: (UIApplication*) pUIApp;
- (void) applicationDidEnterBackground: (UIApplication*) pUIApp;
- (void) applicationDidReceiveMemoryWarning: (UIApplication*) pUIApp;
- (void) applicationSignificantTimeChange: (UIApplication*) pUIApp;
- (void) applicationWillTerminate: (UIApplication*) pUIApp;
Expand Down
4 changes: 2 additions & 2 deletions src/Application/UIKit/nglApplication_UIKit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ - (void) applicationDidBecomeActive: (UIApplication*) pUIApplication
}
}

- (void) applicationWillResignActive: (UIApplication*) pUIApplication
- (void) applicationDidEnterBackground: (UIApplication*) pUIApplication
{
NGL_DEBUG( NGL_OUT(_T("[nglUIApplicationDelegate applicationWillResignActive]\n")); )
NGL_DEBUG( NGL_OUT(_T("[nglUIApplicationDelegate applicationDidEnterBackground]\n")); )
NGL_ASSERT(App);

NSEnumerator *e = [[pUIApplication windows] objectEnumerator];
Expand Down
95 changes: 65 additions & 30 deletions src/Audio/AudioUnit/nuiAudioDevice_AudioUnit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,7 @@ void audioRouteChangeListenerCallback (void *inUserData,
nuiAudioDevice_AudioUnit::nuiAudioDevice_AudioUnit()
{
mpIData = NULL;

OSStatus err;
UInt32 size = sizeof (UInt32);
UInt32 value = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, size, &value);

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, this);

// Initialize our session
err = AudioSessionInitialize(NULL, NULL, interruptionListener, NULL);


// Set the category
UInt32 uCategory = kAudioSessionCategory_LiveAudio;
if (HasAudioInput())
uCategory = kAudioSessionCategory_PlayAndRecord;

err = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &uCategory);
// if (err != noErr)
// {
// NGL_ASSERT(0);
// return false;
// }

mAudioUnit = NULL;

EnumSampleRates();
EnumBufferSizes();
Expand All @@ -98,8 +75,7 @@ void audioRouteChangeListenerCallback (void *inUserData,

nuiAudioDevice_AudioUnit::~nuiAudioDevice_AudioUnit()
{
AudioOutputUnitStop(mAudioUnit);
AudioComponentInstanceDispose(mAudioUnit);
Close();
}


Expand Down Expand Up @@ -146,10 +122,25 @@ OSStatus AudioUnitInputCallback(void* inRefCon,

void nuiAudioDevice_AudioUnit::Process(uint uNumFrames, AudioBufferList* ioData)
{
//NGL_OUT(_T("nuiAudioDevice_AudioUnit::Process uNumFrames %d %d %d\n"),uNumFrames, ioData->mBuffers[0].mNumberChannels, ioData->mBuffers[1].mNumberChannels );
//NGL_OUT(_T("nuiAudioDevice_AudioUnit::Process uNumFrames %d (%d) %d %d\n"),uNumFrames, ioData->mNumberBuffers, ioData->mBuffers[0].mNumberChannels, ioData->mBuffers[1].mNumberChannels );

mAudioProcessFn(mInputBuffers, mOutputBuffers, uNumFrames);

if (0)
{ //#DEBUG TEST
static float t = 0;
static float incr = 440 * (M_PI * 2 / 44100.0);
for (int32 i = 0; i < uNumFrames; i++)
{
const float v = sinf(t);
t += incr;
mOutputBuffers[0][i] = v;
mOutputBuffers[1][i] = v;

if (t >= M_PI * 2)
t -= M_PI * 2;
}
}

// copy buffers (int -> float)
if (ioData->mNumberBuffers == 2)
Expand Down Expand Up @@ -261,7 +252,7 @@ OSStatus AudioUnitInputCallback(void* inRefCon,
bool nuiAudioDevice_AudioUnit::Open(std::vector<uint32>& rInputChannels, std::vector<uint32>& rOutputChannels, double SampleRate, uint32 BufferSize, nuiAudioProcessFn pProcessFunction)
{
OSStatus err;

mAudioProcessFn = pProcessFunction;
mSampleRate = SampleRate;
mBufferSize = BufferSize;
Expand All @@ -275,6 +266,28 @@ OSStatus AudioUnitInputCallback(void* inRefCon,
for (uint32 i = 0; i < mOutputBuffers.size(); i++)
mOutputBuffers[i] = (float*)malloc(mBufferSize * sizeof(float));

{
UInt32 size = sizeof (UInt32);
UInt32 value = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, size, &value);

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, this);


// Set the category
UInt32 uCategory = kAudioSessionCategory_LiveAudio;
if (HasAudioInput())
uCategory = kAudioSessionCategory_PlayAndRecord;

err = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &uCategory);
// if (err != noErr)
// {
// NGL_ASSERT(0);
// return false;
// }
}



// Set the buffer size
Float32 fBufferSize = (float)mBufferSize / mSampleRate; //256frames @ 44.1khz
Expand Down Expand Up @@ -358,9 +371,18 @@ OSStatus AudioUnitInputCallback(void* inRefCon,

UInt32 flag = 1;
err = AudioUnitSetProperty(mAudioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag));

if (err != noErr)
{
NGL_ASSERT(0);
}


size = sizeof (AudioStreamBasicDescription);
err = AudioUnitSetProperty(mAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &out_fmt_desc, size);
if (err != noErr)
{
NGL_ASSERT(0);
}

// same for input device:
AudioStreamBasicDescription in_fmt_desc;
Expand Down Expand Up @@ -516,6 +538,14 @@ OSStatus AudioUnitInputCallback(void* inRefCon,
// uses. In the current iPhone OS devices, it's always 24

err = AudioUnitInitialize(mAudioUnit);

uint32 count = 4;
while (err == -12983 && count--)
{
nglThread::MsSleep(100);
err = AudioUnitInitialize(mAudioUnit);
}

if (err != noErr)
{
NGL_ASSERT(0);
Expand All @@ -540,8 +570,10 @@ OSStatus AudioUnitInputCallback(void* inRefCon,

bool nuiAudioDevice_AudioUnit::Close()
{

AudioOutputUnitStop(mAudioUnit);
AudioUnitUninitialize(mAudioUnit);
AudioComponentInstanceDispose(mAudioUnit);
mAudioUnit = NULL;
return true;
}

Expand Down Expand Up @@ -586,6 +618,9 @@ OSStatus AudioUnitInputCallback(void* inRefCon,
nuiAudioDeviceAPI_AudioUnit::nuiAudioDeviceAPI_AudioUnit()
{
mName = _T("AudioUnit");
// Initialize our session
OSStatus err = AudioSessionInitialize(NULL, NULL, interruptionListener, NULL);

}

nuiAudioDeviceAPI_AudioUnit::~nuiAudioDeviceAPI_AudioUnit()
Expand Down
51 changes: 41 additions & 10 deletions src/AudioEngine/nuiAudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,63 @@ bool nuiAudioEngine::AudioInit(ChannelConfig inputConfig)

NGL_OUT(_T("Default output: %ls\n"), mpOutAudioDevice->GetName().GetChars());

bool res = ActivateOutputDevice();

if (inputConfig == eNone)
return res;

mpInAudioDevice = nuiAudioDeviceManager::Get().GetDefaultInputDevice();
res &= ActivateInputDevice(inputConfig);

return res;
}





void nuiAudioEngine::DeactivateOutputDevice()
{
mpOutAudioDevice->Close();
}

void nuiAudioEngine::DeactivateInputDevice()
{
mpInAudioDevice->Close();
}



bool nuiAudioEngine::ActivateOutputDevice()
{
std::vector<uint32> InputChannels;
std::vector<uint32> OutputChannels;
OutputChannels.push_back(0);
OutputChannels.push_back(1);

bool res = mpOutAudioDevice->Open(InputChannels, OutputChannels, mSampleRate, mBufferSize, nuiMakeDelegate(this, &nuiAudioEngine::ProcessAudioOutput));


if (inputConfig == eNone)
return res;

InputChannels.clear();
OutputChannels.clear();
return res;
}



bool nuiAudioEngine::ActivateInputDevice(ChannelConfig inputConfig)
{
std::vector<uint32> InputChannels;
std::vector<uint32> OutputChannels;

if (inputConfig == eMono)
InputChannels.push_back(0);
else if (inputConfig == eStereo)
{
InputChannels.push_back(0);
InputChannels.push_back(1);
}
mpInAudioDevice = nuiAudioDeviceManager::Get().GetDefaultInputDevice();
res &= mpInAudioDevice->Open(InputChannels, OutputChannels, mSampleRate, mBufferSize, nuiMakeDelegate(this, &nuiAudioEngine::ProcessAudioInput));

bool res = mpInAudioDevice->Open(InputChannels, OutputChannels, mSampleRate, mBufferSize, nuiMakeDelegate(this, &nuiAudioEngine::ProcessAudioInput));
return res;
}


double nuiAudioEngine::GetSampleRate() const
{
return mSampleRate;
Expand Down
Loading