Browse files

added fixes to AudioThread and updated RomoSample app

  • Loading branch information...
2 parents ba829e2 + 0bafd33 commit 4e1fc172b81fcd0467387a27a29991f0d29ebd27 @phmagic phmagic committed May 2, 2012
View
58 iOS/RomoLibrary/AudioThread.cpp
@@ -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);
@@ -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);
}
}
@@ -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)
@@ -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)
@@ -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);
}
View
10 iOS/RomoLibrary/AudioThread.h
@@ -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
View
BIN ...p.xcodeproj/project.xcworkspace/xcuserdata/phu.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.

0 comments on commit 4e1fc17

Please sign in to comment.