Duckstation/src/common/audio_stream.h

91 lines
2.4 KiB
C
Raw Normal View History

2019-10-10 16:20:10 +00:00
#pragma once
#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;
enum : u32
2019-10-10 16:20:10 +00:00
{
DefaultOutputSampleRate = 44100,
DefaultBufferSize = 2048,
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; }
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,
u32 buffer_size = DefaultBufferSize);
2019-10-11 03:24:41 +00:00
void SetSync(bool enable) { m_sync = enable; }
virtual void SetOutputVolume(u32 volume);
2019-10-10 16:20:10 +00:00
void PauseOutput(bool paused);
void EmptyBuffers();
void Shutdown();
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();
// Latency computation - returns values in seconds
static float GetMaxLatency(u32 sample_rate, u32 buffer_size);
2019-10-10 16:20:10 +00:00
protected:
virtual bool OpenDevice() = 0;
virtual void PauseDevice(bool paused) = 0;
virtual void CloseDevice() = 0;
virtual void FramesAvailable() = 0;
2019-10-10 16:20:10 +00:00
ALWAYS_INLINE static SampleType ApplyVolume(SampleType sample, u32 volume)
{
return s16((s32(sample) * s32(volume)) / 100);
}
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;
u32 GetSamplesAvailableLocked() const;
void ReadFrames(SampleType* samples, u32 num_frames, bool apply_volume);
void DropFrames(u32 count);
2019-10-10 16:20:10 +00:00
u32 m_output_sample_rate = 0;
u32 m_channels = 0;
u32 m_buffer_size = 0;
// volume, 0-100
u32 m_output_volume = FullVolume;
2019-10-10 16:20:10 +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
HeapFIFOQueue<SampleType, MaxSamples> m_buffer;
2020-01-07 04:17:41 +00:00
mutable std::mutex m_buffer_mutex;
std::condition_variable m_buffer_draining_cv;
std::vector<SampleType> m_resample_buffer;
u32 m_max_samples = 0;
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
};