diff options
Diffstat (limited to 'plugins/alexainterface/QtMicrophoneWrapper.cpp')
-rw-r--r-- | plugins/alexainterface/QtMicrophoneWrapper.cpp | 105 |
1 files changed, 69 insertions, 36 deletions
diff --git a/plugins/alexainterface/QtMicrophoneWrapper.cpp b/plugins/alexainterface/QtMicrophoneWrapper.cpp index cfbfed4..c3fe0ef 100644 --- a/plugins/alexainterface/QtMicrophoneWrapper.cpp +++ b/plugins/alexainterface/QtMicrophoneWrapper.cpp @@ -41,14 +41,15 @@ static const int SAMPLE_SIZE = 16; static const double LATENCY = 0.2; //seconds -std::unique_ptr<QtMicrophoneWrapper> QtMicrophoneWrapper::create( - std::shared_ptr<AudioInputStream> stream) { +std::unique_ptr<QtMicrophoneWrapper> QtMicrophoneWrapper::create(std::shared_ptr<AudioInputStream> stream, + const QString &deviceName) +{ if (!stream) { qWarning() << "QtMicrophoneWrapper: Invalid stream passed to QtMicrophoneWrapper"; return nullptr; } std::unique_ptr<QtMicrophoneWrapper> qtMicrophoneWrapper(new QtMicrophoneWrapper(stream)); - if (!qtMicrophoneWrapper->initialize()) { + if (!qtMicrophoneWrapper->initialize(deviceName)) { qWarning() << "QtMicrophoneWrapper: Failed to initialize QtMicrophoneWrapper"; return nullptr; } @@ -62,45 +63,14 @@ QtMicrophoneWrapper::QtMicrophoneWrapper(std::shared_ptr<AudioInputStream> strea QtMicrophoneWrapper::~QtMicrophoneWrapper() { } -bool QtMicrophoneWrapper::initialize() { +bool QtMicrophoneWrapper::initialize(const QString &deviceName) { m_writer = m_audioInputStream->createWriter(AudioInputStream::Writer::Policy::NONBLOCKABLE); if (!m_writer) { qWarning("QtMicrophoneWrapper: Failed to create stream writer"); return false; } - QAudioFormat format; - format.setSampleRate(SAMPLE_RATE); - format.setChannelCount(NUM_INPUT_CHANNELS); - format.setSampleSize(SAMPLE_SIZE); - format.setCodec("audio/pcm"); - format.setByteOrder(QAudioFormat::LittleEndian); - format.setSampleType(QAudioFormat::SignedInt); - - QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice(); - if (!info.isFormatSupported(format)) { - qWarning() << "QtMicrophoneWrapper: Default format not supported, trying to use the nearest."; - format = info.nearestFormat(format); - } - - m_audioInput = new QAudioInput(format, this); - QObject::connect( m_audioInput, &QAudioInput::notify, this, [this](){ - QByteArray readBytes = m_audioInputIODevice->readAll(); - m_readAudioData.append(readBytes); - m_readAudioDataBytes += readBytes.count(); - - size_t nWords = m_writer->getWordSize() != 0 ? - static_cast<size_t>(m_readAudioDataBytes)/m_writer->getWordSize() : - static_cast<size_t>(m_readAudioDataBytes); - m_writer->write(m_readAudioData.data(), nWords); - - m_readAudioData.clear(); - m_readAudioDataBytes = 0; - }); - - int latency = static_cast<int>(LATENCY * 1000); - m_audioInput->setNotifyInterval(latency); - qDebug("QtMicrophoneWrapper: Latency is configured to: %d ms", m_audioInput->notifyInterval()); + setAudioDevice(deviceName); return true; } @@ -110,6 +80,18 @@ bool QtMicrophoneWrapper::startStreamingMicrophoneData() { m_readAudioDataBytes = 0; m_audioInputIODevice = m_audioInput->start(); + + if (m_audioInput->error() != QAudio::NoError) { + qWarning() << "Start stream error:" << m_audioInput->error(); + return false; + } + + if (m_audioInput->state() != QAudio::ActiveState + && m_audioInput->state() != QAudio::IdleState) { + qWarning() << "Wrong input state:" << m_audioInput->state(); + return false; + } + QByteArray readBytes = m_audioInputIODevice->readAll(); m_readAudioData.append(readBytes); @@ -128,3 +110,54 @@ bool QtMicrophoneWrapper::stopStreamingMicrophoneData() { return true; } +void QtMicrophoneWrapper::setAudioDevice(const QString &deviceName) { + qDebug() << "Trying to select input device: " << deviceName; + + QAudioFormat format; + format.setSampleRate(SAMPLE_RATE); + format.setChannelCount(NUM_INPUT_CHANNELS); + format.setSampleSize(SAMPLE_SIZE); + format.setCodec("audio/pcm"); + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + + m_audioInfo = QAudioDeviceInfo::defaultInputDevice(); + + QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); + + qDebug() << "Available capture devices:" << devices.size(); + for (QAudioDeviceInfo &device : devices) { + qDebug() << " device name: " << device.deviceName(); + if (device.deviceName() == deviceName) { + m_audioInfo = device; + } + } + + qDebug() << "Selected capture device:" << m_audioInfo.deviceName(); + qDebug() << "Requested format" << format; + + if (!m_audioInfo.isFormatSupported(format)) { + qWarning() << "QtMicrophoneWrapper: Default format not supported, trying to use the nearest."; + format = m_audioInfo.nearestFormat(format); + qWarning() << "QtMicrophoneWrapper: Nearest format" << format; + } + + m_audioInput = new QAudioInput(m_audioInfo, format, this); + QObject::connect(m_audioInput, &QAudioInput::notify, this, [this](){ + + QByteArray readBytes = m_audioInputIODevice->readAll(); + m_readAudioData.append(readBytes); + m_readAudioDataBytes += readBytes.count(); + + size_t nWords = m_writer->getWordSize() != 0 ? + static_cast<size_t>(m_readAudioDataBytes)/m_writer->getWordSize() : + static_cast<size_t>(m_readAudioDataBytes); + m_writer->write(m_readAudioData.data(), nWords); + m_readAudioData.clear(); + m_readAudioDataBytes = 0; + }); + + int latency = static_cast<int>(LATENCY * 1000); + m_audioInput->setNotifyInterval(latency); + qDebug("QtMicrophoneWrapper: Latency is configured to: %d ms", m_audioInput->notifyInterval()); +} |