2021-02-11 19:18:35 +00:00
/*
2022-03-23 15:45:51 +00:00
This class handles both RX and TX audio , each is created as a separate instance of the class
2021-05-23 15:09:41 +00:00
but as the setup / handling if output ( RX ) and input ( TX ) devices is so similar I have combined them .
2021-02-11 19:18:35 +00:00
*/
2021-06-16 22:44:59 +00:00
2021-02-11 19:18:35 +00:00
# include "audiohandler.h"
2021-05-16 20:16:59 +00:00
2021-02-23 21:21:22 +00:00
# include "logcategories.h"
2021-05-24 17:00:38 +00:00
# include "ulaw.h"
2021-02-13 00:45:59 +00:00
2021-11-01 20:27:33 +00:00
2021-06-16 22:44:59 +00:00
2022-05-08 18:31:05 +00:00
audioHandler : : audioHandler ( QObject * parent ) : QObject ( parent )
2021-02-11 19:18:35 +00:00
{
2021-05-30 11:07:51 +00:00
Q_UNUSED ( parent )
2021-02-11 19:18:35 +00:00
}
audioHandler : : ~ audioHandler ( )
{
2021-06-02 17:36:33 +00:00
2022-07-05 09:37:10 +00:00
if ( converterThread ! = Q_NULLPTR ) {
converterThread - > quit ( ) ;
converterThread - > wait ( ) ;
2021-06-02 17:36:33 +00:00
}
2021-06-03 11:05:28 +00:00
if ( isInitialized ) {
2021-06-04 07:24:26 +00:00
stop ( ) ;
2021-06-03 11:05:28 +00:00
}
2021-06-04 07:24:26 +00:00
2022-04-03 23:01:08 +00:00
if ( audioInput ! = Q_NULLPTR ) {
delete audioInput ;
2022-07-05 09:37:10 +00:00
audioInput = Q_NULLPTR ;
2021-06-16 08:49:38 +00:00
}
2022-04-03 23:01:08 +00:00
if ( audioOutput ! = Q_NULLPTR ) {
delete audioOutput ;
audioOutput = Q_NULLPTR ;
2021-06-16 08:49:38 +00:00
}
2021-02-11 19:18:35 +00:00
2022-05-10 22:55:18 +00:00
} bool audioHandler : : init ( audioSetup setup )
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
if ( isInitialized ) {
return false ;
}
2021-02-11 19:18:35 +00:00
2022-05-08 10:46:33 +00:00
this - > setup = setup ;
2022-01-04 21:26:03 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " audio handler starting: " < < setup . name ;
2022-04-05 15:47:43 +00:00
if ( setup . port . isNull ( ) )
{
2022-09-27 07:30:17 +00:00
# ifdef Q_OS_LINUX
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " No audio device was found. You probably need to install libqt5multimedia-plugins. " ;
# else
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio device is NULL, please check device selection in settings. " ;
# endif
2022-04-05 15:47:43 +00:00
return false ;
2021-06-04 07:24:26 +00:00
}
2021-03-09 17:22:16 +00:00
2022-01-14 19:40:25 +00:00
qDebug ( logAudio ( ) ) < < " Creating " < < ( setup . isinput ? " Input " : " Output " ) < < " audio device: " < < setup . name < <
2022-12-29 15:26:41 +00:00
# if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2023-01-09 10:44:02 +00:00
" , bits " < < radioFormat . sampleSize ( ) < <
2022-08-22 22:09:09 +00:00
# else
2023-01-09 10:44:02 +00:00
" , format " < < radioFormat . sampleFormat ( ) < <
2022-08-22 22:09:09 +00:00
# endif
2022-01-14 19:40:25 +00:00
" , codec " < < setup . codec < <
" , latency " < < setup . latency < <
" , localAFGain " < < setup . localAFgain < <
2023-01-09 10:44:02 +00:00
" , radioChan " < < radioFormat . channelCount ( ) < <
2022-01-14 19:40:25 +00:00
" , resampleQuality " < < setup . resampleQuality < <
2023-01-09 10:44:02 +00:00
" , samplerate " < < radioFormat . sampleRate ( ) < <
2022-01-14 19:40:25 +00:00
" , uLaw " < < setup . ulaw ;
2021-06-04 07:24:26 +00:00
2023-01-09 10:44:02 +00:00
radioFormat = toQAudioFormat ( setup . codec , setup . sampleRate ) ;
2023-01-01 16:35:47 +00:00
codec = LPCM ;
2022-08-22 22:09:09 +00:00
if ( setup . codec = = 0x01 | | setup . codec = = 0x20 )
codec = PCMU ;
else if ( setup . codec = = 0x40 | | setup . codec = = 0x40 )
codec = OPUS ;
2021-06-04 07:24:26 +00:00
2021-06-02 17:35:04 +00:00
2023-01-09 10:44:02 +00:00
nativeFormat = setup . port . preferredFormat ( ) ;
2022-08-22 22:09:09 +00:00
2022-12-29 15:26:41 +00:00
# if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2023-01-09 10:44:02 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Preferred Format: SampleSize " < < nativeFormat . sampleSize ( ) < < " Channel Count " < < nativeFormat . channelCount ( ) < <
" Sample Rate " < < nativeFormat . sampleRate ( ) < < " Codec " < < codec < < " Sample Type " < < nativeFormat . sampleType ( ) ;
2022-08-22 22:09:09 +00:00
# else
2023-01-09 10:44:02 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Preferred Format: SampleFormat " < < nativeFormat . sampleFormat ( ) < < " Channel Count " < < nativeFormat . channelCount ( ) < <
" Sample Rate " < < nativeFormat . sampleRate ( ) ;
2022-08-22 22:09:09 +00:00
# endif
2023-01-09 10:44:02 +00:00
if ( nativeFormat . channelCount ( ) > 2 ) {
nativeFormat . setChannelCount ( 2 ) ;
2021-05-23 15:09:41 +00:00
}
2023-01-09 10:44:02 +00:00
else if ( nativeFormat . channelCount ( ) < 1 )
2021-05-23 15:09:41 +00:00
{
2021-06-07 13:04:52 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " No channels found, aborting setup. " ;
return false ;
}
2021-05-23 15:09:41 +00:00
2023-01-09 10:44:02 +00:00
if ( nativeFormat . channelCount ( ) = = 1 & & radioFormat . channelCount ( ) = = 2 ) {
nativeFormat . setChannelCount ( 2 ) ;
if ( ! setup . port . isFormatSupported ( nativeFormat ) ) {
2022-05-09 17:47:12 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Cannot request stereo reverting to mono " ;
2023-01-09 10:44:02 +00:00
nativeFormat . setChannelCount ( 1 ) ;
2021-05-23 15:09:41 +00:00
}
}
2021-05-16 20:16:59 +00:00
2023-01-09 10:44:02 +00:00
if ( nativeFormat . sampleRate ( ) < 48000 ) {
int tempRate = nativeFormat . sampleRate ( ) ;
nativeFormat . setSampleRate ( 48000 ) ;
if ( ! setup . port . isFormatSupported ( nativeFormat ) ) {
2022-05-09 17:47:12 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Cannot request 48K, reverting to " < < tempRate ;
2023-01-09 10:44:02 +00:00
nativeFormat . setSampleRate ( tempRate ) ;
2022-05-09 17:47:12 +00:00
}
}
2021-05-29 17:59:45 +00:00
2022-12-29 15:26:41 +00:00
# if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2021-11-01 20:27:33 +00:00
2023-01-09 10:44:02 +00:00
if ( nativeFormat . sampleType ( ) = = QAudioFormat : : UnSignedInt & & nativeFormat . sampleSize ( ) = = 8 ) {
nativeFormat . setSampleType ( QAudioFormat : : SignedInt ) ;
nativeFormat . setSampleSize ( 16 ) ;
2021-11-01 20:27:33 +00:00
2023-01-09 10:44:02 +00:00
if ( ! setup . port . isFormatSupported ( nativeFormat ) ) {
2022-08-22 22:09:09 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Cannot request 16bit Signed samples, reverting to 8bit Unsigned " ;
2023-01-09 10:44:02 +00:00
nativeFormat . setSampleType ( QAudioFormat : : UnSignedInt ) ;
nativeFormat . setSampleSize ( 8 ) ;
2022-08-22 22:09:09 +00:00
}
}
# else
2023-01-09 10:44:02 +00:00
if ( nativeFormat . sampleFormat ( ) = = QAudioFormat : : UInt8 ) {
nativeFormat . setSampleFormat ( QAudioFormat : : Int16 ) ;
2021-11-01 20:27:33 +00:00
2023-01-09 10:44:02 +00:00
if ( ! setup . port . isFormatSupported ( nativeFormat ) ) {
2022-08-22 22:09:09 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Cannot request 16bit Signed samples, reverting to 8bit Unsigned " ;
2023-01-09 10:44:02 +00:00
nativeFormat . setSampleFormat ( QAudioFormat : : UInt8 ) ;
2022-08-22 22:09:09 +00:00
}
}
# endif
2021-11-01 20:27:33 +00:00
2022-08-22 22:09:09 +00:00
/*
2023-01-09 10:44:02 +00:00
if ( nativeFormat . sampleType ( ) = = QAudioFormat : : SignedInt ) {
nativeFormat . setSampleType ( QAudioFormat : : Float ) ;
nativeFormat . setSampleSize ( 32 ) ;
if ( ! setup . port . isFormatSupported ( nativeFormat ) ) {
2022-05-04 21:29:05 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Attempt to select 32bit Float failed, reverting to SignedInt " ;
2023-01-09 10:44:02 +00:00
nativeFormat . setSampleType ( QAudioFormat : : SignedInt ) ;
nativeFormat . setSampleSize ( 16 ) ;
2022-05-04 21:29:05 +00:00
}
2021-11-09 15:55:43 +00:00
2022-05-04 21:29:05 +00:00
}
2022-05-08 23:29:07 +00:00
*/
2021-11-01 20:27:33 +00:00
2022-12-29 15:26:41 +00:00
# if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2022-08-22 22:09:09 +00:00
2023-01-09 10:44:02 +00:00
if ( nativeFormat . sampleSize ( ) = = 24 ) {
2022-04-28 09:02:51 +00:00
// We can't convert this easily so use 32 bit instead.
2023-01-09 10:44:02 +00:00
nativeFormat . setSampleSize ( 32 ) ;
if ( ! setup . port . isFormatSupported ( nativeFormat ) ) {
2022-04-28 09:02:51 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " 24 bit requested and 32 bit audio not supported, try 16 bit instead " ;
2023-01-09 10:44:02 +00:00
nativeFormat . setSampleSize ( 16 ) ;
2022-04-28 09:02:51 +00:00
}
2021-11-15 15:26:18 +00:00
}
2022-08-22 22:09:09 +00:00
2021-11-01 20:27:33 +00:00
2023-01-09 10:44:02 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Selected format: SampleSize " < < nativeFormat . sampleSize ( ) < < " Channel Count " < < nativeFormat . channelCount ( ) < <
" Sample Rate " < < nativeFormat . sampleRate ( ) < < " Codec " < < codec < < " Sample Type " < < nativeFormat . sampleType ( ) ;
2022-08-22 22:09:09 +00:00
# else
2023-01-09 10:44:02 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Selected format: SampleFormat " < < nativeFormat . sampleFormat ( ) < < " Channel Count " < < nativeFormat . channelCount ( ) < <
" Sample Rate " < < nativeFormat . sampleRate ( ) < < " Codec " < < codec ;
2022-08-22 22:09:09 +00:00
# endif
2021-11-01 20:27:33 +00:00
2021-06-04 07:24:26 +00:00
// We "hopefully" now have a valid format that is supported so try connecting
2021-11-01 20:27:33 +00:00
2022-05-08 18:35:47 +00:00
converter = new audioConverter ( ) ;
2022-05-06 22:10:46 +00:00
converterThread = new QThread ( this ) ;
2021-11-01 20:27:33 +00:00
if ( setup . isinput ) {
2022-05-08 10:46:33 +00:00
converterThread - > setObjectName ( " audioConvIn() " ) ;
2021-11-01 20:27:33 +00:00
}
else {
2022-05-08 10:46:33 +00:00
converterThread - > setObjectName ( " audioConvOut() " ) ;
2021-06-07 13:04:52 +00:00
}
2022-05-06 22:10:46 +00:00
converter - > moveToThread ( converterThread ) ;
2021-06-07 13:04:52 +00:00
2022-08-22 22:09:09 +00:00
connect ( this , SIGNAL ( setupConverter ( QAudioFormat , codecType , QAudioFormat , codecType , quint8 , quint8 ) ) , converter , SLOT ( init ( QAudioFormat , codecType , QAudioFormat , codecType , quint8 , quint8 ) ) ) ;
2022-05-06 22:10:46 +00:00
connect ( converterThread , SIGNAL ( finished ( ) ) , converter , SLOT ( deleteLater ( ) ) ) ;
connect ( this , SIGNAL ( sendToConverter ( audioPacket ) ) , converter , SLOT ( convert ( audioPacket ) ) ) ;
converterThread - > start ( QThread : : TimeCriticalPriority ) ;
2021-06-07 09:40:04 +00:00
2021-06-04 07:24:26 +00:00
if ( setup . isinput ) {
2021-08-22 09:16:16 +00:00
2022-12-29 15:26:41 +00:00
# if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2023-01-09 10:44:02 +00:00
audioInput = new QAudioInput ( setup . port , nativeFormat , this ) ;
2021-08-22 09:16:16 +00:00
# else
2023-01-09 10:44:02 +00:00
audioInput = new QAudioSource ( setup . port , nativeFormat , this ) ;
2021-08-22 09:16:16 +00:00
# endif
2021-06-04 07:24:26 +00:00
connect ( audioInput , SIGNAL ( stateChanged ( QAudio : : State ) ) , SLOT ( stateChanged ( QAudio : : State ) ) ) ;
2023-01-09 10:44:02 +00:00
emit setupConverter ( nativeFormat , codecType : : LPCM , radioFormat , codec , 7 , setup . resampleQuality ) ;
2022-05-06 22:10:46 +00:00
connect ( converter , SIGNAL ( converted ( audioPacket ) ) , this , SLOT ( convertedInput ( audioPacket ) ) ) ;
2021-06-04 07:24:26 +00:00
}
else {
2021-08-22 09:16:16 +00:00
2022-12-29 15:26:41 +00:00
# if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
2023-01-09 10:44:02 +00:00
audioOutput = new QAudioOutput ( setup . port , nativeFormat , this ) ;
2021-08-22 09:16:16 +00:00
# else
2023-01-09 10:44:02 +00:00
audioOutput = new QAudioSink ( setup . port , nativeFormat , this ) ;
2021-08-22 09:16:16 +00:00
# endif
2021-06-07 09:40:04 +00:00
2021-06-04 07:24:26 +00:00
connect ( audioOutput , SIGNAL ( stateChanged ( QAudio : : State ) ) , SLOT ( stateChanged ( QAudio : : State ) ) ) ;
2023-01-09 10:44:02 +00:00
emit setupConverter ( radioFormat , codec , nativeFormat , codecType : : LPCM , 7 , setup . resampleQuality ) ;
2022-05-06 22:10:46 +00:00
connect ( converter , SIGNAL ( converted ( audioPacket ) ) , this , SLOT ( convertedOutput ( audioPacket ) ) ) ;
2021-06-04 07:24:26 +00:00
}
2021-08-14 09:29:22 +00:00
2022-05-06 22:10:46 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " thread id " < < QThread : : currentThreadId ( ) ;
2021-06-04 07:24:26 +00:00
2022-04-06 21:02:43 +00:00
underTimer = new QTimer ( ) ;
underTimer - > setSingleShot ( true ) ;
2022-05-02 14:58:41 +00:00
connect ( underTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( clearUnderrun ( ) ) ) ;
2022-04-06 21:02:43 +00:00
2022-05-13 08:55:16 +00:00
this - > setVolume ( setup . localAFgain ) ;
2021-03-13 09:50:43 +00:00
2022-04-03 23:01:08 +00:00
this - > start ( ) ;
2022-04-06 21:02:43 +00:00
2022-04-05 15:47:43 +00:00
return true ;
2021-02-11 19:18:35 +00:00
}
2021-06-04 07:24:26 +00:00
void audioHandler : : start ( )
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " start() running " ;
if ( setup . isinput ) {
2022-05-07 09:43:28 +00:00
//this->open(QIODevice::WriteOnly);
//audioInput->start(this);
2023-01-02 16:11:48 +00:00
# if (defined(Q_OS_WIN) && (QT_VERSION < QT_VERSION_CHECK(6,0,0)))
2023-01-09 10:44:02 +00:00
audioInput - > setBufferSize ( nativeFormat . bytesForDuration ( setup . latency * 100 ) ) ;
2021-06-07 09:58:58 +00:00
# else
2023-01-09 10:44:02 +00:00
audioInput - > setBufferSize ( nativeFormat . bytesForDuration ( setup . latency * 1000 ) ) ;
2021-06-07 09:58:58 +00:00
# endif
2022-05-07 09:43:28 +00:00
audioDevice = audioInput - > start ( ) ;
2022-05-02 14:58:41 +00:00
connect ( audioInput , SIGNAL ( destroyed ( ) ) , audioDevice , SLOT ( deleteLater ( ) ) , Qt : : UniqueConnection ) ;
2022-05-07 09:54:20 +00:00
connect ( audioDevice , SIGNAL ( readyRead ( ) ) , this , SLOT ( getNextAudioChunk ( ) ) , Qt : : UniqueConnection ) ;
2022-05-07 09:51:02 +00:00
//audioInput->setNotifyInterval(setup.blockSize/2);
//connect(audioInput, SIGNAL(notify()), this, SLOT(getNextAudioChunk()), Qt::UniqueConnection);
2021-06-04 07:24:26 +00:00
}
else {
2022-05-02 10:52:23 +00:00
// Buffer size must be set before audio is started.
2023-01-02 16:11:48 +00:00
# if (defined(Q_OS_WIN) && (QT_VERSION < QT_VERSION_CHECK(6,0,0)))
2023-01-09 10:44:02 +00:00
audioOutput - > setBufferSize ( nativeFormat . bytesForDuration ( setup . latency * 100 ) ) ;
2021-06-04 07:24:26 +00:00
# else
2023-01-09 10:44:02 +00:00
audioOutput - > setBufferSize ( nativeFormat . bytesForDuration ( setup . latency * 1000 ) ) ;
2021-06-04 07:24:26 +00:00
# endif
2022-04-03 19:16:52 +00:00
audioDevice = audioOutput - > start ( ) ;
2022-05-02 14:58:41 +00:00
connect ( audioOutput , SIGNAL ( destroyed ( ) ) , audioDevice , SLOT ( deleteLater ( ) ) , Qt : : UniqueConnection ) ;
2021-07-06 09:04:35 +00:00
}
2022-04-03 23:01:08 +00:00
if ( ! audioDevice ) {
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio device failed to start() " ;
return ;
2021-05-23 21:45:10 +00:00
}
2021-05-23 15:09:41 +00:00
}
2021-02-11 19:18:35 +00:00
2022-04-04 10:47:55 +00:00
void audioHandler : : stop ( )
2021-02-11 19:18:35 +00:00
{
2022-04-04 10:47:55 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " stop() running " ;
2021-05-29 17:59:45 +00:00
2022-04-04 10:47:55 +00:00
if ( audioOutput ! = Q_NULLPTR & & audioOutput - > state ( ) ! = QAudio : : StoppedState ) {
// Stop audio output
audioOutput - > stop ( ) ;
2021-05-27 10:41:08 +00:00
}
2021-03-09 17:22:16 +00:00
2022-04-04 10:47:55 +00:00
if ( audioInput ! = Q_NULLPTR & & audioInput - > state ( ) ! = QAudio : : StoppedState ) {
// Stop audio output
audioInput - > stop ( ) ;
2021-05-29 17:59:45 +00:00
}
2022-04-04 10:47:55 +00:00
audioDevice = Q_NULLPTR ;
}
2021-03-09 17:22:16 +00:00
2022-05-06 22:10:46 +00:00
void audioHandler : : setVolume ( unsigned char volume )
{
this - > volume = audiopot [ volume ] ;
}
2021-03-09 17:22:16 +00:00
2022-05-06 22:10:46 +00:00
void audioHandler : : incomingAudio ( audioPacket packet )
{
2021-05-29 17:59:45 +00:00
2022-05-08 17:44:20 +00:00
if ( audioDevice ! = Q_NULLPTR & & packet . data . size ( ) > 0 ) {
2022-05-08 09:04:36 +00:00
packet . volume = volume ;
2021-03-09 17:22:16 +00:00
2022-05-08 09:04:36 +00:00
emit sendToConverter ( packet ) ;
2021-05-27 10:41:08 +00:00
}
2021-05-27 17:34:44 +00:00
return ;
2021-02-11 19:18:35 +00:00
}
2022-05-06 22:10:46 +00:00
void audioHandler : : convertedOutput ( audioPacket packet ) {
2022-05-08 17:44:20 +00:00
if ( packet . data . size ( ) > 0 ) {
2023-01-09 10:44:02 +00:00
currentLatency = packet . time . msecsTo ( QTime : : currentTime ( ) ) + ( nativeFormat . durationForBytes ( audioOutput - > bufferSize ( ) - audioOutput - > bytesFree ( ) ) / 1000 ) ;
2022-05-08 17:44:20 +00:00
if ( audioDevice ! = Q_NULLPTR ) {
2022-05-10 22:55:18 +00:00
if ( audioDevice - > write ( packet . data ) < packet . data . size ( ) ) {
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Buffer full! " ;
isOverrun = true ;
} else {
isOverrun = false ;
}
2022-05-08 17:44:20 +00:00
if ( lastReceived . msecsTo ( QTime : : currentTime ( ) ) > 100 ) {
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Time since last audio packet " < < lastReceived . msecsTo ( QTime : : currentTime ( ) ) < < " Expected around " < < setup . blockSize ;
}
lastReceived = QTime : : currentTime ( ) ;
}
/*if ((packet.seq > lastSentSeq + 1) && (setup.codec == 0x40 || setup.codec == 0x80)) {
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Attempting FEC on packet " < < packet . seq < < " as last is " < < lastSentSeq ;
lastSentSeq = packet . seq ;
incomingAudio ( packet ) ; // Call myself again to run the packet a second time (FEC)
}
*/
lastSentSeq = packet . seq ;
2022-08-24 05:24:05 +00:00
amplitude = packet . amplitudePeak ;
2022-08-24 22:02:00 +00:00
emit haveLevels ( getAmplitude ( ) , static_cast < quint16 > ( packet . amplitudeRMS * 255.0 ) , setup . latency , currentLatency , isUnderrun , isOverrun ) ;
2022-05-08 17:44:20 +00:00
}
2021-02-11 19:18:35 +00:00
}
2022-04-10 22:13:51 +00:00
void audioHandler : : getNextAudioChunk ( )
2021-02-11 19:18:35 +00:00
{
2022-05-08 17:44:20 +00:00
if ( audioDevice ) {
tempBuf . data . append ( audioDevice - > readAll ( ) ) ;
}
2023-01-09 10:44:02 +00:00
if ( tempBuf . data . length ( ) > = nativeFormat . bytesForDuration ( setup . blockSize * 1000 ) ) {
2022-05-06 22:10:46 +00:00
audioPacket packet ;
packet . time = QTime : : currentTime ( ) ;
packet . sent = 0 ;
packet . volume = volume ;
memcpy ( & packet . guid , setup . guid , GUIDLEN ) ;
2022-05-07 09:43:28 +00:00
//QTime startProcessing = QTime::currentTime();
2022-05-06 22:10:46 +00:00
packet . data . clear ( ) ;
2023-01-09 10:44:02 +00:00
packet . data = tempBuf . data . mid ( 0 , nativeFormat . bytesForDuration ( setup . blockSize * 1000 ) ) ;
tempBuf . data . remove ( 0 , nativeFormat . bytesForDuration ( setup . blockSize * 1000 ) ) ;
2022-05-06 22:10:46 +00:00
emit sendToConverter ( packet ) ;
2021-02-27 09:34:56 +00:00
}
2021-05-27 12:54:52 +00:00
2022-05-08 17:44:20 +00:00
/* If there is still enough data in the buffer, call myself again in 20ms */
2023-01-09 10:44:02 +00:00
if ( tempBuf . data . length ( ) > = nativeFormat . bytesForDuration ( setup . blockSize * 1000 ) ) {
2022-05-08 17:44:20 +00:00
QTimer : : singleShot ( setup . blockSize , this , & audioHandler : : getNextAudioChunk ) ;
}
2021-02-11 19:18:35 +00:00
2022-05-08 17:44:20 +00:00
return ;
2021-02-12 20:42:56 +00:00
}
2021-08-14 15:04:50 +00:00
2022-05-06 22:10:46 +00:00
void audioHandler : : convertedInput ( audioPacket audio )
2021-02-12 20:42:56 +00:00
{
2022-05-08 17:44:20 +00:00
if ( audio . data . size ( ) > 0 ) {
emit haveAudioData ( audio ) ;
if ( lastReceived . msecsTo ( QTime : : currentTime ( ) ) > 100 ) {
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Time since last audio packet " < < lastReceived . msecsTo ( QTime : : currentTime ( ) ) < < " Expected around " < < setup . blockSize ;
}
lastReceived = QTime : : currentTime ( ) ;
2022-08-24 05:24:05 +00:00
amplitude = audio . amplitudePeak ;
2022-08-24 22:02:00 +00:00
emit haveLevels ( getAmplitude ( ) , audio . amplitudeRMS , setup . latency , currentLatency , isUnderrun , isOverrun ) ;
2022-05-08 17:44:20 +00:00
}
2022-05-06 22:10:46 +00:00
}
2021-05-29 17:59:45 +00:00
2022-04-05 15:47:43 +00:00
void audioHandler : : changeLatency ( const quint16 newSize )
{
2021-05-27 12:54:52 +00:00
2022-04-05 15:47:43 +00:00
setup . latency = newSize ;
2021-08-14 11:08:50 +00:00
2022-04-05 15:47:43 +00:00
if ( ! setup . isinput ) {
stop ( ) ;
start ( ) ;
2021-02-27 09:34:56 +00:00
}
2023-01-09 10:44:02 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Configured latency: " < < setup . latency < < " Buffer Duration: " < < nativeFormat . durationForBytes ( audioOutput - > bufferSize ( ) ) / 1000 < < " ms " ;
2021-05-27 12:54:52 +00:00
2021-02-12 20:42:56 +00:00
}
2022-04-05 15:47:43 +00:00
int audioHandler : : getLatency ( )
2021-06-04 07:24:26 +00:00
{
2022-04-05 15:47:43 +00:00
return currentLatency ;
2021-06-04 07:24:26 +00:00
}
2022-01-22 15:12:36 +00:00
quint16 audioHandler : : getAmplitude ( )
2021-06-04 07:24:26 +00:00
{
2022-04-03 23:01:08 +00:00
return static_cast < quint16 > ( amplitude * 255.0 ) ;
2021-06-04 07:24:26 +00:00
}
2022-04-06 20:52:22 +00:00
2021-06-04 07:24:26 +00:00
void audioHandler : : stateChanged ( QAudio : : State state )
{
// Process the state
switch ( state )
{
2022-04-06 20:52:22 +00:00
case QAudio : : IdleState :
{
isUnderrun = true ;
2022-05-02 10:52:23 +00:00
if ( underTimer - > isActive ( ) ) {
underTimer - > stop ( ) ;
2021-06-04 07:24:26 +00:00
}
2022-04-06 20:52:22 +00:00
break ;
}
case QAudio : : ActiveState :
{
2022-05-02 10:52:23 +00:00
//qDebug(logAudio()) << (setup.isinput ? "Input" : "Output") << "Audio started!";
if ( ! underTimer - > isActive ( ) ) {
underTimer - > start ( 500 ) ;
2021-06-04 07:24:26 +00:00
}
2022-04-06 20:52:22 +00:00
break ;
2021-06-04 07:24:26 +00:00
}
2022-04-06 20:52:22 +00:00
case QAudio : : SuspendedState :
{
break ;
2021-06-04 07:24:26 +00:00
}
2022-04-06 20:52:22 +00:00
case QAudio : : StoppedState :
{
break ;
}
default : {
}
break ;
2021-06-04 07:24:26 +00:00
}
}
2022-04-06 20:52:22 +00:00
void audioHandler : : clearUnderrun ( )
{
isUnderrun = false ;
2022-05-04 21:29:05 +00:00
}