-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathAudioSource.cpp
More file actions
241 lines (198 loc) · 7.88 KB
/
AudioSource.cpp
File metadata and controls
241 lines (198 loc) · 7.88 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
/* Audio Source
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <QtGlobal>
#if QT_VERSION_MAJOR == 5
#include <QAudioInput>
using NativeAudioSource = QAudioInput;
#elif QT_VERSION_MAJOR == 6
#include <QAudioSource>
using NativeAudioSource = QAudioSource;
#endif
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/PrettyPrint.h"
#include "Common/Cpp/Time.h"
//#include "Common/Cpp/StreamConverters.h"
#include "CommonFramework/AudioPipeline/AudioStream.h"
#include "CommonFramework/AudioPipeline/Tools/AudioFormatUtils.h"
#include "AudioFileLoader.h"
#include "AudioSource.h"
#ifdef _WIN32
#include "DirectShowAudioCapture.h"
#endif
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
class AudioInputFile final : public QObject{
public:
AudioInputFile(
Logger& logger, AudioStreamToFloat& reader,
const std::string& file, const QAudioFormat& format
)
: m_reader(reader)
{
logger.log("AudioInputFile(): " + dump_audio_format(format));
m_source = std::make_unique<AudioFileLoader>(nullptr, file, format);
connect(
m_source.get(), &AudioFileLoader::bufferReady,
this, [this](const char* data, size_t len){
m_reader.push_bytes(data, len);
}
);
m_source->start();
}
private:
AudioStreamToFloat& m_reader;
std::unique_ptr<AudioFileLoader> m_source;
};
class AudioInputDevice final : public QIODevice{
public:
AudioInputDevice(
Logger& logger, AudioStreamToFloat& reader,
const NativeAudioInfo& device, const QAudioFormat& format
)
: m_reader(reader)
{
logger.log("AudioInputDevice(): " + dump_audio_format(format));
if (!device.isFormatSupported(format)){
// throw InternalProgramError(&logger, PA_CURRENT_FUNCTION, "Format not supported: " + dump_audio_format(format));
logger.log("Format not supported: " + dump_audio_format(format), COLOR_RED);
return;
}
m_source = std::make_unique<NativeAudioSource>(device, format);
this->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
WallClock start = current_time();
m_source->start(this);
WallClock end = current_time();
double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
logger.log("Done starting audio... " + tostr_fixed(seconds, 3) + " seconds", COLOR_CYAN);
}
~AudioInputDevice(){
if (m_source){
m_source->stop();
}
}
virtual bool isSequential() const override { return true; }
virtual qint64 readData(char* data, qint64 maxlen) override { return 0; }
virtual qint64 writeData(const char* data, qint64 len) override{
m_reader.push_bytes(data, len);
return len;
}
private:
AudioStreamToFloat& m_reader;
std::unique_ptr<NativeAudioSource> m_source;
};
class AudioSource::InternalListener : public AudioFloatStreamListener{
public:
InternalListener(AudioSource& parent)
: AudioFloatStreamListener(parent.m_channels * parent.m_multiplier)
, m_parent(parent)
{}
private:
virtual void on_samples(const float* data, size_t objects) override{
// cout << "objects = " << objects << endl;
m_parent.m_listeners.run_method(
&AudioFloatStreamListener::on_samples,
data, objects
);
}
AudioSource& m_parent;
};
void AudioSource::add_listener(AudioFloatStreamListener& listener){
auto scope_check = m_sanitizer.check_scope();
m_listeners.add(listener);
}
void AudioSource::remove_listener(AudioFloatStreamListener& listener){
auto scope_check = m_sanitizer.check_scope();
m_listeners.remove(listener);
}
AudioSource::~AudioSource(){}
AudioSource::AudioSource(Logger& logger, const std::string& file, AudioChannelFormat format, float volume_multiplier){
QAudioFormat native_format;
set_sample_format_to_float(native_format);
set_format(native_format, format);
init(format, AudioSampleFormat::FLOAT32, volume_multiplier);
m_input_file = std::make_unique<AudioInputFile>(logger, *m_reader, file, native_format);
}
AudioSource::AudioSource(Logger& logger, const AudioDeviceInfo& device, AudioChannelFormat format, float volume_multiplier){
#ifdef _WIN32
// DirectShow audio from a video capture device's audio pin.
const std::string& dev_name = device.device_name();
const std::string DSHOW_AUDIO_PREFIX = "dshow_audio:";
if (dev_name.size() > DSHOW_AUDIO_PREFIX.size() &&
dev_name.compare(0, DSHOW_AUDIO_PREFIX.size(), DSHOW_AUDIO_PREFIX) == 0)
{
std::string friendly_name = dev_name.substr(DSHOW_AUDIO_PREFIX.size());
// DirectShow will deliver 16-bit PCM.
init(format, AudioSampleFormat::SINT16, volume_multiplier);
int sample_rate = static_cast<int>(m_sample_rate);
int channels = static_cast<int>(m_channels);
m_dshow_audio_capture = std::make_unique<DirectShowAudioCapture>(
logger, *m_reader, friendly_name, sample_rate, channels
);
return;
}
#endif
NativeAudioInfo native_info = device.native_info();
QAudioFormat native_format = native_info.preferredFormat();
set_format(native_format, format);
AudioSampleFormat stream_format = get_sample_format(native_format);
if (stream_format == AudioSampleFormat::INVALID){
stream_format = AudioSampleFormat::FLOAT32;
set_sample_format_to_float(native_format);
}
init(format, stream_format, volume_multiplier);
m_input_device = std::make_unique<AudioInputDevice>(logger, *m_reader, native_info, native_format);
}
void AudioSource::init(AudioChannelFormat format, AudioSampleFormat stream_format, float volume_multiplier){
auto scope_check = m_sanitizer.check_scope();
switch (format){
case AudioChannelFormat::MONO_48000:
m_sample_rate = 48000;
m_channels = 1;
m_multiplier = 1;
m_reader.reset(new AudioStreamToFloat(stream_format, 1, volume_multiplier, false));
break;
case AudioChannelFormat::DUAL_44100:
m_sample_rate = 44100;
m_channels = 2;
m_multiplier = 1;
m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false));
break;
case AudioChannelFormat::DUAL_48000:
m_sample_rate = 48000;
m_channels = 2;
m_multiplier = 1;
m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false));
break;
case AudioChannelFormat::MONO_96000:
// Treat mono-96000 as 2-sample frames.
// The FFT will then average each pair to produce 48000Hz.
// The output will push the same stream at the original 4 bytes * 96000Hz.
m_sample_rate = 96000;
m_channels = 1;
m_multiplier = 2;
m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false));
break;
case AudioChannelFormat::INTERLEAVE_LR_96000:
m_sample_rate = 48000;
m_channels = 2;
m_multiplier = 1;
m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false));
break;
case AudioChannelFormat::INTERLEAVE_RL_96000:
m_sample_rate = 48000;
m_channels = 2;
m_multiplier = 1;
m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, true));
break;
default:
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid AudioFormat: " + std::to_string((size_t)format));
}
m_internal_listener = std::make_unique<InternalListener>(*this);
m_reader->add_listener(*m_internal_listener);
}
}