Skip to content

Commit 87eebcc

Browse files
committed
-Tried suspending and recording to save resources, worked on Desktop but recorder stops posting data on Android.
-Added speech prompts -Split up next question transitions via events and callbacks, screen will now congratulate user and repeat the answer before continuing
1 parent 77b97f7 commit 87eebcc

8 files changed

+205
-122
lines changed

DeepSpeechC.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,23 @@ DeepSpeech::~DeepSpeech()
8484

8585
void DeepSpeech::startRecording()
8686
{
87+
qInfo()<<"Recording";
8788
std::lock_guard<std::mutex> guard(lock);
89+
if(recorder->state() == QAudio::State::SuspendedState)
90+
{
91+
recorder->resume();
92+
}
93+
qInfo()<<"Recorder state now is "<<recorder->state();
8894
int ret = DS_CreateStream(model, &state);
8995
qInfo()<<"Setting up Model Stream : "<<ret;
9096
}
9197

9298
void DeepSpeech::stopRecording()
9399
{
100+
qInfo()<<"Stopping Recording";
94101
std::lock_guard<std::mutex> guard(lock);
102+
//recorder->suspend();
103+
qInfo()<<"Recorder state now is "<<recorder->state();
95104
qInfo()<<"Clearing Hotwords and Freeing stream"<<this->thread();
96105
DS_ClearHotWords(model);
97106
if(state)
@@ -107,24 +116,24 @@ void DeepSpeech::setHotword(QString hotword)
107116
DS_AddHotWord(model, word.c_str(), 5.0f );
108117
}
109118

110-
void DeepSpeech::clearHotwords()
111-
{
112-
// DS_ClearHotWords(model);
113-
}
114-
115-
116119
void DeepSpeech::transcribeAudio()
117120
{
118121
try {
122+
//qInfo()<<"Buffer size "<<recorder->bufferSize();
119123
std::lock_guard<std::mutex> guard(lock);
124+
qInfo()<<"Bytes ready"<<recorder->bytesReady();
125+
QIODevice* device = static_cast<QIODevice*>(sender());
120126
if(state)
121127
{
122-
QIODevice* device = static_cast<QIODevice*>(sender());
123128
QByteArray data = device->readAll();
124129
qInfo()<<"Received audio bytes "<<data.size();
130+
if(data.size() > 50000)
131+
{//Optimisation for phones or slow hardware, skip these bytes to prevent snowballing buffer size
132+
return;
133+
}
125134
DS_FeedAudioContent(state, reinterpret_cast<const short*>(data.data()), data.size()/2);
126-
// qInfo()<<"Transcribed "<<data.size()<<" bytes...";
127-
qInfo()<<"Transcribing on thread "<<this->thread();
135+
// qInfo()<<"Transcribed "<<data.size()<<" bytes...";
136+
// qInfo()<<"Transcribing on thread "<<this->thread();
128137
samplesCollected += data.size()/2;
129138

130139
if(samplesCollected > 16000)
@@ -140,6 +149,7 @@ void DeepSpeech::transcribeAudio()
140149
samplesCollected = 0;
141150
}
142151
}
152+
143153
} catch (std::exception& e) {
144154
qInfo()<<e.what();
145155
}

DeepSpeechC.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public slots:
2020
void init();
2121
void transcribeAudio();
2222
void setHotword(QString hotword);
23-
void clearHotwords();
2423
void stopRecording();
2524
void startRecording();
2625
signals:
@@ -31,8 +30,11 @@ public slots:
3130
StreamingState* state = nullptr;
3231
int samplesCollected = 0;
3332
void doTranscription(QIODevice* device);
33+
void createRecorder();
3434

3535
std::mutex lock;
36+
bool recording = false;
37+
int idealSample = 0;
3638
};
3739

3840

Game.cpp

+58-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <QFileInfo>
22
#include <stdlib.h> /* srand, rand */
33
#include <time.h> /* time */
4+
#include <QCoreApplication>
5+
46

57
#include "Game.h"
68
#include "DeepSpeechC.h"
@@ -13,13 +15,15 @@ Game::Game(QObject *parent) : QObject(parent)
1315
player = new QMediaPlayer(this);
1416
loadMusic("audio/theme.mp3"); //To cache opening music for faster playback
1517

18+
speech = new QTextToSpeech(this);
19+
1620
qInfo()<<"Game created on thread "<<this->thread();
1721
ds.moveToThread(&deepThread);
1822
deepThread.start();
1923
ds.init();
2024
//Subscribe to all transcribed text
2125
connect(&ds,&DeepSpeech::transcript, this, &Game::userSpeech);
22-
26+
connect(speech,&QTextToSpeech::stateChanged, this, &Game::speechDone);
2327
//Create questions
2428
qsns.push_back( {"cat", "qrc:/img/cat.png"});
2529
qsns.push_back( {"bus", "qrc:/img/bus.png"});
@@ -46,9 +50,9 @@ Game* Game::getGame(QQmlEngine *engine, QJSEngine *scriptEngine)
4650
void Game::listenFor(QString word)
4751
{
4852
qInfo()<<"Game listenFor "<<word<<" on "<<this->thread();
49-
listeningFor = word;
50-
ds.stopRecording(); //Clear stream and recoup memory
53+
listeningFor = word;
5154
ds.startRecording();
55+
ds.setHotword("quit");
5256
ds.setHotword(word);
5357
}
5458

