From f91525b65b07d97b045d70a523fa1cfd964f03d9 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Sat, 30 Nov 2013 18:24:46 +0100 Subject: [PATCH] Expanded voice sample to an interactive app refs #36 - Based on the existing network sample, expanded the voice sample to an interactive app - Implmenented commands: mic test, squelch test refs #81 --- samples/voiceclient/client.cpp | 102 ++++++++++++++++++++ samples/voiceclient/client.h | 45 +++++++++ samples/voiceclient/main.cpp | 19 ++-- samples/voiceclient/reader.cpp | 24 +++++ samples/voiceclient/reader.h | 25 +++++ samples/voiceclient/sample_voice_client.pro | 4 +- src/blackcore/voiceclient.h | 1 + src/blackcore/voiceclient_vatlib.cpp | 9 +- src/blackcore/voiceclient_vatlib.h | 2 + 9 files changed, 218 insertions(+), 13 deletions(-) create mode 100644 samples/voiceclient/client.cpp create mode 100644 samples/voiceclient/client.h create mode 100644 samples/voiceclient/reader.cpp create mode 100644 samples/voiceclient/reader.h diff --git a/samples/voiceclient/client.cpp b/samples/voiceclient/client.cpp new file mode 100644 index 000000000..1a8f94785 --- /dev/null +++ b/samples/voiceclient/client.cpp @@ -0,0 +1,102 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "client.h" + +Client::Client(QObject *parent) : + QObject(parent), + m_voiceClient(&BlackMisc::IContext::getInstance().getObject()) +{ + using namespace BlackCore; + connect(m_voiceClient, &IVoiceClient::squelchTestFinished, this, &Client::onSquelchTestFinished); + connect(m_voiceClient, &IVoiceClient::micTestFinished, this, &Client::onMicTestFinished); + + + + using namespace std::placeholders; + m_commands["help"] = std::bind(&Client::help, this, _1); + m_commands["echo"] = std::bind(&Client::echo, this, _1); + m_commands["exit"] = std::bind(&Client::exit, this, _1); + m_commands["runsquelchtest"] = std::bind(&Client::runSquelchTest, this, _1); + m_commands["runmictest"] = std::bind(&Client::runMicTest, this, _1); + +} + +void Client::command(QString line) +{ + QTextStream stream(&line, QIODevice::ReadOnly); + QString cmd; + stream >> cmd; + stream.skipWhiteSpace(); + + auto found = m_commands.find(cmd); + if (found == m_commands.end()) + { + std::cout << "No such command" << std::endl; + printLinePrefix(); + } + else + { + (*found)(stream); + } +} + +void Client::printLinePrefix() +{ + std::cout << "voice> "; + std::cout.flush(); +} + +/****************************************************************************/ +/************ Commands *************/ +/****************************************************************************/ + +void Client::help(QTextStream &) +{ + std::cout << "Commands:" << std::endl; + auto keys = m_commands.keys(); + for (auto i = keys.begin(); i != keys.end(); ++i) + { + std::cout << " " << i->toStdString() << std::endl; + } + printLinePrefix(); +} + +void Client::echo(QTextStream &line) +{ + std::cout << "echo: " << line.readAll().toStdString() << std::endl; + printLinePrefix(); +} + +void Client::exit(QTextStream &) +{ + emit quit(); +} + +void Client::runSquelchTest(QTextStream &args) +{ + std::cout << "Running squelch test. Please be quiet for 5 seconds..." << std::endl; + printLinePrefix(); + m_voiceClient->runSquelchTest(); +} + +void Client::runMicTest(QTextStream &args) +{ + std::cout << "Running mic test. Speak normally for 5 seconds..." << std::endl; + printLinePrefix(); + m_voiceClient->runMicTest(); +} + +void Client::onSquelchTestFinished() +{ + std::cout << "Input squelch: " << m_voiceClient->inputSquelch() << std::endl; + printLinePrefix(); +} + +void Client::onMicTestFinished() +{ + std::cout << "Mic test result: " << (int)m_voiceClient->micTestResult() << std::endl; + printLinePrefix(); +} diff --git a/samples/voiceclient/client.h b/samples/voiceclient/client.h new file mode 100644 index 000000000..32dc783cc --- /dev/null +++ b/samples/voiceclient/client.h @@ -0,0 +1,45 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef CLIENT_H +#define CLIENT_H + +#include "blackcore/voiceclient.h" + +#include +#include + +class Client : public QObject +{ + Q_OBJECT +public: + Client(QObject *parent = 0); + +signals: + void quit(); + +public slots: + void command(QString line); + +private: //commands + void help(QTextStream &args); + void echo(QTextStream &args); + void exit(QTextStream &args); + void runSquelchTest(QTextStream &args); + void runMicTest(QTextStream &args); + + void printLinePrefix(); + +public slots: + void onSquelchTestFinished(); + void onMicTestFinished(); + +private: + QMap> m_commands; + BlackCore::IVoiceClient *m_voiceClient; + +}; + +#endif // CLIENT_H diff --git a/samples/voiceclient/main.cpp b/samples/voiceclient/main.cpp index 8c9d56b7b..877d5f42d 100644 --- a/samples/voiceclient/main.cpp +++ b/samples/voiceclient/main.cpp @@ -4,6 +4,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "blackcore/voiceclient_vatlib.h" + +#include "client.h" +#include "reader.h" + #include #include @@ -15,15 +19,12 @@ int main(int argc, char *argv[]) BlackMisc::IContext::getInstance().setObject(*new BlackMisc::CDebug()); BlackMisc::IContext::getInstance().setObject(*new BlackCore::CVoiceClientVatlib()); - BlackCore::IVoiceClient *voiceClient = BlackMisc::IContext::getInstance().singleton(); - QList outputDevices = voiceClient->audioOutputDevices(0); - - qDebug() << "Found " << outputDevices.size() << " output devices:"; - - foreach (COutputAudioDevice device, outputDevices) - { - qDebug() << device.name(); - } + Client client; + LineReader reader; + QObject::connect(&reader, SIGNAL(command(const QString&)), &client, SLOT(command(const QString&))); + QObject::connect(&client, SIGNAL(quit()), &reader, SLOT(terminate())); + QObject::connect(&client, SIGNAL(quit()), &app, SLOT(quit())); + reader.start(); app.exec(); } diff --git a/samples/voiceclient/reader.cpp b/samples/voiceclient/reader.cpp new file mode 100644 index 000000000..6265963c6 --- /dev/null +++ b/samples/voiceclient/reader.cpp @@ -0,0 +1,24 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +#include "reader.h" +#include +#include + +void LineReader::run() +{ + QFile file; + file.open(stdin, QIODevice::ReadOnly | QIODevice::Text); + forever + { + std::cout << "voice> "; + QString line = file.readLine().trimmed(); + if (! line.isEmpty()) + { + emit command(line); + } + } +} diff --git a/samples/voiceclient/reader.h b/samples/voiceclient/reader.h new file mode 100644 index 000000000..5c80214e7 --- /dev/null +++ b/samples/voiceclient/reader.h @@ -0,0 +1,25 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef READER_H +#define READER_H + +#include + +class LineReader : public QThread +{ + Q_OBJECT +public: + explicit LineReader() {} + +protected: + void run(); + +signals: + void command(const QString& line); + +}; + +#endif // READER_H diff --git a/samples/voiceclient/sample_voice_client.pro b/samples/voiceclient/sample_voice_client.pro index ddd23a14c..df8fee6ac 100644 --- a/samples/voiceclient/sample_voice_client.pro +++ b/samples/voiceclient/sample_voice_client.pro @@ -13,7 +13,9 @@ DEPENDPATH += . ../../src INCLUDEPATH += . ../../src SOURCES += *.cpp -HEADERS += *.h +HEADERS += *.h \ + client.h \ + reader.h LIBS += -L../../lib -lblackcore -lblackmisc LIBS += -lvatlib diff --git a/src/blackcore/voiceclient.h b/src/blackcore/voiceclient.h index 23e3dba8a..ea9045d78 100644 --- a/src/blackcore/voiceclient.h +++ b/src/blackcore/voiceclient.h @@ -76,6 +76,7 @@ namespace BlackCore virtual void runMicTest() = 0; virtual float inputSquelch() const = 0; + virtual Cvatlib_Voice_Simple::agc micTestResult() const = 0; virtual const BlackMisc::Voice::CVoiceRoom &voiceRoom (const uint32_t comUnit) = 0; diff --git a/src/blackcore/voiceclient_vatlib.cpp b/src/blackcore/voiceclient_vatlib.cpp index edd3aa32d..4d8b8a282 100644 --- a/src/blackcore/voiceclient_vatlib.cpp +++ b/src/blackcore/voiceclient_vatlib.cpp @@ -177,6 +177,11 @@ namespace BlackCore return m_inputSquelch; } + Cvatlib_Voice_Simple::agc CVoiceClientVatlib::micTestResult() const + { + return m_micTestResult; + } + const BlackMisc::Voice::CVoiceRoom &CVoiceClientVatlib::voiceRoom(const uint32_t comUnit) { @@ -202,7 +207,6 @@ namespace BlackCore //TODO: store captured squelch m_inputSquelch = m_voice->GetInputSquelch(); - qDebug() << m_inputSquelch; emit squelchTestFinished(); } catch (...) { this->exceptionDispatcher(Q_FUNC_INFO); } @@ -214,8 +218,7 @@ namespace BlackCore { Q_ASSERT_X (m_voice->IsValid() && m_voice->IsSetup(), "CVoiceClientVatlib", "Cvatlib_Voice_Simple invalid or not setup!"); - Cvatlib_Voice_Simple::agc micTestResult = m_voice->EndMicTest(); - qDebug() << micTestResult; + m_micTestResult = m_voice->EndMicTest(); emit micTestFinished(); } catch (...) { this->exceptionDispatcher(Q_FUNC_INFO); } diff --git a/src/blackcore/voiceclient_vatlib.h b/src/blackcore/voiceclient_vatlib.h index 4700f85a2..68585206b 100644 --- a/src/blackcore/voiceclient_vatlib.h +++ b/src/blackcore/voiceclient_vatlib.h @@ -53,6 +53,7 @@ namespace BlackCore virtual void runMicTest(); virtual float inputSquelch() const; + virtual Cvatlib_Voice_Simple::agc micTestResult() const; virtual const BlackMisc::Voice::CVoiceRoom &voiceRoom (const uint32_t comUnit); @@ -94,6 +95,7 @@ namespace BlackCore QList m_outputDevices; float m_inputSquelch; + Cvatlib_Voice_Simple::agc m_micTestResult; }; } // namespace BlackCore