2019-10-10 16:20:10 +00:00
|
|
|
#pragma once
|
|
|
|
#include "types.h"
|
2019-10-11 03:24:41 +00:00
|
|
|
#include <condition_variable>
|
2019-10-10 16:20:10 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Uses signed 16-bits samples.
|
|
|
|
|
|
|
|
class AudioStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using SampleType = s16;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
DefaultOutputSampleRate = 44100,
|
|
|
|
DefaultBufferSize = 2048,
|
|
|
|
DefaultBufferCount = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioStream();
|
|
|
|
virtual ~AudioStream();
|
|
|
|
|
|
|
|
u32 GetOutputSampleRate() const { return m_output_sample_rate; }
|
|
|
|
u32 GetChannels() const { return m_channels; }
|
|
|
|
u32 GetBufferSize() const { return m_buffer_size; }
|
|
|
|
u32 GetBufferCount() const { return static_cast<u32>(m_buffers.size()); }
|
2019-10-11 03:24:41 +00:00
|
|
|
bool IsSyncing() const { return m_sync; }
|
2019-10-10 16:20:10 +00:00
|
|
|
|
|
|
|
bool Reconfigure(u32 output_sample_rate = DefaultOutputSampleRate, u32 channels = 1,
|
|
|
|
u32 buffer_size = DefaultBufferSize, u32 buffer_count = DefaultBufferCount);
|
2019-10-11 03:24:41 +00:00
|
|
|
void SetSync(bool enable) { m_sync = enable; }
|
2019-10-10 16:20:10 +00:00
|
|
|
|
|
|
|
void PauseOutput(bool paused);
|
|
|
|
void EmptyBuffers();
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
void BeginWrite(SampleType** buffer_ptr, u32* num_samples);
|
|
|
|
void WriteSamples(const SampleType* samples, u32 num_samples);
|
|
|
|
void EndWrite(u32 num_samples);
|
|
|
|
|
2020-01-11 03:28:40 +00:00
|
|
|
static std::unique_ptr<AudioStream> CreateNullAudioStream();
|
|
|
|
|
|
|
|
static std::unique_ptr<AudioStream> CreateCubebAudioStream();
|
|
|
|
|
2019-10-10 16:20:10 +00:00
|
|
|
protected:
|
|
|
|
virtual bool OpenDevice() = 0;
|
|
|
|
virtual void PauseDevice(bool paused) = 0;
|
|
|
|
virtual void CloseDevice() = 0;
|
2019-12-23 07:02:11 +00:00
|
|
|
virtual void BufferAvailable() = 0;
|
2019-10-10 16:20:10 +00:00
|
|
|
|
|
|
|
bool IsDeviceOpen() const { return (m_output_sample_rate > 0); }
|
|
|
|
|
2020-01-07 04:17:41 +00:00
|
|
|
u32 GetSamplesAvailable() const;
|
2019-10-10 16:20:10 +00:00
|
|
|
u32 ReadSamples(SampleType* samples, u32 num_samples);
|
|
|
|
|
2019-12-23 07:02:11 +00:00
|
|
|
void DropBuffer();
|
|
|
|
|
2019-10-10 16:20:10 +00:00
|
|
|
u32 m_output_sample_rate = 0;
|
|
|
|
u32 m_channels = 0;
|
|
|
|
u32 m_buffer_size = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Buffer
|
|
|
|
{
|
|
|
|
std::vector<SampleType> data;
|
|
|
|
u32 write_position;
|
|
|
|
u32 read_position;
|
|
|
|
};
|
|
|
|
|
|
|
|
void AllocateBuffers(u32 buffer_count);
|
2019-10-11 03:24:41 +00:00
|
|
|
void EnsureBuffer();
|
2019-10-10 16:20:10 +00:00
|
|
|
|
|
|
|
std::vector<Buffer> m_buffers;
|
2020-01-07 04:17:41 +00:00
|
|
|
mutable std::mutex m_buffer_mutex;
|
2019-10-10 16:20:10 +00:00
|
|
|
|
|
|
|
// For input.
|
|
|
|
u32 m_first_free_buffer = 0;
|
|
|
|
u32 m_num_free_buffers = 0;
|
|
|
|
|
|
|
|
// For output.
|
|
|
|
u32 m_num_available_buffers = 0;
|
|
|
|
u32 m_first_available_buffer = 0;
|
|
|
|
|
2019-10-11 03:24:41 +00:00
|
|
|
// TODO: Switch to semaphore
|
|
|
|
std::condition_variable m_buffer_available_cv;
|
|
|
|
|
2019-10-10 16:20:10 +00:00
|
|
|
bool m_output_paused = true;
|
2019-10-11 03:24:41 +00:00
|
|
|
bool m_sync = true;
|
2019-10-10 16:20:10 +00:00
|
|
|
};
|