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
*/
# 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-02-11 19:18:35 +00:00
audioHandler : : audioHandler ( QObject * parent ) :
2021-05-23 15:09:41 +00:00
isInitialized ( false ) ,
isUlaw ( false ) ,
audioLatency ( 0 ) ,
isInput ( 0 ) ,
2021-03-03 09:49:49 +00:00
chunkAvailable ( false )
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-05-23 15:09:41 +00:00
//stop();
if ( resampler ! = Q_NULLPTR ) {
2021-03-09 17:22:16 +00:00
speex_resampler_destroy ( resampler ) ;
}
2021-05-23 15:09:41 +00:00
if ( audio . isStreamRunning ( ) )
{
audio . stopStream ( ) ;
2021-05-24 17:00:38 +00:00
audio . closeStream ( ) ;
2021-05-23 15:09:41 +00:00
}
2021-05-27 10:41:08 +00:00
if ( ringBuf ! = Q_NULLPTR )
delete ringBuf ;
2021-02-11 19:18:35 +00:00
}
2021-05-29 17:59:45 +00:00
bool audioHandler : : init ( const quint8 bits , const quint8 radioChan , const quint16 samplerate , const quint16 latency , const bool ulaw , const bool isinput , int port , quint8 resampleQuality )
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
2021-05-16 20:16:59 +00:00
this - > audioLatency = latency ;
2021-02-27 00:37:00 +00:00
this - > isUlaw = ulaw ;
2021-05-23 15:09:41 +00:00
this - > isInput = isinput ;
this - > radioSampleBits = bits ;
this - > radioSampleRate = samplerate ;
2021-05-29 17:59:45 +00:00
this - > radioChannels = radioChan ;
2021-03-09 17:22:16 +00:00
2021-03-09 19:53:28 +00:00
// chunk size is always relative to Internal Sample Rate.
2021-03-09 17:22:16 +00:00
2021-05-27 10:41:08 +00:00
ringBuf = new wilt : : Ring < audioPacket > ( 100 ) ; // Should be customizable.
tempBuf . sent = 0 ;
2021-05-23 15:09:41 +00:00
if ( port > 0 ) {
aParams . deviceId = port ;
}
else if ( isInput ) {
aParams . deviceId = audio . getDefaultInputDevice ( ) ;
}
else {
aParams . deviceId = audio . getDefaultOutputDevice ( ) ;
}
aParams . firstChannel = 0 ;
2021-02-11 19:18:35 +00:00
2021-05-24 17:00:38 +00:00
try {
info = audio . getDeviceInfo ( aParams . deviceId ) ;
}
catch ( RtAudioError & e ) {
qInfo ( logAudio ( ) ) < < " Device error: " < < aParams . deviceId < < " : " < < QString : : fromStdString ( e . getMessage ( ) ) ;
return false ;
}
2021-05-22 09:43:57 +00:00
2021-05-23 15:09:41 +00:00
if ( info . probed )
{
2021-05-29 17:59:45 +00:00
// Per channel chunk size.
2021-05-29 19:13:52 +00:00
aParams . nChannels = 2 ; // Internally this is always 2 channels for TX and RX.
2021-05-29 17:59:45 +00:00
this - > chunkSize = ( info . preferredSampleRate / 50 ) ;
2021-05-23 15:09:41 +00:00
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < QString : : fromStdString ( info . name ) < < " ( " < < aParams . deviceId < < " ) successfully probed " ;
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-05-29 19:13:52 +00:00
if ( isInput ) {
devChannels = info . inputChannels ;
}
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
qInfo ( logAudio ( ) ) < < " chunkSize: " < < chunkSize ;
2021-03-01 20:31:05 +00:00
}
else
{
2021-05-23 15:09:41 +00:00
qCritical ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < QString : : fromStdString ( info . name ) < < " ( " < < aParams . deviceId < < " ) could not be probed, check audio configuration! " ;
2021-05-29 19:13:52 +00:00
return false ;
2021-05-23 15:09:41 +00:00
}
2021-03-13 09:50:43 +00:00
2021-05-23 15:09:41 +00:00
int resample_error = 0 ;
2021-05-22 09:43:57 +00:00
2021-05-23 15:09:41 +00:00
if ( isInput ) {
2021-05-29 19:13:52 +00:00
resampler = wf_resampler_init ( devChannels , info . preferredSampleRate , samplerate , resampleQuality , & resample_error ) ;
2021-05-23 15:09:41 +00:00
try {
2021-05-27 22:23:23 +00:00
audio . openStream ( NULL , & aParams , RTAUDIO_SINT16 , info . preferredSampleRate , & this - > chunkSize , & staticWrite , this ) ;
2021-05-23 15:09:41 +00:00
audio . startStream ( ) ;
}
catch ( RtAudioError & e ) {
qInfo ( logAudio ( ) ) < < " Error opening: " < < QString : : fromStdString ( e . getMessage ( ) ) ;
return false ;
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-05-29 19:13:52 +00:00
resampler = wf_resampler_init ( devChannels , samplerate , info . preferredSampleRate , resampleQuality , & resample_error ) ;
2021-05-23 15:09:41 +00:00
try {
2021-05-29 17:59:45 +00:00
audio . openStream ( & aParams , NULL , RTAUDIO_SINT16 , info . preferredSampleRate , & this - > chunkSize , & staticRead , this ) ;
2021-05-23 15:09:41 +00:00
audio . startStream ( ) ;
}
catch ( RtAudioError & e ) {
qInfo ( logAudio ( ) ) < < " Error opening: " < < QString : : fromStdString ( e . getMessage ( ) ) ;
return false ;
}
}
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " device successfully opened " ;
2021-05-16 20:16:59 +00:00
2021-05-29 17:59:45 +00:00
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " detected latency: " < < audio . getStreamLatency ( ) ;
2021-03-13 09:50:43 +00:00
wf_resampler_get_ratio ( resampler , & ratioNum , & ratioDen ) ;
2021-05-23 15:09:41 +00:00
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " wf_resampler_init() returned: " < < resample_error < < " ratioNum " < < ratioNum < < " ratioDen " < < ratioDen ;
2021-03-13 09:50:43 +00:00
2021-05-31 16:55:01 +00:00
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " thread id " < < QThread : : currentThreadId ( ) ;
2021-05-23 15:09:41 +00:00
return isInitialized ;
2021-02-11 19:18:35 +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-05-29 17:59:45 +00:00
this - > volume = ( qreal ) volume / 255.0 ;
qInfo ( logAudio ( ) ) < < ( 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-05-23 15:09:41 +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-23 15:09:41 +00:00
// Calculate output length, always full samples
2021-02-27 00:37:00 +00:00
int sentlen = 0 ;
2021-05-27 10:41:08 +00:00
quint8 * buffer = ( quint8 * ) outputBuffer ;
2021-05-24 08:27:18 +00:00
if ( status = = RTAUDIO_OUTPUT_UNDERFLOW )
qDebug ( logAudio ( ) ) < < " Underflow detected " ;
2021-02-27 00:37:00 +00:00
2021-05-29 19:13:52 +00:00
int nBytes = nFrames * devChannels * 2 ; // This is ALWAYS 2 bytes per sample and 2 channels
2021-05-23 21:45:10 +00:00
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
audioPacket packet ;
if ( ! ringBuf - > try_read ( packet ) )
{
qDebug ( ) < < " No more data available but buffer is not full! 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-05-27 17:46:01 +00:00
if ( currentLatency > ( int ) audioLatency ) {
2021-05-27 22:23:23 +00:00
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " Packet " < < hex < < packet . seq < <
" arrived too late (increase output latency!) " < <
dec < < packet . time . msecsTo ( QTime : : currentTime ( ) ) < < " ms " ;
lastSeq = packet . seq ;
if ( ! ringBuf - > try_read ( packet ) )
2021-05-29 17:59:45 +00:00
break ;
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
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-05-27 10:41:08 +00:00
if ( packet . seq < = lastSeq ) {
2021-05-27 22:23:23 +00:00
qDebug ( logAudio ( ) ) < < ( 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-05-27 22:23:23 +00:00
qDebug ( logAudio ( ) ) < < ( 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-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 ) ;
}
2021-05-23 15:09:41 +00:00
return 0 ;
}
2021-02-11 19:18:35 +00:00
2021-05-23 15:09:41 +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-05-23 15:09:41 +00:00
int sentlen = 0 ;
2021-05-29 19:13:52 +00:00
int nBytes = nFrames * devChannels * 2 ; // This is ALWAYS 2 bytes per sample
2021-05-27 12:54:52 +00:00
const char * data = ( const char * ) inputBuffer ;
2021-05-29 17:59:45 +00:00
//qDebug(logAudio()) << "nFrames" << nFrames << "nBytes" << nBytes;
2021-05-27 12:54:52 +00:00
while ( sentlen < nBytes ) {
2021-05-29 17:59:45 +00:00
if ( tempBuf . sent ! = nBytes )
2021-02-27 11:00:44 +00:00
{
2021-05-29 17:59:45 +00:00
int send = qMin ( ( int ) ( nBytes - sentlen ) , ( int ) nBytes - tempBuf . sent ) ;
2021-05-27 12:54:52 +00:00
tempBuf . data . append ( QByteArray : : fromRawData ( data + sentlen , send ) ) ;
sentlen = sentlen + send ;
tempBuf . seq = 0 ; // Not used in TX
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
{
2021-05-27 12:54:52 +00:00
qDebug ( logAudio ( ) ) < < " outgoing audio buffer full! " ;
2021-05-27 13:09:12 +00:00
break ;
2021-02-27 11:00:44 +00:00
}
2021-05-27 12:54:52 +00:00
tempBuf . data . clear ( ) ;
tempBuf . sent = 0 ;
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-05-27 12:54:52 +00:00
return 0 ;
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-05-23 21:45:10 +00:00
if ( ! audio . isStreamRunning ( ) )
{
2021-05-27 10:41:08 +00:00
qDebug ( logAudio ( ) ) < < " Packet received before stream was started " ;
2021-05-27 17:34:44 +00:00
return ;
2021-05-23 21:45:10 +00:00
}
2021-05-29 19:50:27 +00:00
//qDebug(logAudio()) << "Got" << radioSampleBits << "bits, length" << inPacket.data.length();
2021-05-27 10:41:08 +00:00
// Incoming data is 8bits?
if ( radioSampleBits = = 8 )
{
2021-05-29 17:59:45 +00:00
// Current packet is 8bit so need to create a new buffer that is 16bit
2021-05-29 19:13:52 +00:00
QByteArray outPacket ( ( int ) inPacket . data . length ( ) * 2 * ( devChannels / radioChannels ) , ( char ) 0xff ) ;
2021-05-27 10:41:08 +00:00
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
2021-05-29 17:59:45 +00:00
for ( int f = 0 ; f < inPacket . data . length ( ) ; f + + )
2021-03-09 17:22:16 +00:00
{
2021-05-29 19:13:52 +00:00
for ( int g = radioChannels ; g < = devChannels ; g + + )
2021-03-09 17:22:16 +00:00
{
2021-05-29 19:13:52 +00:00
if ( isUlaw )
2021-05-29 17:59:45 +00:00
* out + + = ulaw_decode [ ( quint8 ) inPacket . data [ f ] ] * this - > volume ;
else
* out + + = ( qint16 ) ( ( ( quint8 ) inPacket . data [ f ] < < 8 ) - 32640 * this - > volume ) ;
2021-03-09 17:22:16 +00:00
}
}
2021-05-29 17:59:45 +00:00
inPacket . data . clear ( ) ;
inPacket . 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-05-29 19:13:52 +00:00
if ( radioChannels = = 1 & & devChannels > 1 ) {
2021-05-29 17:59:45 +00:00
// Yes
QByteArray outPacket ( inPacket . data . length ( ) * 2 , ( char ) 0xff ) ; // Preset the output buffer size.
qint16 * in = ( qint16 * ) inPacket . data . data ( ) ;
qint16 * out = ( qint16 * ) outPacket . data ( ) ;
for ( int f = 0 ; f < inPacket . data . length ( ) / 2 ; f + + )
{
* out + + = ( qint16 ) * in * this - > volume ;
* out + + = ( qint16 ) * in + + * this - > volume ;
}
inPacket . data . clear ( ) ;
inPacket . data = outPacket ; // Replace incoming data with converted.
}
else
{
2021-05-29 19:13:52 +00:00
// We already have the same number of channels so just update volume.
2021-05-29 17:59:45 +00:00
qint16 * in = ( qint16 * ) inPacket . data . data ( ) ;
for ( int f = 0 ; f < inPacket . data . length ( ) / 2 ; f + + )
{
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 .
*/
2021-05-29 19:50:27 +00:00
//qDebug(logAudio()) << "Now 16 bit stereo, length" << inPacket.data.length();
2021-03-09 17:22:16 +00:00
2021-05-27 10:41:08 +00:00
if ( ratioDen ! = 1 ) {
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.
2021-05-29 19:13:52 +00:00
quint32 outFrames = ( ( inPacket . data . length ( ) / 2 / devChannels ) * ratioDen ) ;
quint32 inFrames = ( inPacket . data . length ( ) / 2 / devChannels ) ;
2021-05-29 17:59:45 +00:00
QByteArray outPacket ( outFrames * 4 , ( char ) 0xff ) ; // Preset the output buffer size.
const qint16 * in = ( qint16 * ) inPacket . data . constData ( ) ;
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 ) {
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " Resampler error " < < err < < " inFrames: " < < inFrames < < " outFrames: " < < outFrames ;
2021-03-09 17:22:16 +00:00
}
2021-05-29 17:59:45 +00:00
inPacket . data . clear ( ) ;
inPacket . data = outPacket ; // Replace incoming data with converted.
2021-05-27 10:41:08 +00:00
}
2021-05-29 19:50:27 +00:00
//qDebug(logAudio()) << "Adding packet to buffer:" << inPacket.seq << ": " << inPacket.data.length();
2021-05-27 12:54:52 +00:00
2021-05-29 17:59:45 +00:00
if ( ! ringBuf - > try_write ( inPacket ) )
2021-05-27 10:41:08 +00:00
{
qDebug ( logAudio ( ) ) < < " Buffer full! capacity: " < < ringBuf - > capacity ( ) < < " length " < < ringBuf - > size ( ) ;
}
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-05-23 15:09:41 +00:00
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " Changing latency to: " < < newSize < < " from " < < audioLatency ;
audioLatency = newSize ;
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-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-05-27 12:54:52 +00:00
if ( ringBuf ! = Q_NULLPTR & & ringBuf - > try_read ( packet ) )
2021-02-27 09:34:56 +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-05-27 12:54:52 +00:00
if ( ratioNum ! = 1 )
2021-02-27 09:34:56 +00:00
{
2021-05-29 19:13:52 +00:00
quint32 outFrames = ( ( packet . data . length ( ) / 2 / devChannels ) / ratioNum ) ;
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 ) {
qInfo ( logAudio ( ) ) < < ( isInput ? " Input " : " Output " ) < < " Resampler error " < < err < < " inFrames: " < < inFrames < < " outFrames: " < < outFrames ;
}
//qInfo(logAudio()) << "Resampler run " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
//qInfo(logAudio()) << "Resampler run inLen:" << packet->datain.length() << " outLen:" << packet->dataout.length();
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-05-29 19:13:52 +00:00
if ( radioChannels = = 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-05-27 12:54:52 +00:00
// Do we need to convert 16-bit to 8-bit?
if ( radioSampleBits = = 8 ) {
2021-05-29 17:59:45 +00:00
QByteArray outPacket ( ( int ) packet . data . length ( ) / 2 , ( char ) 0xff ) ;
2021-05-27 12:54:52 +00:00
qint16 * in = ( qint16 * ) packet . data . data ( ) ;
2021-05-29 17:59:45 +00:00
for ( int f = 0 ; f < outPacket . length ( ) ; f + + )
2021-05-27 12:54:52 +00:00
{
quint8 outdata = 0 ;
if ( isUlaw ) {
qint16 enc = qFromLittleEndian < quint16 > ( in + f ) ;
if ( enc > = 0 )
outdata = ulaw_encode [ enc ] ;
else
outdata = 0x7f & ulaw_encode [ - enc ] ;
2021-02-27 13:43:53 +00:00
}
else {
2021-05-27 12:54:52 +00:00
outdata = ( quint8 ) ( ( ( qFromLittleEndian < qint16 > ( in + f ) > > 8 ) ^ 0x80 ) & 0xff ) ;
2021-02-27 13:43:53 +00:00
}
2021-05-29 17:59:45 +00:00
outPacket [ f ] = ( char ) outdata ;
2021-02-27 13:43:53 +00:00
}
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-02-27 09:34:56 +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();
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-02-11 19:18:35 +00:00