@ -5,6 +5,8 @@ extern "C" {
# include "../../../lib/opus_codec.h"
# include "../../../lib/opus_codec.h"
}
}
# include "../../lib/debug_config.h"
# include <QFile>
# include <QFile>
# include <QDebug>
# include <QDebug>
@ -14,14 +16,14 @@ int VoicePlayer::decodeOpusFile(const QString& filePath, std::vector<int16_t>& o
int & outSampleRate , int & outChannels , float & outDurationSec ) {
int & outSampleRate , int & outChannels , float & outDurationSec ) {
QFile file ( filePath ) ;
QFile file ( filePath ) ;
if ( ! file . exists ( ) ) {
if ( ! file . exists ( ) ) {
qWarning ( " VoicePlayer: file not found: %s " , qPrintable ( filePath ) ) ;
DEBUG_WARN ( DEBUG_CATEGORY_DEBUG , " VoicePlayer: file not found: %s " , qPrintable ( filePath ) ) ;
return - 1 ;
return - 1 ;
}
}
if ( ! file . open ( QIODevice : : ReadOnly ) ) {
if ( ! file . open ( QIODevice : : ReadOnly ) ) {
qWarning ( " VoicePlayer: cannot open %s (%s) " , qPrintable ( filePath ) , qPrintable ( file . errorString ( ) ) ) ;
DEBUG_WARN ( DEBUG_CATEGORY_DEBUG , " VoicePlayer: cannot open %s (%s) " , qPrintable ( filePath ) , qPrintable ( file . errorString ( ) ) ) ;
return - 1 ;
return - 1 ;
}
}
qDebug ( " VoicePlayer: opened file %s size=%lld " , qPrintable ( filePath ) , ( long long ) file . size ( ) ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " VoicePlayer: opened file %s size=%lld " , qPrintable ( filePath ) , ( long long ) file . size ( ) ) ;
uint32_t magic , sr32 ;
uint32_t magic , sr32 ;
uint16_t ch16 , frameSamples ;
uint16_t ch16 , frameSamples ;
@ -29,7 +31,7 @@ int VoicePlayer::decodeOpusFile(const QString& filePath, std::vector<int16_t>& o
file . read ( ( char * ) & sr32 , 4 ) ! = 4 | |
file . read ( ( char * ) & sr32 , 4 ) ! = 4 | |
file . read ( ( char * ) & ch16 , 2 ) ! = 2 | |
file . read ( ( char * ) & ch16 , 2 ) ! = 2 | |
file . read ( ( char * ) & frameSamples , 2 ) ! = 2 ) {
file . read ( ( char * ) & frameSamples , 2 ) ! = 2 ) {
qWarning ( " VoicePlayer: invalid header in %s " , qPrintable ( filePath ) ) ;
DEBUG_WARN ( DEBUG_CATEGORY_DEBUG , " VoicePlayer: invalid header in %s " , qPrintable ( filePath ) ) ;
return - 1 ;
return - 1 ;
}
}
@ -40,7 +42,7 @@ int VoicePlayer::decodeOpusFile(const QString& filePath, std::vector<int16_t>& o
opus_codec_decoder_t * dec = opus_codec_decoder_create ( sampleRate , channels ) ;
opus_codec_decoder_t * dec = opus_codec_decoder_create ( sampleRate , channels ) ;
if ( ! dec ) {
if ( ! dec ) {
qWarning ( " VoicePlayer: decoder create failed " ) ;
DEBUG_WARN ( DEBUG_CATEGORY_DEBUG , " VoicePlayer: decoder create failed " ) ;
return - 1 ;
return - 1 ;
}
}
@ -67,23 +69,23 @@ int VoicePlayer::decodeOpusFile(const QString& filePath, std::vector<int16_t>& o
}
}
outDurationSec = ( float ) frameCount * 20.0f / 1000.0f ;
outDurationSec = ( float ) frameCount * 20.0f / 1000.0f ;
qDebug ( " VoicePlayer: decoded %d frames (%.1fs) from %s " , frameCount , outDurationSec , qPrintable ( filePath ) ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " VoicePlayer: decoded %d frames (%.1fs) from %s " , frameCount , outDurationSec , qPrintable ( filePath ) ) ;
opus_codec_decoder_destroy ( dec ) ;
opus_codec_decoder_destroy ( dec ) ;
return frameCount ;
return frameCount ;
}
}
void VoicePlayer : : playOpusFile ( const QString & filePath ) {
void VoicePlayer : : playOpusFile ( const QString & filePath ) {
qDebug ( " VoicePlayer::playOpusFile: %s " , qPrintable ( filePath ) ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " VoicePlayer::playOpusFile: %s " , qPrintable ( filePath ) ) ;
std : : vector < int16_t > pcm ;
std : : vector < int16_t > pcm ;
int sampleRate , channels ;
int sampleRate , channels ;
float duration ;
float duration ;
if ( decodeOpusFile ( filePath , pcm , sampleRate , channels , duration ) < = 0 ) {
if ( decodeOpusFile ( filePath , pcm , sampleRate , channels , duration ) < = 0 ) {
qWarning ( " VoicePlayer::playOpusFile: decode failed for %s " , qPrintable ( filePath ) ) ;
DEBUG_WARN ( DEBUG_CATEGORY_DEBUG , " VoicePlayer::playOpusFile: decode failed for %s " , qPrintable ( filePath ) ) ;
return ;
return ;
}
}
qDebug ( " VoicePlayer::playOpusFile: decoded %zu samples %dHz %dch %.1fs → playing " ,
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " VoicePlayer::playOpusFile: decoded %zu samples %dHz %dch %.1fs " ,
pcm . size ( ) , sampleRate , channels , duration ) ;
pcm . size ( ) , sampleRate , channels , duration ) ;
SoundManager : : instance ( ) - > playRawPcm ( pcm . data ( ) , pcm . size ( ) / channels , sampleRate , channels ) ;
SoundManager : : instance ( ) - > playRawPcm ( pcm . data ( ) , pcm . size ( ) / channels , sampleRate , channels ) ;
}
}