diff --git a/Core/HW/MediaEngine.cpp b/Core/HW/MediaEngine.cpp index ebeb6e91d..ebd35bde9 100644 --- a/Core/HW/MediaEngine.cpp +++ b/Core/HW/MediaEngine.cpp @@ -281,7 +281,7 @@ bool MediaEngine::openContext() { return false; setVideoDim(); - m_audioContext = new SimpleAudio(m_audioType); + m_audioContext = new SimpleAudio(m_audioType, 44100, 2); m_isVideoEnd = false; m_mpegheaderReadPos++; av_seek_frame(m_pFormatCtx, m_videoStream, 0, 0); @@ -862,7 +862,13 @@ int MediaEngine::getAudioSamples(u32 bufferPtr) { } int outbytes = 0; - if (m_audioContext != NULL) { + if (m_audioContext != nullptr) { + if (headerCode1 == 0x24) { + // This means mono audio - tell the decoder to expect it before the first frame. + // Note that it will always send us back stereo audio. + m_audioContext->SetChannels(1); + } + if (!m_audioContext->Decode(audioFrame, frameSize, buffer, &outbytes)) { ERROR_LOG(ME, "Audio (%s) decode failed during video playback", GetCodecName(m_audioType)); } @@ -871,17 +877,6 @@ int MediaEngine::getAudioSamples(u32 bufferPtr) { #endif } - if (headerCode1 == 0x24) { - // it a mono atrac3plus, convert it to stereo - s16 *outbuf = (s16*)buffer; - s16 *inbuf = (s16*)buffer; - for (int i = 0x800 - 1; i >= 0; i--) { - s16 sample = inbuf[i]; - outbuf[i * 2] = sample; - outbuf[i * 2 + 1] = sample; - } - } - return 0x2000; } diff --git a/Core/HW/SimpleAudioDec.cpp b/Core/HW/SimpleAudioDec.cpp index f8074f0e9..356c83e60 100644 --- a/Core/HW/SimpleAudioDec.cpp +++ b/Core/HW/SimpleAudioDec.cpp @@ -129,6 +129,21 @@ void SimpleAudio::SetExtraData(u8 *data, int size, int wav_bytes_per_packet) { #endif } +void SimpleAudio::SetChannels(int channels) { + if (channels_ == channels) { + // Do nothing, already set. + return; + } + + if (codecOpen_) { + ERROR_LOG(ME, "Codec already open, cannot change channels"); + } else { + channels_ = channels; + codecCtx_->channels = channels_; + codecCtx_->channel_layout = channels_ == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO; + } +} + SimpleAudio::~SimpleAudio() { #ifdef USE_FFMPEG swr_free(&swrCtx_); diff --git a/Core/HW/SimpleAudioDec.h b/Core/HW/SimpleAudioDec.h index 03826ca0c..22ec180a8 100644 --- a/Core/HW/SimpleAudioDec.h +++ b/Core/HW/SimpleAudioDec.h @@ -56,6 +56,8 @@ public: // Not save stated, only used by UI. Used for ATRAC3 (non+) files. void SetExtraData(u8 *data, int size, int wav_bytes_per_packet); + void SetChannels(int channels); + // These two are only here because of save states. int GetAudioType() const { return audioType; } void SetResampleFrequency(int freq) { wanted_resample_freq = freq; }