-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
136 lines (122 loc) · 3.74 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
//static int OPS[9] = {0x0, 0x1, 0x2, 0x8, 0x9, 0xA, 0x10, 0x11, 0x12};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
audBuf(32768, 0)
{
ui->setupUi(this);
setupAudioOutput();
pullTimer = new QTimer(this);
connect(pullTimer, SIGNAL(timeout()), SLOT(timerTimeout()));
playThread = new PlayThread(this, synthThread);
audioBuf = NULL;
//pullTimer->start(15);
//playThread = new PlayThread(this, synthThread);
//playThread->start();
}
MainWindow::~MainWindow()
{
audioOut->stop();
playThread->quit();
synthThread->quit();
delete ui;
//delete txtStream;
delete audioOut;
delete playThread;
delete synthThread;
//delete ymf262;
}
void MainWindow::soundOn()
{
QFileDialog fileDlg;
fileDlg.setFileMode(QFileDialog::ExistingFile);
fileDlg.setFilters(QString("MIDI Files (*.mid)|All Files (*.*)").split("|"));
fileDlg.setOption(QFileDialog::DontUseNativeDialog, false);
//synthThread->ymf262io->writeReg(0xB0, 0x32);
//audioOut->start(synthThread->getSynthBuffer());
//audioOut->resume();
if (fileDlg.exec()) {
stopMIDISong();
playThread->setMIDIFile(fileDlg.selectedFiles().first());
playMIDISong();
}
}
void MainWindow::soundOff()
{
//synthThread->ymf262io->writeReg(0xB0, 0x12);
}
void MainWindow::setupAudioOutput()
{
QAudioFormat fmt;
fmt.setFrequency(44100);
fmt.setChannels(2);
fmt.setSampleSize(16);
fmt.setCodec("audio/pcm");
fmt.setByteOrder(QAudioFormat::LittleEndian);
fmt.setSampleType(QAudioFormat::SignedInt);
synthThread = new SynthThread(this);
audioOut = new QAudioOutput(fmt);
//audioOut->setNotifyInterval(100);
connect(audioOut,SIGNAL(stateChanged(QAudio::State)),SLOT(stateChanged(QAudio::State)));
//connect(audioOut, SIGNAL(notify()), SLOT(notify()));
//ymf262 = new YMF262IO(this, audioOut->periodSize());
//ymf262->start();
//audioOut->setBufferSize(4410);
//pullTimer->start();
//audioOut->start(synthThread->getSynthBuffer());
}
void MainWindow::playMIDISong()
{
audioBuf = audioOut->start();
synthThread->start();
pullTimer->start(15);
playThread->start();
}
void MainWindow::stopMIDISong()
{
playThread->stop();
pullTimer->stop();
synthThread->stop();
if (audioBuf != NULL) {
audioBuf->close();
audioBuf = NULL;
}
audioOut->stop();
audioOut->reset();
}
void MainWindow::stateChanged(QAudio::State state)
{
qDebug("Audio State: %d\nError State: %d", state, audioOut->error());
if (state == QAudio::IdleState) {
//audioOut->stop();
//audioOut->start(synthThread->getSynthBuffer());
//audioOut->resume();
//audioBuf->write(synthThread->getSynthBuffer()->data());
}
}
void MainWindow::timerTimeout()
{
//audioBuf->write(synthThread->getSynthBuffer()->data());
//qDebug("%d", audioOut->bytesFree());
//audioBuf->write(synthThread->getSynthBuffer()->read(audioOut->bytesFree()));
//qDebug("%d", audioBuf->write(synthThread->getSynthBuffer()->read(audioOut->periodSize())));
//qDebug("%d", synthThread->getSynthBuffer()->read(1024).size());
if (audioOut->error() != QAudio::UnderrunError) {
int chunks = audioOut->bytesFree()/audioOut->periodSize();
while (chunks) {
const qint64 len = synthThread->getSynthBuffer()->read(audBuf.data(), audioOut->periodSize());
if (len)
audioBuf->write(audBuf.data(), len);
if (len != audioOut->periodSize())
break;
--chunks;
}
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
stopMIDISong();
event->accept();
}