forked from csete/qtmm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
330 lines (271 loc) · 10.8 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
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
/*
* mainwindow.cpp -- Main application window for AFSK1200 demodulator
*
* Copyright (C) 2011 Alexandru Csete (oz9aec at gmail.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <QDebug>
#include <QAudio>
#include <QMessageBox>
#include <QTextStream>
#include <QFileDialog>
#include <QDir>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* select font for text viewer */
#ifdef Q_OS_MAC
ui->textView->setFont(QFont("Monaco", 12));
#else
ui->textView->setFont(QFont("Monospace", 9));
#endif
initialiseAudio();
createDeviceSelector();
/* create SSI and to toolbar */
ssiSpacer = new QWidget();
ssiSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->mainToolBar->addWidget(ssiSpacer);
ssi = new CSsi(this);
ui->mainToolBar->addWidget(ssi);
connect(audioBuffer, SIGNAL(update(qreal)), ssi, SLOT(setLevel(qreal)));
connect(audioBuffer, SIGNAL(newData(float*,int)), this, SLOT(samplesReceived(float*,int)));
/* initialise decoders */
afsk12 = new CAfsk12();
connect(afsk12, SIGNAL(newMessage(QString)), ui->textView, SLOT(appendPlainText(QString)));
ui->statusBar->showMessage(tr("Decoder ready - select and input source then press start"));
}
MainWindow::~MainWindow()
{
if (ui->actionDecode->isChecked()) {
audioBuffer->stop();
audioInput->stop();
delete audioInput;
}
delete audioBuffer;
delete inputLabel;
delete inputSelector;
delete ssi;
delete afsk12;
delete ui;
}
/*! \brief Create device selector widget.
*
* The device selector widget is a combo box on the main toolbar
* listing all available input devices.
*/
void MainWindow::createDeviceSelector()
{
/* Add audio device selector */
inputSelector = new QComboBox(this);
inputSelector->setToolTip(tr("Select audio input device"));
connect(inputSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(inputSelectionChanged(int)));
foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioInput))
{
inputSelector->addItem(deviceInfo.deviceName());
/* store deviceInfo */
inputDevices.append(deviceInfo);
}
inputLabel = new QLabel(tr(" Input:"), this);
ui->mainToolBar->insertWidget(ui->actionDecode, inputLabel);
ui->mainToolBar->insertWidget(ui->actionDecode, inputSelector);
ui->actionDecode->setToolTip(tr("Start decoder"));
}
/*! \brief Initialise audio related data. */
void MainWindow::initialiseAudio()
{
audioFormat.setFrequency(22050);
audioFormat.setChannels(1);
audioFormat.setSampleSize(16);
audioFormat.setSampleType(QAudioFormat::SignedInt);
audioFormat.setByteOrder(QAudioFormat::LittleEndian);
audioFormat.setCodec("audio/pcm");
audioBuffer = new CAudioBuffer(audioFormat, this);
}
/*! \brief Input selection has changed.
* \param index The index to the new selection in the input selector combo box.
*
* This slot is activated when the user selects a new input device. The selection can be made
* both when the decoder is active and when it is idle. If the decoder is active, firs we
* disable the decoder by toggling the "Decode" action, then we simply enable it by toggling
* the action again.
*/
void MainWindow::inputSelectionChanged(int index)
{
Q_UNUSED(index);
/* check whether decoder is running */
if (ui->actionDecode->isChecked()) {
ui->actionDecode->toggle(); // stop decoder
ui->actionDecode->toggle(); // start decoder
}
/* if decoder is not runnign there is nothing to do */
}
/*! \brief Decoder status changed
* \param enabled True if the decoder has been enabled, false if it has been disabled.
*/
void MainWindow::on_actionDecode_toggled(bool enabled)
{
if (enabled) {
ui->statusBar->showMessage(tr("Starting decoder..."));
/* check that selected input device supports desired format, if not try nearest */
QAudioDeviceInfo info(inputDevices.at(inputSelector->currentIndex()));
if (!info.isFormatSupported(audioFormat)) {
qWarning() << "Default format not supported - trying to use nearest";
audioFormat = info.nearestFormat(audioFormat);
}
#if 0
qDebug() << "----------------------------------------------------";
qDebug() << "Input device: " << inputDevices.at(inputSelector->currentIndex()).deviceName();
qDebug() << " Codecs: " << inputDevices.at(inputSelector->currentIndex()).supportedCodecs();
qDebug() << " Channels: " << inputDevices.at(inputSelector->currentIndex()).supportedChannelCounts();
qDebug() << "Sample rates: " << inputDevices.at(inputSelector->currentIndex()).supportedSampleRates();
qDebug() << "Sample types: " << inputDevices.at(inputSelector->currentIndex()).supportedSampleTypes();
qDebug() << "Sample sizes: " << inputDevices.at(inputSelector->currentIndex()).supportedSampleSizes();
qDebug() << " Byte orders: " << inputDevices.at(inputSelector->currentIndex()).supportedByteOrders();
qDebug() << "----------------------------------------------------";
#endif
#if 0
qDebug() << "----------------------------------------------------";
qDebug() << " Codec: " << audioFormat.codec();
qDebug() << " Byte order: " << audioFormat.byteOrder();
qDebug() << "Sample rate: " << audioFormat.sampleRate();
qDebug() << "Sample size: " << audioFormat.sampleSize();
qDebug() << "Sample type: " << audioFormat.sampleType();
qDebug() << " Channels: " << audioFormat.channelCount();
qDebug() << "----------------------------------------------------";
#endif
/* initialise decoder; looks weird but dmeods were organised in array in multimon */
afsk12->reset();
audioInput = new QAudioInput(inputDevices.at(inputSelector->currentIndex()), audioFormat, this);
/** TODO: connect signals and slots */
//connect(audioInput, SIGNAL(notify()), SLOT(notified()));
connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(audioStateChanged(QAudio::State)));
audioBuffer->start();
audioInput->start(audioBuffer);
ui->actionDecode->setToolTip(tr("Stop decoder"));
ui->statusBar->showMessage(tr("Decoder running"));
}
else {
ui->statusBar->showMessage(tr("Stopping decoder"));
/* stop audio processing */
audioBuffer->stop();
audioInput->stop();
/** TODO: disconnect signals and slots */
delete audioInput;
ui->actionDecode->setToolTip(tr("Start decoder"));
ui->statusBar->showMessage(tr("Decoder stopped"));
/* reset input level indicator */
ssi->setLevel(0.0);
}
}
/*! \brief Slot for receiveing new audio samples.
* \param data The sample buffer.
* \param length The number of samples in the buffer.
*
* Calls the afsk1200 decoder.
*/
void MainWindow::samplesReceived(float *buffer, const int length)
{
int overlap = 18;
int i;
for (i = 0; i < length/*-overlap*/; i++) {
tmpbuf.append(buffer[i]);
}
afsk12->demod(tmpbuf.data(), length);
/* clear tmpbuf and store "overlap" */
tmpbuf.clear();
for (i = length-overlap; i < length; i++) {
tmpbuf.append(buffer[i]);
}
}
/*! \brief Slot for audio input state changes.
* \param state The new state of the audio input.
*/
void MainWindow::audioStateChanged(QAudio::State state)
{
qDebug() << "Audio state change: " << state << " ERROR: " << audioInput->error();
#ifdef Q_OS_MAC
/* On OSX audio stops due to underrun when window is minimized */
if (state == 3) {
audioBuffer->stop();
audioBuffer->start();
audioInput->start(audioBuffer);
}
#endif
}
/*! \brief Action: Clear the text view.
*
* This slot is activated when the user clicks on the Clear button
* on the main toolbar.
*/
void MainWindow::on_actionClear_triggered()
{
ui->textView->clear();
}
/*! \brief Action: Save
*
* This slot is called when the user activates the File|Save menu item.
* It asks for a file name, then saves the contents of the text viewer
* to a plain text file.
*/
void MainWindow::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
QDir::homePath(),
tr("Text Files (*.txt)"));
if (fileName.isEmpty()) {
qDebug() << "Save as cancelled by user";
return;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Error creating file: " << fileName;
return;
}
QTextStream out(&file);
out << ui->textView->toPlainText();
file.close();
}
/*! \brief Action: About AFSK1200 Decoder
*
* This slot is called when the user activates the
* Help|About menu item (or AFSK1200 Decoder|About on Mac)
*/
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(this, tr("About AFSK1200 Decoder"),
tr("<p>AFSK1200 Decoder %1</p>"
"<p>A simple AFSK1200 decoder that uses the computer's soundcard "
"for input. It can decode AX.25 packets and displays them in a text view.</p>"
"<p>AFSK1200 is the modulation mode used by radio amateurs for packet radio "
"transmissions, including APRS.</p>"
"<p>AFSK1200 decoder is written using the Qt toolkit (see About Qt) and is avaialble "
"for Linux, Mac and Windows. You can download the latest version from the "
"<a href='http://qtmm.sf.net/'>application website</a>."
).arg(VERSION));
}
/*! \brief Action: About Qt
*
* This slot is called when the user activates the
* Help|About Qt menu item (or AFSK Decoder|About Qt on Mac)
*/
void MainWindow::on_actionAboutQt_triggered()
{
QMessageBox::aboutQt(this, tr("About Qt"));
}