1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
// Copyright 2016 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <string>
#include <vector>
#include <SDL.h>
#include "audio_core/audio_types.h"
#include "audio_core/sdl2_sink.h"
#include "common/assert.h"
#include "common/logging/log.h"
namespace AudioCore {
struct SDL2Sink::Impl {
unsigned int sample_rate = 0;
SDL_AudioDeviceID audio_device_id = 0;
std::function<void(s16*, std::size_t)> cb;
static void Callback(void* impl_, u8* buffer, int buffer_size_in_bytes);
};
SDL2Sink::SDL2Sink(std::string device_name) : impl(std::make_unique<Impl>()) {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
LOG_CRITICAL(Audio_Sink, "SDL_Init(SDL_INIT_AUDIO) failed with: {}", SDL_GetError());
impl->audio_device_id = 0;
return;
}
SDL_AudioSpec desired_audiospec;
SDL_zero(desired_audiospec);
desired_audiospec.format = AUDIO_S16;
desired_audiospec.channels = 2;
desired_audiospec.freq = native_sample_rate;
desired_audiospec.samples = 512;
desired_audiospec.userdata = impl.get();
desired_audiospec.callback = &Impl::Callback;
SDL_AudioSpec obtained_audiospec;
SDL_zero(obtained_audiospec);
const char* device = nullptr;
if (device_name != auto_device_name && !device_name.empty()) {
device = device_name.c_str();
}
impl->audio_device_id =
SDL_OpenAudioDevice(device, false, &desired_audiospec, &obtained_audiospec, 0);
if (impl->audio_device_id <= 0) {
LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed with code {} for device \"{}\"",
impl->audio_device_id, device_name);
return;
}
impl->sample_rate = obtained_audiospec.freq;
// SDL2 audio devices start out paused, unpause it:
SDL_PauseAudioDevice(impl->audio_device_id, 0);
}
SDL2Sink::~SDL2Sink() {
if (impl->audio_device_id <= 0)
return;
SDL_CloseAudioDevice(impl->audio_device_id);
}
unsigned int SDL2Sink::GetNativeSampleRate() const {
if (impl->audio_device_id <= 0)
return native_sample_rate;
return impl->sample_rate;
}
void SDL2Sink::SetCallback(std::function<void(s16*, std::size_t)> cb) {
impl->cb = cb;
}
void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) {
Impl* impl = reinterpret_cast<Impl*>(impl_);
if (!impl || !impl->cb)
return;
const std::size_t num_frames = buffer_size_in_bytes / (2 * sizeof(s16));
impl->cb(reinterpret_cast<s16*>(buffer), num_frames);
}
std::vector<std::string> ListSDL2SinkDevices() {
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem failed with: {}", SDL_GetError());
return {};
}
std::vector<std::string> device_list;
const int device_count = SDL_GetNumAudioDevices(0);
for (int i = 0; i < device_count; ++i) {
device_list.push_back(SDL_GetAudioDeviceName(i, 0));
}
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return device_list;
}
} // namespace AudioCore
|