-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdevice_io.cpp
More file actions
326 lines (263 loc) Β· 10.9 KB
/
device_io.cpp
File metadata and controls
326 lines (263 loc) Β· 10.9 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "core/atkaudio/DeviceIo/DeviceIo.h"
#include <atomic>
#include <obs-frontend-api.h>
#include <obs-module.h>
#define FILTER_NAME "atkAudio DeviceIo"
#define FILTER_ID "atkaudio_device_io"
#define OPEN_DEVICE_SETTINGS "open_device_settings"
#define OPEN_DEVICE_TEXT "Open Device Settings"
#define CLOSE_DEVICE_SETTINGS "close_device_settings"
#define CLOSE_DEVICE_TEXT "Close Device Settings"
#define MIX_INPUT_TEXT "Mix Input"
#define S_MIX_INPUT "mix_input"
#define IG_ID "input_gain"
#define OG_ID "output_gain"
#define IG_NAME "Input Gain"
#define OG_NAME "Output Gain"
#define FOLLOW_ID "follow_source_volume"
#define FOLLOW_NAME "Follow Source Volume/Mute"
#define FOLLOW_SCENE_ID "follow_scene"
#define FOLLOW_SCENE_NAME "Follow Scene"
#define OUTPUT_DELAY_ID "output_delay"
#define OUTPUT_DELAY_NAME "Output Delay"
struct adio_data
{
obs_source_t* context = nullptr;
obs_data_t* settings = nullptr;
int channels = 0;
double sampleRate = 0.0;
std::atomic_bool mixInput = false;
std::atomic_bool followSourceVolume = false;
std::atomic_bool followScene = true;
std::atomic<float> inputGain = 1.0f;
std::atomic<float> outputGain = 1.0f;
std::atomic<float> outputDelay = 0.0f;
std::atomic<double> fadeTimeSeconds = 0.5;
std::atomic_bool shouldBypass = false;
std::unique_ptr<atk::DeviceIo> deviceIo;
bool hasLoadedState = false;
};
static const char* devio_name(void* unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text(FILTER_NAME);
}
static void devio_destroy(void* data)
{
struct adio_data* adio = (struct adio_data*)data;
delete adio;
}
static void load(void* data, obs_data_t* settings)
{
auto* adio = (struct adio_data*)data;
if (adio->hasLoadedState)
return;
adio->hasLoadedState = true;
const char* chunkData = obs_data_get_string(settings, FILTER_ID);
std::string stateStr = chunkData ? chunkData : "";
adio->deviceIo->setState(stateStr);
}
static void devio_update(void* data, obs_data_t* s)
{
struct adio_data* adio = (struct adio_data*)data;
adio->settings = s;
adio->channels = (int)audio_output_get_channels(obs_get_audio());
adio->mixInput.store(obs_data_get_bool(s, S_MIX_INPUT), std::memory_order_release);
adio->followSourceVolume.store(obs_data_get_bool(s, FOLLOW_ID), std::memory_order_release);
adio->followScene.store(obs_data_get_bool(s, FOLLOW_SCENE_ID), std::memory_order_release);
auto inputGain = (float)obs_data_get_double(s, IG_ID);
inputGain = obs_db_to_mul(inputGain);
adio->inputGain.store(inputGain, std::memory_order_release);
auto outputDelay = (float)obs_data_get_double(s, OUTPUT_DELAY_ID);
adio->outputDelay.store(outputDelay, std::memory_order_release);
adio->deviceIo->setOutputDelay(outputDelay);
// auto outputGain = (float)obs_data_get_double(s, OG_ID);
// outputGain = obs_db_to_mul(outputGain);
// adio->outputGain.store(outputGain, std::memory_order_release);
}
static void* devio_create(obs_data_t* settings, obs_source_t* filter)
{
struct adio_data* adio = new adio_data();
adio->context = filter;
auto numChannels = (int)audio_output_get_channels(obs_get_audio());
auto sampleRate = audio_output_get_sample_rate(obs_get_audio());
adio->channels = numChannels;
adio->sampleRate = sampleRate;
adio->deviceIo = std::make_unique<atk::DeviceIo>();
devio_update(adio, settings);
// Load state from settings if present (OBS load callback may not be called for all source types)
const char* chunkData = obs_data_get_string(settings, FILTER_ID);
if (chunkData && strlen(chunkData) > 0)
{
std::string stateStr = chunkData;
adio->deviceIo->setState(stateStr);
adio->hasLoadedState = true;
}
return adio;
}
static void devio_defaults(obs_data_t* s)
{
obs_data_set_default_bool(s, S_MIX_INPUT, false);
obs_data_set_default_bool(s, FOLLOW_ID, false);
obs_data_set_default_bool(s, FOLLOW_SCENE_ID, true);
obs_data_set_default_double(s, IG_ID, 0.0);
obs_data_set_default_double(s, OG_ID, 0.0);
obs_data_set_default_double(s, OUTPUT_DELAY_ID, 0.0);
}
static bool open_editor_button_clicked(obs_properties_t* props, obs_property_t* property, void* data)
{
obs_property_set_visible(obs_properties_get(props, OPEN_DEVICE_SETTINGS), false);
obs_property_set_visible(obs_properties_get(props, CLOSE_DEVICE_SETTINGS), true);
adio_data* adio = (adio_data*)data;
adio->deviceIo->setVisible(true);
return true;
}
static bool close_editor_button_clicked(obs_properties_t* props, obs_property_t* property, void* data)
{
obs_property_set_visible(obs_properties_get(props, OPEN_DEVICE_SETTINGS), true);
obs_property_set_visible(obs_properties_get(props, CLOSE_DEVICE_SETTINGS), false);
adio_data* adio = (adio_data*)data;
adio->deviceIo->setVisible(false);
return true;
}
static obs_properties_t* devio_properties(void* data)
{
obs_properties_t* props = obs_properties_create();
obs_properties_add_button(props, OPEN_DEVICE_SETTINGS, OPEN_DEVICE_TEXT, open_editor_button_clicked);
obs_properties_add_button(props, CLOSE_DEVICE_SETTINGS, CLOSE_DEVICE_TEXT, close_editor_button_clicked);
bool open_settings_vis = true;
bool close_settings_vis = false;
obs_property_set_visible(obs_properties_get(props, OPEN_DEVICE_SETTINGS), open_settings_vis);
obs_property_set_visible(obs_properties_get(props, CLOSE_DEVICE_SETTINGS), close_settings_vis);
obs_properties_add_bool(props, S_MIX_INPUT, obs_module_text(MIX_INPUT_TEXT));
std::string propText = FOLLOW_ID;
std::string textLabel = FOLLOW_NAME;
obs_properties_add_bool(props, propText.c_str(), textLabel.c_str());
obs_properties_add_bool(props, FOLLOW_SCENE_ID, FOLLOW_SCENE_NAME);
propText = IG_ID;
textLabel = IG_NAME;
obs_property_t* p = obs_properties_add_float_slider(props, propText.c_str(), textLabel.c_str(), -30.0, 30.0, 0.1);
obs_property_float_set_suffix(p, " dB");
propText = OG_ID;
textLabel = OG_NAME;
p = obs_properties_add_float_slider(props, propText.c_str(), textLabel.c_str(), -30.0, 30.0, 0.1);
obs_property_float_set_suffix(p, " dB");
propText = OUTPUT_DELAY_ID;
textLabel = OUTPUT_DELAY_NAME;
p = obs_properties_add_float_slider(props, propText.c_str(), textLabel.c_str(), 0.0, 10000.0, 0.1);
obs_property_float_set_suffix(p, " ms");
UNUSED_PARAMETER(data);
return props;
}
static struct obs_audio_data* devio_filter(void* data, struct obs_audio_data* audio)
{
struct adio_data* adio = (struct adio_data*)data;
auto channels = adio->channels;
auto frames = audio->frames;
float** adata = (float**)audio->data;
auto outputGain = adio->outputGain.load(std::memory_order_acquire);
for (int i = 0; i < channels; i++)
for (size_t j = 0; j < frames; j++)
adata[i][j] *= outputGain;
if (adio->followScene.load(std::memory_order_acquire))
{
adio->deviceIo->setFadeTime(adio->fadeTimeSeconds.load(std::memory_order_acquire));
adio->deviceIo->setBypass(adio->shouldBypass.load(std::memory_order_acquire));
}
else
{
adio->deviceIo->setBypass(false);
}
adio->deviceIo->setMixInput(adio->mixInput.load(std::memory_order_acquire));
adio->deviceIo->process(adata, channels, frames, adio->sampleRate);
auto inputGain = adio->inputGain.load(std::memory_order_acquire);
for (int i = 0; i < channels; i++)
for (size_t j = 0; j < frames; j++)
adata[i][j] *= inputGain;
return audio;
}
static void save(void* data, obs_data_t* settings)
{
auto* adio = (struct adio_data*)data;
std::string s;
adio->deviceIo->getState(s);
obs_data_set_string(settings, FILTER_ID, s.c_str());
}
static void tick(void* data, float seconds)
{
struct adio_data* adio = (struct adio_data*)data;
auto* settings = adio->settings;
// Cache transition duration from frontend API (called on main thread)
if (adio->followScene.load(std::memory_order_acquire))
{
int transitionDurationMs = obs_frontend_get_transition_duration();
adio->fadeTimeSeconds.store(transitionDurationMs / 1000.0, std::memory_order_release);
}
// Compute bypass state on main thread for audio thread to read
obs_source_t* parent = obs_filter_get_parent(adio->context);
bool bypass = false;
if (adio->followScene.load(std::memory_order_acquire) && parent)
{
bypass = !obs_source_active(parent);
// Early fade-out: detect if parent is fading OUT during an active transition
constexpr float kTransitionEpsilon = 0.001f;
obs_source_t* transition = obs_frontend_get_current_transition();
if (transition != nullptr)
{
float transitionTime = obs_transition_get_time(transition);
if (transitionTime > kTransitionEpsilon)
{
// Transition is in progress - check if parent is in destination scene
obs_source_t* destSceneSrc = obs_transition_get_source(transition, OBS_TRANSITION_SOURCE_B);
if (destSceneSrc != nullptr)
{
obs_scene_t* destScene = obs_scene_from_source(destSceneSrc);
if (destScene != nullptr)
{
const char* parentName = obs_source_get_name(parent);
obs_sceneitem_t* item = obs_scene_find_source(destScene, parentName);
if (item == nullptr)
{
// Parent not in destination scene - fade out now
bypass = true;
}
}
obs_source_release(destSceneSrc);
}
}
obs_source_release(transition);
}
}
adio->shouldBypass.store(bypass, std::memory_order_release);
auto outputGain = adio->outputGain.load(std::memory_order_acquire);
if (settings)
outputGain = (float)obs_data_get_double(settings, OG_ID);
outputGain = obs_db_to_mul(outputGain);
if (adio->followSourceVolume.load(std::memory_order_acquire) && parent)
{
bool obsMuted = obs_source_muted(parent);
int monitoringType = (int)obs_source_get_monitoring_type(parent);
bool effectiveMuted = obsMuted || (monitoringType == OBS_MONITORING_TYPE_MONITOR_ONLY);
auto fader = obs_source_get_volume(parent);
if (effectiveMuted)
fader = 0.0f;
outputGain *= fader;
}
adio->outputGain.store(outputGain, std::memory_order_release);
UNUSED_PARAMETER(seconds);
}
struct obs_source_info device_io_filter = {
.id = FILTER_ID,
.type = OBS_SOURCE_TYPE_FILTER,
.output_flags = OBS_SOURCE_AUDIO,
.get_name = devio_name,
.create = devio_create,
.destroy = devio_destroy,
.get_defaults = devio_defaults,
.get_properties = devio_properties,
.update = devio_update,
.video_tick = tick,
.filter_audio = devio_filter,
.save = save,
.load = load,
};