AlsaSoundStream.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <condition_variable>
  6. #include <mutex>
  7. #include <thread>
  8. #if defined(HAVE_ALSA) && HAVE_ALSA
  9. #include <alsa/asoundlib.h>
  10. #endif
  11. #include "AudioCommon/SoundStream.h"
  12. #include "Common/CommonTypes.h"
  13. class AlsaSound final : public SoundStream
  14. {
  15. #if defined(HAVE_ALSA) && HAVE_ALSA
  16. public:
  17. AlsaSound();
  18. ~AlsaSound() override;
  19. bool Init() override;
  20. bool SetRunning(bool running) override;
  21. static bool IsValid() { return true; }
  22. private:
  23. void SoundLoop();
  24. // maximum number of frames the buffer can hold
  25. static constexpr size_t BUFFER_SIZE_MAX = 8192;
  26. // minimum number of frames to deliver in one transfer
  27. static constexpr u32 FRAME_COUNT_MIN = 256;
  28. // number of channels per frame
  29. static constexpr u32 CHANNEL_COUNT = 2;
  30. enum class ALSAThreadStatus
  31. {
  32. RUNNING,
  33. PAUSED,
  34. STOPPING,
  35. STOPPED,
  36. };
  37. bool AlsaInit();
  38. void AlsaShutdown();
  39. s16 mix_buffer[BUFFER_SIZE_MAX * CHANNEL_COUNT];
  40. std::thread thread;
  41. std::atomic<ALSAThreadStatus> m_thread_status;
  42. std::condition_variable cv;
  43. std::mutex cv_m;
  44. snd_pcm_t* handle;
  45. unsigned int frames_to_deliver;
  46. #endif
  47. };