-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMorse.cpp
688 lines (564 loc) · 18.2 KB
/
Morse.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include "Morse.h"
#include <QtCore/QSettings>
#include <QMenu>
#include <QMenuBar>
#include <QSizePolicy>
#include <QtMultimedia/QAudioDevice>
#include <QRandomGenerator>
#include <qdebug.h>
#include "MainWindow.h"
#include "Prefs.h"
#include "modes/FrontPage.h"
#include "modes/PlayMode.h"
#include "modes/LetterTrainingMode.h"
#include "modes/SpeedTrainingMode.h"
#include "modes/WordTrainingMode.h"
#include "modes/KeyTraining.h"
#include "modes/ReadMode.h"
#include "modes/GroupingMode.h"
#include "modes/games/WordGame.h"
#include "modes/games/GroupGame.h"
#include "modes/games/KeyInvaders.h"
#define WPMGOAL 20
#define WPMACCEPT 2
Morse::Morse()
: QObject(), m_parent(0), m_audioOutput(),
m_dahMult(3), m_pauseMult(1), m_letterPauseMult(3), m_spaceMult(7),
m_dit(0), m_dah(0), m_space(0), m_pause(0), m_letterPause(0), m_playBuffer(0), m_playingMode(STOPPED), m_gameMode(PLAY),
m_currentWPMGoal(WPMGOAL), m_currentWPMAccept(WPMACCEPT), m_ui(0), m_tone(DEFAULT_TONE), m_leadInPause(0,0), m_signalMapper(new QSignalMapper(this))
{
m_audioOutput = NULL;
qDebug() << "new morse";
m_modes.insert(PLAY, new PlayMode(this, m_ui));
#include "morse_code.h"
}
Morse::Morse(MainWindow *parent, QAudioSink *output, Ui::MainWindow *ui)
: QObject(parent), m_parent(parent), m_audioOutput(output),
m_dahMult(3), m_pauseMult(1), m_letterPauseMult(3), m_spaceMult(7),
m_dit(0), m_dah(0), m_space(0), m_pause(0), m_letterPause(0), m_playBuffer(0), m_playingMode(STOPPED), m_gameMode(PLAY),
m_currentWPMGoal(WPMGOAL), m_currentWPMAccept(WPMACCEPT),
m_ui(ui), m_tone(DEFAULT_TONE), m_leadInPause(0,0), m_signalMapper(new QSignalMapper(this))
{
qDebug() << "new morse2";
#include "morse_code.h"
createTones(WPMGOAL);
// QRandomGenerator::global()->seed(QTime::currentTime().msec());
qDebug() << "original buffer size: " << m_audioOutput->bufferSize();
m_audioOutput->setBufferSize(qMin(m_audioOutput->bufferSize() * 4, 1024*64));
m_modes.insert(FRONTPAGE, new FrontPage(this, m_ui));
m_modes.insert(PLAY, new PlayMode(this, m_ui));
m_modes.insert(TRAIN, new LetterTrainingMode(this, m_ui));
m_modes.insert(SPEEDTRAIN, new SpeedTrainingMode(this, m_ui));
m_modes.insert(WORDS, new WordTrainingMode(this, m_ui));
m_modes.insert(GROUPS, new GroupingMode(this, m_ui));
m_modes.insert(KEYING, new KeyTraining(this, m_ui));
m_modes.insert(WORDGAME, new WordGame(this, m_ui));
m_modes.insert(GROUPGAME, new GroupGame(this, m_ui));
m_modes.insert(READ, new ReadMode(this, m_ui));
m_modes.insert(KEYINVADERS, new KeyInvaders(this, m_ui));
loadSettings();
switchMode(Morse::FRONTPAGE);
}
MorseMode *Morse::getMode(TrainingMode which) const {
return m_modes[which];
}
void Morse::prefsButton() {
Prefs prefs(this);
prefs.exec();
}
void Morse::aboutButton() {
Ui::AboutDialog about;
QDialog *dialog = new QDialog(m_parent);
about.setupUi(dialog);
about.helpicon->setPixmap(QPixmap(":/icons/64x64/cutecw.png"));
#ifdef SMALL_DEVICE
dialog->setAttribute(Qt::WA_Maemo5StackedWindow);
#endif
dialog->exec();
}
QMenuBar *
Morse::menuBar()
{
return m_parent->menuBar();
}
void Morse::saveSettings() {
qDebug() << "saving!";
QSettings settings("WS6Z", "qtcw");
settings.setValue("WPM/Goal", m_currentWPMGoal);
settings.setValue("WPM/Accept", m_currentWPMAccept);
settings.setValue("Tone", m_tone);
//settings.setValue("LetterWeighting", int(m_badLetterWeighting));
for(int i = PLAY; i <= maximumTrainingMode; i++) {
m_modes[(TrainingMode) i]->saveSettings(settings);
}
}
void Morse::loadSettings() {
QSettings settings("WS6Z", "qtcw");
m_currentWPMGoal = settings.value("WPM/Goal", WPMGOAL).toInt();
m_currentWPMAccept = settings.value("WPM/Accept", WPMACCEPT).toInt();
//m_badLetterWeighting = (BadLetterWeighting) settings.value("LetterWeighting", HIGH).toInt();
m_tone = settings.value("Tone", DEFAULT_TONE).toInt();
for(int i = PLAY; i <= maximumTrainingMode; i++) {
if (! m_modes.contains((TrainingMode) i))
continue;
m_modes[(TrainingMode) i]->loadSettings(settings);
}
createTones(m_currentWPMGoal);
}
void Morse::clearStatsButton() {
m_modes[m_gameMode]->clear();
}
void
Morse::playSequence()
{
//qDebug() << "init playing mode: " << m_playingMode;
m_playBuffer->restartData();
m_playBuffer->start();
m_playingMode = PLAYING;
//qDebug() << "after morse playing mode: " << m_playingMode;
//qDebug() << "PLAYING";
if (m_audioOutput->error() != QAudio::NoError) {
// on OS X especially, this is needed on a regular basis.
// (on OS X, it's every time the audio pauses)
//qDebug() << "ERROR in audio output" << m_audioOutput->error();
m_audioOutput = m_parent->createAudioOutput();
}
//qDebug() << "looading play buffer";
m_audioOutput->setBufferSize(640000);
m_audioOutput->start(m_playBuffer);
//qDebug() << " volume:" << m_audioOutput->volume();
//qDebug() << " buffer:" << m_audioOutput->bufferSize();
//qDebug() << " error:" << m_audioOutput->error();
//qDebug() << " format:" << m_audioOutput->format();
//qDebug() << "morse playing mode: " << m_playingMode;
return;
}
QTime Morse::sequenceTime() {
return m_playBuffer->timeLeft();
}
QTime Morse::maybePlaySequence(bool addPause) {
//qDebug() << "playing mode: " << m_playingMode;
//qDebug() << "audio state: " << m_audioOutput->state();
if (m_playingMode == STOPPED || m_playingMode == PAUSED ||
m_audioOutput->state() != QAudio::ActiveState) {
m_playBuffer->restartData();
QTime playTime = sequenceTime();
if (addPause)
add(pause());
//qDebug() << " playingx mode: " << m_playingMode;
playSequence();
//qDebug() << " new audio state: " << m_audioOutput->state();
//qDebug() << " playingy mode: " << m_playingMode;
return QTime::currentTime().addSecs(playTime.second()).addMSecs(playTime.msec());
}
return QTime(0,0,0);
}
void Morse::keyPressed(QString newtext) {
QChar newletter = newtext.at(newtext.length()-1).toLower();
qDebug() << "*** user pressed: " << newletter;
keyPressed(newletter);
}
void Morse::keyPressed(QChar newletter) {
m_modes[m_gameMode]->handleKeyPress(newletter);
}
bool Morse::enterPressed() {
return m_modes[m_gameMode]->enterPressed();
}
void Morse::keyReleased(QString newtext) {
QChar newletter = newtext.at(newtext.length()-1).toLower();
qDebug() << "*** user pressed: " << newletter;
keyReleased(newletter);
}
void Morse::keyReleased(QChar newletter) {
m_modes[m_gameMode]->handleKeyRelease(newletter);
}
Morse::AudioMode Morse::audioMode() {
return m_playingMode;
}
void Morse::playButton() {
m_modes[m_gameMode]->playButton();
}
void Morse::generatorDone() {
audioFinished(QAudio::StoppedState); // fixes windows issues
}
void Morse::setAudioMode(AudioMode newmode) {
m_playingMode = newmode;
}
void Morse::pauseAudio() {
setAudioMode(PAUSED);
}
void Morse::playAudio() {
setAudioMode(PLAYING);
}
Morse::TrainingMode Morse::trainingMode() {
return m_gameMode;
}
Morse::BadLetterWeighting Morse::badLetterWeighting() {
return m_badLetterWeighting;
}
void
Morse::audioFinished(QAudio::State state)
{
#if (defined(Q_WS_MAEMO_5) || defined(MAEMO_CHANGES))
// This triggers a nasty hang on linux with Qt 4.7.1
// this also makes popping sounds on windows
// it also stops audio from working at all on the mac
// but it seems like the right thing to do, and fixes maemo
if (state != QAudio::ActiveState)
m_audioOutput->stop();
#endif
m_modes[m_gameMode]->audioFinished(state);
}
void Morse::switchMode(int newmode) {
qDebug() << "switch to:" << newmode;
m_playBuffer->stop();
m_modes[m_gameMode]->switchFromYou();
m_gameMode = (Morse::TrainingMode) newmode;
setupWidgets();
m_modes[(TrainingMode) newmode]->switchToYou();
}
//
// HERE and below is tone generation and sequence playing
//
QTime Morse::addAndPlayIt(QChar c) {
if (m_playingMode == STOPPED || m_playingMode == PAUSED) {
clearList();
add(pause());
}
add(c, false);
add(m_letterPause);
return maybePlaySequence();
}
QTime Morse::playIt(QChar c, bool addLeadInPause) {
clearList();
if (addLeadInPause)
add(pause()); // allows audio device to kick in (otherwise distortion can occur)
m_leadInPause = sequenceTime();
add(c, false); // add the pause in maybePlaySequence so it can calculate the sequence end
return maybePlaySequence(true);
}
void
Morse::clearList()
{
m_playBuffer->clearBuffer();
}
void
Morse::add(Generator *nextSound)
{
m_playBuffer->appendDataFrom(nextSound);
}
void
Morse::add(QChar c, bool addpause)
{
c = c.toLower();
if (! code.contains(c))
return;
QList<ditdah>::iterator iter;
QList<ditdah>::iterator endat = code[c]->end();
bool lastWasPause = false;
for(iter = code[c]->begin(); iter != endat; iter++)
{
lastWasPause = false;
switch (*iter) {
case DIT:
add(m_dit);
break;
case DAH:
add(m_dah);
break;
case PAUSE:
add(m_pause);
lastWasPause = true;
break;
case SPACE:
add(m_space);
lastWasPause = true;
break;
default:
qWarning() << "error: illegal morse type added";
}
add(m_pause);
}
if (addpause && !lastWasPause) {
add(m_letterPause);
}
}
void Morse::add(const QString &textToAdd) {
QString::const_iterator letter;
QString::const_iterator lastLetter = textToAdd.end();
clearList();
for (letter = textToAdd.begin(); letter != lastLetter; ++letter) {
add(*letter, true);
}
}
void
Morse::createTones(int wpm, int spacewpm, int letterspacewpm)
{
if (spacewpm == -1)
spacewpm = wpm;
if (letterspacewpm == -1)
letterspacewpm = spacewpm;
float multiplier = 25.0; // should be 50, but is now double-speeding
createSpacedTones(float(60.0/float(wpm*multiplier)),
float(float(m_spaceMult) * float(60.0/float(spacewpm*multiplier))),
float(float(m_letterPauseMult) * float(60.0/float(letterspacewpm*multiplier))));
}
void
Morse::_createTones()
{
if (m_dit)
delete m_dit;
m_dit = new Generator(m_ditSecs, m_tone);
m_dit->start();
if (m_dah)
delete m_dah;
m_dah = new Generator(m_dahSecs, m_tone);
m_dah->start();
if (m_pause)
delete m_pause;
m_pause = new Generator(m_pauseSecs, 0);
m_pause->start();
if (m_letterPause)
delete m_letterPause;
m_letterPause = new Generator(m_letterPauseSecs, 0);
m_letterPause->start();
if (m_space)
delete m_space;
m_space = new Generator(m_spaceSecs, 0);
m_space->start();
if (!m_playBuffer) {
m_playBuffer = new Generator(m_pause);
m_playBuffer->start();
// windows Qt fails to generate an audio state change, so we detect
// the end of the sound buffer this way instead
connect(m_playBuffer, SIGNAL(generatorDone()), this, SLOT(generatorDone()), Qt::QueuedConnection);
}
connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), this, SLOT(audioFinished(QAudio::State)));
}
void
Morse::createTones(float ditSecs, int dahMult, int pauseMult, int letterPauseMult, int spaceMult)
{
m_ditSecs = ditSecs;
m_dahSecs = ditSecs * dahMult;
m_pauseSecs = ditSecs * pauseMult;
m_letterPauseSecs = ditSecs * letterPauseMult;
m_spaceSecs = ditSecs * spaceMult;
m_dahMult = dahMult;
m_pauseMult = pauseMult;
m_letterPauseMult = letterPauseMult;
m_spaceMult = spaceMult;
_createTones();
}
void
Morse::createSpacedTones(float ditSecs, float spaceSecs, float letterSpaceSecs)
{
m_ditSecs = ditSecs;
m_spaceSecs = spaceSecs;
m_letterPauseSecs = letterSpaceSecs;
m_dahSecs = ditSecs * m_dahMult;
m_pauseSecs = ditSecs * m_pauseMult;
_createTones();
}
int Morse::currentWPMAccept() {
return m_currentWPMAccept;
}
int Morse::currentWPMGoal() {
return m_currentWPMGoal;
}
Generator *
Morse::dit()
{
return m_dit;
}
Generator *
Morse::dah()
{
return m_dah;
}
Generator *
Morse::pause()
{
return m_pause;
}
Generator *
Morse::letterPause()
{
return m_letterPause;
}
Generator *
Morse::space()
{
return m_space;
}
float
Morse::ditSecsF()
{
return m_ditSecs;
}
void Morse::setupWidgets()
{
QWidget *theMainThing = m_ui->centralWidget = new QWidget(m_parent);
QVBoxLayout *topvbox = m_ui->verticalLayout = new QVBoxLayout(theMainThing);
QHBoxLayout *buttonHBox = m_ui->horizontalLayout = new QHBoxLayout(theMainThing);
topvbox->addLayout(buttonHBox);
setupTopButtons(buttonHBox);
topvbox->addWidget(m_ui->helpBar = new QLabel("help"));
setupWPMLayout(topvbox, theMainThing);
topvbox->addLayout(m_ui->forModes = new QHBoxLayout(theMainThing));
topvbox->setStretchFactor(m_ui->forModes, 10);
setupConnections();
m_parent->setCentralWidget(theMainThing);
}
void Morse::setupWPMLayout(QVBoxLayout *parentLayout, QWidget *theMainThing)
{
QHBoxLayout *WPMLayout = new QHBoxLayout(theMainThing);
parentLayout->addLayout(WPMLayout);
WPMLayout->addWidget(m_ui->letter = new QLabel(""));
WPMLayout->addWidget(m_ui->WPM = new QLabel(""));
QFont font = m_ui->WPM->font();
font.setPointSize(font.pointSize() * 2); // A cheap way of getting bigger regardless of the default stylee
font.setBold(true);
m_ui->WPM->setFont(font);
font = m_ui->letter->font();
font.setPointSize(font.pointSize() * 2);
font.setBold(true);
m_ui->letter->setFont(font);
}
void Morse::setupTopButtons(QLayout *parentLayout)
{
QPushButton *button;
QSizePolicy policy;
QIcon homeIcon(":/icons/go-home.png");
button = m_ui->homeButton = new QPushButton(homeIcon, tr("Home"));
policy = button->sizePolicy();
policy.setHorizontalPolicy(QSizePolicy::Fixed);
button->setSizePolicy(policy);
parentLayout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(goHome()));
button = m_ui->helpButton = new QPushButton(tr("Help"));
policy = button->sizePolicy();
policy.setHorizontalPolicy(QSizePolicy::Fixed);
button->setSizePolicy(policy);
parentLayout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(help()));
button = m_ui->play = new QPushButton(tr("Play"));
parentLayout->addWidget(button);
m_ui->play->setIcon(QIcon(":/icons/play.png"));
button = m_ui->changeSequence = new QPushButton(tr("Sequence"));
parentLayout->addWidget(button);
button = m_ui->changeWords = new QPushButton(tr("Change Words"));
parentLayout->addWidget(button);
}
void Morse::createModesMenuButton(QPushButton *modeButton) {
// Create the "mode" menu
QMenu *modeMenu = new QMenu(modeButton);
modeButton->setMenu(modeMenu);
createModesMenu(modeMenu);
}
void Morse::createModesMenu(QMenu *modeMenu) {
QAction *action = modeMenu->addAction(m_modes[PLAY]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::PLAY);
QMenu *trainingMenu = modeMenu->addMenu(tr("Training"));
action = trainingMenu->addAction(m_modes[TRAIN]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::TRAIN);
action = trainingMenu->addAction(m_modes[SPEEDTRAIN]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::SPEEDTRAIN);
action = trainingMenu->addAction(m_modes[WORDS]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::WORDS);
action = trainingMenu->addAction(m_modes[GROUPS]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::GROUPS);
action = trainingMenu->addAction(m_modes[KEYING]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::KEYING);
QMenu *gamesMenu = modeMenu->addMenu(tr("Games"));
action = gamesMenu->addAction(m_modes[WORDGAME]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::WORDGAME);
action = gamesMenu->addAction(m_modes[GROUPGAME]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::GROUPGAME);
action = modeMenu->addAction(m_modes[READ]->name());
connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(action, (int) Morse::READ);
connect(m_signalMapper, SIGNAL(mappedInt(int)), this, SLOT(switchMode(int)));
#ifdef SMALL_DEVICE
m_ui->modeMenu->setText(m_modes[PLAY]->name());
#endif
}
void Morse::setupConnections()
{
connect(m_ui->play, SIGNAL(clicked()), this, SLOT(playButton()));
}
MainWindow *Morse::parent()
{
return m_parent;
}
void Morse::goHome()
{
switchMode(FRONTPAGE);
}
void Morse::help()
{
m_modes[m_gameMode]->help();
}
int Morse::tone()
{
return m_tone;
}
void Morse::setWPMGoal(int wpmGoal)
{
m_currentWPMGoal = wpmGoal;
}
void Morse::setWPMAccept(int wpmAccept)
{
m_currentWPMAccept = wpmAccept;
}
void Morse::setTone(int tone)
{
m_tone = tone;
}
float Morse::dahSecsF()
{
return m_dahSecs;
}
float Morse::pauseSecsF()
{
return m_pauseSecs;
}
float Morse::letterPauseSecsF()
{
return m_letterPauseSecs;
}
float Morse::spaceSecsF()
{
return m_spaceSecs;
}
const QList<Morse::ditdah> *Morse::getLetterCode(QChar letter) const
{
return code[letter];
}
int Morse::ditSecsMS()
{
return int(m_ditSecs * 1000.0);
}
int Morse::dahSecsMS()
{
return int(m_dahSecs * 1000.0);
}
int Morse::pauseSecsMS()
{
return int(m_pauseSecs * 1000.0);
}
int Morse::letterPauseSecsMS()
{
return int(m_letterPauseSecs * 1000.0);
}
int Morse::spaceSecsMS()
{
return int(m_spaceSecs * 1000.0);
}