aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic
diff options
context:
space:
mode:
authorPaul Gilbert2017-09-16 15:23:43 -0400
committerPaul Gilbert2017-09-16 15:23:43 -0400
commit3e5515cc21d8e898fd69464643255d135c5b942d (patch)
tree2f0c9351f8fdfb517d4e743d54f7d7e60f5e6aa4 /engines/titanic
parent09931ae0aa11aafa8ad6324a92fafd1fbcd678af (diff)
downloadscummvm-rg350-3e5515cc21d8e898fd69464643255d135c5b942d.tar.gz
scummvm-rg350-3e5515cc21d8e898fd69464643255d135c5b942d.tar.bz2
scummvm-rg350-3e5515cc21d8e898fd69464643255d135c5b942d.zip
TITANIC: Improve audio buffer critial section handling
Diffstat (limited to 'engines/titanic')
-rw-r--r--engines/titanic/sound/audio_buffer.cpp23
-rw-r--r--engines/titanic/sound/audio_buffer.h19
-rw-r--r--engines/titanic/sound/music_room_handler.cpp4
-rw-r--r--engines/titanic/sound/wave_file.cpp9
4 files changed, 35 insertions, 20 deletions
diff --git a/engines/titanic/sound/audio_buffer.cpp b/engines/titanic/sound/audio_buffer.cpp
index 0b0b6a53ad..0e1abc4213 100644
--- a/engines/titanic/sound/audio_buffer.cpp
+++ b/engines/titanic/sound/audio_buffer.cpp
@@ -34,16 +34,37 @@ void CAudioBuffer::reset() {
}
void CAudioBuffer::push(int16 value) {
+ enterCriticalSection();
_data.push(value);
+ leaveCriticalSection();
}
void CAudioBuffer::push(const int16 *values, int count) {
+ enterCriticalSection();
+
for (; count > 0; --count, ++values)
_data.push(*values);
+
+ leaveCriticalSection();
}
int16 CAudioBuffer::pop() {
- return _data.pop();
+ enterCriticalSection();
+ int16 value = _data.pop();
+ leaveCriticalSection();
+
+ return value;
+}
+
+int CAudioBuffer::read(int16 *values, int count) {
+ enterCriticalSection();
+
+ int bytesRead = 0;
+ for (; count > 0 && !_data.empty(); --count, ++bytesRead)
+ *values++ = _data.pop();
+
+ leaveCriticalSection();
+ return bytesRead;
}
void CAudioBuffer::enterCriticalSection() {
diff --git a/engines/titanic/sound/audio_buffer.h b/engines/titanic/sound/audio_buffer.h
index ad8c3e4064..c775c5bb35 100644
--- a/engines/titanic/sound/audio_buffer.h
+++ b/engines/titanic/sound/audio_buffer.h
@@ -34,6 +34,16 @@ class CAudioBuffer {
private:
Common::Mutex _mutex;
FixedQueue<int16, AUDIO_SAMPLING_RATE * 4> _data;
+private:
+ /**
+ * Enters a critical section
+ */
+ void enterCriticalSection();
+
+ /**
+ * Leave a critical section
+ */
+ void leaveCriticalSection();
public:
bool _finished;
public:
@@ -80,14 +90,9 @@ public:
int16 pop();
/**
- * Enters a critical section
- */
- void enterCriticalSection();
-
- /**
- * Leave a critical section
+ * Reads out a specified number of samples
*/
- void leaveCriticalSection();
+ int read(int16 *values, int count);
};
} // End of namespace Titanic
diff --git a/engines/titanic/sound/music_room_handler.cpp b/engines/titanic/sound/music_room_handler.cpp
index dbbe4e4ca8..ffc90ffca6 100644
--- a/engines/titanic/sound/music_room_handler.cpp
+++ b/engines/titanic/sound/music_room_handler.cpp
@@ -202,8 +202,6 @@ bool CMusicRoomHandler::update() {
}
void CMusicRoomHandler::updateAudio() {
- _audioBuffer->enterCriticalSection();
-
int size = _audioBuffer->freeSize();
int count;
int16 *ptr;
@@ -234,8 +232,6 @@ void CMusicRoomHandler::updateAudio() {
_audioBuffer->push(audioData, size);
delete[] audioData;
}
-
- _audioBuffer->leaveCriticalSection();
}
void CMusicRoomHandler::updateInstruments() {
diff --git a/engines/titanic/sound/wave_file.cpp b/engines/titanic/sound/wave_file.cpp
index e57e9f0ee2..ba895088ad 100644
--- a/engines/titanic/sound/wave_file.cpp
+++ b/engines/titanic/sound/wave_file.cpp
@@ -48,14 +48,7 @@ public:
};
int AudioBufferStream::readBuffer(int16 *buffer, const int numSamples) {
- _audioBuffer->enterCriticalSection();
- int samplesToRead = MIN((const int)numSamples, (const int)_audioBuffer->size());
-
- for (int idx = 0; idx < samplesToRead; ++idx)
- *buffer++ = _audioBuffer->pop();
-
- _audioBuffer->leaveCriticalSection();
- return samplesToRead;
+ return _audioBuffer->read(buffer, numSamples);
}
bool AudioBufferStream::endOfData() const {