2020-12-05 13:08:24 +01:00
|
|
|
#pragma once
|
2018-12-20 23:35:49 +01:00
|
|
|
|
2020-12-12 13:01:29 +01:00
|
|
|
#include "util/types.hpp"
|
2020-12-22 09:42:57 +01:00
|
|
|
#include "Utilities/StrFmt.h"
|
2018-12-20 23:35:49 +01:00
|
|
|
|
|
|
|
|
enum : u32
|
|
|
|
|
{
|
|
|
|
|
DEFAULT_AUDIO_SAMPLING_RATE = 48000,
|
|
|
|
|
MAX_AUDIO_BUFFERS = 64,
|
|
|
|
|
AUDIO_BUFFER_SAMPLES = 256
|
|
|
|
|
};
|
2015-01-11 00:46:10 +01:00
|
|
|
|
2018-12-16 18:40:50 +01:00
|
|
|
class AudioBackend
|
2015-01-11 00:46:10 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2018-12-16 18:40:50 +01:00
|
|
|
enum Capabilities : u32
|
|
|
|
|
{
|
2018-12-27 00:12:21 +01:00
|
|
|
PLAY_PAUSE_FLUSH = 0x1, // Implements Play, Pause, Flush
|
|
|
|
|
IS_PLAYING = 0x2, // Implements IsPlaying
|
|
|
|
|
GET_NUM_ENQUEUED_SAMPLES = 0x4, // Implements GetNumEnqueuedSamples
|
|
|
|
|
SET_FREQUENCY_RATIO = 0x8, // Implements SetFrequencyRatio
|
2018-12-16 18:40:50 +01:00
|
|
|
};
|
|
|
|
|
|
2020-06-20 02:44:32 +02:00
|
|
|
AudioBackend();
|
|
|
|
|
|
2018-12-16 18:40:50 +01:00
|
|
|
virtual ~AudioBackend() = default;
|
2015-01-11 00:46:10 +01:00
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
/*
|
|
|
|
|
* Pure virtual methods
|
|
|
|
|
*/
|
2018-12-16 18:40:50 +01:00
|
|
|
virtual const char* GetName() const = 0;
|
|
|
|
|
virtual u32 GetCapabilities() const = 0;
|
|
|
|
|
|
2018-12-21 02:16:54 +01:00
|
|
|
virtual void Open(u32 num_buffers) = 0;
|
2015-01-11 00:46:10 +01:00
|
|
|
virtual void Close() = 0;
|
2018-12-20 23:35:49 +01:00
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
virtual bool AddData(const void* src, u32 num_samples) = 0;
|
2018-12-16 18:40:50 +01:00
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
|
2020-02-18 20:42:55 +01:00
|
|
|
/*
|
|
|
|
|
* This virtual method should be reimplemented if backend can fail to be initialized under non-error conditions
|
|
|
|
|
* eg. when there is no audio devices attached
|
|
|
|
|
*/
|
|
|
|
|
virtual bool Initialized() const { return true; }
|
|
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
/*
|
|
|
|
|
* Virtual methods - should be implemented depending on backend capabilities
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Start playing enqueued data
|
|
|
|
|
// Should be implemented if capabilities & PLAY_PAUSE_FLUSH
|
|
|
|
|
virtual void Play()
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("Play() not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pause playing enqueued data
|
|
|
|
|
// Should be implemented if capabilities & PLAY_PAUSE_FLUSH
|
|
|
|
|
virtual void Pause()
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("Pause() not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pause audio, and unqueue all currently queued buffers
|
|
|
|
|
// Should be implemented if capabilities & PLAY_PAUSE_FLUSH
|
|
|
|
|
virtual void Flush()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
fmt::throw_exception("Flush() not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if audio is currently being played, false otherwise
|
|
|
|
|
// Should be implemented if capabilities & IS_PLAYING
|
2018-12-16 18:40:50 +01:00
|
|
|
virtual bool IsPlaying()
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("IsPlaying() not implemented");
|
2018-12-21 03:13:22 +01:00
|
|
|
}
|
2018-12-20 23:35:49 +01:00
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
// Returns the number of currently enqueued samples
|
|
|
|
|
// Should be implemented if capabilities & GET_NUM_ENQUEUED_SAMPLES
|
2018-12-16 18:40:50 +01:00
|
|
|
virtual u64 GetNumEnqueuedSamples()
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("GetNumEnqueuedSamples() not implemented");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
// Sets a new frequency ratio. Backend is allowed to modify the ratio value, e.g. clamping it to the allowed range
|
|
|
|
|
// Returns the new frequency ratio set
|
|
|
|
|
// Should be implemented if capabilities & SET_FREQUENCY_RATIO
|
2018-12-21 02:16:54 +01:00
|
|
|
virtual f32 SetFrequencyRatio(f32 /* new_ratio */) // returns the new ratio
|
2018-12-16 22:12:58 +01:00
|
|
|
{
|
|
|
|
|
fmt::throw_exception("SetFrequencyRatio() not implemented");
|
|
|
|
|
return 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 03:13:22 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Helper methods
|
|
|
|
|
*/
|
2020-06-20 02:44:32 +02:00
|
|
|
u32 get_sampling_rate() const;
|
2018-12-20 23:35:49 +01:00
|
|
|
|
2020-06-20 02:44:32 +02:00
|
|
|
u32 get_sample_size() const;
|
2018-12-20 23:35:49 +01:00
|
|
|
|
2020-06-20 02:44:32 +02:00
|
|
|
u32 get_channels() const;
|
2020-12-12 13:01:29 +01:00
|
|
|
|
2020-06-20 02:44:32 +02:00
|
|
|
bool get_convert_to_u16() const;
|
2018-12-16 22:12:58 +01:00
|
|
|
|
2020-02-15 23:36:20 +01:00
|
|
|
bool has_capability(u32 cap) const;
|
2018-12-16 22:12:58 +01:00
|
|
|
|
2020-02-15 23:36:20 +01:00
|
|
|
void dump_capabilities(std::string& out) const;
|
2020-06-20 02:44:32 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool m_convert_to_u16 = false;
|
|
|
|
|
u32 m_sample_size = sizeof(float);
|
|
|
|
|
u32 m_sampling_rate = DEFAULT_AUDIO_SAMPLING_RATE;
|
|
|
|
|
u32 m_channels = 0;
|
|
|
|
|
u32 m_start_threshold = 1;
|
2015-06-19 17:49:38 +02:00
|
|
|
};
|