yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
sdl3_audio_backend.h
Go to the documentation of this file.
1// sdl3_audio_backend.h - SDL3 Audio Backend Implementation
2// Stream-based audio implementation for SDL3
3
4#ifndef YAZE_APP_EMU_AUDIO_SDL3_AUDIO_BACKEND_H
5#define YAZE_APP_EMU_AUDIO_SDL3_AUDIO_BACKEND_H
6
7#ifdef YAZE_USE_SDL3
8
9#include <SDL3/SDL.h>
10#include <SDL3/SDL_audio.h>
11
12#include <atomic>
13#include <cstdint>
14#include <memory>
15#include <string>
16#include <vector>
17
19
20namespace yaze {
21namespace emu {
22namespace audio {
23
31class SDL3AudioBackend : public IAudioBackend {
32 public:
33 SDL3AudioBackend() = default;
34 ~SDL3AudioBackend() override;
35
36 // Initialization
37 bool Initialize(const AudioConfig& config) override;
38 void Shutdown() override;
39
40 // Playback control
41 void Play() override;
42 void Pause() override;
43 void Stop() override;
44 void Clear() override;
45
46 // Audio data
47 bool QueueSamples(const int16_t* samples, int num_samples) override;
48 bool QueueSamples(const float* samples, int num_samples) override;
49 bool QueueSamplesNative(const int16_t* samples, int frames_per_channel,
50 int channels, int native_rate) override;
51
52 // Status queries
53 AudioStatus GetStatus() const override;
54 bool IsInitialized() const override;
55 AudioConfig GetConfig() const override;
56
57 // Volume control (0.0 to 1.0)
58 void SetVolume(float volume) override;
59 float GetVolume() const override;
60
61 // SDL3 supports audio stream resampling natively
62 void SetAudioStreamResampling(bool enable, int native_rate,
63 int channels) override;
64 bool SupportsAudioStream() const override { return true; }
65
66 // Backend identification
67 std::string GetBackendName() const override { return "SDL3"; }
68
69 private:
70 // Helper functions
71 bool ApplyVolume(int16_t* samples, int num_samples) const;
72 bool ApplyVolume(float* samples, int num_samples) const;
73
74 // SDL3 audio stream - primary interface for audio output
75 SDL_AudioStream* audio_stream_ = nullptr;
76
77 // Resampling stream for native rate support
78 SDL_AudioStream* resampling_stream_ = nullptr;
79
80 // Audio device ID
81 SDL_AudioDeviceID device_id_ = 0;
82
83 // Configuration
84 AudioConfig config_;
85
86 // State
87 std::atomic<bool> initialized_{false};
88 std::atomic<float> volume_{1.0f};
89
90 // Resampling configuration
91 bool resampling_enabled_ = false;
92 int native_rate_ = 0;
93 int native_channels_ = 0;
94
95 // Buffer for resampling operations
96 std::vector<int16_t> resample_buffer_;
97
98 // Format information
99 SDL_AudioFormat device_format_ = SDL_AUDIO_S16;
100 int device_channels_ = 2;
101 int device_freq_ = 48000;
102};
103
104} // namespace audio
105} // namespace emu
106} // namespace yaze
107
108#endif // YAZE_USE_SDL3
109
110#endif // YAZE_APP_EMU_AUDIO_SDL3_AUDIO_BACKEND_H