2014-05-18 11:52:39 -04: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 05:32:19 -04:00
m_audioOutput ( 0 ) ,
m_audioUsageCount ( 0 ) ,
2014-05-18 11:52:39 -04:00
m_audioFifos ( )
{
}
AudioOutput : : ~ AudioOutput ( )
{
stop ( ) ;
QMutexLocker mutexLocker ( & m_mutex ) ;
2015-08-19 21:38:31 -04:00
for ( AudioFifos : : iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
{
2014-05-18 11:52:39 -04:00
delete * it ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
m_audioFifos . clear ( ) ;
}
bool AudioOutput : : start ( int device , int rate )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
2016-05-18 05:32:19 -04:00
if ( m_audioUsageCount = = 0 )
2015-08-19 21:38:31 -04:00
{
2016-05-18 05:32:19 -04: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 ( ) ) ;
}
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 11:52:39 -04:00
}
2016-05-18 05:32:19 -04:00
m_audioUsageCount + + ;
2015-08-23 18:51:27 -04:00
2014-05-18 11:52:39 -04:00
return true ;
}
void AudioOutput : : stop ( )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
2016-05-18 05:32:19 -04:00
if ( m_audioUsageCount > 0 )
{
m_audioUsageCount - - ;
if ( m_audioUsageCount = = 0 )
{
QIODevice : : close ( ) ;
2016-05-20 16:11:02 -04:00
// delete m_audioOutput;
2016-05-18 05:32:19 -04:00
}
}
2014-05-18 11:52:39 -04: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-19 21:38:31 -04:00
/*
2014-05-18 11:52:39 -04:00
bool AudioOutput : : open ( OpenMode mode )
{
Q_UNUSED ( mode ) ;
return false ;
2015-08-19 21:38:31 -04:00
} */
2014-05-18 11:52:39 -04:00
qint64 AudioOutput : : readData ( char * data , qint64 maxLen )
{
2016-06-29 04:00:38 -04:00
//qDebug("AudioOutput::readData: %lld", maxLen);
// @TODO: Study this mutex on OSX, for now deadlocks possible
# ifndef __APPLE__
QMutexLocker mutexLocker ( & m_mutex ) ;
# endif
2014-05-18 11:52:39 -04:00
2014-11-21 10:41:58 -05:00
unsigned int framesPerBuffer = maxLen / 4 ;
2015-08-19 21:38:31 -04:00
if ( framesPerBuffer = = 0 )
{
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
2015-08-19 21:38:31 -04:00
if ( m_mixBuffer . size ( ) < framesPerBuffer * 2 )
{
2014-05-18 11:52:39 -04:00
m_mixBuffer . resize ( framesPerBuffer * 2 ) ; // allocate 2 qint32 per frame (stereo)
2015-08-19 21:38:31 -04:00
if ( m_mixBuffer . size ( ) ! = framesPerBuffer * 2 )
{
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
2014-05-18 11:52:39 -04:00
memset ( & m_mixBuffer [ 0 ] , 0x00 , 2 * framesPerBuffer * sizeof ( m_mixBuffer [ 0 ] ) ) ; // start with silence
// sum up a block from all fifos
2015-08-19 21:38:31 -04:00
for ( AudioFifos : : iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
{
2014-05-18 11:52:39 -04:00
// use outputBuffer as temp - yes, one memcpy could be saved
2015-08-19 21:38:31 -04:00
uint samples = ( * it ) - > read ( ( quint8 * ) data , framesPerBuffer , 1 ) ;
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
2016-07-31 00:05:32 -04:00
// if (samples != framesPerBuffer)
// {
// qDebug("AudioOutput::readData: read %d samples vs %d requested", samples, framesPerBuffer);
// }
2015-08-19 21:38:31 -04:00
for ( uint i = 0 ; i < samples ; i + + )
{
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 ;
2015-08-28 21:09:00 -04:00
qint32 s ;
2015-08-19 21:38:31 -04:00
for ( uint i = 0 ; i < framesPerBuffer ; i + + )
{
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
s = m_mixBuffer [ 2 * i ] ;
2015-08-19 21:38:31 -04:00
2014-05-18 11:52:39 -04:00
if ( s < - 32768 )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
s = - 32768 ;
2015-08-19 21:38:31 -04:00
}
else if ( s > 32767 )
{
2014-05-18 11:52:39 -04:00
s = 32767 ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
* dst + + = s ;
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
s = m_mixBuffer [ 2 * i + 1 ] ;
2015-08-19 21:38:31 -04:00
2014-05-18 11:52:39 -04:00
if ( s < - 32768 )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
s = - 32768 ;
2015-08-19 21:38:31 -04:00
}
else if ( s > 32767 )
{
2014-05-18 11:52:39 -04:00
s = 32767 ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
* dst + + = s ;
}
return framesPerBuffer * 4 ;
}
qint64 AudioOutput : : writeData ( const char * data , qint64 len )
{
Q_UNUSED ( data ) ;
Q_UNUSED ( len ) ;
return 0 ;
}