Line data Source code
1 : #pragma once
2 :
3 : #include <SDL.h>
4 :
5 : #include "audio/backend/i_audio_backend.h"
6 :
7 : namespace Amplitron {
8 :
9 : class SdlBackend : public IAudioBackend {
10 : public:
11 : SdlBackend();
12 : ~SdlBackend() override;
13 :
14 : bool initialize(IAudioEngine* engine) override;
15 : void shutdown() override;
16 : bool start() override;
17 : void stop() override;
18 :
19 : std::vector<AudioDeviceInfo> get_input_devices() const override;
20 : std::vector<AudioDeviceInfo> get_output_devices() const override;
21 :
22 : bool set_input_device(int device_index) override;
23 : bool set_output_device(int device_index) override;
24 :
25 : std::string get_input_device_name() const override;
26 : std::string get_output_device_name() const override;
27 :
28 : int get_sample_rate() const override;
29 : int get_buffer_size() const override;
30 :
31 : /**
32 : * @brief Return the selected input device index.
33 : * Note: Unlike PortAudioBackend, SDL does not support setting custom input hardware
34 : * devices dynamically by index under all platforms, but the index is tracked here.
35 : */
36 0 : int get_input_device() const override { return selected_input_device_; }
37 :
38 : /**
39 : * @brief Return the selected output device index.
40 : * Note: Unlike PortAudioBackend, SDL does not support setting custom output hardware
41 : * devices dynamically by index under all platforms, but the index is tracked here.
42 : */
43 0 : int get_output_device() const override { return selected_output_device_; }
44 :
45 : // Callback helpers
46 0 : IAudioEngine* get_engine() const { return engine_; }
47 0 : SDL_AudioDeviceID get_capture_device() const { return capture_device_; }
48 0 : std::vector<float>& get_capture_buffer() { return capture_buffer_; }
49 :
50 : private:
51 : IAudioEngine* engine_ = nullptr;
52 : SDL_AudioDeviceID audio_device_ = 0;
53 : SDL_AudioDeviceID capture_device_ = 0;
54 : std::vector<float> capture_buffer_;
55 : bool initialized_ = false;
56 : bool running_ = false;
57 : int sample_rate_ = 48000;
58 : int buffer_size_ = 512;
59 : int selected_input_device_ = 0;
60 : int selected_output_device_ = 0;
61 : };
62 :
63 : } // namespace Amplitron
|