@@ -66,17 +70,49 @@ void Game::loadMusic(QString file)
6670
player->setMedia(QUrl("qrc:/"+file));
6771
}
6872

73+
void Game::speechDone(QTextToSpeech::State state)
74+
{
75+
if(state == QTextToSpeech::State::Ready)
76+
{
77+
if(gameState == GAME)
78+
{
79+
showNextQsn();
80+
}
81+
else if(gameState == QUIT)
82+
{
83+
QCoreApplication::quit();
84+
}
85+
}
86+
}
87+
6988
void Game::openingMenu()
7089
{
7190
qInfo()<<"Opening Menu";
7291
loadMusic("audio/theme.mp3");
7392
player->play();
93+
listenFor("start");
94+
}
7495

96+
void Game::speak(QString text)
97+
{
98+
speech->say(text);
7599
}
76100

77-
void Game::nextQsn()
101+
void Game::nextQsn(bool sayit)
78102
{
103+
if(sayit)
104+
{
105+
ds.stopRecording(); //Clear stream and recoup memory
106+
speak("Correct, this is a " + listeningFor);
107+
}
108+
else
109+
{
110+
showNextQsn();
111+
}
112+
}
79113

114+
void Game::showNextQsn()
115+
{
80116
int qsnNum =rand() % qsns.size();
81117
listenFor(qsns[qsnNum].name);
82118
emit showQsn(qsns[qsnNum].name, qsns[qsnNum].img);
@@ -89,20 +125,36 @@ void Game::userSpeech(QString text)
89125
{//Word detected
90126
if(gameState == INTRO)
91127
{
92-
93128
gameState = GAME;
94129
emit gameStart();
95130
nextQsn();
96131
}
97132
else
133+
{
134+
qInfo()<<"Correct, moving on to next question";
135+
nextQsn(true);
136+
}
137+
}
138+
else if(text.contains("quit"))
139+
{
140+
ds.stopRecording();
141+
if(gameState == GAME)
98142
{
99-
nextQsn();
143+
speak("This was a "+listeningFor+". Thank you for playing What's That, try again next time! Bye bye");
144+
gameState = QUIT;
100145
}
146+
else
147+
{
148+
QCoreApplication::quit();
149+
}
150+
151+
101152
}
102153
}
103154

104155
void Game::destroy()
105156
{
106157
if(backend)
107158
delete backend;
159+
//No need to delete QT classes, QT will automatically delete them
108160
}

Game.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <QMediaPlayer>
77
#include <QThread>
88
#include <string>
9+
#include <QTextToSpeech>
910

1011
#include "DeepSpeechC.h"
1112

1213
enum GameState
1314
{
1415
INTRO,
15-
GAME
16+
GAME,
17+
QUIT
1618
};
1719

1820
struct Question
@@ -33,6 +35,8 @@ public slots:
3335
void openingMenu();
3436
void userSpeech(QString text);
3537
void listenFor(QString word);
38+
void speak(QString text);
39+
void speechDone(QTextToSpeech::State state);
3640

3741
signals:
3842
void gameStart();
@@ -42,7 +46,8 @@ public slots:
4246
static Game* backend;
4347
QMediaPlayer* player=nullptr;
4448
void loadMusic(QString file);
45-
void nextQsn();
49+
void nextQsn(bool sayit=false);
50+
void showNextQsn();
4651

4752
DeepSpeech ds;
4853
QThread deepThread; //Run DeepSpeech in diff thread to prevent UI Lag
@@ -51,6 +56,8 @@ public slots:
5156

5257
std::vector<Question> qsns;
5358

59+
QTextToSpeech* speech;
60+
5461
};
5562

5663
#endif // GAME_H

Intro.qml

+12-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Page {
3333
]
3434

3535
Component.onCompleted: {
36-
Game.listenFor("start");
36+
//Game.listenFor("start");
3737
}
3838

3939
ColumnLayout {
@@ -113,10 +113,20 @@ Page {
113113
endFrame: 230
114114
enabled: true
115115
startFrame: 0
116+
property bool playedIntro: false
117+
property bool playedPrompt: false
116118
onCurrentFrameChanged: {
117-
if (currentFrame == 100)
119+
var frame = Math.floor(currentFrame);
120+
//console.log(frame);
121+
if (frame == 100 && playedIntro==false)
118122
{
119123
Game.openingMenu();
124+
playedIntro = true;
125+
}
126+
else if(frame == 199 && playedPrompt==false)
127+
{
128+
Game.speak("Say start to begin!");
129+
playedPrompt = true;
120130
}
121131
}
122132

WhatsThat.pro

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
QT += quick multimedia
1+
QT += quick multimedia texttospeech
22

33
CONFIG += c++11
44

0 commit comments

Comments
 (0)