2016-12-25 19:04:19 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2016 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
2019-04-11 12:32:15 +00:00
|
|
|
// (at your option) any later version. //
|
2016-12-25 19:04:19 +00:00
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <QAudioFormat>
|
|
|
|
#include <QAudioDeviceInfo>
|
|
|
|
#include <QAudioInput>
|
|
|
|
#include "audio/audioinput.h"
|
|
|
|
#include "audio/audiofifo.h"
|
|
|
|
|
|
|
|
AudioInput::AudioInput() :
|
2018-01-06 05:12:30 +00:00
|
|
|
m_mutex(QMutex::Recursive),
|
2016-12-25 19:04:19 +00:00
|
|
|
m_audioInput(0),
|
|
|
|
m_audioUsageCount(0),
|
|
|
|
m_onExit(false),
|
2017-01-06 14:28:01 +00:00
|
|
|
m_volume(0.5f),
|
2016-12-25 19:04:19 +00:00
|
|
|
m_audioFifos()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioInput::~AudioInput()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
2018-03-24 03:18:58 +00:00
|
|
|
for (std::list<AudioFifo*>::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
|
2016-12-25 19:04:19 +00:00
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_audioFifos.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioInput::start(int device, int rate)
|
|
|
|
{
|
|
|
|
if (m_audioUsageCount == 0)
|
|
|
|
{
|
2018-01-06 05:12:30 +00:00
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2016-12-25 19:04:19 +00:00
|
|
|
QAudioDeviceInfo devInfo;
|
|
|
|
|
|
|
|
if (device < 0)
|
|
|
|
{
|
|
|
|
devInfo = QAudioDeviceInfo::defaultInputDevice();
|
|
|
|
qWarning("AudioInput::start: using default device %s", qPrintable(devInfo.defaultInputDevice().deviceName()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QList<QAudioDeviceInfo> devicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
|
|
|
|
|
|
|
|
if (device < devicesInfo.size())
|
|
|
|
{
|
|
|
|
devInfo = devicesInfo[device];
|
2018-11-19 01:33:44 +00:00
|
|
|
qWarning("AudioInput::start: using audio device #%d: %s", device, qPrintable(devInfo.deviceName()));
|
2016-12-25 19:04:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
devInfo = QAudioDeviceInfo::defaultInputDevice();
|
2018-11-19 01:33:44 +00:00
|
|
|
qWarning("AudioInput::start: audio device #%d does not exist. Using default device %s", device, qPrintable(devInfo.deviceName()));
|
2016-12-25 19:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//QAudioDeviceInfo devInfo(QAudioDeviceInfo::defaultOutputDevice());
|
|
|
|
|
|
|
|
m_audioFormat.setSampleRate(rate);
|
|
|
|
m_audioFormat.setChannelCount(2);
|
|
|
|
m_audioFormat.setSampleSize(16);
|
|
|
|
m_audioFormat.setCodec("audio/pcm");
|
|
|
|
m_audioFormat.setByteOrder(QAudioFormat::LittleEndian);
|
2018-11-19 01:33:44 +00:00
|
|
|
m_audioFormat.setSampleType(QAudioFormat::SignedInt); // Unknown, SignedInt, UnSignedInt, Float
|
2016-12-25 19:04:19 +00:00
|
|
|
|
|
|
|
if (!devInfo.isFormatSupported(m_audioFormat))
|
|
|
|
{
|
|
|
|
m_audioFormat = devInfo.nearestFormat(m_audioFormat);
|
2018-11-19 01:33:44 +00:00
|
|
|
qWarning("AudioInput::start: %d Hz S16_LE audio format not supported. Nearest is sampleRate: %d channelCount: %d sampleSize: %d sampleType: %d",
|
|
|
|
rate, m_audioFormat.sampleRate(), m_audioFormat.channelCount(), m_audioFormat.sampleSize(), (int) m_audioFormat.sampleType());
|
2016-12-25 19:04:19 +00:00
|
|
|
}
|
2017-08-06 00:26:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qInfo("AudioInput::start: audio format OK");
|
|
|
|
}
|
2016-12-25 19:04:19 +00:00
|
|
|
|
|
|
|
if (m_audioFormat.sampleSize() != 16)
|
|
|
|
{
|
2018-10-06 20:34:48 +00:00
|
|
|
qWarning("AudioInput::start: Audio device '%s' failed", qPrintable(devInfo.defaultInputDevice().deviceName()));
|
2016-12-25 19:04:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_audioInput = new QAudioInput(devInfo, m_audioFormat);
|
2017-01-06 14:28:01 +00:00
|
|
|
m_audioInput->setVolume(m_volume);
|
2016-12-25 19:04:19 +00:00
|
|
|
|
|
|
|
QIODevice::open(QIODevice::ReadWrite);
|
|
|
|
|
|
|
|
m_audioInput->start(this);
|
|
|
|
|
|
|
|
if (m_audioInput->state() != QAudio::ActiveState)
|
|
|
|
{
|
|
|
|
qWarning("AudioInput::start: cannot start");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_audioUsageCount++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioInput::stop()
|
|
|
|
{
|
|
|
|
qDebug("AudioInput::stop");
|
|
|
|
|
|
|
|
if (m_audioUsageCount > 0)
|
|
|
|
{
|
|
|
|
m_audioUsageCount--;
|
|
|
|
|
|
|
|
if (m_audioUsageCount == 0)
|
|
|
|
{
|
2017-12-10 00:57:50 +00:00
|
|
|
qDebug("AudioInput::stop: effectively close QIODevice");
|
2018-01-06 05:12:30 +00:00
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2016-12-25 19:04:19 +00:00
|
|
|
QIODevice::close();
|
|
|
|
|
|
|
|
if (!m_onExit) {
|
|
|
|
delete m_audioInput;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioInput::addFifo(AudioFifo* audioFifo)
|
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
|
|
|
m_audioFifos.push_back(audioFifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioInput::removeFifo(AudioFifo* audioFifo)
|
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
|
|
|
m_audioFifos.remove(audioFifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudioInput::readData(char* data, qint64 maxLen)
|
|
|
|
{
|
|
|
|
Q_UNUSED(data);
|
|
|
|
Q_UNUSED(maxLen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudioInput::writeData(const char *data, qint64 len)
|
|
|
|
{
|
|
|
|
// Study this mutex on OSX, for now deadlocks possible
|
|
|
|
// Removed as it may indeed cause lockups and is in fact useless.
|
|
|
|
//#ifndef __APPLE__
|
|
|
|
// QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
if ((m_audioFormat.sampleSize() != 16)
|
|
|
|
|| (m_audioFormat.sampleType() != QAudioFormat::SignedInt)
|
|
|
|
|| (m_audioFormat.byteOrder() != QAudioFormat::LittleEndian))
|
|
|
|
{
|
|
|
|
qCritical("AudioInput::writeData: invalid format not S16LE");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_audioFormat.channelCount() != 2) {
|
|
|
|
qCritical("AudioInput::writeData: invalid format not stereo");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-24 03:18:58 +00:00
|
|
|
for (std::list<AudioFifo*>::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
|
2016-12-25 19:04:19 +00:00
|
|
|
{
|
2018-09-12 13:30:53 +00:00
|
|
|
(*it)->write(reinterpret_cast<const quint8*>(data), len/4);
|
2016-12-25 19:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:52:25 +00:00
|
|
|
void AudioInput::setVolume(float volume)
|
|
|
|
{
|
|
|
|
m_volume = volume;
|
|
|
|
if (m_audioInput != nullptr)
|
|
|
|
m_audioInput->setVolume(m_volume);
|
|
|
|
}
|