-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaudioengine.cpp
More file actions
144 lines (116 loc) · 4.44 KB
/
audioengine.cpp
File metadata and controls
144 lines (116 loc) · 4.44 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "audioengine.h"
#include "vendor/elementary/runtime/elem/AudioBufferResource.h"
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
namespace elementary {
AudioEngine::AudioEngine() : deviceConfig(ma_device_config_init(ma_device_type_playback)), deviceInitialized(false) {
initializeDevice();
}
AudioEngine::~AudioEngine() {
if (deviceInitialized) {
ma_device_uninit(&device);
}
}
elem::Runtime<float>& AudioEngine::getRuntime() {
return proxy->runtime;
}
int AudioEngine::getSampleRate() {
return device.sampleRate;
}
int AudioEngine::getNumChannels() {
return deviceInitialized ? static_cast<int>(device.playback.channels) : 0;
}
bool AudioEngine::isDeviceRunning() {
if (!deviceInitialized) return false;
return ma_device_get_state(&device) == ma_device_state_started;
}
void AudioEngine::stopDevice() {
if (deviceInitialized) {
proxy->muted.store(true, std::memory_order_relaxed);
ma_device_stop(&device);
}
}
void AudioEngine::startDevice() {
if (!deviceInitialized) return;
proxy->muted.store(false, std::memory_order_relaxed);
ma_result result = ma_device_start(&device);
if (result != MA_SUCCESS) {
// Device start failed — reinitialize
ma_device_uninit(&device);
deviceInitialized = false;
deviceConfig.pUserData = proxy.get();
result = ma_device_init(nullptr, &deviceConfig, &device);
if (result == MA_SUCCESS) {
deviceInitialized = true;
ma_device_start(&device);
}
}
}
AudioLoadResult AudioEngine::loadAudioResource(const std::string& key, const std::string& filePath) {
AudioLoadResult result = AudioResourceLoader::loadFile(key, filePath);
if (!result.success) {
return result;
}
size_t numChannels = result.info.channels;
size_t numSamples = result.info.sampleCount;
std::vector<float*> channelPtrs(numChannels);
for (size_t ch = 0; ch < numChannels; ++ch) {
channelPtrs[ch] = result.data.data() + (ch * numSamples);
}
auto resource = std::make_unique<elem::AudioBufferResource>(
channelPtrs.data(),
numChannels,
numSamples
);
bool added = proxy->runtime.addSharedResource(key, std::move(resource));
if (!added) {
result.success = false;
result.error = "Resource with key '" + key + "' already exists";
return result;
}
{
std::lock_guard<std::mutex> lock(resourceMutex);
loadedResources.insert(key);
}
return result;
}
bool AudioEngine::unloadAudioResource(const std::string& key) {
bool found = false;
// Only hold the lock while modifying loadedResources
{
std::lock_guard<std::mutex> lock(resourceMutex);
auto it = loadedResources.find(key);
if (it != loadedResources.end()) {
loadedResources.erase(it);
found = true;
}
}
if (found) {
proxy->runtime.pruneSharedResources();
}
return found;
}
void AudioEngine::initializeDevice() {
proxy = std::make_unique<DeviceProxy>(44100.0, 1024);
deviceConfig.playback.pDeviceID = nullptr;
deviceConfig.playback.format = ma_format_f32;
deviceConfig.playback.channels = 2;
deviceConfig.sampleRate = 44100;
deviceConfig.dataCallback = audioCallback;
deviceConfig.pUserData = proxy.get();
ma_result result = ma_device_init(nullptr, &deviceConfig, &device);
if (result != MA_SUCCESS) {
std::cerr << "Failed to start the audio device! Exiting..." << std::endl;
return;
}
deviceInitialized = true;
ma_device_start(&device);
}
// Audio callback function
void AudioEngine::audioCallback(ma_device* pDevice, void* pOutput, const void* /* pInput */, ma_uint32 frameCount) {
auto* proxy = static_cast<DeviceProxy*>(pDevice->pUserData);
auto numChannels = static_cast<size_t>(pDevice->playback.channels);
auto numFrames = static_cast<size_t>(frameCount);
proxy->process(static_cast<float*>(pOutput), numChannels, numFrames);
}
}