|
From: Lasse Kärkkäi. <tr...@us...> - 2010-08-12 22:45:02
|
Module: performous
Branch: master
Commit: e9e9e096e2e2982b881f29cfd8fa7c2ee7b0473a
Author: Lasse Karkkainen <tro...@tr...>
Date: Sat Jun 19 05:36:08 2010 +0300
More audio cleanup and fixed a bug causing streams with negative starting pos never getting played.
---
game/audio.cc | 15 ++++++++++-----
game/audio.hh | 9 ++++-----
game/ffmpeg.hh | 1 +
3 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/game/audio.cc b/game/audio.cc
index 7fd5b5f..b82be8d 100644
--- a/game/audio.cc
+++ b/game/audio.cc
@@ -119,16 +119,21 @@ struct Audio::Impl {
Audio::Audio(): self(new Impl) {}
Audio::~Audio() {}
+bool Audio::isOpen() const {
+ boost::mutex::scoped_lock l(self->mutex);
+ return !self->streams.empty();
+}
+
void Audio::play(Sample const& s, std::string const& volumeSetting) {
}
void Audio::playMusic(std::map<std::string,std::string> const& filenames, bool preview, double fadeTime, double startPos) {
boost::mutex::scoped_lock l(self->mutex);
- self->disposing.clear();
- self->preloading.reset(new Music(filenames, 48000));
+ self->disposing.clear(); // Delete disposed streams
+ self->preloading.reset(new Music(filenames, getSR()));
Music& m = *self->preloading.get();
m.seek(startPos);
- m.fadeRate = fadeTime / 48000.0f;
+ m.fadeRate = 1.0 / getSR() / fadeTime;
}
void Audio::playMusic(std::string const& filename, bool preview, double fadeTime, double startPos) {
@@ -149,12 +154,12 @@ void Audio::fadeout(double fadeTime) {
double Audio::getPosition() const {
boost::mutex::scoped_lock l(self->mutex);
- return self->playing.empty() ? getNaN() : self->playing[0].pos();
+ return (self->playing.empty() || self->preloading.get()) ? getNaN() : self->playing[0].pos();
}
double Audio::getLength() const {
boost::mutex::scoped_lock l(self->mutex);
- return self->playing.empty() ? getNaN() : self->playing[0].duration();
+ return (self->playing.empty() || self->preloading.get()) ? getNaN() : self->playing[0].duration();
}
bool Audio::isPlaying() const {
diff --git a/game/audio.hh b/game/audio.hh
index 1585d23..ab310a8 100644
--- a/game/audio.hh
+++ b/game/audio.hh
@@ -23,17 +23,16 @@ public:
Audio();
~Audio();
boost::ptr_vector<Analyzer>& analyzers() { static boost::ptr_vector<Analyzer> ana; return ana; }
- /// if audio is currently playing
- bool isOpen() const { return false; }
+ bool isOpen() const;
/** Play a song beginning at startPos (defaults to 0)
* @param filename the track filename
* @param preview if the song preview is to play
* @param fadeTime time to fade
* @param startPos starting position
*/
- void playMusic(std::string const& filename, bool preview = false, double fadeTime = 0.5, double startPos = -0.2);
+ void playMusic(std::string const& filename, bool preview = false, double fadeTime = 0.5, double startPos = 0.0);
/// plays a list of songs
- void playMusic(std::map<std::string,std::string> const& filenames, bool preview = false, double fadeTime = 0.5, double startPos = -0.2);
+ void playMusic(std::map<std::string,std::string> const& filenames, bool preview = false, double fadeTime = 0.5, double startPos = 0.0);
/// plays a sample
void play(Sample const& s, std::string const& volumeSetting);
/// stops music
@@ -59,6 +58,6 @@ public:
bool isPaused() const;
void toggleSynth(Notes const& notes) { /*m_notes = (m_notes ? NULL : ¬es); */} ///< toggles synth playback
void streamFade(std::string stream_id, double level);
- unsigned int getSR() const { return 48000.0; }
+ double getSR() const { return 48000.0; }
};
diff --git a/game/ffmpeg.hh b/game/ffmpeg.hh
index 8836fde..246f436 100644
--- a/game/ffmpeg.hh
+++ b/game/ffmpeg.hh
@@ -140,6 +140,7 @@ class AudioBuffer {
bool prepare(int64_t pos) {
boost::mutex::scoped_try_lock l(m_mutex);
if (!l.owns_lock()) return false;
+ if (pos < 0) pos = 0;
m_posReq = pos;
return m_pos > m_posReq + m_data.capacity() / 4 && m_pos - m_data.size() <= m_pos;
}
|