-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiomidi.cpp
335 lines (262 loc) · 9.98 KB
/
audiomidi.cpp
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
327
328
329
330
331
332
333
#include "audiomidi.h"
#include <QtCore/qthread.h>
#include <QQmlComponent>
#include <QtQml/qqmlapplicationengine.h>
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <signal.h>
#include <map>
#include "libs/rtaudio/RtAudio.h"
#include "libs/rtmidi/RtMidi.h"
#include "libs/QMidi/src/QMidiFile.h"
int SAMPLE_RATE = 48000;
int N_CHANNELS = 2;
auto AUDIO_PRIORITY = QThread::HighestPriority;
std::map<QThread *, std::pair<std::atomic<bool>, std::atomic<double>>> relTimeDic;
std::map<int, QThread *> noteThreadDic;
struct UserData{ // data structure passed to RtCallback
UserData(){}
double channelData[2] = {0, 0};
double freq = 0;
QThread * thread = nullptr;
bool isReleased = false;
};
int sine( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void * userDataArg )
{
unsigned int i, j;
double *buffer = (double *) outputBuffer;
UserData *userData = (UserData *) userDataArg;
double freq = userData->freq;
double amp = 0.3;
double a = 0.005;
double h = 0;
double r = 0.3;
bool isNoteReleased = true;
double relStartTime = -1;
if (relTimeDic.find(userData->thread) == relTimeDic.end() ){
std::cout << "Not finding the thread (this is not supposed to happen)" << std::endl;
}
else{
isNoteReleased = relTimeDic.find(userData->thread)->second.first.load();
relStartTime = relTimeDic.find(userData->thread)->second.second.load();
}
if (relStartTime < a + h)
relStartTime = a + h;
if ( status )
std::cout << "Stream underflow detected!" << std::endl;
// Write interleaved audio data.
for ( i=0; i<nBufferFrames; i++ ) {
for ( j=0; j<N_CHANNELS; j++ ) {
*buffer++ = userData->channelData[j];
double t = streamTime + (double)i/SAMPLE_RATE;
if (t <= a)
userData->channelData[j] = amp * std::sin(2 * M_PI * freq * t) * t/a; // t/a is 1 when t == a
else if (t <= a + h || !isNoteReleased)
userData->channelData[j] = amp * std::sin(2 * M_PI * freq * t); // hold/sustain
else if (isNoteReleased && t <= relStartTime + r){
userData->channelData[j] = amp * std::sin(2 * M_PI * freq * t) * ((relStartTime - t) / r + 1);
}
else{
userData->channelData[j] = 0;
userData->isReleased = true;
}
}
}
return 0;
}
int audioFun(const double freq)
{
RtAudio dac;
std::vector<unsigned int> deviceIds = dac.getDeviceIds();
if ( deviceIds.size() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 0 );
}
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = N_CHANNELS;
parameters.firstChannel = 0;
unsigned int sampleRate = SAMPLE_RATE;
unsigned int bufferFrames = 256; // 256 sample frames
UserData data{};
data.freq = freq;
data.thread = QThread::currentThread();
//RtAudio::StreamOptions options;
//options.flags = RTAUDIO_SCHEDULE_REALTIME;
if ( dac.openStream( ¶meters, NULL, RTAUDIO_FLOAT64, sampleRate,
&bufferFrames, &sine, (void *)&data ) ) {
std::cout << '\n' << dac.getErrorText() << '\n' << std::endl;
exit( 0 ); // problem with device settings
}
// Stream is open ... now start it.
if ( dac.startStream() ) {
std::cout << dac.getErrorText() << std::endl;
goto cleanup;
}
std::cout << "\nPlaying \n" << std::endl;
while (!data.isReleased)
QThread::msleep(2000);
std::cout << "releasing thread" << std::endl;
// Block released ... stop the stream
if ( dac.isStreamRunning() )
dac.stopStream(); // or could call dac.abortStream();
cleanup:
if ( dac.isStreamOpen() ) dac.closeStream();
return 0;
}
bool done;
static void finish(int ignore){ done = true; }
void inline noteOff(int note, QList<bool> * pressedNotesList, Audiomidi * audioMidiObject){
auto n = note;
QThread* thread = noteThreadDic.find(n)->second;
auto currentTimeSec = QTime::currentTime().msec()/1000.0 + QTime::currentTime().second();
auto relStartTime = currentTimeSec - relTimeDic.find(thread)->second.second.load();
relTimeDic.find(thread)->second.first.store(true);
relTimeDic.find(thread)->second.second.store(relStartTime);
noteThreadDic.erase(n);
pressedNotesList->replace(n, false);
emit audioMidiObject->pressedNotesListChanged();
}
int midiFun (QThread * audioThread, QList<bool> * pressedNotesList, Audiomidi * audioMidiObject){
RtMidiIn *midiin = new RtMidiIn();
std::vector<unsigned char> message;
int nBytes, i;
double stamp;
// Check available ports.
unsigned int nPorts = midiin->getPortCount();
if ( nPorts == 0 ) {
std::cout << "No ports available!\n";
goto cleanup;
}
midiin->openPort( 0 );
// Don't ignore sysex, timing, or active sensing messages.
midiin->ignoreTypes( false, false, false );
// Install an interrupt handler function.
done = false;
(void) signal(SIGINT, finish);
// Periodically check input queue.
while ( !done ) {
stamp = midiin->getMessage( &message );
nBytes = message.size();
if (nBytes >2){
if ( (int)message[0] == 144 ){
// Note On
auto n = (int)message[1];
auto freq = utils::midiToFreq(n);
auto newThread = audioThread->create(audioFun, freq);
auto currentTimeSec = QTime::currentTime().msec()/1000.0 + QTime::currentTime().second();
relTimeDic.insert(std::make_pair(newThread, std::make_pair(false, currentTimeSec)));
noteThreadDic.insert(std::pair(n, newThread));
newThread->start(AUDIO_PRIORITY);
pressedNotesList->replace(n, true);
emit audioMidiObject->pressedNotesListChanged();
}
else if ( (int)message[0] == 128 ){
// Note Off
noteOff((int)message[1], pressedNotesList, audioMidiObject);
}
}
for ( i=0; i<nBytes; i++ ){
std::cout << "Byte " << i << " = " << (int)message[i] << ", ";
}
if ( nBytes > 0 )
std::cout << "stamp = " << stamp << std::endl;
// Sleep for 10 milliseconds ... platform-dependent.
//std::this_thread::sleep_for(std::chrono::milliseconds( 10 ));
QThread::msleep(10);
}
// Clean up
cleanup:
delete midiin;
return 0;
}
void Audiomidi::debug(){
qDebug() << pressedNotesList ;
}
Audiomidi::Audiomidi(QObject *parent)
: QObject{parent}
{
auto myThread = midiThread.create(midiFun, &audioThread, &pressedNotesList, this);
myThread->start(QThread::HighPriority);
for (qsizetype i = 0; i < 128; ++i) {
pressedNotesList.append(false);
}
//readMidiData(":/test.mid");
}
Audiomidi::~Audiomidi()
{
}
void Audiomidi::readMidiData (QString path){
midiMessages.clear();
QMidiFile* midi_file = new QMidiFile();
std::cout << "Reading MIDI file " << path.toStdString() << std::endl;
midi_file->load(path);
QList<QMidiEvent*> events = midi_file->events();
for (QMidiEvent* e : events) {
if (e->type() != QMidiEvent::Meta) {
qint64 eventTime = midi_file->timeFromTick(e->tick()) *1000;
if (e->type() != QMidiEvent::SysEx) {
qint32 message = e->message();
qint32 status = message & 0x0000F0;
qint32 channel = message & 0x00000F;
if (status == 144 && channel == 0){
// note on
qint32 note = (message & 0x00FF00) >> 8;
//qint32 velocity = (message & 0xFF0000) >> 8*2;
MIDIMessageStruct newMessage {eventTime, note, status};
midiMessages.append(newMessage);
}
if (status == 128 && channel == 0){
// note off
qint32 note = (message & 0x00FF00) >> 8;
MIDIMessageStruct newMessage {eventTime, note, status};
midiMessages.append(newMessage);
}
}
}
}
qDebug() << midiMessages.size() << " events read.";
// clear and fill midiNotesInfo
midiNotesInfo.clear();
for (qsizetype i = 0; i < midiMessages.size(); ++i) {
auto status = midiMessages.at(i).status;
if (status == 144){
// note on: search note off
auto noteOnTimeInSecs = midiMessages.at(i).eventTime/1000.0;
double noteDurationInSecs = 0;
float xPos = midiMessages.at(i).note - 60; // 60 being C3
for (qsizetype j = i; j < midiMessages.size(); ++j){
if (midiMessages.at(j).status == 128 && midiMessages.at(j).note == midiMessages.at(i).note){
// note off
noteDurationInSecs = midiMessages.at(j).eventTime/1000.0 - noteOnTimeInSecs;
break;
}
}
float zScale = noteDurationInSecs / 2;
float zPos = noteOnTimeInSecs + noteDurationInSecs/2;
float xScale = 0.25;
if (utils::isBlackMidiNote(midiMessages.at(i).note)){
xScale *= 0.5;
}
// note, x, y, z, xScale, yScale, zScale
midiNotesInfo.append(midiMessages.at(i).note);
midiNotesInfo.append(-xPos*25);
midiNotesInfo.append(0);
midiNotesInfo.append(zPos*50 -200);
midiNotesInfo.append(xScale);
midiNotesInfo.append(0.25);
midiNotesInfo.append(zScale);
}
}
}
void Audiomidi::allNotesOff(){
return; // not working
for (int n = 0; n<=127; ++n){
if (noteThreadDic.find(n)->second)
noteOff(n, &pressedNotesList, this);
else
qDebug() << n << " not playing";
}
}