Repository navigation
Expand file tree
/
Copy pathsamplesender.cpp
More file actions
242 lines (205 loc) · 7.04 KB
/
Copy pathsamplesender.cpp
File metadata and controls
242 lines (205 loc) · 7.04 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
// ==============================================================================
// PROJECT: zqloader
// FILE: samplesender.cpp
// DESCRIPTION: Implementation for class SampleSender.
//
// Copyright (c) 2023 Daan Scherft [Oxidaan]
// This project uses the MIT license. See LICENSE.txt for details.
// ==============================================================================
#include "samplesender.h"
#include <miniaudio.h>
#include <iostream>
#include <thread>
// CTORs
SampleSender::SampleSender() noexcept = default;
SampleSender::SampleSender(SampleSender &&) noexcept = default;
SampleSender& SampleSender::operator = (SampleSender&&) noexcept = default;
SampleSender::~SampleSender() noexcept
{
if (m_device)
{
Stop();
// Some weird COM error/bug causes QT's clipboard to stop working when this is done in
// QT's main thread. (ChatGpt hint). Whatever. (Was not needed for ma_device_init)
// Also causes open file QDialog problems.
// Hence the std::thread here.
std::thread t([this]{
ma_device_uninit(m_device.get());
});
t.join();
}
}
/// TODO "add output sink/device selection for those why have more than one sound card?"
/// Intialize miniadio when not done so already.
SampleSender& SampleSender::Init()
{
if (!m_device)
{
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32; // Set to ma_format_unknown to use the device's native format.
config.playback.channels = 2; // Set to 0 to use the device's native channel count.
// config.playback.pDeviceID = some_id // when more hardware present. See ma_context_get_devices
config.sampleRate = m_sample_rate; // Set to 0 to use the device's native sample rate.
config.dataCallback = data_callback; // This function will be called when miniaudio needs more data.
config.pUserData = this; // Can be accessed from the device object (device.pUserData).
auto device = std::make_unique<ma_device>();
if (ma_device_init(nullptr, &config, device.get()) != MA_SUCCESS)
{
throw std::runtime_error("Failed to initialize the device.");
}
std::cout << "Start playing with sample rate = " << device->sampleRate << std::endl;
m_sample_period = 1s / double(device->sampleRate);
m_device = std::move(device);
}
return *this;
}
/// Start SampleSender / start miniaudio thread.
SampleSender& SampleSender::Start()
{
if (!IsRunning())
{
Reset();
Init();
ma_device_start(m_device.get()); // The device is sleeping by default so you'll need to start it manually.
}
return *this;
}
/// Stop SampleSender (that is: stop miniaudio thread)
/// Call *after* Wait else aborts sending data.
SampleSender& SampleSender::Stop() noexcept
{
if (IsRunning())
{
ma_device_stop(m_device.get());
}
return *this;
}
/// Wait for SampleSender (that is: wait for all data to have been send)
/// Has nothing to do with waiting for thread to finish.
SampleSender& SampleSender::WaitUntilDone()
{
if (IsRunning())
{
m_event.wait();
}
return *this;
}
bool SampleSender::IsRunning() const noexcept
{
return m_device && ma_device_is_started(m_device.get());
}
/// Run SampleSender (miniadio thread) so start, wait, stop.
/// convenience.
SampleSender& SampleSender::Run()
{
Start();
WaitUntilDone();
// std::this_thread::sleep_for(500ms); // otherwise stops before all data was send
// see https://github.com/mackron/miniaudio/discussions/490
// replaced with m_done_event_cnt
Stop();
return *this;
}
// A (static/global) data callback function for MiniAudio.
// Redirect immidiately to corresponding class member function immidiately.
static_assert(sizeof(ma_uint32) == sizeof(uint32_t));
void SampleSender::data_callback(ma_device * pDevice, void* pOutput, const void*, ma_uint32 frameCount)
{
SampleSender& samplesender = *static_cast<SampleSender*>(pDevice->pUserData);
samplesender.DataCallback(pDevice, pOutput, frameCount);
}
// static
uint32_t SampleSender::GetDeviceSampleRate()
{
SampleSender temp;
temp.Init();
return temp.m_device->sampleRate;
}
inline void SampleSender::Reset()
{
m_done_cnt = 0;
m_output_value = false;
// m_edge = true; // Does NOT work
m_sample_time = 0ms;
m_event.Reset();
}
/// Check if done with m_OnCheckDone call back,
/// Signal event when truly done for m_done_event_cnt times.
/// See https://github.com/mackron/miniaudio/discussions/490
inline bool SampleSender::CheckDone()
{
bool done = true;
if (m_OnCheckDone)
{
done = m_OnCheckDone();
}
if(!done)
{
if(m_done_cnt > 0)
{
m_event.Reset();
m_done_cnt = 0;
}
}
else
{
m_done_cnt++;
if(m_done_cnt > m_done_cnt_max) // see https://github.com/mackron/miniaudio/discussions/490
{
m_event.Signal();
}
}
return done;
}
/// Get next sound samples,
/// called in miniaudio device thread
inline void SampleSender::DataCallback(ma_device* p_device, void* out_data, uint32_t p_frame_count)
{
auto channels = p_device->playback.channels; // # channels eg 2 is stereo
float* foutput = reinterpret_cast<float*>(out_data);
int index = 0;
bool done = CheckDone();
for (auto n = 0u; n < p_frame_count; n++)
{
float value = done ? 0.0f : GetNextSample(done); // GetNextSample can also set done
for (auto chan = 0u; chan < channels; chan++)
{
foutput[index] = (chan == 0) ? m_volume_left * value :
(chan == 1) ? m_volume_right * value : 0.0f;
index++;
}
}
}
/// Get next sample (audio value), depending on attached Pulsers.
/// This is typically 1.0 or -1.0 depending on edge; s/a GetEdge.
/// (any volume setting not used here)
inline float SampleSender::GetNextSample(bool &out_done)
{
m_sample_time += m_sample_period;
// Get duration to wait for next edge change based on what we need to do
Doublesec time_to_wait = GetDurationWait();
if (m_sample_time >= time_to_wait)
{
// Get value what edge needs to become (normally that is toggle)
Edge edge = GetEdge();
if (edge == Edge::toggle)
{
m_output_value = !m_output_value;
}
else if (edge == Edge::one)
{
m_output_value = true;
}
else if (edge == Edge::zero)
{
m_output_value = false;
}
// catch up. Causes sound to be irregular.
// Even leader tone is noticable (hearable) irregular.
// But this makes loading take a little longer than expected (s/a SpectrumLoader::GetDuration)
// m_sample_time -= time_to_wait ;
m_sample_time = 0s;
out_done = NextSample(); // true when at end.
}
return m_output_value ? 1.0f : -1.0f;
}