- Fixed a few bugs with ringbuffer, and some cleanup.

- Reworked audio system to use semaphores instead of events for waiting.
  Should fix rare issues where the XAudio2 driver would run out of buffers
  even though it was supposed to be guarded against that.
This commit is contained in:
gibbed
2015-06-19 21:48:51 -05:00
parent f3547a832f
commit edbd724370
8 changed files with 118 additions and 111 deletions

View File

@@ -14,60 +14,50 @@
namespace xe {
RingBuffer::RingBuffer(uint8_t* raw_buffer, size_t size, size_t read_offset, size_t write_offset)
: raw_buffer_(raw_buffer)
, size_(size)
, read_offset_(read_offset)
, write_offset_(write_offset) {}
RingBuffer::RingBuffer(uint8_t* buffer, size_t capacity)
: buffer_(buffer)
, capacity_(capacity)
, read_offset_(0)
, write_offset_(0) {}
size_t RingBuffer::Skip(size_t num_bytes) {
num_bytes = std::min(read_size(), num_bytes);
if (read_offset_ + num_bytes < size_) {
read_offset_ += num_bytes;
} else {
read_offset_ = num_bytes - (size_ - read_offset_);
}
return num_bytes;
}
size_t RingBuffer::Read(uint8_t* buffer, size_t num_bytes) {
num_bytes = std::min(read_size(), num_bytes);
if (!num_bytes) {
size_t RingBuffer::Read(uint8_t* buffer, size_t count) {
count = std::min(count, capacity_);
if (!count) {
return 0;
}
if (read_offset_ + num_bytes < size_) {
std::memcpy(buffer, raw_buffer_ + read_offset_, num_bytes);
read_offset_ += num_bytes;
if (read_offset_ + count < capacity_) {
std::memcpy(buffer, buffer_ + read_offset_, count);
read_offset_ += count;
} else {
size_t left_half = size_ - read_offset_;
size_t right_half = size_ - left_half;
std::memcpy(buffer, raw_buffer_ + read_offset_, left_half);
std::memcpy(buffer + left_half, raw_buffer_, right_half);
size_t left_half = capacity_ - read_offset_;
size_t right_half = count - left_half;
std::memcpy(buffer, buffer_ + read_offset_, left_half);
std::memcpy(buffer + left_half, buffer_, right_half);
read_offset_ = right_half;
}
return num_bytes;
return count;
}
size_t RingBuffer::Write(uint8_t* buffer, size_t num_bytes) {
num_bytes = std::min(num_bytes, write_size());
if (!num_bytes) {
size_t RingBuffer::Write(uint8_t* buffer, size_t count) {
count = std::min(count, capacity_);
if (!count) {
return 0;
}
if (write_offset_ + num_bytes < size_) {
std::memcpy(raw_buffer_ + write_offset_, buffer, num_bytes);
write_offset_ += num_bytes;
if (write_offset_ + count < capacity_) {
std::memcpy(buffer_ + write_offset_, buffer, count);
write_offset_ += count;
} else {
size_t left_half = size_ - write_offset_;
size_t right_half = size_ - left_half;
std::memcpy(raw_buffer_ + write_offset_, buffer, left_half);
std::memcpy(raw_buffer_, buffer + left_half, right_half);
size_t left_half = capacity_ - write_offset_;
size_t right_half = count - left_half;
std::memcpy(buffer_ + write_offset_, buffer, left_half);
std::memcpy(buffer_, buffer + left_half, right_half);
write_offset_ = right_half;
}
return num_bytes;
return count;
}
} // namespace xe