| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <atomic> |
| 7 |
#include <condition_variable> |
| 8 |
#include <mutex> |
| 9 |
#include <thread> |
| 10 |
|
| 11 |
#if defined(HAVE_ALSA) && HAVE_ALSA |
| 12 |
#include <alsa/asoundlib.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "AudioCommon/SoundStream.h" |
| 16 |
#include "Common/CommonTypes.h" |
| 17 |
|
| 18 |
class AlsaSound final : public SoundStream |
| 19 |
{ |
| 20 |
#if defined(HAVE_ALSA) && HAVE_ALSA |
| 21 |
public: |
| 22 |
AlsaSound(); |
| 23 |
~AlsaSound() override; |
| 24 |
|
| 25 |
bool Init() override; |
| 26 |
bool SetRunning(bool running) override; |
| 27 |
|
| 28 |
static bool IsValid() { return true; } |
| 29 |
|
| 30 |
private: |
| 31 |
void SoundLoop(); |
| 32 |
|
| 33 |
|
| 34 |
static constexpr size_t BUFFER_SIZE_MAX = 8192; |
| 35 |
|
| 36 |
|
| 37 |
static constexpr u32 FRAME_COUNT_MIN = 256; |
| 38 |
|
| 39 |
|
| 40 |
static constexpr u32 CHANNEL_COUNT = 2; |
| 41 |
|
| 42 |
enum class ALSAThreadStatus |
| 43 |
{ |
| 44 |
RUNNING, |
| 45 |
PAUSED, |
| 46 |
STOPPING, |
| 47 |
STOPPED, |
| 48 |
}; |
| 49 |
|
| 50 |
bool AlsaInit(); |
| 51 |
void AlsaShutdown(); |
| 52 |
|
| 53 |
s16 mix_buffer[BUFFER_SIZE_MAX * CHANNEL_COUNT]; |
| 54 |
std::thread thread; |
| 55 |
std::atomic<ALSAThreadStatus> m_thread_status; |
| 56 |
std::condition_variable cv; |
| 57 |
std::mutex cv_m; |
| 58 |
|
| 59 |
snd_pcm_t* handle; |
| 60 |
unsigned int frames_to_deliver; |
| 61 |
#endif |
| 62 |
}; |