2019-10-10 16:20:10 +00:00
|
|
|
#pragma once
|
2020-06-06 04:40:20 +00:00
|
|
|
#include "fifo_queue.h"
|
2019-10-10 16:20:10 +00:00
|
|
|
#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;
|
|
|
|
|
2020-06-06 04:40:20 +00:00
|
|
|
enum : u32
|
2019-10-10 16:20:10 +00:00
|
|
|
{
|
|
|
|
DefaultOutputSampleRate = 44100,
|
|
|
|
DefaultBufferSize = 2048,
|
2020-06-06 04:40:20 +00:00
|
|
|
MaxSamples = 32768,
|
|
|
|
FullVolume = 100
|
2019-10-10 16:20:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AudioStream();
|
|
|
|
virtual ~AudioStream();
|
|
|
|
|
|
|
|
u32 GetOutputSampleRate() const { return m_output_sample_rate; }
|
|
|
|
u32 GetChannels() const { return m_channels; }
|
|
|
|
u32 GetBufferSize() const { return m_buffer_size; }
|
2020-05-09 14:44:23 +00:00
|
|
|
s32 GetOutputVolume() const { return m_output_volume; }
|
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,
|
2020-06-06 04:40:20 +00:00
|
|
|
u32 buffer_size = DefaultBufferSize);
|
2019-10-11 03:24:41 +00:00
|
|
|
void SetSync(bool enable) { m_sync = enable; }
|
2020-06-06 04:40:20 +00:00
|
|
|
|
|
|
|
virtual void SetOutputVolume(u32 volume);
|
2019-10-10 16:20:10 +00:00
|
|
|
|
|
|
|
void PauseOutput(bool paused);
|
|
|
|
void EmptyBuffers();
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
void BeginWrite(SampleType** buffer_ptr, u32* num_frames);
|
|
|
|
void WriteFrames(const SampleType* frames, u32 num_frames);
|
|
|
|
void EndWrite(u32 num_frames);
|
2019-10-10 16:20:10 +00:00
|
|
|
|
2020-01-11 03:28:40 +00:00
|
|
|
static std::unique_ptr<AudioStream> CreateNullAudioStream();
|
|
|
|
|
2020-05-09 14:44:23 +00:00
|
|
|
// Latency computation - returns values in seconds
|
2020-06-06 04:40:20 +00:00
|
|
|
static float GetMaxLatency(u32 sample_rate, u32 buffer_size);
|
2020-05-09 14:44:23 +00:00
|
|
|
|
2019-10-10 16:20:10 +00:00
|
|
|
protected:
|
|
|
|
virtual bool OpenDevice() = 0;
|
|
|
|
virtual void PauseDevice(bool paused) = 0;
|
|
|
|
virtual void CloseDevice() = 0;
|
2020-06-06 04:40:20 +00:00
|
|
|
virtual void FramesAvailable() = 0;
|
2019-10-10 16:20:10 +00:00
|
|
|
|
2020-06-06 04:40:20 +00:00
|
|
|
ALWAYS_INLINE static SampleType ApplyVolume(SampleType sample, u32 volume)
|
2020-05-09 14:44:23 +00:00
|
|
|
{
|
2020-06-06 04:40:20 +00:00
|
|
|
return s16((s32(sample) * s32(volume)) / 100);
|
2020-05-09 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 04:40:20 +00:00
|
|
|
bool SetBufferSize(u32 buffer_size);
|
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;
|
2020-06-06 04:40:20 +00:00
|
|
|
u32 GetSamplesAvailableLocked() const;
|
|
|
|
void ReadFrames(SampleType* samples, u32 num_frames, bool apply_volume);
|
|
|
|
void DropFrames(u32 count);
|
2019-12-23 07:02:11 +00:00
|
|
|
|
2019-10-10 16:20:10 +00:00
|
|
|
u32 m_output_sample_rate = 0;
|
|
|
|
u32 m_channels = 0;
|
|
|
|
u32 m_buffer_size = 0;
|
|
|
|
|
2020-06-06 04:40:20 +00:00
|
|
|
// volume, 0-100
|
|
|
|
u32 m_output_volume = FullVolume;
|
2019-10-10 16:20:10 +00:00
|
|
|
|
2020-06-06 04:40:20 +00:00
|
|
|
private:
|
|
|
|
ALWAYS_INLINE u32 GetBufferSpace() const { return (m_max_samples - m_buffer.GetSize()); }
|
|
|
|
void EnsureBuffer(u32 size);
|
2019-10-10 16:20:10 +00:00
|
|
|
|
2020-06-06 04:40:20 +00:00
|
|
|
HeapFIFOQueue<SampleType, MaxSamples> m_buffer;
|
2020-01-07 04:17:41 +00:00
|
|
|
mutable std::mutex m_buffer_mutex;
|
2020-06-06 04:40:20 +00:00
|
|
|
std::condition_variable m_buffer_draining_cv;
|
|
|
|
std::vector<SampleType> m_resample_buffer;
|
|
|
|
u32 m_max_samples = 0;
|
2020-05-09 14:44:23 +00:00
|
|
|
|
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
|
|
|
};
|