2017-11-24 11:12:53 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2017 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
|
|
|
// //
|
|
|
|
// 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 //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
#include <audio/audiodevicemanager.h>
|
2017-11-24 11:12:53 -05:00
|
|
|
#include "util/simpleserializer.h"
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
AudioDeviceManager::AudioDeviceManager() :
|
2017-11-24 11:12:53 -05:00
|
|
|
m_inputDeviceIndex(-1), // default device
|
|
|
|
m_outputDeviceIndex(-1), // default device
|
2018-03-23 12:52:16 -04:00
|
|
|
m_audioOutputSampleRate(48000), // Use default output device at 48 kHz
|
|
|
|
m_audioInputSampleRate(48000), // Use default input device at 48 kHz
|
2017-11-24 11:12:53 -05:00
|
|
|
m_inputVolume(1.0f)
|
|
|
|
{
|
|
|
|
m_inputDevicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
|
|
|
|
m_outputDevicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::resetToDefaults()
|
2017-11-24 11:12:53 -05:00
|
|
|
{
|
|
|
|
m_inputDeviceIndex = -1;
|
|
|
|
m_outputDeviceIndex = -1;
|
|
|
|
m_inputVolume = 1.0f;
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
QByteArray AudioDeviceManager::serialize() const
|
2017-11-24 11:12:53 -05:00
|
|
|
{
|
|
|
|
SimpleSerializer s(1);
|
|
|
|
s.writeS32(1, m_inputDeviceIndex);
|
|
|
|
s.writeS32(2, m_outputDeviceIndex);
|
|
|
|
s.writeFloat(3, m_inputVolume);
|
|
|
|
return s.final();
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
bool AudioDeviceManager::deserialize(const QByteArray& data)
|
2017-11-24 11:12:53 -05:00
|
|
|
{
|
|
|
|
SimpleDeserializer d(data);
|
|
|
|
|
|
|
|
if(!d.isValid()) {
|
|
|
|
resetToDefaults();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(d.getVersion() == 1)
|
|
|
|
{
|
|
|
|
d.readS32(1, &m_inputDeviceIndex, -1);
|
|
|
|
d.readS32(2, &m_outputDeviceIndex, -1);
|
|
|
|
d.readFloat(3, &m_inputVolume, 1.0f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resetToDefaults();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::setInputDeviceIndex(int inputDeviceIndex)
|
2017-11-24 11:12:53 -05:00
|
|
|
{
|
|
|
|
int nbDevices = m_inputDevicesInfo.size();
|
|
|
|
m_inputDeviceIndex = inputDeviceIndex < -1 ? -1 : inputDeviceIndex >= nbDevices ? nbDevices-1 : inputDeviceIndex;
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::setOutputDeviceIndex(int outputDeviceIndex)
|
2017-11-24 11:12:53 -05:00
|
|
|
{
|
|
|
|
int nbDevices = m_outputDevicesInfo.size();
|
|
|
|
m_outputDeviceIndex = outputDeviceIndex < -1 ? -1 : outputDeviceIndex >= nbDevices ? nbDevices-1 : outputDeviceIndex;
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::setInputVolume(float inputVolume)
|
2017-11-24 11:12:53 -05:00
|
|
|
{
|
|
|
|
m_inputVolume = inputVolume < 0.0 ? 0.0 : inputVolume > 1.0 ? 1.0 : inputVolume;
|
|
|
|
}
|
2018-03-23 12:52:16 -04:00
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::addAudioSink(AudioFifo* audioFifo)
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
qDebug("AudioDeviceInfo::addAudioSink");
|
2018-03-23 23:18:58 -04:00
|
|
|
|
|
|
|
if (m_audioOutput.getNbFifos() == 0) {
|
|
|
|
startAudioOutput();
|
|
|
|
}
|
|
|
|
|
2018-03-23 12:52:16 -04:00
|
|
|
m_audioOutput.addFifo(audioFifo);
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::removeAudioSink(AudioFifo* audioFifo)
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
qDebug("AudioDeviceInfo::removeAudioSink");
|
2018-03-23 23:18:58 -04:00
|
|
|
|
2018-03-23 12:52:16 -04:00
|
|
|
m_audioOutput.removeFifo(audioFifo);
|
2018-03-23 23:18:58 -04:00
|
|
|
|
|
|
|
if (m_audioOutput.getNbFifos() == 0) {
|
|
|
|
stopAudioOutput();
|
|
|
|
}
|
2018-03-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::addAudioSource(AudioFifo* audioFifo)
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
qDebug("AudioDeviceInfo::addAudioSource");
|
2018-03-23 23:18:58 -04:00
|
|
|
|
|
|
|
if (m_audioInput.getNbFifos() == 0) {
|
|
|
|
startAudioInput();
|
|
|
|
}
|
|
|
|
|
2018-03-23 12:52:16 -04:00
|
|
|
m_audioInput.addFifo(audioFifo);
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::removeAudioSource(AudioFifo* audioFifo)
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
qDebug("AudioDeviceInfo::removeAudioSource");
|
2018-03-23 23:18:58 -04:00
|
|
|
|
2018-03-23 12:52:16 -04:00
|
|
|
m_audioInput.removeFifo(audioFifo);
|
2018-03-23 23:18:58 -04:00
|
|
|
|
|
|
|
if (m_audioInput.getNbFifos() == 0) {
|
|
|
|
stopAudioInput();
|
|
|
|
}
|
2018-03-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::startAudioOutput()
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
m_audioOutput.start(m_outputDeviceIndex, m_audioOutputSampleRate);
|
|
|
|
m_audioOutputSampleRate = m_audioOutput.getRate(); // update with actual rate
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::stopAudioOutput()
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
m_audioOutput.stop();
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::startAudioInput()
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
m_audioInput.start(m_inputDeviceIndex, m_audioInputSampleRate);
|
|
|
|
m_audioInputSampleRate = m_audioInput.getRate(); // update with actual rate
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:08:38 -04:00
|
|
|
void AudioDeviceManager::stopAudioInput()
|
2018-03-23 12:52:16 -04:00
|
|
|
{
|
|
|
|
m_audioInput.stop();
|
|
|
|
}
|