diff --git a/include/oaml.h b/include/oaml.h index be4e299..378835e 100644 --- a/include/oaml.h +++ b/include/oaml.h @@ -291,6 +291,9 @@ class oamlApi { /** Set a condition */ void SetCondition(int id, int value); + /** Clears current conditions */ + void ClearConditions(); + /** Set gain (0.f - 1.f) of a layer */ void SetLayerGain(const char *layer, float gain); diff --git a/include/oamlBase.h b/include/oamlBase.h index b816a19..d9ffb38 100644 --- a/include/oamlBase.h +++ b/include/oamlBase.h @@ -51,6 +51,8 @@ class oamlBase { std::vector sfxTracks; std::vector layers; + std::vector> conditions; + float bpm; int beatsPerBar; @@ -106,6 +108,7 @@ class oamlBase { oamlLayer *GetLayer(std::string layer); void UpdateTension(uint64_t ms); + void UpdateCondition(); oamlTrack* GetTrack(std::string name); oamlAudio* GetAudio(std::string trackName, std::string audioName); @@ -158,6 +161,7 @@ class oamlBase { void SetMainLoopCondition(int value); + void ClearConditions(); void SetCondition(int id, int value); void SetLayerGain(const char *layer, float gain); diff --git a/include/oamlMusicTrack.h b/include/oamlMusicTrack.h index 8a8c6ea..aba7c1d 100644 --- a/include/oamlMusicTrack.h +++ b/include/oamlMusicTrack.h @@ -72,7 +72,7 @@ class oamlMusicTrack : public oamlTrack { void AddAudio(oamlAudio *audio); oamlAudio* GetAudio(std::string filename); oamlRC RemoveAudio(std::string filename); - oamlRC Play(); + oamlRC Play(int mainCondValue); oamlRC Load(); float LoadProgress(); void Stop(); diff --git a/include/oamlTrack.h b/include/oamlTrack.h index 75bee6b..694255a 100644 --- a/include/oamlTrack.h +++ b/include/oamlTrack.h @@ -89,9 +89,6 @@ class oamlTrack { virtual void AddAudio(oamlAudio *) { } virtual oamlAudio* GetAudio(std::string) { return NULL; } virtual oamlRC RemoveAudio(std::string) { return OAML_NOT_FOUND; } - virtual oamlRC Play() { return OAML_NOT_FOUND; } - virtual oamlRC Play(const char *) { return OAML_NOT_FOUND; } - virtual oamlRC Play(const char *, float, float) { return OAML_NOT_FOUND; } virtual oamlRC Load() { return OAML_NOT_FOUND; } virtual float LoadProgress() { return -1.f; } virtual void Stop() { } diff --git a/src/oaml.cpp b/src/oaml.cpp index aa7849a..a7a6ba3 100644 --- a/src/oaml.cpp +++ b/src/oaml.cpp @@ -136,6 +136,10 @@ void oamlApi::SetCondition(int id, int value) { oaml->SetCondition(id, value); } +void oamlApi::ClearConditions() { + oaml->ClearConditions(); +} + void oamlApi::SetVolume(float vol) { oaml->SetVolume(vol); } diff --git a/src/oamlAudio.cpp b/src/oamlAudio.cpp index c2b8982..3a88cbb 100644 --- a/src/oamlAudio.cpp +++ b/src/oamlAudio.cpp @@ -412,35 +412,14 @@ void oamlAudio::SaveState(tinyxml2::XMLElement *node) { } void oamlAudio::LoadState(tinyxml2::XMLElement *node) { - const char *str; - - str = node->Attribute("isOpen"); - if (str) { - int isOpen = strtol(str, NULL, 0); - if (isOpen) { - Open(); - } + if (node->IntAttribute("isOpen")) { + Open(); } - str = node->Attribute("samplesCount"); - if (str) { - samplesCount = strtol(str, NULL, 0); - } - - str = node->Attribute("fadeInSamples"); - if (str) { - fadeInSamples = strtol(str, NULL, 0); - } - - str = node->Attribute("fadeOutSamples"); - if (str) { - fadeOutSamples = strtol(str, NULL, 0); - } - - str = node->Attribute("fadeOutCount"); - if (str) { - fadeOutCount = strtol(str, NULL, 0); - } + samplesCount = node->IntAttribute("samplesCount"); + fadeInSamples = node->IntAttribute("fadeInSamples"); + fadeOutSamples = node->IntAttribute("fadeOutSamples"); + fadeOutCount = node->IntAttribute("fadeOutCount"); } void oamlAudio::ReadInfo(oamlAudioInfo *info) { diff --git a/src/oamlBase.cpp b/src/oamlBase.cpp index 06c5c75..47710c5 100644 --- a/src/oamlBase.cpp +++ b/src/oamlBase.cpp @@ -412,16 +412,27 @@ void oamlBase::SetAudioFormat(int audioSampleRate, int audioChannels, int audioB } oamlRC oamlBase::PlayTrackId(int id) { + oamlRC err = OAML_ERROR; + if (id < (int)musicTracks.size()) { if (curTrack >= 0 && (size_t)curTrack < musicTracks.size()) { musicTracks[curTrack]->Stop(); } curTrack = id; - return musicTracks[id]->Play(); + int mainCondValue = 0; + for (size_t i=0; iPlay(mainCondValue); + UpdateCondition(); } - return OAML_ERROR; + return err; } oamlRC oamlBase::PlayTrack(const char *name) { @@ -458,7 +469,7 @@ oamlRC oamlBase::PlaySfxEx(const char *name, float vol, float pan) { mutex.lock(); for (std::vector::iterator it=sfxTracks.begin(); itPlay(name, vol, pan) == 0) { err = OAML_OK; break; @@ -861,12 +872,36 @@ void oamlBase::MixToBuffer(void *buffer, int size) { mutex.unlock(); } +void oamlBase::UpdateCondition() { + if (curTrack >= 0 && (size_t)curTrack < musicTracks.size()) { + for (size_t i=0; iSetCondition(conditions[i].first, conditions[i].second); + } + } +} + void oamlBase::SetCondition(int id, int value) { // printf("%s %d %d\n", __FUNCTION__, id, value); mutex.lock(); - if (curTrack >= 0 && (size_t)curTrack < musicTracks.size()) { - musicTracks[curTrack]->SetCondition(id, value); + bool found = false; + for (size_t i=0; i(id, value)); } + + UpdateCondition(); + mutex.unlock(); +} + +void oamlBase::ClearConditions() { + mutex.lock(); + conditions.clear(); mutex.unlock(); } @@ -1068,6 +1103,13 @@ std::string oamlBase::SaveState() { rootNode->InsertEndChild(baseNode); mutex.lock(); + for (size_t i=0; iSetAttribute("id", conditions[i].first); + node->SetAttribute("value", conditions[i].second); + rootNode->InsertEndChild(node); + } + for (size_t i=0; iSaveState(doc, node); @@ -1088,45 +1130,41 @@ void oamlBase::LoadState(std::string state) { return; } + mutex.lock(); + + conditions.clear(); + tinyxml2::XMLElement *rootNode = doc.FirstChildElement("oamlState"); if (rootNode) { tinyxml2::XMLElement *el = rootNode->FirstChildElement(); while (el != NULL) { if (strcmp(el->Name(), "version") == 0) { - int version; int major; int minor; int patch; sscanf(el->GetText(), "%d.%d.%d", &major, &minor, &patch); - version = (major << 16) | (minor << 8) | (patch); + int version = (major << 16) | (minor << 8) | (patch); if (version < 0x00010001) { fprintf(stderr, "old version! %X\n", version); - return; + break; } } else if (strcmp(el->Name(), "base") == 0) { - const char *str; - - str = el->Attribute("curTrack"); - if (str) { - curTrack = strtol(str, NULL, 0); - } - - str = el->Attribute("tension"); - if (str) { - tension = strtol(str, NULL, 0); - } + curTrack = el->IntAttribute("curTrack"); + tension = el->IntAttribute("tension"); + } else if (strcmp(el->Name(), "condition") == 0) { + int id = el->IntAttribute("id"); + int value = el->IntAttribute("value"); + conditions.push_back(std::pair(id, value)); } else if (strcmp(el->Name(), "musicTrack") == 0) { const char *str = el->Attribute("name"); if (str) { - mutex.lock(); for (size_t i=0; iGetNameStr(), str) == 0) { musicTracks[i]->LoadState(el); break; } } - mutex.unlock(); } } else { fprintf(stderr, "%s: Unknown state tag: %s\n", __FUNCTION__, el->Name()); @@ -1135,6 +1173,10 @@ void oamlBase::LoadState(std::string state) { el = el->NextSiblingElement(); } } + + UpdateCondition(); + + mutex.unlock(); } const char* oamlBase::GetDefsFile() { diff --git a/src/oamlMusicTrack.cpp b/src/oamlMusicTrack.cpp index a7b2ace..edfb7d3 100644 --- a/src/oamlMusicTrack.cpp +++ b/src/oamlMusicTrack.cpp @@ -230,7 +230,7 @@ void oamlMusicTrack::PlayCond(int audio) { } } -oamlRC oamlMusicTrack::Play() { +oamlRC oamlMusicTrack::Play(int mainCondValue) { int doFade = 0; if (lock > 0) { @@ -244,7 +244,8 @@ oamlRC oamlMusicTrack::Play() { doFade = 1; } - SetCondition(OAML_CONDID_MAIN_LOOP, 0); + printf("mainCondValue=%d\n", mainCondValue); + SetCondition(OAML_CONDID_MAIN_LOOP, mainCondValue); playingOrder = 0; maxPlayOrder = 0; @@ -647,40 +648,13 @@ void oamlMusicTrack::LoadState(tinyxml2::XMLElement *node) { playing = false; } - attr = node->Attribute("playingOrder"); - if (attr) { - playingOrder = strtol(attr, NULL, 0); - } - - attr = node->Attribute("tailPos"); - if (attr) { - tailPos = strtol(attr, NULL, 0); - } - - attr = node->Attribute("curAudio"); - if (attr) { - curAudio = strtol(attr, NULL, 0); - } - - attr = node->Attribute("fadeAudio"); - if (attr) { - fadeAudio = strtol(attr, NULL, 0); - } - - attr = node->Attribute("tailAudio"); - if (attr) { - tailAudio = strtol(attr, NULL, 0); - } - - attr = node->Attribute("playCondAudio"); - if (attr) { - playCondAudio = strtol(attr, NULL, 0); - } - - attr = node->Attribute("playCondSamples"); - if (attr) { - playCondSamples = strtol(attr, NULL, 0); - } + playingOrder = node->IntAttribute("playingOrder"); + tailPos = node->IntAttribute("tailPos"); + curAudio = node->IntAttribute("curAudio"); + fadeAudio = node->IntAttribute("fadeAudio"); + tailAudio = node->IntAttribute("tailAudio"); + playCondAudio = node->IntAttribute("playCondAudio"); + playCondSamples = node->IntAttribute("playCondSamples"); tinyxml2::XMLElement *el = node->FirstChildElement(); while (el != NULL) {