2014-05-18 11:52:39 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
2023-11-19 00:43:20 -05:00
// Copyright (C) 2014 John Greb <hexameron@spam.no> //
// Copyright (C) 2015-2020, 2022-2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2016 Ziga S <ziga.svetina@gmail.com> //
// Copyright (C) 2022-2023 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai> //
2014-05-18 11:52:39 -04:00
// //
// 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 08:32:15 -04:00
// (at your option) any later version. //
2014-05-18 11:52:39 -04: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>
2023-06-28 18:49:19 -04:00
# include <QThread>
2014-05-18 11:52:39 -04:00
# include <QAudioFormat>
2022-11-17 09:50:16 -05:00
# if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
# include <QAudioSink>
# else
2014-05-18 11:52:39 -04:00
# include <QAudioOutput>
2022-11-17 09:50:16 -05:00
# endif
2020-11-12 16:13:44 -05:00
# include "audiooutputdevice.h"
2022-11-17 09:50:16 -05:00
# include "audiodeviceinfo.h"
2018-03-25 05:50:34 -04:00
# include "audiofifo.h"
# include "audionetsink.h"
2022-11-11 14:53:50 -05:00
# include "dsp/wavfilerecord.h"
2014-05-18 11:52:39 -04:00
2023-06-28 18:49:19 -04:00
MESSAGE_CLASS_DEFINITION ( AudioOutputDevice : : MsgStart , Message )
MESSAGE_CLASS_DEFINITION ( AudioOutputDevice : : MsgStop , Message )
2023-07-06 22:58:05 -04:00
MESSAGE_CLASS_DEFINITION ( AudioOutputDevice : : MsgReportSampleRate , Message )
2023-06-28 18:49:19 -04:00
2020-11-12 16:13:44 -05:00
AudioOutputDevice : : AudioOutputDevice ( ) :
2022-11-11 14:53:50 -05:00
m_audioOutput ( nullptr ) ,
m_audioNetSink ( nullptr ) ,
m_wavFileRecord ( nullptr ) ,
m_copyAudioToUdp ( false ) ,
2018-03-26 15:10:09 -04:00
m_udpChannelMode ( UDPChannelLeft ) ,
2019-02-13 01:53:38 -05:00
m_udpChannelCodec ( UDPCodecL16 ) ,
2016-05-18 05:32:19 -04:00
m_audioUsageCount ( 0 ) ,
2016-10-08 00:54:49 -04:00
m_onExit ( false ) ,
2020-11-21 03:41:35 -05:00
m_volume ( 1.0 ) ,
2022-11-11 14:53:50 -05:00
m_recordToFile ( false ) ,
m_recordSilenceTime ( 0 ) ,
m_recordSilenceNbSamples ( 0 ) ,
m_recordSilenceCount ( 0 ) ,
2023-07-06 22:58:05 -04:00
m_audioFifos ( ) ,
m_managerMessageQueue ( nullptr )
2014-05-18 11:52:39 -04:00
{
2023-06-28 18:49:19 -04:00
connect ( & m_inputMessageQueue , SIGNAL ( messageEnqueued ( ) ) , this , SLOT ( handleInputMessages ( ) ) , Qt : : QueuedConnection ) ;
2014-05-18 11:52:39 -04:00
}
2020-11-12 16:13:44 -05:00
AudioOutputDevice : : ~ AudioOutputDevice ( )
2014-05-18 11:52:39 -04:00
{
2018-03-24 18:50:28 -04:00
// stop();
//
// QMutexLocker mutexLocker(&m_mutex);
//
// for (std::list<AudioFifo*>::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
// {
// delete *it;
// }
//
// m_audioFifos.clear();
2014-05-18 11:52:39 -04:00
}
2023-07-06 22:58:05 -04:00
bool AudioOutputDevice : : start ( int deviceIndex , int sampleRate )
2014-05-18 11:52:39 -04:00
{
2023-06-28 18:49:19 -04:00
// if (m_audioOutput) {
// return true;
// }
2018-03-24 18:50:28 -04:00
// if (m_audioUsageCount == 0)
// {
2023-07-06 22:58:05 -04:00
qDebug ( " AudioOutputDevice::start: device: %d rate: %d thread: %p " , deviceIndex , sampleRate , QThread : : currentThread ( ) ) ;
2018-01-06 00:12:30 -05:00
QMutexLocker mutexLocker ( & m_mutex ) ;
2022-11-17 09:50:16 -05:00
AudioDeviceInfo devInfo ;
2016-05-18 05:32:19 -04:00
2023-07-06 22:58:05 -04:00
if ( deviceIndex < 0 )
2016-05-18 05:32:19 -04:00
{
2022-11-17 09:50:16 -05:00
devInfo = AudioDeviceInfo : : defaultOutputDevice ( ) ;
2020-11-12 16:13:44 -05:00
qWarning ( " AudioOutputDevice::start: using system default device %s " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
}
else
{
2022-11-17 09:50:16 -05:00
QList < AudioDeviceInfo > devicesInfo = AudioDeviceInfo : : availableOutputDevices ( ) ;
2016-05-18 05:32:19 -04:00
2023-07-06 22:58:05 -04:00
if ( deviceIndex < devicesInfo . size ( ) )
2016-05-18 05:32:19 -04:00
{
2023-07-06 22:58:05 -04:00
devInfo = devicesInfo [ deviceIndex ] ;
qWarning ( " AudioOutputDevice::start: using audio device #%d: %s " , deviceIndex , qPrintable ( devInfo . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
}
else
{
2022-11-17 09:50:16 -05:00
devInfo = AudioDeviceInfo : : defaultOutputDevice ( ) ;
2023-07-06 22:58:05 -04:00
qWarning ( " AudioOutputDevice::start: audio device #%d does not exist. Using system default device %s " , deviceIndex , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
deviceIndex = - 1 ;
2016-05-18 05:32:19 -04:00
}
}
//QAudioDeviceInfo devInfo(QAudioDeviceInfo::defaultOutputDevice());
2022-11-17 09:50:16 -05:00
# if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
// Start with a valid format
m_audioFormat = devInfo . deviceInfo ( ) . preferredFormat ( ) ;
# endif
2016-05-18 05:32:19 -04:00
2023-07-06 22:58:05 -04:00
m_audioFormat . setSampleRate ( sampleRate ) ;
2016-05-18 05:32:19 -04:00
m_audioFormat . setChannelCount ( 2 ) ;
2022-11-17 09:50:16 -05:00
# if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
m_audioFormat . setSampleFormat ( QAudioFormat : : Int16 ) ;
# else
2016-05-18 05:32:19 -04:00
m_audioFormat . setSampleSize ( 16 ) ;
m_audioFormat . setCodec ( " audio/pcm " ) ;
m_audioFormat . setByteOrder ( QAudioFormat : : LittleEndian ) ;
m_audioFormat . setSampleType ( QAudioFormat : : SignedInt ) ;
2022-11-17 09:50:16 -05:00
# endif
2016-05-18 05:32:19 -04:00
if ( ! devInfo . isFormatSupported ( m_audioFormat ) )
{
2022-11-17 09:50:16 -05:00
# if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
2023-07-06 22:58:05 -04:00
qWarning ( " AudioOutputDevice::start: format %d Hz 2xS16LE audio/pcm not supported. " , sampleRate ) ;
2022-11-17 09:50:16 -05:00
# else
m_audioFormat = devInfo . deviceInfo ( ) . nearestFormat ( m_audioFormat ) ;
2018-03-26 12:41:09 -04:00
std : : ostringstream os ;
os < < " sampleRate: " < < m_audioFormat . sampleRate ( )
< < " channelCount: " < < m_audioFormat . channelCount ( )
< < " sampleSize: " < < m_audioFormat . sampleSize ( )
< < " codec: " < < m_audioFormat . codec ( ) . toStdString ( )
< < " byteOrder: " < < ( m_audioFormat . byteOrder ( ) = = QAudioFormat : : BigEndian ? " BE " : " LE " )
< < " sampleType: " < < ( int ) m_audioFormat . sampleType ( ) ;
2023-07-06 22:58:05 -04:00
qWarning ( " AudioOutputDevice::start: format %d Hz 2xS16LE audio/pcm not supported. Using: %s " , sampleRate , os . str ( ) . c_str ( ) ) ;
2022-11-17 09:50:16 -05:00
# endif
2016-05-18 05:32:19 -04:00
}
2017-08-05 20:26:34 -04:00
else
{
2020-11-12 16:13:44 -05:00
qInfo ( " AudioOutputDevice::start: audio format OK " ) ;
2017-08-05 20:26:34 -04:00
}
2016-05-18 05:32:19 -04:00
2022-11-17 09:50:16 -05:00
# if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if ( m_audioFormat . sampleFormat ( ) ! = QAudioFormat : : Int16 )
{
qWarning ( " AudioOutputDevice::start: Audio device '%s' failed " , qPrintable ( devInfo . deviceName ( ) ) ) ;
return false ;
}
# else
2016-05-18 05:32:19 -04:00
if ( m_audioFormat . sampleSize ( ) ! = 16 )
{
2022-11-17 09:50:16 -05:00
qWarning ( " AudioOutputDevice::start: Audio device '%s' failed " , qPrintable ( devInfo . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
return false ;
}
2022-11-17 09:50:16 -05:00
# endif
2016-05-18 05:32:19 -04:00
2022-11-17 09:50:16 -05:00
# if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
m_audioOutput = new QAudioSink ( devInfo . deviceInfo ( ) , m_audioFormat ) ;
# else
m_audioOutput = new QAudioOutput ( devInfo . deviceInfo ( ) , m_audioFormat ) ;
# endif
2018-03-27 03:04:10 -04:00
m_audioNetSink = new AudioNetSink ( 0 , m_audioFormat . sampleRate ( ) , false ) ;
2022-11-11 14:53:50 -05:00
m_wavFileRecord = new WavFileRecord ( m_audioFormat . sampleRate ( ) ) ;
2020-11-21 03:41:35 -05:00
m_audioOutput - > setVolume ( m_volume ) ;
2023-06-29 12:19:40 -04:00
// m_audioOutput->setBufferSize(m_audioFormat.sampleRate() / 5); FIXME: does not work generally
2022-11-11 14:53:50 -05:00
m_recordSilenceNbSamples = ( m_recordSilenceTime * m_audioFormat . sampleRate ( ) ) / 10 ; // time in 100'ś ms
2016-05-18 05:32:19 -04:00
2023-07-06 22:58:05 -04:00
QIODevice : : open ( QIODevice : : ReadOnly | QIODevice : : Unbuffered ) ;
2016-05-18 05:32:19 -04:00
m_audioOutput - > start ( this ) ;
2022-11-11 14:53:50 -05:00
if ( m_audioOutput - > state ( ) ! = QAudio : : ActiveState ) {
2022-11-17 09:50:16 -05:00
qWarning ( ) < < " AudioOutputDevice::start: cannot start - " < < m_audioOutput - > error ( ) ;
2023-06-28 18:49:19 -04:00
} else {
qDebug ( " AudioOutputDevice::start: started buffer: %d bytes " , m_audioOutput - > bufferSize ( ) ) ;
2016-05-18 05:32:19 -04:00
}
2023-07-06 22:58:05 -04:00
if ( m_managerMessageQueue ) {
m_managerMessageQueue - > push ( AudioOutputDevice : : MsgReportSampleRate : : create ( deviceIndex , devInfo . deviceName ( ) , m_audioFormat . sampleRate ( ) ) ) ;
}
2018-03-24 18:50:28 -04:00
// }
//
// m_audioUsageCount++;
2015-08-23 18:51:27 -04:00
2014-05-18 11:52:39 -04:00
return true ;
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : stop ( )
2014-05-18 11:52:39 -04:00
{
2023-06-28 18:49:19 -04:00
if ( ! m_audioOutput ) {
return ;
}
qDebug ( " AudioOutputDevice::stop: thread: %p " , QThread : : currentThread ( ) ) ;
2016-10-07 23:59:59 -04:00
2018-03-24 18:50:28 -04:00
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioOutput - > stop ( ) ;
QIODevice : : close ( ) ;
2018-03-25 05:50:34 -04:00
delete m_audioNetSink ;
2022-11-11 14:53:50 -05:00
m_audioNetSink = nullptr ;
delete m_wavFileRecord ;
m_wavFileRecord = nullptr ;
2018-03-24 18:50:28 -04:00
delete m_audioOutput ;
2022-11-11 14:53:50 -05:00
m_audioOutput = nullptr ;
2018-03-24 18:50:28 -04:00
// if (m_audioUsageCount > 0)
// {
// m_audioUsageCount--;
//
// if (m_audioUsageCount == 0)
// {
// QMutexLocker mutexLocker(&m_mutex);
// QIODevice::close();
//
// if (!m_onExit) {
// delete m_audioOutput;
// }
// }
// }
2014-05-18 11:52:39 -04:00
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : addFifo ( AudioFifo * audioFifo )
2014-05-18 11:52:39 -04:00
{
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioFifos . push_back ( audioFifo ) ;
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : removeFifo ( AudioFifo * audioFifo )
2014-05-18 11:52:39 -04:00
{
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioFifos . remove ( audioFifo ) ;
}
2015-08-19 21:38:31 -04:00
/*
2020-11-12 16:13:44 -05:00
bool AudioOutputDevice : : open ( OpenMode mode )
2014-05-18 11:52:39 -04:00
{
Q_UNUSED ( mode ) ;
return false ;
2015-08-19 21:38:31 -04:00
} */
2014-05-18 11:52:39 -04:00
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : setUdpDestination ( const QString & address , uint16_t port )
2018-03-25 05:50:34 -04:00
{
if ( m_audioNetSink ) {
m_audioNetSink - > setDestination ( address , port ) ;
}
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : setUdpCopyToUDP ( bool copyToUDP )
2018-03-25 05:50:34 -04:00
{
m_copyAudioToUdp = copyToUDP ;
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : setUdpUseRTP ( bool useRTP )
2018-03-25 05:50:34 -04:00
{
if ( m_audioNetSink ) {
2018-03-26 15:10:09 -04:00
m_audioNetSink - > selectType ( useRTP ? AudioNetSink : : SinkRTP : AudioNetSink : : SinkUDP ) ;
2018-03-25 05:50:34 -04:00
}
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : setUdpChannelMode ( UDPChannelMode udpChannelMode )
2018-03-26 18:09:52 -04:00
{
m_udpChannelMode = udpChannelMode ;
}
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : setUdpChannelFormat ( UDPChannelCodec udpChannelCodec , bool stereo , int sampleRate )
2018-03-25 05:50:34 -04:00
{
2019-02-13 01:53:38 -05:00
m_udpChannelCodec = udpChannelCodec ;
2018-03-25 05:50:34 -04:00
if ( m_audioNetSink ) {
2019-02-13 01:53:38 -05:00
m_audioNetSink - > setParameters ( ( AudioNetSink : : Codec ) m_udpChannelCodec , stereo , sampleRate ) ;
2018-03-25 05:50:34 -04:00
}
2022-11-11 17:25:04 -05:00
if ( m_wavFileRecord )
{
if ( m_wavFileRecord - > isRecording ( ) ) {
m_wavFileRecord - > stopRecording ( ) ;
}
m_wavFileRecord - > setMono ( ! stereo ) ;
}
2018-03-26 15:10:09 -04:00
}
2018-03-25 05:50:34 -04:00
2020-11-12 16:13:44 -05:00
void AudioOutputDevice : : setUdpDecimation ( uint32_t decimation )
2019-02-14 11:21:14 -05:00
{
if ( m_audioNetSink ) {
m_audioNetSink - > setDecimation ( decimation ) ;
}
}
2022-11-11 14:53:50 -05:00
void AudioOutputDevice : : setFileRecordName ( const QString & fileRecordName )
{
if ( ! m_wavFileRecord ) {
return ;
}
QStringList dotBreakout = fileRecordName . split ( QLatin1Char ( ' . ' ) ) ;
if ( dotBreakout . size ( ) > 1 ) {
QString extension = dotBreakout . last ( ) ;
if ( extension ! = " wav " ) {
dotBreakout . last ( ) = " wav " ;
}
}
else
{
dotBreakout . append ( " wav " ) ;
}
QString newFileRecordName = dotBreakout . join ( QLatin1Char ( ' . ' ) ) ;
QString fileBase ;
FileRecordInterface : : guessTypeFromFileName ( newFileRecordName , fileBase ) ;
qDebug ( " AudioOutputDevice::setFileRecordName: newFileRecordName: %s fileBase: %s " , qPrintable ( newFileRecordName ) , qPrintable ( fileBase ) ) ;
m_wavFileRecord - > setFileName ( fileBase ) ;
}
void AudioOutputDevice : : setRecordToFile ( bool recordToFile )
{
if ( ! m_wavFileRecord ) {
return ;
}
if ( recordToFile )
{
if ( ! m_wavFileRecord - > isRecording ( ) ) {
m_wavFileRecord - > startRecording ( ) ;
}
}
else
{
if ( m_wavFileRecord - > isRecording ( ) ) {
m_wavFileRecord - > stopRecording ( ) ;
}
}
m_recordToFile = recordToFile ;
m_recordSilenceCount = 0 ;
}
void AudioOutputDevice : : setRecordSilenceTime ( int recordSilenceTime )
{
m_recordSilenceNbSamples = ( recordSilenceTime * m_audioFormat . sampleRate ( ) ) / 10 ; // time in 100'ś ms
m_recordSilenceCount = 0 ;
m_recordSilenceTime = recordSilenceTime ;
}
2020-11-12 16:13:44 -05:00
qint64 AudioOutputDevice : : readData ( char * data , qint64 maxLen )
2014-05-18 11:52:39 -04:00
{
2016-12-25 14:04:19 -05:00
// 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
2023-06-28 18:49:19 -04:00
// qDebug("AudioOutputDevice::readData: thread %p (%s)", (void *) QThread::currentThread(), qPrintable(m_deviceName));
2014-05-18 11:52:39 -04:00
2018-03-27 08:54:48 -04:00
unsigned int samplesPerBuffer = maxLen / 4 ;
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
if ( samplesPerBuffer = = 0 )
2015-08-19 21:38:31 -04:00
{
2014-11-21 10:41:58 -05:00
return 0 ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
2018-03-27 08:54:48 -04:00
if ( m_mixBuffer . size ( ) < samplesPerBuffer * 2 )
2015-08-19 21:38:31 -04:00
{
2018-03-27 08:54:48 -04:00
m_mixBuffer . resize ( samplesPerBuffer * 2 ) ; // allocate 2 qint32 per sample (stereo)
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
if ( m_mixBuffer . size ( ) ! = samplesPerBuffer * 2 )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
return 0 ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
}
2015-08-19 21:38:31 -04:00
2023-11-13 07:15:56 -05:00
// See how much data we have available
// If we have less than the requested amount, we only output what we have
// If we have no data, then we output some zeros to avoid underflow
// (bytesAvailable() returns this amount when none available)
unsigned int samplesAvailable = bytesAvailable ( ) / 4 ;
samplesPerBuffer = std : : min ( samplesAvailable , samplesPerBuffer ) ;
2018-03-27 08:54:48 -04:00
memset ( & m_mixBuffer [ 0 ] , 0x00 , 2 * samplesPerBuffer * sizeof ( m_mixBuffer [ 0 ] ) ) ; // start with silence
2014-05-18 11:52:39 -04:00
// sum up a block from all fifos
2015-08-19 21:38:31 -04:00
2018-03-23 22:50:09 -04:00
for ( std : : list < AudioFifo * > : : iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
// use outputBuffer as temp - yes, one memcpy could be saved
2018-09-12 09:30:53 -04:00
unsigned int samples = ( * it ) - > read ( ( quint8 * ) data , samplesPerBuffer ) ;
2015-08-19 21:38:31 -04:00
const qint16 * src = ( const qint16 * ) data ;
2014-05-18 11:52:39 -04:00
std : : vector < qint32 > : : iterator dst = m_mixBuffer . begin ( ) ;
2015-08-19 21:38:31 -04:00
2023-11-13 07:15:56 -05:00
if ( samples ! = samplesPerBuffer )
{
//qDebug("AudioOutputDevice::readData: read %d samples vs %d requested", samples, samplesPerBuffer);
emit ( * it ) - > underflow ( ) ;
}
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
for ( unsigned int i = 0 ; i < samples ; i + + )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
* dst + = * src ;
+ + src ;
+ + dst ;
* dst + = * src ;
+ + src ;
+ + dst ;
}
2016-06-29 04:00:38 -04:00
}
2014-05-18 11:52:39 -04:00
// convert to int16
2015-08-19 21:38:31 -04:00
2015-08-28 21:09:00 -04:00
//std::vector<qint32>::const_iterator src = m_mixBuffer.begin(); // Valgrind optim
2015-08-19 21:38:31 -04:00
qint16 * dst = ( qint16 * ) data ;
2018-03-26 16:58:17 -04:00
qint32 sl , sr ;
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
for ( unsigned int i = 0 ; i < samplesPerBuffer ; i + + )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
// left channel
2015-08-19 21:38:31 -04:00
2015-08-28 21:09:00 -04:00
//s = *src++; // Valgrind optim
2018-03-26 16:58:17 -04:00
sl = m_mixBuffer [ 2 * i ] ;
2015-08-19 21:38:31 -04:00
2018-03-26 16:58:17 -04:00
if ( sl < - 32768 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sl = - 32768 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
else if ( sl > 32767 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sl = 32767 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
* dst + + = sl ;
2015-08-19 21:38:31 -04:00
2014-05-18 11:52:39 -04:00
// right channel
2015-08-19 21:38:31 -04:00
2015-08-28 21:09:00 -04:00
//s = *src++; // Valgrind optim
2018-03-26 16:58:17 -04:00
sr = m_mixBuffer [ 2 * i + 1 ] ;
2015-08-19 21:38:31 -04:00
2018-03-26 16:58:17 -04:00
if ( sr < - 32768 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sr = - 32768 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
else if ( sr > 32767 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sr = 32767 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
* dst + + = sr ;
if ( ( m_copyAudioToUdp ) & & ( m_audioNetSink ) )
{
switch ( m_udpChannelMode )
{
case UDPChannelStereo :
m_audioNetSink - > write ( sl , sr ) ;
break ;
case UDPChannelMixed :
m_audioNetSink - > write ( ( sl + sr ) / 2 ) ;
break ;
case UDPChannelRight :
m_audioNetSink - > write ( sr ) ;
break ;
case UDPChannelLeft :
default :
m_audioNetSink - > write ( sl ) ;
break ;
}
}
2022-11-11 14:53:50 -05:00
if ( ( m_recordToFile ) & & ( m_wavFileRecord ) )
{
if ( ( sr = = 0 ) & & ( sl = = 0 ) )
{
if ( m_recordSilenceNbSamples < = 0 )
{
2022-11-11 17:25:04 -05:00
writeSampleToFile ( sl , sr ) ;
2022-11-11 14:53:50 -05:00
m_recordSilenceCount = 0 ;
}
else if ( m_recordSilenceCount < m_recordSilenceNbSamples )
{
2022-11-11 17:25:04 -05:00
writeSampleToFile ( sl , sr ) ;
2022-11-11 14:53:50 -05:00
m_recordSilenceCount + + ;
}
else
{
m_wavFileRecord - > stopRecording ( ) ;
}
}
else
{
if ( ! m_wavFileRecord - > isRecording ( ) ) {
m_wavFileRecord - > startRecording ( ) ;
}
2022-11-11 17:25:04 -05:00
writeSampleToFile ( sl , sr ) ;
2022-11-11 14:53:50 -05:00
m_recordSilenceCount = 0 ;
}
}
2014-05-18 11:52:39 -04:00
}
2018-03-27 08:54:48 -04:00
return samplesPerBuffer * 4 ;
2014-05-18 11:52:39 -04:00
}
2022-11-11 17:25:04 -05:00
void AudioOutputDevice : : writeSampleToFile ( qint16 lSample , qint16 rSample )
{
switch ( m_udpChannelMode )
{
case UDPChannelStereo :
m_wavFileRecord - > write ( lSample , rSample ) ;
break ;
case UDPChannelMixed :
m_wavFileRecord - > writeMono ( ( lSample + rSample ) / 2 ) ;
break ;
case UDPChannelRight :
m_wavFileRecord - > writeMono ( rSample ) ;
break ;
case UDPChannelLeft :
default :
m_wavFileRecord - > writeMono ( lSample ) ;
break ;
}
}
2020-11-12 16:13:44 -05:00
qint64 AudioOutputDevice : : writeData ( const char * data , qint64 len )
2014-05-18 11:52:39 -04:00
{
Q_UNUSED ( data ) ;
Q_UNUSED ( len ) ;
return 0 ;
}
2020-11-21 03:41:35 -05:00
void AudioOutputDevice : : setVolume ( float volume )
{
m_volume = volume ;
if ( m_audioOutput ) {
m_audioOutput - > setVolume ( m_volume ) ;
}
}
2022-11-17 09:50:16 -05:00
// Qt6 requires bytesAvailable to be implemented. Not needed for Qt5.
qint64 AudioOutputDevice : : bytesAvailable ( ) const
{
qint64 available = 0 ;
for ( std : : list < AudioFifo * > : : const_iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
{
qint64 fill = ( * it ) - > fill ( ) ;
if ( available = = 0 ) {
available = fill ;
} else {
available = std : : min ( fill , available ) ;
}
}
// If we return 0 from this twice in a row, audio will stop.
// So we always return a value, and if we don't have enough data in the FIFOs
// when readData is called, that will output silence
2023-11-13 07:15:56 -05:00
if ( available = = 0 )
{
// Use a small value, so padding is minimized, but not too small, we get underflow again straightaway
// Could make this a function of sample rate
available = 512 ;
2022-11-17 09:50:16 -05:00
}
return available * 2 * 2 ; // 2 Channels of 16-bit data
}
2023-06-28 18:49:19 -04:00
bool AudioOutputDevice : : handleMessage ( const Message & cmd )
{
if ( MsgStart : : match ( cmd ) )
{
MsgStart ctl = ( MsgStart & ) cmd ;
start ( ctl . getDeviceIndex ( ) , ctl . getSampleRate ( ) ) ;
return true ;
}
else if ( MsgStop : : match ( cmd ) )
{
stop ( ) ;
return true ;
}
return false ;
}
void AudioOutputDevice : : handleInputMessages ( )
{
Message * message ;
while ( ( message = m_inputMessageQueue . pop ( ) ) ! = nullptr )
{
if ( handleMessage ( * message ) ) {
delete message ;
}
}
}