Skip to content

Commit

Permalink
Fix playing non-looped ambiance tracks
Browse files Browse the repository at this point in the history
Because of a misplaced parenthesis, non-looped ambiance tracks were
only played in debug builds.

Fixes bug #341
  • Loading branch information
dscharrer committed Jul 25, 2012
1 parent 1165dd9 commit b8f2726
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
83 changes: 55 additions & 28 deletions src/audio/Ambiance.cpp
Expand Up @@ -101,7 +101,9 @@ struct KeySetting {
unsigned interval; // Interval between updates (On Start = 0)
int tupdate; // Last update time

KeySetting() : flags(0), min(0), max(0), from(0), to(0), cur(0), interval(0), tupdate(0) { }
KeySetting()
: flags(0), min(0), max(0), from(0), to(0), cur(0), interval(0), tupdate(0)
{ }

bool load(PakFileHandle * file) {

Expand Down Expand Up @@ -195,7 +197,8 @@ struct TrackKey {
!z.load(file)) {
return false;
}
start = _start, loop = _loop + 1, delay_min = _delay_min, delay_max = _delay_max;
start = _start, loop = _loop + 1;
delay_min = _delay_min, delay_max = _delay_max;

return true;
}
Expand Down Expand Up @@ -251,7 +254,8 @@ struct Ambiance::Track : public Source::Callback {
}

bool operator==(const std::string & str) const {
return (name == str || _sample[Backend::getSampleId(s_id)]->getName() == str);
return (name == str
|| _sample[Backend::getSampleId(s_id)]->getName() == str);
}

private:
Expand All @@ -272,8 +276,8 @@ struct Ambiance::Track : public Source::Callback {
size_t loopc; // How often the sample still needs to loop.
size_t queued; // How many loop counts are already queued.

explicit Track(Ambiance * _ambiance) : s_id(INVALID_ID), ambiance(_ambiance), flags(0),
loopc(0), queued(0) { }
explicit Track(Ambiance * _ambiance)
: s_id(INVALID_ID), ambiance(_ambiance), flags(0), loopc(0), queued(0) { }

void keyPlay();

Expand Down Expand Up @@ -345,12 +349,15 @@ void Ambiance::Track::keyPlay() {
if(!key_i->delay_min && !key_i->delay_max) {
size_t toqueue = loopc - queued;
queued += toqueue;
LogDebug("ambiance " << ambiance->getName() << ": playing " << source->getSample()->getName() << " " << toqueue << " -> " << queued << " / " << loopc);
LogDebug("ambiance " << ambiance->getName() << ": playing "
<< source->getSample()->getName() << " " << toqueue
<< " -> " << queued << " / " << loopc);
source->play(toqueue);
arx_assert(loopc >= queued);
} else {
LogDebug("ambiance " << ambiance->getName() << ": playing " << source->getSample()->getName();
source->play());
LogDebug("ambiance " << ambiance->getName() << ": playing "
<< source->getSample()->getName());
source->play();
queued++;
}
}
Expand All @@ -360,7 +367,8 @@ void Ambiance::Track::keyPlay() {

void Ambiance::Track::onSampleStart(Source & source) {

LogDebug("ambiance " << ambiance->getName() << ": " << source.getSample()->getName() << " started");
LogDebug("ambiance " << ambiance->getName() << ": "
<< source.getSample()->getName() << " started");

if(flags & Ambiance::Track::PREFETCHED) {
flags &= ~Ambiance::Track::PREFETCHED;
Expand All @@ -379,8 +387,10 @@ void Ambiance::Track::onSampleStart(Source & source) {
if(keyPrefetch == keys.end()) {
keyPrefetch = keys.begin();
}
if(!keyPrefetch->start && !keyPrefetch->delay_min && !keyPrefetch->delay_max) {
LogDebug("ambiance " << ambiance->getName() << ": prefetching " << source.getSample()->getName() << " " << keyPrefetch->loop);
if(!keyPrefetch->start && !keyPrefetch->delay_min
&& !keyPrefetch->delay_max) {
LogDebug("ambiance " << ambiance->getName() << ": prefetching "
<< source.getSample()->getName() << " " << keyPrefetch->loop);
queued += keyPrefetch->loop;
loopc += keyPrefetch->loop;
source.play(keyPrefetch->loop);
Expand Down Expand Up @@ -412,7 +422,8 @@ void Ambiance::Track::onSampleEnd(Source & source) {

ARX_UNUSED(source);

LogDebug("ambiance " << ambiance->getName() << ": " << source.getSample()->getName() << " ended");
LogDebug("ambiance " << ambiance->getName() << ": "
<< source.getSample()->getName() << " ended");

arx_assert(queued > 0);

Expand Down Expand Up @@ -442,7 +453,8 @@ void Ambiance::Track::onSampleEnd(Source & source) {
LogDebug("ambiance " << ambiance->getName() << ": master track ended");

if(ambiance->isLooped()) {
for(TrackList::iterator i = ambiance->tracks.begin(); i != ambiance->tracks.end(); ++i) {
TrackList::iterator i = ambiance->tracks.begin();
for(; i != ambiance->tracks.end(); ++i) {
if(!(i->flags & Track::PREFETCHED)) {
i->key_i = i->keys.begin();
}
Expand Down Expand Up @@ -489,7 +501,9 @@ void Ambiance::Track::update(size_t time, size_t diff) {

if(key_i->volume.interval) {
float value = key_i->volume.update(time);
if (ambiance->channel.flags & FLAG_VOLUME) value *= ambiance->channel.volume;
if(ambiance->channel.flags & FLAG_VOLUME) {
value *= ambiance->channel.volume;
}
source->setVolume(value);
} else {
source->setVolume(key_i->volume.cur * ambiance->channel.volume);
Expand Down Expand Up @@ -536,7 +550,8 @@ aalError Ambiance::Track::load(PakFileHandle * file, u32 version) {
}
Sample * sample = new Sample(res::path::load(sampleName));
if(sample->load() || (s_id = _sample.add(sample)) == INVALID_ID) {
LogError << "ambiance \"" << ambiance->name << "\": missing sample \"" << sampleName << '"';
LogError << "ambiance \"" << ambiance->name
<< "\": missing sample \"" << sampleName << '"';
delete sample;
return AAL_ERROR_FILEIO;
} else {
Expand All @@ -558,7 +573,8 @@ aalError Ambiance::Track::load(PakFileHandle * file, u32 version) {
}

flags = Ambiance::Track::TrackFlags::load(iflags); // TODO save/load flags
flags &= ~(Ambiance::Track::MUTED | Ambiance::Track::PAUSED | Ambiance::Track::PREFETCHED);
flags &= ~(Ambiance::Track::MUTED | Ambiance::Track::PAUSED
| Ambiance::Track::PREFETCHED);

keys.resize(key_c);

Expand All @@ -582,8 +598,9 @@ aalError Ambiance::Track::load(PakFileHandle * file, u32 version) {
return AAL_OK;
}

Ambiance::Ambiance(const res::path & _name) :
status(Idle), loop(false), fade(None), start(0), time(0), name(_name), data(NULL) {
Ambiance::Ambiance(const res::path & _name)
: status(Idle), loop(false), fade(None), start(0),
time(0), name(_name), data(NULL) {
channel.flags = 0;
}

Expand Down Expand Up @@ -618,7 +635,8 @@ aalError Ambiance::load() {
file->read(&nbtracks, 4);
tracks.resize(nbtracks, Track(this));

for(Ambiance::TrackList::iterator track = tracks.begin(); track != tracks.end(); ++track) {
Ambiance::TrackList::iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {
if(aalError error = track->load(file.get(), version)) {
return error;
}
Expand All @@ -639,7 +657,8 @@ aalError Ambiance::setVolume(float volume) {
return AAL_OK;
}

for(TrackList::const_iterator track = tracks.begin(); track != tracks.end(); ++track) {
TrackList::const_iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {
if(Source * source = backend->getSource(track->s_id)) {
if(track->key_i != track->keys.end()) {
source->setVolume(track->key_i->volume.cur * channel.volume);
Expand Down Expand Up @@ -679,7 +698,8 @@ aalError Ambiance::muteTrack(const string & name, bool mute) {
return AAL_OK;
}

aalError Ambiance::play(const Channel & _channel, bool _loop, size_t _fade_interval) {
aalError Ambiance::play(const Channel & _channel, bool _loop,
size_t _fade_interval) {

channel = _channel;

Expand All @@ -699,10 +719,12 @@ aalError Ambiance::play(const Channel & _channel, bool _loop, size_t _fade_inter
fade = None;
}

for(TrackList::iterator track = tracks.begin(); track != tracks.end(); ++track) {
TrackList::iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {

//Init track keys
for(Track::KeyList::iterator key = track->keys.begin(); key != track->keys.end(); ++key) {
Track::KeyList::iterator key = track->keys.begin();
for(; key != track->keys.end(); ++key) {

key->delay = key->delay_max;
key->updateSynch();
Expand All @@ -716,7 +738,8 @@ aalError Ambiance::play(const Channel & _channel, bool _loop, size_t _fade_inter
key->z.reset();
}

arx_assert(backend->getSource(track->s_id) == NULL || backend->getSource(track->s_id)->isIdle());
arx_assert(backend->getSource(track->s_id) == NULL
|| backend->getSource(track->s_id)->isIdle());

track->key_i = track->keys.begin();
track->loopc = track->key_i->loop;
Expand Down Expand Up @@ -750,7 +773,8 @@ aalError Ambiance::stop(size_t _fade_interval) {
status = Idle;
time = 0;

for(TrackList::iterator track = tracks.begin(); track != tracks.end(); ++track) {
TrackList::iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {
if(Source * source = backend->getSource(track->s_id)) {
source->stop();
}
Expand All @@ -769,7 +793,8 @@ aalError Ambiance::pause() {
status = Paused;
time = session_time;

for(TrackList::iterator track = tracks.begin(); track != tracks.end(); ++track) {
TrackList::iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {
if(Source * source = backend->getSource(track->s_id)) {
source->pause();
track->flags |= Track::PAUSED;
Expand All @@ -785,7 +810,8 @@ aalError Ambiance::resume() {
return AAL_ERROR;
}

for(TrackList::iterator track = tracks.begin(); track != tracks.end(); ++track) {
TrackList::iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {
if(track->flags & Track::PAUSED) {
if(Source * source = backend->getSource(track->s_id)) {
source->resume();
Expand Down Expand Up @@ -832,7 +858,8 @@ aalError Ambiance::update() {
}

// Update tracks
for(TrackList::iterator track = tracks.begin(); track != tracks.end(); ++track) {
TrackList::iterator track = tracks.begin();
for(; track != tracks.end(); ++track) {
track->update(time, interval);
}

Expand Down
3 changes: 2 additions & 1 deletion src/audio/Ambiance.h
Expand Up @@ -76,7 +76,8 @@ class Ambiance {
inline bool isIdle() const { return status == Idle; }
inline bool isLooped() const { return loop; }

aalError play(const Channel & channel, bool loop = true, size_t fade_interval = 0);
aalError play(const Channel & channel, bool loop = true,
size_t fade_interval = 0);
aalError stop(size_t fade_interval = 0);
aalError pause();
aalError resume();
Expand Down
8 changes: 3 additions & 5 deletions src/audio/Audio.cpp
Expand Up @@ -75,7 +75,7 @@ static Lock * mutex = NULL;

aalError init(const string & backendName, bool enableEAX) {

//Clean any initialized data
// Clean any initialized data
clean();

LogDebug("Init");
Expand All @@ -95,7 +95,6 @@ aalError init(const string & backendName, bool enableEAX) {
matched = true;
LogDebug("initializing OpenAL backend");
OpenALBackend * _backend = new OpenALBackend();

error = _backend->init(enableEAX);
if(!error) {
backend = _backend;
Expand All @@ -110,7 +109,6 @@ aalError init(const string & backendName, bool enableEAX) {
matched = true;
LogDebug("initializing DirectSound backend");
DSoundBackend * _backend = new DSoundBackend();

error = _backend->init(enableEAX);
if(!error) {
backend = _backend;
Expand All @@ -119,14 +117,14 @@ aalError init(const string & backendName, bool enableEAX) {
}
}
#endif

if(first && !matched) {
LogError << "unknown backend: " << backendName;
}
}

#if !defined(ARX_HAVE_OPENAL) && !defined(ARX_HAVE_DSOUND)
ARX_UNUSED(autoBackend), ARX_UNUSED(enableEAX);
ARX_UNUSED(autoBackend), ARX_UNUSED(enableEAX);
#endif

if(!backend) {
Expand Down

0 comments on commit b8f2726

Please sign in to comment.