Skip to content

Commit

Permalink
added fixes to AudioThread and updated RomoSample app
Browse files Browse the repository at this point in the history
  • Loading branch information
phmagic committed May 3, 2012
2 parents ba829e2 + 0bafd33 commit 4e1fc17
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
58 changes: 48 additions & 10 deletions iOS/RomoLibrary/AudioThread.cpp
Expand Up @@ -21,24 +21,32 @@

AudioThread::AudioThread()
{
pthread_mutex_init(&mDirtyMutex, NULL);
pthread_cond_init(&mDirtyCond, NULL);

mDirty = FALSE;

pthread_mutex_init(&mEffectMutex, NULL);
pthread_mutex_init(&mMotorMutex, NULL);
pthread_mutex_init(&mAuxMutex, NULL);

mEffectDirty = FALSE;
mEffectFilename = std::string();

mEffectId = 0;

mMotorDirty = FALSE;
mMotorCommand = MotorCommand();

mAuxDirty = FALSE;
mAuxCommand = AuxCommand();

mAudioInterface = AudioInterface::create();
}

AudioThread::~AudioThread()
{
pthread_mutex_destroy(&mDirtyMutex);
pthread_cond_destroy(&mDirtyCond);

pthread_mutex_destroy(&mEffectMutex);
pthread_mutex_destroy(&mMotorMutex);
pthread_mutex_destroy(&mAuxMutex);
Expand All @@ -52,30 +60,46 @@ AudioThread::~AudioThread()

void AudioThread::run()
{
mAudioInterface = AudioInterface::create();


while (mState == THREAD_RUNNING)
{
pthread_mutex_lock(&mDirtyMutex);
while (!mDirty)
pthread_cond_wait(&mDirtyCond, &mDirtyMutex);
pthread_mutex_unlock(&mDirtyMutex);

mAudioInterface->reclaimBuffers();

pthread_mutex_lock(&mEffectMutex);
if (mEffectDirty)
{
pthread_mutex_lock(&mEffectMutex);
mEffectDirty = !(mAudioInterface->playSoundFile(mEffectFilename));
pthread_mutex_unlock(&mEffectMutex);
if (mEffectId) {
mEffectId = 0;
} else {
mEffectDirty = !(mAudioInterface->playSoundFile(mEffectFilename));
}
}
pthread_mutex_unlock(&mEffectMutex);

pthread_mutex_lock(&mMotorMutex);
if (mMotorDirty)
{
pthread_mutex_lock(&mMotorMutex);
mMotorDirty = !(mAudioInterface->playMotorCommand(mMotorCommand));
pthread_mutex_unlock(&mMotorMutex);
}
pthread_mutex_unlock(&mMotorMutex);

pthread_mutex_lock(&mAuxMutex);
if (mAuxDirty)
{
pthread_mutex_lock(&mAuxMutex);
mAuxDirty = !(mAudioInterface->playAuxCommand(mAuxCommand));
pthread_mutex_unlock(&mAuxMutex);
}
pthread_mutex_unlock(&mAuxMutex);

mAudioInterface->reclaimBuffers();
pthread_mutex_lock(&mDirtyMutex);
mDirty = mEffectDirty || mMotorDirty || mAuxDirty;
pthread_mutex_unlock(&mDirtyMutex);
}
}

Expand All @@ -89,6 +113,11 @@ void AudioThread::playSoundFile(std::string filename)
mEffectDirty = TRUE;

pthread_mutex_unlock(&mEffectMutex);

pthread_mutex_lock(&mDirtyMutex);
mDirty = TRUE;
pthread_mutex_unlock(&mDirtyMutex);
pthread_cond_signal(&mDirtyCond);
}

void AudioThread::playMotorCommand(uint8_t leftValue, uint8_t rightValue)
Expand All @@ -105,6 +134,11 @@ void AudioThread::playMotorCommand(uint8_t leftValue, uint8_t rightValue)
mMotorDirty = TRUE;

pthread_mutex_unlock(&mMotorMutex);

pthread_mutex_lock(&mDirtyMutex);
mDirty = TRUE;
pthread_mutex_unlock(&mDirtyMutex);
pthread_cond_signal(&mDirtyCond);
}

void AudioThread::playAuxCommand(uint8_t auxValue)
Expand All @@ -115,4 +149,8 @@ void AudioThread::playAuxCommand(uint8_t auxValue)
mAuxDirty = TRUE;

pthread_mutex_unlock(&mAuxMutex);
pthread_mutex_lock(&mDirtyMutex);
mDirty = TRUE;
pthread_mutex_unlock(&mDirtyMutex);
pthread_cond_signal(&mDirtyCond);
}
10 changes: 10 additions & 0 deletions iOS/RomoLibrary/AudioThread.h
Expand Up @@ -38,19 +38,29 @@ class AudioThread: public Thread
void playAuxCommand(uint8_t auxValue);

private:
pthread_mutex_t mDirtyMutex;
pthread_cond_t mDirtyCond;

pthread_mutex_t mEffectMutex;
pthread_mutex_t mMotorMutex;
pthread_mutex_t mAuxMutex;

bool mDirty;

bool mEffectDirty;
bool mMotorDirty;
bool mAuxDirty;

std::string mEffectFilename;
uint32_t mEffectId;

MotorCommand mMotorCommand;
AuxCommand mAuxCommand;

AudioInterface *mAudioInterface;



};

#endif
Binary file not shown.

0 comments on commit 4e1fc17

Please sign in to comment.