Skip to content

Commit

Permalink
Conditions can now be set before a PlayTrack() call, also added Clear…
Browse files Browse the repository at this point in the history
…Conditions()
  • Loading branch information
marcelofg55 committed May 28, 2018
1 parent 138c345 commit 82f423e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 88 deletions.
3 changes: 3 additions & 0 deletions include/oaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions include/oamlBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class oamlBase {
std::vector<oamlSfxTrack*> sfxTracks;
std::vector<oamlLayer*> layers;

std::vector<std::pair<int, int>> conditions;

float bpm;
int beatsPerBar;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/oamlMusicTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions include/oamlTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
4 changes: 4 additions & 0 deletions src/oaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
33 changes: 6 additions & 27 deletions src/oamlAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
84 changes: 63 additions & 21 deletions src/oamlBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; i<conditions.size(); i++) {
if (conditions[i].first == OAML_CONDID_MAIN_LOOP) {
mainCondValue = conditions[i].second;
break;
}
}

err = musicTracks[id]->Play(mainCondValue);
UpdateCondition();
}

return OAML_ERROR;
return err;
}

oamlRC oamlBase::PlayTrack(const char *name) {
Expand Down Expand Up @@ -458,7 +469,7 @@ oamlRC oamlBase::PlaySfxEx(const char *name, float vol, float pan) {

mutex.lock();
for (std::vector<oamlSfxTrack*>::iterator it=sfxTracks.begin(); it<sfxTracks.end(); ++it) {
oamlTrack *track = *it;
oamlSfxTrack *track = *it;
if (track->Play(name, vol, pan) == 0) {
err = OAML_OK;
break;
Expand Down Expand Up @@ -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; i<conditions.size(); i++) {
musicTracks[curTrack]->SetCondition(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<conditions.size() && !found; i++) {
if (conditions[i].first == id) {
conditions[i].second = value;
found = true;
}
}

if (!found) {
conditions.push_back(std::pair<int, int>(id, value));
}

UpdateCondition();
mutex.unlock();
}

void oamlBase::ClearConditions() {
mutex.lock();
conditions.clear();
mutex.unlock();
}

Expand Down Expand Up @@ -1068,6 +1103,13 @@ std::string oamlBase::SaveState() {
rootNode->InsertEndChild(baseNode);

mutex.lock();
for (size_t i=0; i<conditions.size(); i++) {
tinyxml2::XMLElement *node = doc.NewElement("condition");
node->SetAttribute("id", conditions[i].first);
node->SetAttribute("value", conditions[i].second);
rootNode->InsertEndChild(node);
}

for (size_t i=0; i<musicTracks.size(); i++) {
tinyxml2::XMLElement *node = doc.NewElement("musicTrack");
musicTracks[i]->SaveState(doc, node);
Expand All @@ -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<int, int>(id, value));
} else if (strcmp(el->Name(), "musicTrack") == 0) {
const char *str = el->Attribute("name");
if (str) {
mutex.lock();
for (size_t i=0; i<musicTracks.size(); i++) {
if (strcmp(musicTracks[i]->GetNameStr(), str) == 0) {
musicTracks[i]->LoadState(el);
break;
}
}
mutex.unlock();
}
} else {
fprintf(stderr, "%s: Unknown state tag: %s\n", __FUNCTION__, el->Name());
Expand All @@ -1135,6 +1173,10 @@ void oamlBase::LoadState(std::string state) {
el = el->NextSiblingElement();
}
}

UpdateCondition();

mutex.unlock();
}

const char* oamlBase::GetDefsFile() {
Expand Down
46 changes: 10 additions & 36 deletions src/oamlMusicTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void oamlMusicTrack::PlayCond(int audio) {
}
}

oamlRC oamlMusicTrack::Play() {
oamlRC oamlMusicTrack::Play(int mainCondValue) {
int doFade = 0;

if (lock > 0) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 82f423e

Please sign in to comment.