mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 02:57:10 +02:00
Update speech recognizer to fully turn off when disabling
This commit is contained in:
parent
3e572cdf76
commit
3e456b0ec9
2 changed files with 46 additions and 25 deletions
|
@ -13,6 +13,8 @@
|
||||||
#define hifi_SpeechRecognizer_h
|
#define hifi_SpeechRecognizer_h
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QSet>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
class SpeechRecognizer : public QObject {
|
class SpeechRecognizer : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -20,20 +22,24 @@ public:
|
||||||
SpeechRecognizer();
|
SpeechRecognizer();
|
||||||
~SpeechRecognizer();
|
~SpeechRecognizer();
|
||||||
|
|
||||||
void init();
|
|
||||||
void handleCommandRecognized(const char* command);
|
void handleCommandRecognized(const char* command);
|
||||||
bool getEnabled() { return _enabled; }
|
bool getEnabled() const { return _enabled; }
|
||||||
void setEnabled(bool enabled);
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void setEnabled(bool enabled);
|
||||||
void addCommand(const QString& command);
|
void addCommand(const QString& command);
|
||||||
void removeCommand(const QString& command);
|
void removeCommand(const QString& command);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void commandRecognized(const QString& command);
|
void commandRecognized(const QString& command);
|
||||||
|
void enabledUpdated(bool enabled);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void reloadCommands();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _enabled;
|
bool _enabled;
|
||||||
|
QSet<QString> _commands;
|
||||||
void* _speechRecognizerDelegate;
|
void* _speechRecognizerDelegate;
|
||||||
void* _speechRecognizer;
|
void* _speechRecognizer;
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,8 +9,12 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <QtGlobal>
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <AppKit/NSSpeechRecognizer.h>
|
#import <AppKit/NSSpeechRecognizer.h>
|
||||||
|
#import <AppKit/NSWorkspace.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
@ -40,10 +44,11 @@
|
||||||
SpeechRecognizer::SpeechRecognizer() :
|
SpeechRecognizer::SpeechRecognizer() :
|
||||||
QObject(),
|
QObject(),
|
||||||
_enabled(false),
|
_enabled(false),
|
||||||
_speechRecognizerDelegate(NULL),
|
_commands(),
|
||||||
|
_speechRecognizerDelegate([[SpeechRecognizerDelegate alloc] init]),
|
||||||
_speechRecognizer(NULL) {
|
_speechRecognizer(NULL) {
|
||||||
|
|
||||||
init();
|
[(id)_speechRecognizerDelegate setListener:this];
|
||||||
}
|
}
|
||||||
|
|
||||||
SpeechRecognizer::~SpeechRecognizer() {
|
SpeechRecognizer::~SpeechRecognizer() {
|
||||||
|
@ -55,40 +60,50 @@ SpeechRecognizer::~SpeechRecognizer() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeechRecognizer::init() {
|
|
||||||
_speechRecognizerDelegate = [[SpeechRecognizerDelegate alloc] init];
|
|
||||||
[(id)_speechRecognizerDelegate setListener:this];
|
|
||||||
|
|
||||||
_speechRecognizer = [[NSSpeechRecognizer alloc] init];
|
|
||||||
|
|
||||||
[(id)_speechRecognizer setCommands:[NSArray array]];
|
|
||||||
[(id)_speechRecognizer setDelegate:(id)_speechRecognizerDelegate];
|
|
||||||
|
|
||||||
setEnabled(_enabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpeechRecognizer::handleCommandRecognized(const char* command) {
|
void SpeechRecognizer::handleCommandRecognized(const char* command) {
|
||||||
qDebug() << "Got command: " << command;
|
|
||||||
emit commandRecognized(QString(command));
|
emit commandRecognized(QString(command));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeechRecognizer::setEnabled(bool enabled) {
|
void SpeechRecognizer::setEnabled(bool enabled) {
|
||||||
|
if (enabled == _enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_enabled = enabled;
|
_enabled = enabled;
|
||||||
if (enabled) {
|
if (_enabled) {
|
||||||
|
_speechRecognizer = [[NSSpeechRecognizer alloc] init];
|
||||||
|
|
||||||
|
reloadCommands();
|
||||||
|
|
||||||
|
[(id)_speechRecognizer setDelegate:(id)_speechRecognizerDelegate];
|
||||||
[(id)_speechRecognizer startListening];
|
[(id)_speechRecognizer startListening];
|
||||||
} else {
|
} else {
|
||||||
[(id)_speechRecognizer stopListening];
|
[(id)_speechRecognizer stopListening];
|
||||||
|
[(id)_speechRecognizer dealloc];
|
||||||
|
_speechRecognizer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit enabledUpdated(_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpeechRecognizer::reloadCommands() {
|
||||||
|
if (_speechRecognizer) {
|
||||||
|
NSMutableArray* cmds = [NSMutableArray array];
|
||||||
|
for (QSet<QString>::const_iterator iter = _commands.constBegin(); iter != _commands.constEnd(); iter++) {
|
||||||
|
[cmds addObject:[NSString stringWithUTF8String:(*iter).toLocal8Bit().data()]];
|
||||||
|
}
|
||||||
|
[(id)_speechRecognizer setCommands:cmds];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeechRecognizer::addCommand(const QString& command) {
|
void SpeechRecognizer::addCommand(const QString& command) {
|
||||||
NSArray *cmds = [(id)_speechRecognizer commands];
|
_commands.insert(command);
|
||||||
NSString *cmd = [NSString stringWithUTF8String:command.toLocal8Bit().data()];
|
reloadCommands();
|
||||||
[(id)_speechRecognizer setCommands:[cmds arrayByAddingObject:cmd]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeechRecognizer::removeCommand(const QString& command) {
|
void SpeechRecognizer::removeCommand(const QString& command) {
|
||||||
NSMutableArray* cmds = [NSMutableArray arrayWithArray:[(id)_speechRecognizer commands]];
|
_commands.remove(command);
|
||||||
[cmds removeObject:[NSString stringWithUTF8String:command.toLocal8Bit().data()]];
|
reloadCommands();
|
||||||
[(id)_speechRecognizer setCommands:cmds];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif // Q_OS_MAC
|
||||||
|
|
Loading…
Reference in a new issue