-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
153 lines (123 loc) · 3.78 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "SineWave.h"
#include "RtWvOut.h"
#include "OrganSynth.h"
#include <cstdlib>
#include <QDebug>
using namespace stk;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
my_organsynth = NULL;
synth_thread = NULL;
ui->setupUi(this);
initDacThread();
ui->checkBoxRank0->setEnabled(false);
ui->checkBoxRank1->setEnabled(false);
ui->checkBoxRank2->setEnabled(false);
ui->checkBoxRank3->setEnabled(false);
refreshMidi();
}
MainWindow::~MainWindow()
{
stopDacProc();
delete ui;
}
void MainWindow::on_pushButtonSine_clicked()
{
my_organsynth->toggleSound();
return;
}
void MainWindow::stopDacProc()
{
if (my_organsynth)
{
qDebug() <<"stopping dac worker\n";
my_organsynth->stopDac();
qDebug() <<"calling exit on thread...\n";
synth_thread->exit();
synth_thread->wait();
qDebug() <<"threads exited cleanly.\n";
}
}
void MainWindow::refreshMidi()
{
int prevIndex = ui->comboBoxMidiIn->currentIndex();
qDebug()<<"prevIndex = " << prevIndex;
ui->comboBoxMidiIn->clear();
QStringList list = my_organsynth->getMidiPorts();
if (list.isEmpty())
ui->comboBoxMidiIn->setDisabled(true);
else {
for (QStringList::iterator it = list.begin(); it != list.end(); ++it)
{
ui->comboBoxMidiIn->addItem(*it);
}
if (prevIndex != -1) //restore previous selection, if it exists
ui->comboBoxMidiIn->setCurrentIndex(prevIndex);
}
}
void MainWindow::on_pushButtonRefreshMidi_clicked()
{
refreshMidi();
}
void MainWindow::on_StopToggled(int stop, bool active)
{
qDebug() <<"Stop # " <<stop <<" active = " <<active;
switch (stop) {
case 0:
ui->checkBoxRank0->setChecked(active);
break;
case 1:
ui->checkBoxRank1->setChecked(active);
break;
case 2:
ui->checkBoxRank2->setChecked(active);
break;
case 3:
ui->checkBoxRank3->setChecked(active);
break;
default:
break;
}
}
void MainWindow::initDacThread()
{
if (my_organsynth) //stop if running
stopDacProc();
if (synth_thread)
delete synth_thread;
synth_thread = 0;
synth_thread = new QThread;
if (my_organsynth) //TODO get rid of this
{
delete my_organsynth;
my_organsynth = 0;
}
int port_index = ui->comboBoxMidiIn->currentIndex();
if (port_index < 0) //in case something weird happened...
port_index = 0;
my_organsynth = new OrganSynth(0);
my_organsynth->moveToThread(synth_thread);
//connect slots
//connect(my_dac, SIGNAL(testFromDac()), this, SLOT(fromDac()));
//this launches the runSynth routine when thread is started
connect(synth_thread, SIGNAL(started()), my_organsynth, SLOT(runSynth()));
//NOTE: the thread cannot finish until the worker is done,
// so this call below is useless!!
connect(synth_thread, SIGNAL(finished()), my_organsynth, SLOT(stopSynth()));
//connect(this, SIGNAL(toDac(int)), my_organsynth, SLOT(stopSynth()));
//connect(this, SIGNAL(toDac(int)), my_dac, SLOT(toggleSine()));
//from synth->UI, for stop toggle display
connect(my_organsynth, SIGNAL(sendStopSig(int,bool)), this, SLOT(on_StopToggled(int,bool)));
//port changing signal/slot
connect(this, SIGNAL(ChangePort(int)), my_organsynth, SLOT(selectMidiPort(int)));
synth_thread->start();
}
void MainWindow::on_comboBoxMidiIn_currentIndexChanged(int index)
{
qDebug()<<"changing port to " << index;
emit ChangePort(index);
}