From 57e6d49152b79c386bc69c6df126c65afd0368a0 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 21 May 2025 16:05:48 +0800 Subject: [PATCH 1/4] fix multichannel fade & toneplayer problem Signed-off-by: Robin --- .../audiorenderer/src/audio_renderer.cpp | 4 +-- .../native/toneplayer/src/toneplayer_impl.cpp | 3 +- .../audio_engine/buffer/hpae_pcm_buffer.cpp | 4 +++ .../audio_engine/buffer/hpae_pcm_buffer.h | 6 ++++ .../audio_engine/buffer/hpae_pcm_process.h | 2 +- .../manager/include/hpae_manager.h | 2 +- .../node/include/hpae_gain_node.h | 1 + .../audio_engine/node/src/hpae_gain_node.cpp | 34 +++++++++++-------- .../node/src/hpae_sink_input_node.cpp | 5 +-- .../channel_converter/src/down_mixer.cpp | 4 +++ 10 files changed, 44 insertions(+), 21 deletions(-) diff --git a/frameworks/native/audiorenderer/src/audio_renderer.cpp b/frameworks/native/audiorenderer/src/audio_renderer.cpp index 25a27e2646..f7646f4747 100644 --- a/frameworks/native/audiorenderer/src/audio_renderer.cpp +++ b/frameworks/native/audiorenderer/src/audio_renderer.cpp @@ -50,7 +50,7 @@ const std::vector BACKGROUND_NOSTART_STREAM_USAGE { static constexpr uid_t UID_MSDP_SA = 6699; static constexpr int32_t WRITE_UNDERRUN_NUM = 100; static constexpr int32_t MINIMUM_BUFFER_SIZE_MSEC = 5; -static constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 20; +static constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 60; constexpr int32_t TIME_OUT_SECONDS = 10; constexpr int32_t START_TIME_OUT_SECONDS = 15; static constexpr uint32_t BLOCK_INTERRUPT_CALLBACK_IN_MS = 300; // 300ms @@ -1309,7 +1309,7 @@ uint32_t AudioRendererPrivate::GetRendererSamplingRate() const int32_t AudioRendererPrivate::SetBufferDuration(uint64_t bufferDuration) const { CHECK_AND_RETURN_RET_LOG(bufferDuration >= MINIMUM_BUFFER_SIZE_MSEC && bufferDuration <= MAXIMUM_BUFFER_SIZE_MSEC, - ERR_INVALID_PARAM, "Error: Please set the buffer duration between 5ms ~ 20ms"); + ERR_INVALID_PARAM, "Error: Please set the buffer duration between 5ms ~ 60ms"); std::shared_ptr currentStream = GetInnerStream(); CHECK_AND_RETURN_RET_LOG(currentStream != nullptr, ERROR_ILLEGAL_STATE, "audioStream_ is nullptr"); return currentStream->SetBufferSizeInMsec(bufferDuration); diff --git a/frameworks/native/toneplayer/src/toneplayer_impl.cpp b/frameworks/native/toneplayer/src/toneplayer_impl.cpp index 90a6039d7d..d58c593457 100644 --- a/frameworks/native/toneplayer/src/toneplayer_impl.cpp +++ b/frameworks/native/toneplayer/src/toneplayer_impl.cpp @@ -40,6 +40,7 @@ namespace OHOS { namespace AudioStandard { namespace { constexpr int32_t C20MS = 20; +constexpr int32_t C60MS = 60; constexpr int32_t C1000MS = 1000; constexpr int32_t CDOUBLE = 2; constexpr int32_t DIGITAMPLITUDE = 800; @@ -487,7 +488,7 @@ bool TonePlayerImpl::InitAudioRenderer() AUDIO_DEBUG_LOG("SetRenderMode Sucessful"); if (ret == 0 && targetSize != 0) { - size_t bufferDuration = 20; // 20 -> 20ms + uint64_t bufferDuration = GetEngineFlag() == 1 ? C60MS : C20MS; // 20 -> 20ms audioRenderer_->SetBufferDuration(bufferDuration); AUDIO_INFO_LOG("Init renderer with buffer %{public}zu, duration %{public}zu", targetSize, bufferDuration); } diff --git a/services/audio_engine/buffer/hpae_pcm_buffer.cpp b/services/audio_engine/buffer/hpae_pcm_buffer.cpp index 7f0fd9623f..132dfc2229 100644 --- a/services/audio_engine/buffer/hpae_pcm_buffer.cpp +++ b/services/audio_engine/buffer/hpae_pcm_buffer.cpp @@ -48,11 +48,13 @@ HpaePcmBuffer::HpaePcmBuffer(HpaePcmBuffer &&other) pcmBufferInfo_ = other.pcmBufferInfo_; bufferByteSize_ = other.bufferByteSize_; bufferFloatSize_ = other.bufferFloatSize_; + int32_t errNo_ dataByteSize_ = other.dataByteSize_; pcmDataBuffer_ = std::move(other.pcmDataBuffer_); pcmProcessVec_ = std::move(other.pcmProcessVec_); other.pcmBufferInfo_.frames = 0; other.bufferByteSize_ = 0; other.bufferFloatSize_ = 0; + other.dataByteSize_ = 0; } void HpaePcmBuffer::InitPcmProcess() @@ -61,10 +63,12 @@ void HpaePcmBuffer::InitPcmProcess() size_t frameLen = GetFrameLen(); size_t frames = GetFrames(); size_t addBytes = MEMORY_ALIGN_BYTE_NUM - (frameLen * sizeof(float) * ch) % MEMORY_ALIGN_BYTE_NUM; + size_t dataSize = frameLen * sizeof(float) * ch; frameByteSize_ = frameLen * sizeof(float) * ch + addBytes; frameFloatSize_ = frameByteSize_ / sizeof(float); bufferByteSize_ = frameByteSize_ * frames; bufferFloatSize_ = frameFloatSize_ * frames; + dataByteSize_ = dataSize * frames; frameSample_ = frameLen * ch; pcmDataBuffer_.resize(bufferFloatSize_); readPos_.store(0); diff --git a/services/audio_engine/buffer/hpae_pcm_buffer.h b/services/audio_engine/buffer/hpae_pcm_buffer.h index 68e09ddc2f..19eef8173c 100644 --- a/services/audio_engine/buffer/hpae_pcm_buffer.h +++ b/services/audio_engine/buffer/hpae_pcm_buffer.h @@ -158,6 +158,11 @@ public: return bufferByteSize_; } + size_t DataSize() const + { + return dataByteSize_; + } + size_t GetFrames() const { return pcmBufferInfo_.frames; @@ -246,6 +251,7 @@ private: size_t frameFloatSize_; size_t frameByteSize_; size_t frameSample_; + size_t dataByteSize_; std::atomic readPos_; std::atomic writePos_; std::atomic curFrames_; diff --git a/services/audio_engine/buffer/hpae_pcm_process.h b/services/audio_engine/buffer/hpae_pcm_process.h index 04e758fe0a..651756c160 100644 --- a/services/audio_engine/buffer/hpae_pcm_process.h +++ b/services/audio_engine/buffer/hpae_pcm_process.h @@ -81,7 +81,7 @@ public: } private: - int32_t errNo_; + int32_t errNo_ = 0; float* const pcmDataPtr_; const size_t size_; }; diff --git a/services/audio_engine/manager/include/hpae_manager.h b/services/audio_engine/manager/include/hpae_manager.h index a9db55024a..d5a8627565 100644 --- a/services/audio_engine/manager/include/hpae_manager.h +++ b/services/audio_engine/manager/include/hpae_manager.h @@ -52,7 +52,7 @@ public: private: std::atomic running_; std::atomic recvSignal_; - HpaeManager *m_hpaeManager; + HpaeManager *m_hpaeManager = nullptr; std::condition_variable condition_; std::mutex mutex_; std::thread thread_; diff --git a/services/audio_engine/node/include/hpae_gain_node.h b/services/audio_engine/node/include/hpae_gain_node.h index 1d2eb0ff62..f8b8900173 100644 --- a/services/audio_engine/node/include/hpae_gain_node.h +++ b/services/audio_engine/node/include/hpae_gain_node.h @@ -51,6 +51,7 @@ private: void DoFading(HpaePcmBuffer *input); void SlienceData(HpaePcmBuffer *pcmBuffer); bool IsSilentData(HpaePcmBuffer *pcmBuffer); + void GetFadeLength(uint32_t &byteLength, HpaePcmBuffer *input); #ifdef ENABLE_HOOK_PCM std::unique_ptr inputPcmDumper_; std::unique_ptr outputPcmDumper_; diff --git a/services/audio_engine/node/src/hpae_gain_node.cpp b/services/audio_engine/node/src/hpae_gain_node.cpp index c92816b771..e0cc37028b 100644 --- a/services/audio_engine/node/src/hpae_gain_node.cpp +++ b/services/audio_engine/node/src/hpae_gain_node.cpp @@ -157,20 +157,7 @@ void HpaeGainNode::DoFading(HpaePcmBuffer *input) rawFormat.channels = GetChannelCount(); uint32_t byteLength = 0; uint8_t *data = (uint8_t *)input->GetPcmDataBuffer(); - switch (GetNodeInfo().fadeType) { - case FadeType::SHORT_FADE: { - byteLength = static_cast(GetSampleRate()) * SHORT_FADE_PERIOD * rawFormat.channels * sizeof(float); - AUDIO_DEBUG_LOG("GainNode: short fade length in Bytes: %{public}u", byteLength); - break; - } - case FadeType::DEFAULT_FADE: { - byteLength = input->Size(); - AUDIO_DEBUG_LOG("GainNode: default fade length in Bytes: %{public}u", byteLength); - break; - } - default: - break; - } + GetFadeLength(byteLength, input); if (fadeInState_) { CHECK_AND_RETURN_LOG(input->IsValid(), "GainNode: invalid data no need to do fade in"); if (IsSilentData(input)) { @@ -251,6 +238,25 @@ bool HpaeGainNode::IsSilentData(HpaePcmBuffer *pcmBuffer) return fabs(value) < EPSILON; }); } + +void HpaeGainNode::GetFadeLength(uint32_t &byteLength, HpaePcmBuffer *input) +{ + uint32_t channels = GetChannelCount(); + switch (GetNodeInfo().fadeType) { + case FadeType::SHORT_FADE: { + byteLength = static_cast(GetSampleRate()) * SHORT_FADE_PERIOD * channels * sizeof(float); + AUDIO_DEBUG_LOG("GainNode: short fade length in Bytes: %{public}u", byteLength); + break; + } + case FadeType::DEFAULT_FADE: { + byteLength = input->DataSize(); + AUDIO_DEBUG_LOG("GainNode: default fade length in Bytes: %{public}u", byteLength); + break; + } + default: + break; + } +} } // namespace HPAE } // namespace AudioStandard } // namespace OHOS \ No newline at end of file diff --git a/services/audio_engine/node/src/hpae_sink_input_node.cpp b/services/audio_engine/node/src/hpae_sink_input_node.cpp index 5df5edc430..389cb4900c 100644 --- a/services/audio_engine/node/src/hpae_sink_input_node.cpp +++ b/services/audio_engine/node/src/hpae_sink_input_node.cpp @@ -223,8 +223,9 @@ bool HpaeSinkInputNode::GetAudioTime(uint64_t &framePos, int64_t &sec, int64_t & int64_t time = handleTimeModel_->GetTimeOfPos(framePos); int64_t deltaTime = DEFAULT_BUFFER_MICROSECOND; // note: 20ms time += deltaTime; - sec = time / AUDIO_NS_PER_S; - nanoSec = time % AUDIO_NS_PER_S; + CHECK_AND_RETURN_LOG(time >= 0, false, "get time error"); + sec = static_cast(time) / AUDIO_NS_PER_S; + nanoSec = static_cast(time) % AUDIO_NS_PER_S; return true; } diff --git a/services/audio_engine/plugin/channel_converter/src/down_mixer.cpp b/services/audio_engine/plugin/channel_converter/src/down_mixer.cpp index a82bf22292..4ea6c9ec8a 100644 --- a/services/audio_engine/plugin/channel_converter/src/down_mixer.cpp +++ b/services/audio_engine/plugin/channel_converter/src/down_mixer.cpp @@ -676,6 +676,8 @@ void DownMixer::Setup5Point1Point4DmixTablePart1(uint64_t bit, uint32_t i) downMixTable_[SW][i] = COEF_0DB_F; } break; + default: + break; } } @@ -994,6 +996,8 @@ void DownMixer::DownMixMidRear(uint64_t inBit, uint64_t outBit, uint32_t i, uint DownMixMidFront(WIDE_RIGHT, outBit, i, j); } break; + default: + break; } } -- Gitee From 6cead501e127167eb5df04e97646a2f9f2592c1a Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 21 May 2025 16:13:03 +0800 Subject: [PATCH 2/4] fix code error Signed-off-by: Robin --- services/audio_engine/buffer/hpae_pcm_buffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/audio_engine/buffer/hpae_pcm_buffer.cpp b/services/audio_engine/buffer/hpae_pcm_buffer.cpp index 132dfc2229..9140c44289 100644 --- a/services/audio_engine/buffer/hpae_pcm_buffer.cpp +++ b/services/audio_engine/buffer/hpae_pcm_buffer.cpp @@ -48,7 +48,7 @@ HpaePcmBuffer::HpaePcmBuffer(HpaePcmBuffer &&other) pcmBufferInfo_ = other.pcmBufferInfo_; bufferByteSize_ = other.bufferByteSize_; bufferFloatSize_ = other.bufferFloatSize_; - int32_t errNo_ dataByteSize_ = other.dataByteSize_; + dataByteSize_ = other.dataByteSize_; pcmDataBuffer_ = std::move(other.pcmDataBuffer_); pcmProcessVec_ = std::move(other.pcmProcessVec_); other.pcmBufferInfo_.frames = 0; -- Gitee From 1a132de2060eefbd5f84e8dd471771b41a7649a2 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 21 May 2025 16:50:54 +0800 Subject: [PATCH 3/4] fix code error2 Signed-off-by: Robin --- frameworks/native/toneplayer/src/toneplayer_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/toneplayer/src/toneplayer_impl.cpp b/frameworks/native/toneplayer/src/toneplayer_impl.cpp index d58c593457..e9ac716f1b 100644 --- a/frameworks/native/toneplayer/src/toneplayer_impl.cpp +++ b/frameworks/native/toneplayer/src/toneplayer_impl.cpp @@ -488,7 +488,7 @@ bool TonePlayerImpl::InitAudioRenderer() AUDIO_DEBUG_LOG("SetRenderMode Sucessful"); if (ret == 0 && targetSize != 0) { - uint64_t bufferDuration = GetEngineFlag() == 1 ? C60MS : C20MS; // 20 -> 20ms + size_t bufferDuration = GetEngineFlag() == 1 ? C60MS : C20MS; // 20 -> 20ms audioRenderer_->SetBufferDuration(bufferDuration); AUDIO_INFO_LOG("Init renderer with buffer %{public}zu, duration %{public}zu", targetSize, bufferDuration); } -- Gitee From 1228345b736b64ca0d3fe34207ffb4ff340c55b1 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 21 May 2025 16:54:29 +0800 Subject: [PATCH 4/4] fix code error3 Signed-off-by: Robin --- services/audio_engine/node/src/hpae_sink_input_node.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/audio_engine/node/src/hpae_sink_input_node.cpp b/services/audio_engine/node/src/hpae_sink_input_node.cpp index 389cb4900c..725f93180b 100644 --- a/services/audio_engine/node/src/hpae_sink_input_node.cpp +++ b/services/audio_engine/node/src/hpae_sink_input_node.cpp @@ -223,7 +223,7 @@ bool HpaeSinkInputNode::GetAudioTime(uint64_t &framePos, int64_t &sec, int64_t & int64_t time = handleTimeModel_->GetTimeOfPos(framePos); int64_t deltaTime = DEFAULT_BUFFER_MICROSECOND; // note: 20ms time += deltaTime; - CHECK_AND_RETURN_LOG(time >= 0, false, "get time error"); + CHECK_AND_RETURN_RET_LOG(time >= 0, false, "get time error"); sec = static_cast(time) / AUDIO_NS_PER_S; nanoSec = static_cast(time) % AUDIO_NS_PER_S; return true; -- Gitee