-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundLooper.hpp
201 lines (157 loc) · 4.62 KB
/
SoundLooper.hpp
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
#ifndef SOUND_LOOPER_HPP
#define SOUND_LOOPER_HPP
#include <QObject>
#include <bass.h>
#include "util.hpp"
class SoundLooper final : public QObject {
Q_OBJECT
Q_PROPERTY(bool running READ isRunning NOTIFY runningStateChanged)
bool running{};
HRECORD recording{};
HSTREAM stream{};
BASS_DX8_ECHO echoParams{};
BASS_DX8_REVERB reverbParams{};
HFX echoEffect{};
HFX reverbEffect{};
void setRunning(const bool b) {
running = b;
emit runningStateChanged(b);
b ? emit started() : emit stopped();
}
public:
[[nodiscard]]
Q_INVOKABLE bool isRunning() const {
return running;
}
[[nodiscard]]
Q_INVOKABLE QString getOutputDeviceName() const {
BASS_DEVICEINFO deviceinfo;
const auto device = BASS_GetDevice();
BASS_GetDeviceInfo(device, &deviceinfo);
return deviceinfo.name;
}
[[nodiscard]]
Q_INVOKABLE QString getInputDeviceName() const {
BASS_DEVICEINFO deviceinfo;
const auto device = BASS_RecordGetDevice();
BASS_RecordGetDeviceInfo(device, &deviceinfo);
return deviceinfo.name;
}
static std::vector<EXTENDED_DEVICEINFO> getAllRecordingDevices() {
std::vector<EXTENDED_DEVICEINFO> devices;
EXTENDED_DEVICEINFO info{};
for (int i = 0; BASS_RecordGetDeviceInfo(i, &info); i++) {
info.id = i;
devices.emplace_back(info);
}
return devices;
}
static std::vector<EXTENDED_DEVICEINFO> getAllOutputDevices() {
std::vector<EXTENDED_DEVICEINFO> devices;
EXTENDED_DEVICEINFO info{};
for (int i = 1; BASS_GetDeviceInfo(i, &info); i++) {
info.id = i;
devices.emplace_back(info);
}
return devices;
}
static DWORD streamRecordProc(HSTREAM, void* buffer, const DWORD length, void* user) {
const auto _this = static_cast<SoundLooper *>(user);
return BASS_ChannelGetData(_this->recording, buffer, length);
}
Q_INVOKABLE void start() {
if (not BASS_Init(-1, 96'000, BASS_DEVICE_DEFAULT, nullptr, nullptr))
exit(-10);
if (not BASS_RecordInit(0))
exit(-11);
recording = BASS_RecordStart(96'000, 1, 0, nullptr, nullptr);
if (not recording)
exit(-12);
stream = BASS_StreamCreate(96'000, 1, 0, &streamRecordProc, this);
if (not stream)
exit(-13);
BASS_ChannelSetAttribute(stream, BASS_ATTRIB_BUFFER, 0);
BASS_ChannelStart(stream);
setRunning(true);
}
Q_INVOKABLE void stop() {
BASS_ChannelFree(stream);
BASS_RecordFree();
BASS_Free();
echoEffect = 0;
setRunning(false);
}
Q_INVOKABLE void toggle() {
running ? stop() : start();
}
Q_INVOKABLE void echo_turnOn() {
if (not running or echoEffect)
return;
echoEffect = BASS_ChannelSetFX(stream, BASS_FX_DX8_ECHO, 100);
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void echo_turnOff() {
if (not running or not echoEffect)
return;
BASS_ChannelRemoveFX(stream, echoEffect);
echoEffect = 0;
}
Q_INVOKABLE void echo_setMix(const float value) {
echoParams.fWetDryMix = value;
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void echo_setFeedback(const float value) {
echoParams.fFeedback = value;
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void echo_setDelay(const float value) {
echoParams.fLeftDelay = value;
echoParams.fRightDelay = value;
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void echo_setLeftDelay(const float value) {
echoParams.fLeftDelay = value;
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void echo_setRightDelay(const float value) {
echoParams.fRightDelay = value;
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void echo_setPanDelay(const bool value) {
echoParams.lPanDelay = value;
BASS_FXSetParameters(echoEffect, &echoParams);
}
Q_INVOKABLE void reverb_turnOn() {
if (not running or reverbEffect)
return;
reverbEffect = BASS_ChannelSetFX(stream, BASS_FX_DX8_REVERB, 90);
BASS_FXSetParameters(reverbEffect, &reverbParams);
}
Q_INVOKABLE void reverb_turnOff() {
if (not running or not reverbEffect)
return;
BASS_ChannelRemoveFX(stream, reverbEffect);
reverbEffect = 0;
}
Q_INVOKABLE void reverb_setGain(const float value) {
reverbParams.fInGain = value;
BASS_FXSetParameters(reverbEffect, &reverbParams);
}
Q_INVOKABLE void reverb_setMix(const float value) {
reverbParams.fReverbMix = value;
BASS_FXSetParameters(reverbEffect, &reverbParams);
}
Q_INVOKABLE void reverb_setTime(const float value) {
reverbParams.fReverbTime = value;
BASS_FXSetParameters(reverbEffect, &reverbParams);
}
Q_INVOKABLE void reverb_setRatio(const float value) {
reverbParams.fHighFreqRTRatio = value;
BASS_FXSetParameters(reverbEffect, &reverbParams);
}
signals:
void runningStateChanged(bool isRunning);
void started();
void stopped();
};
#endif