From 1dc5cd47d40cb848133133f5de8d56cb4c6adb54 Mon Sep 17 00:00:00 2001 From: Egor Nemtsev Date: Fri, 25 Oct 2019 10:31:58 +0300 Subject: [settings] add ability to change audio device - in settings it is possible to check needed audio device Task-number: AUTOSUITE-1320 Change-Id: I7c094a4b4dba2beba593b3fce050bd3bc62f9309 Reviewed-by: Bramastyo Harimukti Santoso --- app/SettingsView.qml | 19 +++++++++++++++-- plugins/alexainterface/AlexaInterface.cpp | 11 ++++++++++ plugins/alexainterface/AlexaInterface.h | 8 +++++++- plugins/alexainterface/QtMicrophoneWrapper.cpp | 28 ++++++++++++++++++++++---- plugins/alexainterface/QtMicrophoneWrapper.h | 6 +++++- 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/app/SettingsView.qml b/app/SettingsView.qml index 2abd0bf..0a65d21 100644 --- a/app/SettingsView.qml +++ b/app/SettingsView.qml @@ -93,16 +93,21 @@ Item { ListItem { text: qsTr("Recording devices") Layout.fillWidth: true + rightToolSymbol: "ic-update" + onRightToolClicked: { + //refresh model + deviceListBind.value = AlexaInterface.deviceList + } } + ListView { id: listView - model: AlexaInterface.deviceList height: childrenRect.height Layout.fillWidth: true interactive: false delegate: RadioButton { - checked: modelData === "default" + checked: AlexaInterface.deviceName === modelData width: parent.width height: Sizes.dp(50) font.pixelSize: Sizes.fontSizeXS @@ -111,6 +116,16 @@ Item { text: modelData spacing: Sizes.dp(10) checkable: false + onClicked: { + AlexaInterface.deviceName = modelData + } + } + + Binding { + id: deviceListBind + target: listView + property: "model" + value: AlexaInterface.deviceList } } diff --git a/plugins/alexainterface/AlexaInterface.cpp b/plugins/alexainterface/AlexaInterface.cpp index ab4c95a..f3a05e8 100644 --- a/plugins/alexainterface/AlexaInterface.cpp +++ b/plugins/alexainterface/AlexaInterface.cpp @@ -921,6 +921,7 @@ bool AlexaInterface::initialize( bool processAudioLevel = settings.value(QStringLiteral("capture/process_input"), true).toBool(); m_micWrapper->setLevelProcess(processAudioLevel); QObject::connect(m_micWrapper.get(), &QtMicrophoneWrapper::audioLevelChanged, this, &AlexaInterface::audioLevelChanged); + QObject::connect(m_micWrapper.get(), &QtMicrophoneWrapper::deviceNameChanged, this, &AlexaInterface::deviceNameChanged); // Creating wake word audio provider, if necessary @@ -1093,3 +1094,13 @@ void AlexaInterface::onConnectionStatusChanged() { m_connectionStatus = m_connectionManager->connectionStatus(); Q_EMIT connectionStatusChanged(); } + +void AlexaInterface::setDeviceName(const QString &audioDeviceName) +{ + if (m_micWrapper) { + m_micWrapper->setDeviceName(audioDeviceName); + + QSettings settings(QStringLiteral("Luxoft Sweden AB"), QStringLiteral("AlexaApp")); + settings.setValue(QStringLiteral("capture/device_name"), audioDeviceName); + } +} diff --git a/plugins/alexainterface/AlexaInterface.h b/plugins/alexainterface/AlexaInterface.h index 8061531..5a4306f 100644 --- a/plugins/alexainterface/AlexaInterface.h +++ b/plugins/alexainterface/AlexaInterface.h @@ -62,6 +62,7 @@ #include #include #include +#include #include "WeatherCard.h" #include "InfoCard.h" @@ -88,7 +89,8 @@ class AlexaInterface: public QObject { Q_PROPERTY(ConnectionManager::ConnectionStatus connectionStatus READ connectionStatus NOTIFY connectionStatusChanged) Q_PROPERTY(LogLevel logLevel READ logLevel WRITE setLogLevel NOTIFY logLevelChanged) Q_PROPERTY(qreal audioLevel READ audioLevel NOTIFY audioLevelChanged) - Q_PROPERTY(QStringList deviceList READ deviceList) + Q_PROPERTY(QStringList deviceList READ deviceList NOTIFY deviceListChanged) + Q_PROPERTY(QString deviceName READ deviceName WRITE setDeviceName NOTIFY deviceNameChanged) public: @@ -121,6 +123,8 @@ public: LogLevel logLevel() const { return m_logLevel; } qreal audioLevel() const { return m_micWrapper ? m_micWrapper->audioLevel() : 0.0; } QStringList deviceList() const { return m_micWrapper ? m_micWrapper->deviceList() : QStringList(); } + QString deviceName() const { return m_micWrapper ? m_micWrapper->deviceName() : ""; } + void setDeviceName(const QString &audioDeviceName); explicit AlexaInterface(QObject* parent = nullptr); /// Destructor which manages the @c AlexaInterface shutdown sequence. @@ -218,6 +222,8 @@ Q_SIGNALS: void logLevelChanged(); void cardReady(BaseCard *card); void audioLevelChanged(); + void deviceListChanged(); + void deviceNameChanged(); private: static std::unique_ptr instance; diff --git a/plugins/alexainterface/QtMicrophoneWrapper.cpp b/plugins/alexainterface/QtMicrophoneWrapper.cpp index 1573e3a..ffd18ff 100644 --- a/plugins/alexainterface/QtMicrophoneWrapper.cpp +++ b/plugins/alexainterface/QtMicrophoneWrapper.cpp @@ -82,6 +82,7 @@ bool QtMicrophoneWrapper::startStreamingMicrophoneData() { m_audioInputIODevice = m_audioInput->start(); if (m_audioInput->error() != QAudio::NoError) { + m_audioInputIODevice = nullptr; qWarning() << "Start stream error:" << m_audioInput->error(); return false; } @@ -110,8 +111,8 @@ bool QtMicrophoneWrapper::stopStreamingMicrophoneData() { return true; } -void QtMicrophoneWrapper::setAudioDevice(const QString &deviceName) { - qDebug() << "Trying to select input device: " << deviceName; +void QtMicrophoneWrapper::setAudioDevice(const QString &audioDeviceName) { + qDebug() << "Trying to select input device: " << audioDeviceName; QAudioFormat format; format.setSampleRate(SAMPLE_RATE); @@ -128,8 +129,9 @@ void QtMicrophoneWrapper::setAudioDevice(const QString &deviceName) { qDebug() << "Available capture devices:" << devices.size(); for (QAudioDeviceInfo &device : devices) { qDebug() << " device name: " << device.deviceName(); - if (device.deviceName() == deviceName) { + if (device.deviceName() == audioDeviceName) { audioInfo = device; + m_deviceName = audioDeviceName; } } @@ -147,7 +149,6 @@ void QtMicrophoneWrapper::setAudioDevice(const QString &deviceName) { m_audioLevelInfo.init(m_audioInput->format()); QObject::connect(m_audioInput, &QAudioInput::notify, this, [this](){ - QByteArray readBytes = m_audioInputIODevice->readAll(); m_readAudioData.append(readBytes); m_readAudioDataBytes += readBytes.count(); @@ -184,6 +185,25 @@ QStringList QtMicrophoneWrapper::deviceList() const return deviceNames; } +void QtMicrophoneWrapper::setDeviceName(const QString &audioDeviceName) +{ + if (m_audioInput) { + m_audioInput->stop(); + if (m_audioInputIODevice) { + m_audioInputIODevice->close(); + m_audioInputIODevice = nullptr; + } + delete m_audioInput; + m_audioInput = nullptr; + m_readAudioData.clear(); + m_readAudioDataBytes = 0; + m_deviceName = ""; + } + setAudioDevice(audioDeviceName); + startStreamingMicrophoneData(); + Q_EMIT deviceNameChanged(); +} + AudioLevelInfo::AudioLevelInfo(const QAudioFormat &format) { init(format); diff --git a/plugins/alexainterface/QtMicrophoneWrapper.h b/plugins/alexainterface/QtMicrophoneWrapper.h index 1c0def0..6637f5a 100644 --- a/plugins/alexainterface/QtMicrophoneWrapper.h +++ b/plugins/alexainterface/QtMicrophoneWrapper.h @@ -115,9 +115,12 @@ public: qreal audioLevel() const { return m_audioLevel; } void setLevelProcess(bool enable) { m_levelProcess = enable; } QStringList deviceList() const; + QString deviceName() const { return m_deviceName; } + void setDeviceName(const QString &audioDeviceName); Q_SIGNALS: void audioLevelChanged(); + void deviceNameChanged(); private: /** @@ -133,11 +136,12 @@ private: qreal m_audioLevel = 0.0; // 0.0 <= m_audioLevel <= 1.0 bool m_levelProcess = false; AudioLevelInfo m_audioLevelInfo; + QString m_deviceName; /// Initializes Audio bool initialize(const QString &deviceName); - void setAudioDevice(const QString &deviceName); + void setAudioDevice(const QString &audioDeviceName); /// The stream of audio data. const std::shared_ptr m_audioInputStream; -- cgit v1.2.3