2014-05-18 15:52:39 +00:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
# include <string.h>
# include <QAudioFormat>
# include <QAudioDeviceInfo>
# include <QAudioOutput>
# include "audio/audiooutput.h"
# include "audio/audiofifo.h"
AudioOutput : : AudioOutput ( ) :
m_mutex ( ) ,
2016-05-18 09:32:19 +00:00
m_audioOutput ( 0 ) ,
m_audioUsageCount ( 0 ) ,
2016-10-08 04:54:49 +00:00
m_onExit ( false ) ,
2014-05-18 15:52:39 +00:00
m_audioFifos ( )
{
}
AudioOutput : : ~ AudioOutput ( )
{
stop ( ) ;
QMutexLocker mutexLocker ( & m_mutex ) ;
2015-08-20 01:38:31 +00:00
for ( AudioFifos : : iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
{
2014-05-18 15:52:39 +00:00
delete * it ;
2015-08-20 01:38:31 +00:00
}
2014-05-18 15:52:39 +00:00
m_audioFifos . clear ( ) ;
}
bool AudioOutput : : start ( int device , int rate )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
2016-05-18 09:32:19 +00:00
if ( m_audioUsageCount = = 0 )
2015-08-20 01:38:31 +00:00
{
2016-05-18 09:32:19 +00:00
QAudioDeviceInfo devInfo ;
if ( device < 0 )
{
devInfo = QAudioDeviceInfo : : defaultOutputDevice ( ) ;
qWarning ( " AudioOutput::start: using default device %s " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
}
else
{
QList < QAudioDeviceInfo > devicesInfo = QAudioDeviceInfo : : availableDevices ( QAudio : : AudioOutput ) ;
if ( device < devicesInfo . size ( ) )
{
devInfo = devicesInfo [ device ] ;
qWarning ( " AudioOutput::start: using audio device #%d: %s " , device , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
}
else
{
devInfo = QAudioDeviceInfo : : defaultOutputDevice ( ) ;
qWarning ( " AudioOutput::start: audio device #%d does not exist. Using default device %s " , device , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
}
}
//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 ) ;
m_audioFormat . setSampleType ( QAudioFormat : : SignedInt ) ;
if ( ! devInfo . isFormatSupported ( m_audioFormat ) )
{
m_audioFormat = devInfo . nearestFormat ( m_audioFormat ) ;
qWarning ( " AudioOutput::start: %d Hz S16_LE audio format not supported. New rate: %d " , rate , m_audioFormat . sampleRate ( ) ) ;
}
2017-08-06 00:26:34 +00:00
else
{
qInfo ( " AudioOutput::start: audio format OK " ) ;
}
2016-05-18 09:32:19 +00:00
if ( m_audioFormat . sampleSize ( ) ! = 16 )
{
qWarning ( " AudioOutput::start: Audio device ( %s ) failed " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
return false ;
}
m_audioOutput = new QAudioOutput ( devInfo , m_audioFormat ) ;
QIODevice : : open ( QIODevice : : ReadOnly ) ;
m_audioOutput - > start ( this ) ;
if ( m_audioOutput - > state ( ) ! = QAudio : : ActiveState )
{
qWarning ( " AudioOutput::start: cannot start " ) ;
}
2014-05-18 15:52:39 +00:00
}
2016-05-18 09:32:19 +00:00
m_audioUsageCount + + ;
2015-08-23 22:51:27 +00:00
2014-05-18 15:52:39 +00:00
return true ;
}
2017-12-10 00:57:50 +00:00
bool AudioOutput : : startImmediate ( int device , int rate )
{
if ( QIODevice : : isOpen ( ) )
{
qInfo ( " AudioOutput::startImmediate: already open " ) ;
return true ;
}
QMutexLocker mutexLocker ( & m_mutex ) ;
QAudioDeviceInfo devInfo ;
if ( device < 0 )
{
devInfo = QAudioDeviceInfo : : defaultOutputDevice ( ) ;
qWarning ( " AudioOutput::startImmediate: using default device %s " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
}
else
{
QList < QAudioDeviceInfo > devicesInfo = QAudioDeviceInfo : : availableDevices ( QAudio : : AudioOutput ) ;
if ( device < devicesInfo . size ( ) )
{
devInfo = devicesInfo [ device ] ;
qWarning ( " AudioOutput::startImmediate: using audio device #%d: %s " , device , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
}
else
{
devInfo = QAudioDeviceInfo : : defaultOutputDevice ( ) ;
qWarning ( " AudioOutput::startImmediate: audio device #%d does not exist. Using default device %s " , device , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
}
}
//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 ) ;
m_audioFormat . setSampleType ( QAudioFormat : : SignedInt ) ;
if ( ! devInfo . isFormatSupported ( m_audioFormat ) )
{
m_audioFormat = devInfo . nearestFormat ( m_audioFormat ) ;
qWarning ( " AudioOutput::startImmediate: %d Hz S16_LE audio format not supported. New rate: %d " , rate , m_audioFormat . sampleRate ( ) ) ;
}
else
{
qInfo ( " AudioOutput::startImmediate: audio format OK " ) ;
}
if ( m_audioFormat . sampleSize ( ) ! = 16 )
{
qWarning ( " AudioOutput::startImmediate: Audio device ( %s ) failed " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
return false ;
}
m_audioOutput = new QAudioOutput ( devInfo , m_audioFormat ) ;
QIODevice : : open ( QIODevice : : ReadOnly ) ;
m_audioOutput - > start ( this ) ;
if ( m_audioOutput - > state ( ) ! = QAudio : : ActiveState )
{
qWarning ( " AudioOutput::startImmediate: cannot start " ) ;
}
return true ;
}
2014-05-18 15:52:39 +00:00
void AudioOutput : : stop ( )
{
2016-10-08 03:59:59 +00:00
qDebug ( " AudioOutput::stop " ) ;
2014-05-18 15:52:39 +00:00
QMutexLocker mutexLocker ( & m_mutex ) ;
2016-05-18 09:32:19 +00:00
if ( m_audioUsageCount > 0 )
{
m_audioUsageCount - - ;
if ( m_audioUsageCount = = 0 )
{
QIODevice : : close ( ) ;
2016-10-08 04:54:49 +00:00
if ( ! m_onExit ) {
delete m_audioOutput ;
}
2016-05-18 09:32:19 +00:00
}
}
2014-05-18 15:52:39 +00:00
}
2017-12-10 00:57:50 +00:00
void AudioOutput : : stopImmediate ( )
{
if ( ! QIODevice : : isOpen ( ) )
{
qInfo ( " AudioOutput::stopImmediate " ) ;
}
else
{
qDebug ( " AudioOutput::stopImmediate " ) ;
QMutexLocker mutexLocker ( & m_mutex ) ;
QIODevice : : close ( ) ;
delete m_audioOutput ;
}
}
2014-05-18 15:52:39 +00:00
void AudioOutput : : addFifo ( AudioFifo * audioFifo )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioFifos . push_back ( audioFifo ) ;
}
void AudioOutput : : removeFifo ( AudioFifo * audioFifo )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioFifos . remove ( audioFifo ) ;
}
2015-08-20 01:38:31 +00:00
/*
2014-05-18 15:52:39 +00:00
bool AudioOutput : : open ( OpenMode mode )
{
Q_UNUSED ( mode ) ;
return false ;
2015-08-20 01:38:31 +00:00
} */
2014-05-18 15:52:39 +00:00
qint64 AudioOutput : : readData ( char * data , qint64 maxLen )
{
2016-06-29 08:00:38 +00:00
//qDebug("AudioOutput::readData: %lld", maxLen);
2016-12-25 19:04:19 +00: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
2014-05-18 15:52:39 +00:00
2014-11-21 15:41:58 +00:00
unsigned int framesPerBuffer = maxLen / 4 ;
2015-08-20 01:38:31 +00:00
if ( framesPerBuffer = = 0 )
{
2014-11-21 15:41:58 +00:00
return 0 ;
2015-08-20 01:38:31 +00:00
}
2014-05-18 15:52:39 +00:00
2015-08-20 01:38:31 +00:00
if ( m_mixBuffer . size ( ) < framesPerBuffer * 2 )
{
2014-05-18 15:52:39 +00:00
m_mixBuffer . resize ( framesPerBuffer * 2 ) ; // allocate 2 qint32 per frame (stereo)
2015-08-20 01:38:31 +00:00
if ( m_mixBuffer . size ( ) ! = framesPerBuffer * 2 )
{
2014-05-18 15:52:39 +00:00
return 0 ;
2015-08-20 01:38:31 +00:00
}
2014-05-18 15:52:39 +00:00
}
2015-08-20 01:38:31 +00:00
2014-05-18 15:52:39 +00:00
memset ( & m_mixBuffer [ 0 ] , 0x00 , 2 * framesPerBuffer * sizeof ( m_mixBuffer [ 0 ] ) ) ; // start with silence
// sum up a block from all fifos
2015-08-20 01:38:31 +00:00
for ( AudioFifos : : iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
{
2014-05-18 15:52:39 +00:00
// use outputBuffer as temp - yes, one memcpy could be saved
2015-08-20 01:38:31 +00:00
uint samples = ( * it ) - > read ( ( quint8 * ) data , framesPerBuffer , 1 ) ;
const qint16 * src = ( const qint16 * ) data ;
2014-05-18 15:52:39 +00:00
std : : vector < qint32 > : : iterator dst = m_mixBuffer . begin ( ) ;
2015-08-20 01:38:31 +00:00
2016-07-31 04:05:32 +00:00
// if (samples != framesPerBuffer)
// {
// qDebug("AudioOutput::readData: read %d samples vs %d requested", samples, framesPerBuffer);
// }
2015-08-20 01:38:31 +00:00
for ( uint i = 0 ; i < samples ; i + + )
{
2014-05-18 15:52:39 +00:00
* dst + = * src ;
+ + src ;
+ + dst ;
* dst + = * src ;
+ + src ;
+ + dst ;
}
2016-06-29 08:00:38 +00:00
}
2014-05-18 15:52:39 +00:00
// convert to int16
2015-08-20 01:38:31 +00:00
2015-08-29 01:09:00 +00:00
//std::vector<qint32>::const_iterator src = m_mixBuffer.begin(); // Valgrind optim
2015-08-20 01:38:31 +00:00
qint16 * dst = ( qint16 * ) data ;
2015-08-29 01:09:00 +00:00
qint32 s ;
2015-08-20 01:38:31 +00:00
for ( uint i = 0 ; i < framesPerBuffer ; i + + )
{
2014-05-18 15:52:39 +00:00
// left channel
2015-08-20 01:38:31 +00:00
2015-08-29 01:09:00 +00:00
//s = *src++; // Valgrind optim
s = m_mixBuffer [ 2 * i ] ;
2015-08-20 01:38:31 +00:00
2014-05-18 15:52:39 +00:00
if ( s < - 32768 )
2015-08-20 01:38:31 +00:00
{
2014-05-18 15:52:39 +00:00
s = - 32768 ;
2015-08-20 01:38:31 +00:00
}
else if ( s > 32767 )
{
2014-05-18 15:52:39 +00:00
s = 32767 ;
2015-08-20 01:38:31 +00:00
}
2014-05-18 15:52:39 +00:00
* dst + + = s ;
2015-08-20 01:38:31 +00:00
2014-05-18 15:52:39 +00:00
// right channel
2015-08-20 01:38:31 +00:00
2015-08-29 01:09:00 +00:00
//s = *src++; // Valgrind optim
s = m_mixBuffer [ 2 * i + 1 ] ;
2015-08-20 01:38:31 +00:00
2014-05-18 15:52:39 +00:00
if ( s < - 32768 )
2015-08-20 01:38:31 +00:00
{
2014-05-18 15:52:39 +00:00
s = - 32768 ;
2015-08-20 01:38:31 +00:00
}
else if ( s > 32767 )
{
2014-05-18 15:52:39 +00:00
s = 32767 ;
2015-08-20 01:38:31 +00:00
}
2014-05-18 15:52:39 +00:00
* dst + + = s ;
}
return framesPerBuffer * 4 ;
}
qint64 AudioOutput : : writeData ( const char * data , qint64 len )
{
Q_UNUSED ( data ) ;
Q_UNUSED ( len ) ;
return 0 ;
}