2021-02-11 19:18:35 +00:00
/*
2021-05-23 15:09:41 +00:00
This class handles both RX and TX audio , each is created as a seperate instance of the class
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
# if defined(Q_OS_WIN) && defined(PORTAUDIO)
# include <objbase.h>
# endif
2021-06-16 22:44:59 +00:00
2021-06-04 07:24:26 +00:00
audioHandler : : audioHandler ( 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
2021-06-03 11:05:28 +00:00
if ( isInitialized ) {
2021-06-04 07:24:26 +00:00
# if defined(RTAUDIO)
2021-06-02 17:36:33 +00:00
2021-06-02 18:18:44 +00:00
try {
audio - > abortStream ( ) ;
audio - > closeStream ( ) ;
}
catch ( RtAudioError & e ) {
qInfo ( logAudio ( ) ) < < " Error closing stream: " < < aParams . deviceId < < " : " < < QString : : fromStdString ( e . getMessage ( ) ) ;
}
2021-06-02 17:36:33 +00:00
delete audio ;
2021-06-04 07:24:26 +00:00
# elif defined(PORTAUDIO)
2021-11-01 20:27:33 +00:00
Pa_StopStream ( audio ) ;
2021-11-07 12:16:25 +00:00
Pa_CloseStream ( audio ) ;
2021-06-04 07:24:26 +00:00
# else
stop ( ) ;
# endif
2021-06-02 17:36:33 +00:00
}
2021-06-03 11:05:28 +00:00
if ( ringBuf ! = Q_NULLPTR ) {
2021-05-27 10:41:08 +00:00
delete ringBuf ;
2021-06-03 11:05:28 +00:00
}
2021-06-04 07:24:26 +00:00
if ( resampler ! = Q_NULLPTR ) {
speex_resampler_destroy ( resampler ) ;
qDebug ( logAudio ( ) ) < < " Resampler closed " ;
}
2021-06-16 08:49:38 +00:00
if ( encoder ! = Q_NULLPTR ) {
2021-08-15 10:36:08 +00:00
qInfo ( logAudio ( ) ) < < " Destroying opus encoder " ;
2021-06-16 08:49:38 +00:00
opus_encoder_destroy ( encoder ) ;
}
if ( decoder ! = Q_NULLPTR ) {
2021-08-15 10:36:08 +00:00
qInfo ( logAudio ( ) ) < < " Destroying opus decoder " ;
2021-06-16 08:49:38 +00:00
opus_decoder_destroy ( decoder ) ;
}
2021-02-11 19:18:35 +00:00
}
2021-06-04 07:24:26 +00:00
bool audioHandler : : init ( audioSetup setupIn )
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
if ( isInitialized ) {
return false ;
}
2021-06-04 07:24:26 +00:00
/*
0x01 uLaw 1 ch 8 bit
0x02 PCM 1 ch 8 bit
0x04 PCM 1 ch 16 bit
0x08 PCM 2 ch 8 bit
0x10 PCM 2 ch 16 bit
0x20 uLaw 2 ch 8 bit
*/
setup = setupIn ;
setup . radioChan = 1 ;
setup . bits = 8 ;
if ( setup . codec = = 0x01 | | setup . codec = = 0x20 ) {
setup . ulaw = true ;
}
2021-08-14 09:06:17 +00:00
if ( setup . codec = = 0x08 | | setup . codec = = 0x10 | | setup . codec = = 0x20 | | setup . codec = = 0x80 ) {
2021-06-04 07:24:26 +00:00
setup . radioChan = 2 ;
}
2021-08-14 09:06:17 +00:00
if ( setup . codec = = 0x04 | | setup . codec = = 0x10 | | setup . codec = = 0x40 | | setup . codec = = 0x80 ) {
2021-06-04 07:24:26 +00:00
setup . bits = 16 ;
}
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 < <
" , bits " < < setup . bits < <
" , codec " < < setup . codec < <
" , latency " < < setup . latency < <
" , localAFGain " < < setup . localAFgain < <
" , radioChan " < < setup . radioChan < <
" , resampleQuality " < < setup . resampleQuality < <
" , samplerate " < < setup . samplerate < <
" , uLaw " < < setup . ulaw ;
2021-11-01 09:18:01 +00:00
ringBuf = new wilt : : Ring < audioPacket > ( setupIn . latency / 8 + 1 ) ; // Should be customizable.
2021-06-02 17:35:04 +00:00
2021-06-04 07:24:26 +00:00
tempBuf . sent = 0 ;
2021-07-06 06:24:35 +00:00
if ( ! setup . isinput )
{
this - > setVolume ( setup . localAFgain ) ;
}
2021-06-04 07:24:26 +00:00
# if defined(RTAUDIO)
# if !defined(Q_OS_MACX)
options . flags = ( ( ! RTAUDIO_HOG_DEVICE ) | ( RTAUDIO_MINIMIZE_LATENCY ) ) ;
# endif
2021-06-02 19:15:31 +00:00
# if defined(Q_OS_LINUX)
audio = new RtAudio ( RtAudio : : Api : : LINUX_ALSA ) ;
# elif defined(Q_OS_WIN)
audio = new RtAudio ( RtAudio : : Api : : WINDOWS_WASAPI ) ;
# elif defined(Q_OS_MACX)
audio = new RtAudio ( RtAudio : : Api : : MACOSX_CORE ) ;
# endif
2021-06-02 17:35:04 +00:00
2021-06-06 16:56:48 +00:00
if ( setup . port > 0 ) {
aParams . deviceId = setup . port ;
2021-05-23 15:09:41 +00:00
}
2021-06-04 07:24:26 +00:00
else if ( setup . isinput ) {
2021-06-02 17:35:04 +00:00
aParams . deviceId = audio - > getDefaultInputDevice ( ) ;
2021-05-23 15:09:41 +00:00
}
else {
2021-06-02 17:35:04 +00:00
aParams . deviceId = audio - > getDefaultOutputDevice ( ) ;
2021-05-23 15:09:41 +00:00
}
aParams . firstChannel = 0 ;
2021-02-11 19:18:35 +00:00
2021-05-24 17:00:38 +00:00
try {
2021-06-02 17:35:04 +00:00
info = audio - > getDeviceInfo ( aParams . deviceId ) ;
2021-05-24 17:00:38 +00:00
}
catch ( RtAudioError & e ) {
qInfo ( logAudio ( ) ) < < " Device error: " < < aParams . deviceId < < " : " < < QString : : fromStdString ( e . getMessage ( ) ) ;
2021-06-03 11:05:28 +00:00
return isInitialized ;
2021-05-24 17:00:38 +00:00
}
2021-05-22 09:43:57 +00:00
2021-05-23 15:09:41 +00:00
if ( info . probed )
{
2021-11-01 11:17:25 +00:00
// Always use the "preferred" sample rate
// We can always resample if needed
this - > nativeSampleRate = info . preferredSampleRate ;
2021-06-02 11:21:06 +00:00
2021-06-02 11:35:10 +00:00
// Per channel chunk size.
2021-06-02 11:21:06 +00:00
this - > chunkSize = ( this - > nativeSampleRate / 50 ) ;
2021-05-29 17:59:45 +00:00
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < QString : : fromStdString ( info . name ) < < " ( " < < aParams . deviceId < < " ) successfully probed " ;
2021-05-23 15:09:41 +00:00
if ( info . nativeFormats = = 0 )
2021-05-29 19:13:52 +00:00
{
2021-05-23 15:09:41 +00:00
qInfo ( logAudio ( ) ) < < " No natively supported data formats! " ;
2021-05-29 19:13:52 +00:00
return false ;
}
2021-05-23 15:09:41 +00:00
else {
qDebug ( logAudio ( ) ) < < " Supported formats: " < <
( info . nativeFormats & RTAUDIO_SINT8 ? " 8-bit int, " : " " ) < <
( info . nativeFormats & RTAUDIO_SINT16 ? " 16-bit int, " : " " ) < <
( info . nativeFormats & RTAUDIO_SINT24 ? " 24-bit int, " : " " ) < <
( info . nativeFormats & RTAUDIO_SINT32 ? " 32-bit int, " : " " ) < <
( info . nativeFormats & RTAUDIO_FLOAT32 ? " 32-bit float, " : " " ) < <
( info . nativeFormats & RTAUDIO_FLOAT64 ? " 64-bit float, " : " " ) ;
2021-05-29 19:13:52 +00:00
2021-05-23 15:09:41 +00:00
qInfo ( logAudio ( ) ) < < " Preferred sample rate: " < < info . preferredSampleRate ;
2021-06-04 07:24:26 +00:00
if ( setup . isinput ) {
2021-05-29 19:13:52 +00:00
devChannels = info . inputChannels ;
2021-06-04 07:24:26 +00:00
}
2021-05-29 19:13:52 +00:00
else {
devChannels = info . outputChannels ;
}
qInfo ( logAudio ( ) ) < < " Channels: " < < devChannels ;
if ( devChannels > 2 ) {
devChannels = 2 ;
}
aParams . nChannels = devChannels ;
2021-03-01 20:44:09 +00:00
}
2021-05-23 15:09:41 +00:00
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < " chunkSize: " < < chunkSize ;
2021-05-23 15:09:41 +00:00
try {
2021-06-04 07:24:26 +00:00
if ( setup . isinput ) {
2021-06-06 16:56:48 +00:00
audio - > openStream ( NULL , & aParams , RTAUDIO_SINT16 , this - > nativeSampleRate , & this - > chunkSize , & staticWrite , this , & options ) ;
2021-06-04 07:24:26 +00:00
}
else {
audio - > openStream ( & aParams , NULL , RTAUDIO_SINT16 , this - > nativeSampleRate , & this - > chunkSize , & staticRead , this , & options ) ;
}
2021-06-02 17:35:04 +00:00
audio - > startStream ( ) ;
2021-06-03 11:05:28 +00:00
isInitialized = true ;
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " device successfully opened " ;
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " detected latency: " < < audio - > getStreamLatency ( ) ;
2021-05-23 15:09:41 +00:00
}
catch ( RtAudioError & e ) {
qInfo ( logAudio ( ) ) < < " Error opening: " < < QString : : fromStdString ( e . getMessage ( ) ) ;
2021-03-01 20:44:09 +00:00
}
2021-03-01 20:31:05 +00:00
}
2021-05-23 15:09:41 +00:00
else
{
2021-06-04 07:24:26 +00:00
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < QString : : fromStdString ( info . name ) < < " ( " < < aParams . deviceId < < " ) could not be probed, check audio configuration! " ;
2021-05-23 15:09:41 +00:00
}
2021-05-16 20:16:59 +00:00
2021-05-29 17:59:45 +00:00
2021-06-04 07:24:26 +00:00
# elif defined(PORTAUDIO)
2021-11-01 20:27:33 +00:00
PaError err ;
# ifdef Q_OS_WIN
CoInitialize ( 0 ) ;
# endif
memset ( & aParams , 0 , sizeof ( PaStreamParameters ) ) ;
if ( setup . port > 0 ) {
aParams . device = setup . port ;
}
else if ( setup . isinput ) {
aParams . device = Pa_GetDefaultInputDevice ( ) ;
}
else {
aParams . device = Pa_GetDefaultOutputDevice ( ) ;
}
info = Pa_GetDeviceInfo ( aParams . device ) ;
aParams . channelCount = 2 ;
aParams . hostApiSpecificStreamInfo = NULL ;
aParams . sampleFormat = paInt16 ;
2021-11-09 15:55:43 +00:00
if ( setup . isinput ) {
aParams . suggestedLatency = info - > defaultLowInputLatency ;
}
else {
aParams . suggestedLatency = info - > defaultLowOutputLatency ;
}
2021-11-01 20:27:33 +00:00
aParams . hostApiSpecificStreamInfo = NULL ;
2021-11-15 15:26:18 +00:00
// Always use the "preferred" sample rate (unless it is 44100)
2021-11-01 20:27:33 +00:00
// We can always resample if needed
2021-11-15 15:26:18 +00:00
if ( info - > defaultSampleRate = = 44100 ) {
this - > nativeSampleRate = 48000 ;
}
else {
this - > nativeSampleRate = info - > defaultSampleRate ;
}
2021-11-01 20:27:33 +00:00
// Per channel chunk size.
this - > chunkSize = ( this - > nativeSampleRate / 50 ) ;
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < info - > name < < " ( " < < aParams . device < < " ) successfully probed " ;
if ( setup . isinput ) {
devChannels = info - > maxInputChannels ;
}
else {
devChannels = info - > maxOutputChannels ;
}
if ( devChannels > 2 ) {
devChannels = 2 ;
}
aParams . channelCount = devChannels ;
2021-11-07 12:16:25 +00:00
qInfo ( logAudio ( ) ) < < " Channels: " < < devChannels ;
2021-11-01 20:27:33 +00:00
qInfo ( logAudio ( ) ) < < " chunkSize: " < < chunkSize ;
2021-11-07 12:16:25 +00:00
qInfo ( logAudio ( ) ) < < " sampleRate: " < < nativeSampleRate ;
2021-11-01 20:27:33 +00:00
if ( setup . isinput ) {
err = Pa_OpenStream ( & audio , & aParams , 0 , this - > nativeSampleRate , this - > chunkSize , paNoFlag , & audioHandler : : staticWrite , ( void * ) this ) ;
}
else {
err = Pa_OpenStream ( & audio , 0 , & aParams , this - > nativeSampleRate , this - > chunkSize , paNoFlag , & audioHandler : : staticRead , ( void * ) this ) ;
}
if ( err = = paNoError ) {
err = Pa_StartStream ( audio ) ;
}
if ( err = = paNoError ) {
isInitialized = true ;
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " device successfully opened " ;
}
else {
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " failed to open device " < < Pa_GetErrorText ( err ) ;
}
2021-06-04 07:24:26 +00:00
# else
format . setSampleSize ( 16 ) ;
format . setChannelCount ( 2 ) ;
format . setSampleRate ( INTERNAL_SAMPLE_RATE ) ;
format . setCodec ( " audio/pcm " ) ;
format . setByteOrder ( QAudioFormat : : LittleEndian ) ;
format . setSampleType ( QAudioFormat : : SignedInt ) ;
if ( setup . port . isNull ( ) )
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " No audio device was found. You probably need to install libqt5multimedia-plugins. " ;
return false ;
}
else if ( ! setup . port . isFormatSupported ( format ) )
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Format not supported, choosing nearest supported format - which may not work! " ;
format = setup . port . nearestFormat ( format ) ;
}
if ( format . channelCount ( ) > 2 ) {
format . setChannelCount ( 2 ) ;
}
2021-06-07 13:04:52 +00:00
else if ( format . channelCount ( ) < 1 )
{
qCritical ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " No channels found, aborting setup. " ;
return false ;
}
2021-06-04 07:24:26 +00:00
devChannels = format . channelCount ( ) ;
nativeSampleRate = format . sampleRate ( ) ;
// chunk size is always relative to Internal Sample Rate.
this - > chunkSize = ( nativeSampleRate / 50 ) ;
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Internal: sample rate " < < format . sampleRate ( ) < < " channel count " < < format . channelCount ( ) ;
// We "hopefully" now have a valid format that is supported so try connecting
2021-06-07 09:40:04 +00:00
2021-06-04 07:24:26 +00:00
if ( setup . isinput ) {
audioInput = new QAudioInput ( setup . port , format , this ) ;
connect ( audioInput , SIGNAL ( notify ( ) ) , SLOT ( notified ( ) ) ) ;
connect ( audioInput , SIGNAL ( stateChanged ( QAudio : : State ) ) , SLOT ( stateChanged ( QAudio : : State ) ) ) ;
isInitialized = true ;
}
else {
audioOutput = new QAudioOutput ( setup . port , format , this ) ;
2021-06-07 09:40:04 +00:00
# ifdef Q_OS_MAC
audioOutput - > setBufferSize ( chunkSize * 4 ) ;
# endif
2021-06-04 07:24:26 +00:00
connect ( audioOutput , SIGNAL ( notify ( ) ) , SLOT ( notified ( ) ) ) ;
connect ( audioOutput , SIGNAL ( stateChanged ( QAudio : : State ) ) , SLOT ( stateChanged ( QAudio : : State ) ) ) ;
isInitialized = true ;
}
# endif
2021-08-14 09:29:22 +00:00
// Setup resampler and opus if they are needed.
2021-06-04 07:24:26 +00:00
int resample_error = 0 ;
2021-08-14 09:29:22 +00:00
int opus_err = 0 ;
2021-06-04 07:24:26 +00:00
if ( setup . isinput ) {
resampler = wf_resampler_init ( devChannels , nativeSampleRate , setup . samplerate , setup . resampleQuality , & resample_error ) ;
2021-08-14 09:29:22 +00:00
if ( setup . codec = = 0x40 | | setup . codec = = 0x80 ) {
// Opus codec
encoder = opus_encoder_create ( setup . samplerate , setup . radioChan , OPUS_APPLICATION_AUDIO , & opus_err ) ;
opus_encoder_ctl ( encoder , OPUS_SET_LSB_DEPTH ( 16 ) ) ;
opus_encoder_ctl ( encoder , OPUS_SET_INBAND_FEC ( 1 ) ) ;
opus_encoder_ctl ( encoder , OPUS_SET_DTX ( 1 ) ) ;
opus_encoder_ctl ( encoder , OPUS_SET_PACKET_LOSS_PERC ( 5 ) ) ;
2021-08-15 10:36:08 +00:00
qInfo ( logAudio ( ) ) < < " Creating opus encoder: " < < opus_strerror ( opus_err ) ;
2021-08-14 09:29:22 +00:00
}
2021-06-04 07:24:26 +00:00
}
else {
resampler = wf_resampler_init ( devChannels , setup . samplerate , this - > nativeSampleRate , setup . resampleQuality , & resample_error ) ;
2021-08-14 09:29:22 +00:00
if ( setup . codec = = 0x40 | | setup . codec = = 0x80 ) {
// Opus codec
decoder = opus_decoder_create ( setup . samplerate , setup . radioChan , & opus_err ) ;
2021-08-15 10:36:08 +00:00
qInfo ( logAudio ( ) ) < < " Creating opus decoder: " < < opus_strerror ( opus_err ) ;
2021-08-14 09:29:22 +00:00
}
2021-06-04 07:24:26 +00:00
}
2021-11-07 13:27:52 +00:00
unsigned int ratioNum ;
unsigned int ratioDen ;
2021-06-04 07:24:26 +00:00
2021-11-07 13:27:52 +00:00
wf_resampler_get_ratio ( resampler , & ratioNum , & ratioDen ) ;
2021-11-07 13:49:00 +00:00
resampleRatio = static_cast < double > ( ratioDen ) / ratioNum ;
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " wf_resampler_init() returned: " < < resample_error < < " resampleRatio: " < < resampleRatio ;
2021-08-14 09:29:22 +00:00
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " thread id " < < QThread : : currentThreadId ( ) ;
2021-06-06 16:56:48 +00:00
# if !defined (RTAUDIO) && !defined(PORTAUDIO)
2021-06-04 07:24:26 +00:00
if ( isInitialized ) {
this - > start ( ) ;
}
2021-06-06 16:56:48 +00:00
# endif
2021-03-13 09:50:43 +00:00
2021-05-23 15:09:41 +00:00
return isInitialized ;
2021-02-11 19:18:35 +00:00
}
2021-06-06 16:56:48 +00:00
# if !defined (RTAUDIO) && !defined(PORTAUDIO)
2021-06-04 07:24:26 +00:00
void audioHandler : : start ( )
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " start() running " ;
if ( ( audioOutput = = Q_NULLPTR | | audioOutput - > state ( ) ! = QAudio : : StoppedState ) & &
( audioInput = = Q_NULLPTR | | audioInput - > state ( ) ! = QAudio : : StoppedState ) ) {
return ;
}
if ( setup . isinput ) {
2021-11-01 11:17:25 +00:00
# ifndef Q_OS_WIN
2021-06-07 09:58:58 +00:00
this - > open ( QIODevice : : WriteOnly ) ;
# else
this - > open ( QIODevice : : WriteOnly | QIODevice : : Unbuffered ) ;
# endif
2021-06-04 07:24:26 +00:00
audioInput - > start ( this ) ;
}
else {
2021-11-01 11:17:25 +00:00
# ifndef Q_OS_WIN
2021-06-07 09:58:58 +00:00
this - > open ( QIODevice : : ReadOnly ) ;
# else
this - > open ( QIODevice : : ReadOnly | QIODevice : : Unbuffered ) ;
# endif
2021-06-04 07:24:26 +00:00
audioOutput - > start ( this ) ;
}
}
2021-06-06 16:56:48 +00:00
# endif
2021-06-04 07:24:26 +00:00
2021-03-22 18:53:34 +00:00
void audioHandler : : setVolume ( unsigned char volume )
2021-03-22 16:02:22 +00:00
{
2021-07-06 19:02:09 +00:00
//this->volume = (qreal)volume/255.0;
this - > volume = audiopot [ volume ] ;
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " setVolume: " < < volume < < " ( " < < this - > volume < < " ) " ;
2021-02-11 19:18:35 +00:00
}
2021-03-09 17:22:16 +00:00
/// <summary>
/// This function processes the incoming audio FROM the radio and pushes it into the playback buffer *data
/// </summary>
/// <param name="data"></param>
/// <param name="maxlen"></param>
/// <returns></returns>
2021-06-04 07:24:26 +00:00
# if defined(RTAUDIO)
2021-11-01 20:27:33 +00:00
int audioHandler : : readData ( void * outputBuffer , void * inputBuffer ,
unsigned int nFrames , double streamTime , RtAudioStreamStatus status )
2021-02-11 19:18:35 +00:00
{
2021-05-29 17:59:45 +00:00
Q_UNUSED ( inputBuffer ) ;
Q_UNUSED ( streamTime ) ;
2021-05-24 08:27:18 +00:00
if ( status = = RTAUDIO_OUTPUT_UNDERFLOW )
qDebug ( logAudio ( ) ) < < " Underflow detected " ;
2021-05-29 19:13:52 +00:00
int nBytes = nFrames * devChannels * 2 ; // This is ALWAYS 2 bytes per sample and 2 channels
2021-06-04 07:24:26 +00:00
quint8 * buffer = ( quint8 * ) outputBuffer ;
# elif defined(PORTAUDIO)
2021-11-01 20:27:33 +00:00
int audioHandler : : readData ( const void * inputBuffer , void * outputBuffer ,
unsigned long nFrames , const PaStreamCallbackTimeInfo * streamTime , PaStreamCallbackFlags status )
{
Q_UNUSED ( inputBuffer ) ;
Q_UNUSED ( streamTime ) ;
Q_UNUSED ( status ) ;
int nBytes = nFrames * devChannels * 2 ; // This is ALWAYS 2 bytes per sample and 2 channels
quint8 * buffer = ( quint8 * ) outputBuffer ;
2021-06-04 07:24:26 +00:00
# else
qint64 audioHandler : : readData ( char * buffer , qint64 nBytes )
{
# endif
// Calculate output length, always full samples
int sentlen = 0 ;
2021-07-06 09:04:35 +00:00
if ( ! isReady ) {
isReady = true ;
}
2022-01-14 20:23:59 +00:00
if ( ! audioBuffered ) {
# if defined(RTAUDIO)
return 0 ;
# elif defined(PORTAUDIO)
return 0 ;
# else
return nBytes ;
# endif
}
2022-01-14 16:00:05 +00:00
audioPacket packet ;
2021-05-27 10:41:08 +00:00
if ( ringBuf - > size ( ) > 0 )
2021-02-27 00:37:00 +00:00
{
2021-05-23 21:45:10 +00:00
// Output buffer is ALWAYS 16 bit.
2021-05-29 17:59:45 +00:00
//qDebug(logAudio()) << "Read: nFrames" << nFrames << "nBytes" << nBytes;
2021-05-27 10:41:08 +00:00
while ( sentlen < nBytes )
2021-05-23 21:45:10 +00:00
{
2021-05-27 10:41:08 +00:00
if ( ! ringBuf - > try_read ( packet ) )
{
2022-01-14 20:23:59 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " buffer is empty, sentlen: " < < sentlen < < " nBytes: " < < nBytes ;
2021-05-29 17:59:45 +00:00
break ;
2021-05-23 21:45:10 +00:00
}
2021-05-27 10:41:08 +00:00
currentLatency = packet . time . msecsTo ( QTime : : currentTime ( ) ) ;
2021-05-23 21:45:10 +00:00
2021-05-27 10:41:08 +00:00
// This shouldn't be required but if we did output a partial packet
// This will add the remaining packet data to the output buffer.
if ( tempBuf . sent ! = tempBuf . data . length ( ) )
2021-05-23 21:45:10 +00:00
{
2021-05-27 10:41:08 +00:00
int send = qMin ( ( int ) nBytes - sentlen , tempBuf . data . length ( ) - tempBuf . sent ) ;
memcpy ( buffer + sentlen , tempBuf . data . constData ( ) + tempBuf . sent , send ) ;
tempBuf . sent = tempBuf . sent + send ;
sentlen = sentlen + send ;
2021-05-28 17:13:08 +00:00
if ( tempBuf . sent ! = tempBuf . data . length ( ) )
{
// We still don't have enough buffer space for this?
2021-05-29 17:59:45 +00:00
break ;
2021-05-28 17:13:08 +00:00
}
2021-05-27 22:23:23 +00:00
//qDebug(logAudio()) << "Adding partial:" << send;
2021-05-27 10:41:08 +00:00
}
2021-05-23 21:45:10 +00:00
2021-11-09 15:55:43 +00:00
if ( currentLatency > setup . latency ) {
2021-11-18 12:20:36 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Packet " < < hex < < packet . seq < <
2021-05-27 22:23:23 +00:00
" arrived too late (increase output latency!) " < <
dec < < packet . time . msecsTo ( QTime : : currentTime ( ) ) < < " ms " ;
2021-11-09 15:55:43 +00:00
while ( currentLatency > setup . latency / 2 ) {
if ( ! ringBuf - > try_read ( packet ) ) {
break ;
}
currentLatency = packet . time . msecsTo ( QTime : : currentTime ( ) ) ;
}
2022-01-14 16:00:05 +00:00
delayedPackets + + ;
2021-05-27 10:41:08 +00:00
}
2021-05-23 21:45:10 +00:00
2021-05-27 10:41:08 +00:00
int send = qMin ( ( int ) nBytes - sentlen , packet . data . length ( ) ) ;
memcpy ( buffer + sentlen , packet . data . constData ( ) , send ) ;
sentlen = sentlen + send ;
if ( send < packet . data . length ( ) )
{
2021-05-27 22:23:23 +00:00
//qDebug(logAudio()) << "Asking for partial, sent:" << send << "packet length" << packet.data.length();
2021-05-27 10:41:08 +00:00
tempBuf = packet ;
tempBuf . sent = tempBuf . sent + send ;
2021-05-27 22:23:23 +00:00
lastSeq = packet . seq ;
break ;
2021-05-27 10:41:08 +00:00
}
2021-05-23 21:45:10 +00:00
2021-11-18 12:20:36 +00:00
/*
2021-05-27 10:41:08 +00:00
if ( packet . seq < = lastSeq ) {
2021-06-04 07:24:26 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Duplicate/early audio packet: " < < hex < < lastSeq < < " got " < < hex < < packet . seq ;
2021-05-23 21:45:10 +00:00
}
2021-05-27 10:41:08 +00:00
else if ( packet . seq ! = lastSeq + 1 ) {
2021-06-04 07:24:26 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Missing audio packet(s) from: " < < hex < < lastSeq + 1 < < " to " < < hex < < packet . seq - 1 ;
2021-05-23 21:45:10 +00:00
}
2021-11-18 12:20:36 +00:00
*/
2021-05-27 10:41:08 +00:00
lastSeq = packet . seq ;
2021-05-23 21:45:10 +00:00
}
}
2021-05-29 19:50:27 +00:00
//qDebug(logAudio()) << "looking for: " << nBytes << " got: " << sentlen;
2021-05-23 21:45:10 +00:00
2021-05-30 10:36:13 +00:00
// fill the rest of the buffer with silence
if ( nBytes > sentlen ) {
memset ( buffer + sentlen , 0 , nBytes - sentlen ) ;
}
2022-01-14 16:00:05 +00:00
if ( delayedPackets > 10 ) {
while ( ringBuf - > try_read ( packet ) ) ; // Empty buffer
delayedPackets = 0 ;
}
2021-06-04 07:24:26 +00:00
# if defined(RTAUDIO)
2021-05-23 15:09:41 +00:00
return 0 ;
2021-06-04 07:24:26 +00:00
# elif defined(PORTAUDIO)
2021-11-01 20:27:33 +00:00
return 0 ;
2021-06-04 07:24:26 +00:00
# else
return nBytes ;
# endif
2021-05-23 15:09:41 +00:00
}
2021-06-04 07:24:26 +00:00
# if defined(RTAUDIO)
2021-11-01 20:27:33 +00:00
int audioHandler : : writeData ( void * outputBuffer , void * inputBuffer ,
unsigned int nFrames , double streamTime , RtAudioStreamStatus status )
2021-02-11 19:18:35 +00:00
{
2021-05-29 17:59:45 +00:00
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( streamTime ) ;
2021-05-30 11:07:51 +00:00
Q_UNUSED ( status ) ;
2021-06-04 07:24:26 +00:00
int nBytes = nFrames * devChannels * 2 ; // This is ALWAYS 2 bytes per sample and 2 channels
2021-05-27 12:54:52 +00:00
const char * data = ( const char * ) inputBuffer ;
2021-06-04 07:24:26 +00:00
# elif defined(PORTAUDIO)
2021-11-01 20:27:33 +00:00
int audioHandler : : writeData ( const void * inputBuffer , void * outputBuffer ,
unsigned long nFrames , const PaStreamCallbackTimeInfo * streamTime ,
PaStreamCallbackFlags status )
{
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( streamTime ) ;
Q_UNUSED ( status ) ;
int nBytes = nFrames * devChannels * 2 ; // This is ALWAYS 2 bytes per sample and 2 channels
const char * data = ( const char * ) inputBuffer ;
2021-06-04 07:24:26 +00:00
# else
qint64 audioHandler : : writeData ( const char * data , qint64 nBytes )
{
# endif
2021-07-06 09:04:35 +00:00
if ( ! isReady ) {
isReady = true ;
}
2021-06-04 07:24:26 +00:00
int sentlen = 0 ;
2021-05-29 17:59:45 +00:00
//qDebug(logAudio()) << "nFrames" << nFrames << "nBytes" << nBytes;
2021-06-04 12:33:57 +00:00
int chunkBytes = chunkSize * devChannels * 2 ;
2021-05-27 12:54:52 +00:00
while ( sentlen < nBytes ) {
2021-06-04 12:33:57 +00:00
if ( tempBuf . sent ! = chunkBytes )
2021-02-27 11:00:44 +00:00
{
2021-06-04 12:33:57 +00:00
int send = qMin ( ( int ) ( nBytes - sentlen ) , chunkBytes - tempBuf . sent ) ;
2021-05-27 12:54:52 +00:00
tempBuf . data . append ( QByteArray : : fromRawData ( data + sentlen , send ) ) ;
sentlen = sentlen + send ;
2022-01-13 19:34:34 +00:00
tempBuf . seq = lastSentSeq ;
2021-05-27 12:54:52 +00:00
tempBuf . time = QTime : : currentTime ( ) ;
2021-05-27 22:23:23 +00:00
tempBuf . sent = tempBuf . sent + send ;
2021-05-27 12:54:52 +00:00
}
else {
if ( ! ringBuf - > try_write ( tempBuf ) )
2021-02-27 11:00:44 +00:00
{
2022-01-13 19:54:43 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " audio buffer full! " ;
2021-05-27 13:09:12 +00:00
break ;
2021-10-21 23:24:30 +00:00
}
2021-05-27 12:54:52 +00:00
tempBuf . data . clear ( ) ;
tempBuf . sent = 0 ;
2022-01-13 19:34:34 +00:00
lastSentSeq + + ;
2021-02-27 11:00:44 +00:00
}
2021-05-23 15:09:41 +00:00
}
2021-05-27 12:54:52 +00:00
2021-05-29 17:59:45 +00:00
//qDebug(logAudio()) << "sentlen" << sentlen;
2021-06-04 07:24:26 +00:00
# if defined(RTAUDIO)
return 0 ;
# elif defined(PORTAUDIO)
2021-11-01 20:27:33 +00:00
return 0 ;
2021-06-04 07:24:26 +00:00
# else
return nBytes ;
# endif
2021-02-11 19:18:35 +00:00
}
2021-05-29 17:59:45 +00:00
void audioHandler : : incomingAudio ( audioPacket inPacket )
2021-02-11 19:18:35 +00:00
{
2021-05-23 21:45:10 +00:00
// No point buffering audio until stream is actually running.
2021-05-29 17:59:45 +00:00
// Regardless of the radio stream format, the buffered audio will ALWAYS be
// 16bit sample interleaved stereo 48K (or whatever the native sample rate is)
2021-07-06 09:04:35 +00:00
if ( ! isInitialized & & ! isReady )
2021-05-23 21:45:10 +00:00
{
2021-06-07 11:31:58 +00:00
qDebug ( logAudio ( ) ) < < " Packet received when stream was not ready " ;
2021-05-27 17:34:44 +00:00
return ;
2021-05-23 21:45:10 +00:00
}
2022-01-14 20:41:56 +00:00
if ( ! audioBuffered & & ringBuf - > size ( ) > ringBuf - > capacity ( ) / 2 )
{
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio buffering complete, capacity: " < < ringBuf - > capacity ( ) < < " , used: " < < ringBuf - > size ( ) ;
audioBuffered = true ;
}
2021-06-16 08:49:38 +00:00
2022-01-06 13:44:29 +00:00
audioPacket livePacket = inPacket ;
2021-06-16 08:49:38 +00:00
if ( setup . codec = = 0x40 | | setup . codec = = 0x80 ) {
2021-06-16 18:14:21 +00:00
unsigned char * in = ( unsigned char * ) inPacket . data . data ( ) ;
2021-08-13 23:56:16 +00:00
/* Decode the frame. */
2021-08-14 09:06:17 +00:00
QByteArray outPacket ( ( setup . samplerate / 50 ) * sizeof ( qint16 ) * setup . radioChan , ( char ) 0xff ) ; // Preset the output buffer size.
2021-06-16 18:14:21 +00:00
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
2022-01-07 12:28:43 +00:00
int nSamples = opus_packet_get_nb_samples ( in , livePacket . data . size ( ) , setup . samplerate ) ;
2022-01-07 14:52:33 +00:00
if ( nSamples = = - 1 ) {
// No opus data yet?
return ;
}
else if ( nSamples ! = setup . samplerate / 50 )
2021-11-10 18:07:48 +00:00
{
qInfo ( logAudio ( ) ) < < " Opus nSamples= " < < nSamples < < " expected: " < < ( setup . samplerate / 50 ) ;
return ;
2021-11-10 17:44:33 +00:00
}
2022-01-06 13:44:29 +00:00
if ( livePacket . seq > lastSentSeq + 1 ) {
2022-01-07 12:28:43 +00:00
nSamples = opus_decode ( decoder , in , livePacket . data . size ( ) , out , ( setup . samplerate / 50 ) , 1 ) ;
2022-01-05 15:27:46 +00:00
}
else {
2022-01-07 12:28:43 +00:00
nSamples = opus_decode ( decoder , in , livePacket . data . size ( ) , out , ( setup . samplerate / 50 ) , 0 ) ;
2022-01-05 15:27:46 +00:00
}
2021-08-13 23:01:45 +00:00
if ( nSamples < 0 )
{
2022-01-06 13:44:29 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Opus decode failed: " < < opus_strerror ( nSamples ) < < " packet size " < < livePacket . data . length ( ) ;
2021-06-16 08:49:38 +00:00
return ;
}
2021-06-16 09:35:45 +00:00
else {
2021-08-14 11:17:43 +00:00
if ( int ( nSamples * sizeof ( qint16 ) * setup . radioChan ) ! = outPacket . size ( ) )
2021-06-16 22:44:59 +00:00
{
2021-08-14 11:17:43 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Opus decoder mismatch: nBytes: " < < nSamples * sizeof ( qint16 ) * setup . radioChan < < " outPacket: " < < outPacket . size ( ) ;
outPacket . resize ( nSamples * sizeof ( qint16 ) * setup . radioChan ) ;
2021-06-16 22:44:59 +00:00
}
2022-01-06 13:44:29 +00:00
//qInfo(logAudio()) << (setup.isinput ? "Input" : "Output") << "Opus decoded" << livePacket.data.size() << "bytes, into" << outPacket.length() << "bytes";
livePacket . data . clear ( ) ;
livePacket . data = outPacket ; // Replace incoming data with converted.
2021-06-16 09:35:45 +00:00
}
2021-06-16 08:49:38 +00:00
}
2021-06-16 18:14:21 +00:00
2022-01-06 13:44:29 +00:00
//qDebug(logAudio()) << "Got" << setup.bits << "bits, length" << livePacket.data.length();
2021-05-27 10:41:08 +00:00
// Incoming data is 8bits?
2021-06-04 07:24:26 +00:00
if ( setup . bits = = 8 )
2021-05-27 10:41:08 +00:00
{
2021-05-29 17:59:45 +00:00
// Current packet is 8bit so need to create a new buffer that is 16bit
2022-01-06 13:44:29 +00:00
QByteArray outPacket ( ( int ) livePacket . data . length ( ) * 2 * ( devChannels / setup . radioChan ) , ( char ) 0xff ) ;
2021-05-27 10:41:08 +00:00
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
2022-01-06 13:44:29 +00:00
for ( int f = 0 ; f < livePacket . data . length ( ) ; f + + )
2021-03-09 17:22:16 +00:00
{
2022-01-06 13:44:29 +00:00
int samp = ( quint8 ) livePacket . data [ f ] ;
2021-06-04 07:24:26 +00:00
for ( int g = setup . radioChan ; g < = devChannels ; g + + )
2021-03-09 17:22:16 +00:00
{
2021-08-01 17:34:32 +00:00
if ( setup . ulaw )
2021-08-14 12:02:07 +00:00
* out + + = ulaw_decode [ samp ] * this - > volume ;
2021-05-29 17:59:45 +00:00
else
2021-08-14 15:27:31 +00:00
* out + + = ( qint16 ) ( ( samp - 128 ) < < 8 ) * this - > volume ;
2021-03-09 17:22:16 +00:00
}
}
2022-01-06 13:44:29 +00:00
livePacket . data . clear ( ) ;
livePacket . data = outPacket ; // Replace incoming data with converted.
2021-05-27 10:41:08 +00:00
}
2021-05-29 17:59:45 +00:00
else
{
// This is already a 16bit stream, do we need to convert to stereo?
2021-06-04 07:24:26 +00:00
if ( setup . radioChan = = 1 & & devChannels > 1 ) {
2021-05-29 17:59:45 +00:00
// Yes
2022-01-06 13:44:29 +00:00
QByteArray outPacket ( livePacket . data . length ( ) * 2 , ( char ) 0xff ) ; // Preset the output buffer size.
qint16 * in = ( qint16 * ) livePacket . data . data ( ) ;
2021-05-29 17:59:45 +00:00
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
2022-01-06 13:44:29 +00:00
for ( int f = 0 ; f < livePacket . data . length ( ) / 2 ; f + + )
2021-05-29 17:59:45 +00:00
{
* out + + = ( qint16 ) * in * this - > volume ;
* out + + = ( qint16 ) * in + + * this - > volume ;
}
2022-01-06 13:44:29 +00:00
livePacket . data . clear ( ) ;
livePacket . data = outPacket ; // Replace incoming data with converted.
2021-05-29 17:59:45 +00:00
}
else
{
2021-05-29 19:13:52 +00:00
// We already have the same number of channels so just update volume.
2022-01-06 13:44:29 +00:00
qint16 * in = ( qint16 * ) livePacket . data . data ( ) ;
for ( int f = 0 ; f < livePacket . data . length ( ) / 2 ; f + + )
2021-05-29 17:59:45 +00:00
{
2021-05-29 19:50:27 +00:00
* in = * in * this - > volume ;
2021-05-30 11:07:51 +00:00
in + + ;
2021-05-29 17:59:45 +00:00
}
}
2021-03-09 17:22:16 +00:00
2021-05-29 17:59:45 +00:00
}
2021-03-09 17:22:16 +00:00
2021-05-27 10:41:08 +00:00
/* We now have an array of 16bit samples in the NATIVE samplerate of the radio
If the radio sample rate is below 48000 , we need to resample .
*/
2022-01-06 13:44:29 +00:00
//qDebug(logAudio()) << "Now 16 bit stereo, length" << livePacket.data.length();
2021-03-09 17:22:16 +00:00
2021-11-07 13:27:52 +00:00
if ( resampleRatio ! = 1.0 ) {
2021-03-09 17:22:16 +00:00
2021-05-27 10:41:08 +00:00
// We need to resample
2021-05-29 17:59:45 +00:00
// We have a stereo 16bit stream.
2022-01-06 13:44:29 +00:00
quint32 outFrames = ( ( livePacket . data . length ( ) / 2 / devChannels ) * resampleRatio ) ;
quint32 inFrames = ( livePacket . data . length ( ) / 2 / devChannels ) ;
2021-05-29 17:59:45 +00:00
QByteArray outPacket ( outFrames * 4 , ( char ) 0xff ) ; // Preset the output buffer size.
2022-01-06 13:44:29 +00:00
const qint16 * in = ( qint16 * ) livePacket . data . constData ( ) ;
2021-05-29 17:59:45 +00:00
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
2021-03-09 17:22:16 +00:00
2021-05-27 10:41:08 +00:00
int err = 0 ;
2021-05-29 17:59:45 +00:00
err = wf_resampler_process_interleaved_int ( resampler , in , & inFrames , out , & outFrames ) ;
2021-05-27 10:41:08 +00:00
if ( err ) {
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Resampler error " < < err < < " inFrames: " < < inFrames < < " outFrames: " < < outFrames ;
2021-03-09 17:22:16 +00:00
}
2022-01-06 13:44:29 +00:00
livePacket . data . clear ( ) ;
livePacket . data = outPacket ; // Replace incoming data with converted.
2021-05-27 10:41:08 +00:00
}
2022-01-06 13:44:29 +00:00
//qDebug(logAudio()) << "Adding packet to buffer:" << livePacket.seq << ": " << livePacket.data.length();
2021-05-27 12:54:52 +00:00
2022-01-06 13:44:29 +00:00
if ( ! ringBuf - > try_write ( livePacket ) )
2021-05-27 10:41:08 +00:00
{
2021-11-01 20:27:33 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Buffer full! capacity: " < < ringBuf - > capacity ( ) < < " length " < < ringBuf - > size ( ) ;
2022-01-14 16:21:32 +00:00
while ( ringBuf - > try_read ( inPacket ) ) ; // Empty buffer
return ;
2021-05-27 10:41:08 +00:00
}
2022-01-06 13:44:29 +00:00
if ( ( inPacket . seq > lastSentSeq + 1 ) & & ( setup . codec = = 0x40 | | setup . codec = = 0x80 ) ) {
2022-01-14 20:29:27 +00:00
qDebug ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Attempting FEC on packet " < < inPacket . seq < < " as last is " < < lastSentSeq ;
2022-01-05 15:27:46 +00:00
lastSentSeq = inPacket . seq ;
incomingAudio ( inPacket ) ; // Call myself again to run the packet a second time (FEC)
}
lastSentSeq = inPacket . seq ;
2021-05-27 17:34:44 +00:00
return ;
2021-02-11 19:18:35 +00:00
}
2021-02-27 00:37:00 +00:00
void audioHandler : : changeLatency ( const quint16 newSize )
2021-02-11 19:18:35 +00:00
{
2021-06-05 07:26:58 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Changing latency to: " < < newSize < < " from " < < setup . latency ;
setup . latency = newSize ;
2021-11-01 09:18:01 +00:00
delete ringBuf ;
2022-01-14 20:52:25 +00:00
audioBuffered = false ;
2021-11-01 09:18:01 +00:00
ringBuf = new wilt : : Ring < audioPacket > ( setup . latency / 8 + 1 ) ; // Should be customizable.
2021-02-11 19:18:35 +00:00
}
2021-05-27 17:34:44 +00:00
int audioHandler : : getLatency ( )
2021-02-11 19:18:35 +00:00
{
2021-05-27 17:34:44 +00:00
return currentLatency ;
2021-02-11 19:18:35 +00:00
}
2021-08-14 15:04:50 +00:00
2021-02-13 11:04:26 +00:00
void audioHandler : : getNextAudioChunk ( QByteArray & ret )
2021-02-12 20:42:56 +00:00
{
2021-05-27 12:54:52 +00:00
audioPacket packet ;
packet . sent = 0 ;
2021-05-29 17:59:45 +00:00
2021-06-03 11:05:28 +00:00
if ( isInitialized & & ringBuf ! = Q_NULLPTR & & ringBuf - > try_read ( packet ) )
2021-02-27 09:34:56 +00:00
{
2021-06-05 07:42:00 +00:00
currentLatency = packet . time . msecsTo ( QTime : : currentTime ( ) ) ;
2021-05-29 17:59:45 +00:00
2021-11-01 09:18:01 +00:00
if ( currentLatency > setup . latency ) {
2021-06-05 07:42:00 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Packet " < < hex < < packet . seq < <
2022-01-13 19:34:34 +00:00
" arrived too late (increase latency!) " < <
2021-06-05 07:42:00 +00:00
dec < < packet . time . msecsTo ( QTime : : currentTime ( ) ) < < " ms " ;
2022-01-14 16:00:05 +00:00
delayedPackets + + ;
2021-11-01 09:18:01 +00:00
// if (!ringBuf->try_read(packet))
// break;
// currentLatency = packet.time.msecsTo(QTime::currentTime());
2021-06-05 07:42:00 +00:00
}
2021-05-29 17:59:45 +00:00
//qDebug(logAudio) << "Chunksize" << this->chunkSize << "Packet size" << packet.data.length();
// Packet will arrive as stereo interleaved 16bit 48K
2021-11-07 13:27:52 +00:00
if ( resampleRatio ! = 1.0 )
2021-02-27 09:34:56 +00:00
{
2021-11-07 13:49:00 +00:00
quint32 outFrames = ( ( packet . data . length ( ) / 2 / devChannels ) * resampleRatio ) ;
2021-05-29 19:13:52 +00:00
quint32 inFrames = ( packet . data . length ( ) / 2 / devChannels ) ;
QByteArray outPacket ( ( int ) outFrames * 2 * devChannels , ( char ) 0xff ) ;
2021-05-27 12:54:52 +00:00
const qint16 * in = ( qint16 * ) packet . data . constData ( ) ;
2021-05-29 17:59:45 +00:00
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
2021-05-27 12:54:52 +00:00
int err = 0 ;
2021-05-29 17:59:45 +00:00
err = wf_resampler_process_interleaved_int ( resampler , in , & inFrames , out , & outFrames ) ;
2021-05-27 12:54:52 +00:00
if ( err ) {
2021-06-04 07:24:26 +00:00
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Resampler error " < < err < < " inFrames: " < < inFrames < < " outFrames: " < < outFrames ;
2021-05-27 12:54:52 +00:00
}
2021-08-14 11:08:50 +00:00
//qInfo(logAudio()) << "Resampler run " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
//qInfo(logAudio()) << "Resampler run inLen:" << packet->datain.length() << " outLen:" << packet->dataout.length();
2021-05-27 12:54:52 +00:00
packet . data . clear ( ) ;
2021-05-29 17:59:45 +00:00
packet . data = outPacket ; // Copy output packet back to input buffer.
2021-05-27 12:54:52 +00:00
}
2021-05-29 17:59:45 +00:00
//qDebug(logAudio()) << "Now resampled, length" << packet.data.length();
// Do we need to convert mono to stereo?
2021-06-04 07:24:26 +00:00
if ( setup . radioChan = = 1 & & devChannels > 1 )
2021-05-29 17:59:45 +00:00
{
// Strip out right channel?
QByteArray outPacket ( packet . data . length ( ) / 2 , ( char ) 0xff ) ;
const qint16 * in = ( qint16 * ) packet . data . constData ( ) ;
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
for ( int f = 0 ; f < outPacket . length ( ) / 2 ; f + + )
{
* out + + = * in + + ;
2021-05-30 11:07:51 +00:00
in + + ; // Skip each even channel.
2021-05-29 17:59:45 +00:00
}
packet . data . clear ( ) ;
packet . data = outPacket ; // Copy output packet back to input buffer.
}
//qDebug(logAudio()) << "Now mono, length" << packet.data.length();
2021-08-13 19:52:18 +00:00
if ( setup . codec = = 0x40 | | setup . codec = = 0x80 )
2021-08-13 19:41:42 +00:00
{
//Are we using the opus codec?
2021-06-16 18:00:56 +00:00
qint16 * in = ( qint16 * ) packet . data . data ( ) ;
2021-06-16 08:49:38 +00:00
/* Encode the frame. */
2021-06-19 13:09:27 +00:00
QByteArray outPacket ( 1275 , ( char ) 0xff ) ; // Preset the output buffer size to MAXIMUM possible Opus frame size
2021-06-16 08:49:38 +00:00
unsigned char * out = ( unsigned char * ) outPacket . data ( ) ;
2021-08-14 09:06:17 +00:00
int nbBytes = opus_encode ( encoder , in , ( setup . samplerate / 50 ) , out , outPacket . length ( ) ) ;
2021-06-16 08:49:38 +00:00
if ( nbBytes < 0 )
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Opus encode failed: " < < opus_strerror ( nbBytes ) ;
return ;
}
2021-06-16 09:33:16 +00:00
else {
2021-08-14 11:08:50 +00:00
outPacket . resize ( nbBytes ) ;
2021-06-16 09:33:16 +00:00
packet . data . clear ( ) ;
packet . data = outPacket ; // Replace incoming data with converted.
}
2021-08-14 11:08:50 +00:00
2021-06-16 08:49:38 +00:00
}
2021-08-13 19:41:42 +00:00
else if ( setup . bits = = 8 )
{
// Do we need to convert 16-bit to 8-bit?
QByteArray outPacket ( ( int ) packet . data . length ( ) / 2 , ( char ) 0xff ) ;
qint16 * in = ( qint16 * ) packet . data . data ( ) ;
for ( int f = 0 ; f < outPacket . length ( ) ; f + + )
{
2021-08-14 15:11:48 +00:00
qint16 sample = * in + + ;
2021-08-13 19:41:42 +00:00
if ( setup . ulaw ) {
2021-08-14 15:04:50 +00:00
int sign = ( sample > > 8 ) & 0x80 ;
if ( sign )
sample = ( short ) - sample ;
if ( sample > cClip )
sample = cClip ;
sample = ( short ) ( sample + cBias ) ;
int exponent = ( int ) MuLawCompressTable [ ( sample > > 7 ) & 0xFF ] ;
int mantissa = ( sample > > ( exponent + 3 ) ) & 0x0F ;
int compressedByte = ~ ( sign | ( exponent < < 4 ) | mantissa ) ;
2021-08-14 16:15:59 +00:00
outPacket [ f ] = ( quint8 ) compressedByte ;
2021-08-13 19:41:42 +00:00
}
else {
2021-08-14 15:27:31 +00:00
int compressedByte = ( ( ( sample + 32768 ) > > 8 ) & 0xff ) ;
2021-08-14 16:15:59 +00:00
outPacket [ f ] = ( quint8 ) compressedByte ;
2021-08-13 19:41:42 +00:00
}
}
packet . data . clear ( ) ;
packet . data = outPacket ; // Copy output packet back to input buffer.
}
2021-06-16 08:49:38 +00:00
2021-05-27 12:54:52 +00:00
ret = packet . data ;
2021-05-29 17:59:45 +00:00
//qDebug(logAudio()) << "Now radio format, length" << packet.data.length();
2022-01-14 16:00:05 +00:00
if ( delayedPackets > 10 ) {
while ( ringBuf - > try_read ( packet ) ) ; // Empty buffer
delayedPackets = 0 ;
}
2021-02-27 09:34:56 +00:00
}
2021-05-27 12:54:52 +00:00
2021-05-29 17:59:45 +00:00
2021-05-23 15:09:41 +00:00
return ;
2021-05-27 12:54:52 +00:00
2021-02-12 20:42:56 +00:00
}
2021-05-23 15:09:41 +00:00
2021-06-06 16:56:48 +00:00
# if !defined (RTAUDIO) && !defined(PORTAUDIO)
2021-06-04 07:24:26 +00:00
qint64 audioHandler : : bytesAvailable ( ) const
{
return 0 ;
}
bool audioHandler : : isSequential ( ) const
{
return true ;
}
void audioHandler : : notified ( )
{
}
void audioHandler : : stateChanged ( QAudio : : State state )
{
// Process the state
switch ( state )
{
case QAudio : : IdleState :
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio now in idle state: " < < audioBuffer . size ( ) < < " packets in buffer " ;
if ( audioOutput ! = Q_NULLPTR & & audioOutput - > error ( ) = = QAudio : : UnderrunError )
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " buffer underrun " ;
//audioOutput->suspend();
}
break ;
}
case QAudio : : ActiveState :
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio now in active state: " < < audioBuffer . size ( ) < < " packets in buffer " ;
break ;
}
case QAudio : : SuspendedState :
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio now in suspended state: " < < audioBuffer . size ( ) < < " packets in buffer " ;
break ;
}
case QAudio : : StoppedState :
{
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Audio now in stopped state: " < < audioBuffer . size ( ) < < " packets in buffer " ;
break ;
}
default : {
qInfo ( logAudio ( ) ) < < ( setup . isinput ? " Input " : " Output " ) < < " Unhandled audio state: " < < audioBuffer . size ( ) < < " packets in buffer " ;
}
}
}
void audioHandler : : stop ( )
{
if ( audioOutput ! = Q_NULLPTR & & audioOutput - > state ( ) ! = QAudio : : StoppedState ) {
// Stop audio output
audioOutput - > stop ( ) ;
this - > stop ( ) ;
this - > close ( ) ;
2021-06-07 11:31:58 +00:00
delete audioOutput ;
audioOutput = Q_NULLPTR ;
2021-06-04 07:24:26 +00:00
}
if ( audioInput ! = Q_NULLPTR & & audioInput - > state ( ) ! = QAudio : : StoppedState ) {
// Stop audio output
audioInput - > stop ( ) ;
this - > stop ( ) ;
this - > close ( ) ;
2021-06-07 11:31:58 +00:00
delete audioInput ;
audioInput = Q_NULLPTR ;
2021-06-04 07:24:26 +00:00
}
2021-06-07 11:31:58 +00:00
isInitialized = false ;
2021-06-04 07:24:26 +00:00
}
2021-06-06 16:56:48 +00:00
# endif
2021-06-04 07:24:26 +00:00
2021-02-11 19:18:35 +00:00