HarmonyOS 音频通信场景下开启音频采集和播放,采集的音频中有播放的声音内容,回声消除不干净

1、通过HarmonyOSC-API分别开启音频采集设备和播放设备,其中OH_AudioStreamBuilder_SetRendererInfo设置的AUDIOSTREAM_USAGE_VOICE_COMMUNICATION,OH_AudioStreamBuilder_SetCapturerInfo设置的AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION。

音频采集参数:48khz,单通道,采样格式:AUDIOSTREAM_SAMPLE_S16LE。

音频播放参数:48khz,双通道,采样格式:AUDIOSTREAM_SAMPLE_S16LE。

音频播放设备:扬声器。(通过代码强制从听筒切换到扬声器)

2、通过以上方式开启音频输入输出设备,用于实时音频RTC组件,测试发现对端其他设备和HarmonyOS设备说话时,能听到回声。

3、需求是有双向音频通话时,使用扬声器而非听筒播放,并且系统要把回声消除干净。目前测试是没有消干净,不知道是不是这里使用方式不对,希望能给一个满足这个需求的能把回声消掉的demo参考一下。

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

1)OH_AudioStreamBuilder_SetLatencyMode(),低延时不支持回声消除。

2)参考代码如下:

#include "ohaudio_controller.h"
#include <cstddef>
#include <cstdint>
#include <ctime>
#define LOG_TAG "LatencyTest"
#include <ohaudio/native_audiostream_base.h>
#include <ohaudio/native_audiostreambuilder.h>
#include <ohaudio/native_audiorenderer.h>
#include <ohaudio/native_audiocapturer.h>
#include <hilog/log.h>
#include <mutex>
#include <sys/time.h>

static OH_AudioRenderer *audioRenderer = nullptr;
static OH_AudioCapturer *audioCapturer = nullptr;

#define CACHE_ARRAY_SIZE 10 * 1024 // 10k

class RingCache {
  private:
    uint8_t mem_[CACHE_ARRAY_SIZE];
  size_t readIndex_ = 0;
  size_t availableToRead_ = 0;
  size_t writeIndex_ = 0;
  size_t availableToWrite_ = CACHE_ARRAY_SIZE;
  std::mutex mutex_;
  int32_t appLatencyMs = 0;
  int64_t lastWriteMs;
  int64_t lastReadTs;
  int32_t sampleRate_ = 16000;
  int32_t channels_ = 2;
  int32_t bitWidth_ = 2;

  public:
    void SetSampleRate(int32_t sampleRate) { sampleRate_ = sampleRate; }
    void SetChannels(int32_t channels) { channels_ = channels; }
    void SetBitWidth(int32_t bitWidth) { bitWidth_ = bitWidth; }

int32_t GetLatencyMs() {
  return appLatencyMs;
  ;
}

int32_t Write(uint8_t *src, size_t contentSize) {
  std::lock_guard<std::mutex> guard(mutex_);
  OH_LOG_INFO(LOG_APP, "Write availableToWrite_ %{public}zu contentSize %{public}zu availableToRead_ %{public}zu",
    availableToWrite_, contentSize, availableToRead_);
  if (availableToWrite_ < contentSize) {
    OH_LOG_ERROR(LOG_APP, "exceeds, no available space to write availableToWrite_ %zu contentSize %zu",
      availableToWrite_, contentSize);
    return 0;
  }
  timeval tv;
  gettimeofday(&tv, nullptr);
  lastWriteMs = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  if (writeIndex_ + contentSize < CACHE_ARRAY_SIZE) {
    memcpy(mem_ + writeIndex_, src, contentSize);
    writeIndex_ += contentSize;
  } else {
    memcpy(mem_ + writeIndex_, src, CACHE_ARRAY_SIZE - writeIndex_);
    memcpy(mem_, src + CACHE_ARRAY_SIZE - writeIndex_, writeIndex_ + contentSize - CACHE_ARRAY_SIZE);
    writeIndex_ = (writeIndex_ + contentSize) % CACHE_ARRAY_SIZE;
  }
  availableToWrite_ -= contentSize;
  availableToRead_ += contentSize;
  return contentSize;
}

int32_t Read(uint8_t *dest, size_t requestSize) {
  OH_LOG_INFO(LOG_APP, "Read availableToWrite_ %{public}zu requestSize %{public}zu availableToRead_ %{public}zu",
    availableToWrite_, requestSize, availableToRead_);
  std::lock_guard<std::mutex> guard(mutex_);
  if (availableToRead_ < requestSize) {
    OH_LOG_ERROR(LOG_APP, "exceeds, no available data to read");
    return 0;
  }

  timeval tv;
  gettimeofday(&tv, nullptr);
  int64_t offset = tv.tv_sec * 1000 + tv.tv_usec / 1000 - lastWriteMs;
  appLatencyMs = offset + (availableToRead_ - requestSize) * 1000 / sampleRate_ / channels_ / bitWidth_;
  if (readIndex_ + requestSize < CACHE_ARRAY_SIZE) {
    memcpy(dest, mem_ + readIndex_, requestSize);
    readIndex_ += requestSize;
  } else {
    memcpy(dest, mem_ + readIndex_, CACHE_ARRAY_SIZE - readIndex_);
    memcpy(dest + CACHE_ARRAY_SIZE - readIndex_, mem_, requestSize - (CACHE_ARRAY_SIZE - readIndex_));
    readIndex_ = (readIndex_ + requestSize) % CACHE_ARRAY_SIZE;
  }
  availableToWrite_ += requestSize;
  availableToRead_ -= requestSize;
  return requestSize;
}
};

static RingCache rc;

static int32_t CaptureCallabck(OH_AudioCapturer *capturer, void *userData, void *buffer, int32_t lenth) {
  rc.Write(static_cast<uint8_t *>(buffer), lenth);
  return 0;
}

static int32_t CapturerOnInterrupt(OH_AudioCapturer *capturer, void *userData, OH_AudioInterrupt_ForceType type,
OH_AudioInterrupt_Hint hint) {
  OH_LOG_INFO(LOG_APP, "%s OH_AudioInterrupt_ForceType %d, OH_AudioInterrupt_Hint %d", __FUNCTION__, type, hint);
  return 0;
}
static int32_t CaptureOnError(OH_AudioCapturer *capturer, void *userData, OH_AudioStream_Result error) {
  OH_LOG_ERROR(LOG_APP, "capture error %d", error);
  return 0;
}

static OH_AudioCapturer_Callbacks captureCallback = {
  .OH_AudioCapturer_OnReadData = CaptureCallabck,
  .OH_AudioCapturer_OnStreamEvent = nullptr, // 不使用时,必须置空
  .OH_AudioCapturer_OnInterruptEvent = CapturerOnInterrupt,
  .OH_AudioCapturer_OnError = CaptureOnError,
};

static bool CreateAudioCapturer(int32_t sampleRate, int32_t mode) {
  OH_LOG_INFO(LOG_APP, "create audio capture with samplerate %d, mode: %s", sampleRate,
    mode == 0 ? "VoIP" : "Normal");
  OH_AudioStreamBuilder *builder;
  OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_CAPTURER);
  OH_AudioStreamBuilder_SetSamplingRate(builder, sampleRate);
  OH_AudioStreamBuilder_SetSampleFormat(builder, AUDIOSTREAM_SAMPLE_S16LE);
  OH_AudioStreamBuilder_SetChannelCount(builder, 2);
  if (mode == 0) {
    OH_AudioStreamBuilder_SetCapturerInfo(builder, AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION);
  } else {
    OH_AudioStreamBuilder_SetCapturerInfo(builder, AUDIOSTREAM_SOURCE_TYPE_MIC);
  }
  OH_AudioStreamBuilder_SetCapturerCallback(builder, captureCallback, nullptr);
  OH_AudioStream_Result res = AUDIOSTREAM_SUCCESS;
  if ((res = OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer)) != AUDIOSTREAM_SUCCESS) {
    audioCapturer = nullptr;
    OH_LOG_ERROR(LOG_APP, "create audio capture failed %d", res);
    return false;
  }
  return true;
}

static int32_t RenderCallback(OH_AudioRenderer *renderer, void *userData, void *buffer, int32_t lenth) {
  rc.Read(static_cast<uint8_t *>(buffer), lenth);
  return 0;
}

static int32_t RenderOnError(OH_AudioRenderer *render, void *userData, OH_AudioStream_Result error) {
  OH_LOG_ERROR(LOG_APP, "render error %d", error);
  return 0;
}

static OH_AudioRenderer_Callbacks renderCallback = {
  .OH_AudioRenderer_OnWriteData = RenderCallback,
  .OH_AudioRenderer_OnStreamEvent = nullptr,    // 不使用时,必须置空
  .OH_AudioRenderer_OnInterruptEvent = nullptr, // 不使用时,必须置空
  .OH_AudioRenderer_OnError = RenderOnError,
};

static OH_AudioRenderer_Callbacks renderCallback5;

static bool CreateAudioRenderer(int32_t sampleRate, int32_t mode) {
  OH_LOG_INFO(LOG_APP, "create audio render with samplerate %d, mode: %s", sampleRate, mode == 0 ? "VoIP" : "Normal");
  OH_AudioStreamBuilder *builder;
  OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER);
  OH_AudioStreamBuilder_SetSamplingRate(builder, sampleRate);
  if (mode == 0) {
    OH_AudioStreamBuilder_SetRendererInfo(builder, AUDIOSTREAM_USAGE_VOICE_COMMUNICATION);
  } else {
    OH_AudioStreamBuilder_SetRendererInfo(builder, AUDIOSTREAM_USAGE_MOVIE);
  }
  OH_AudioStreamBuilder_SetSampleFormat(builder, AUDIOSTREAM_SAMPLE_S16LE);
  OH_AudioStreamBuilder_SetChannelCount(builder, 2);
  OH_AudioStreamBuilder_SetFrameSizeInCallback(builder, 20 * sampleRate / 1000);
  OH_AudioStreamBuilder_SetRendererCallback(builder, renderCallback, nullptr);
  OH_AudioStream_Result res = AUDIOSTREAM_SUCCESS;
  if ((res = OH_AudioStreamBuilder_GenerateRenderer(builder, &audioRenderer)) != AUDIOSTREAM_SUCCESS) {
    audioRenderer = nullptr;
    OH_LOG_ERROR(LOG_APP, "create audio render failed %d", res);
    return false;
  }
  return true;
}

void Start(int32_t sampleRate, int32_t mode) {
  rc.SetSampleRate(sampleRate);
  Stop();
  if (audioRenderer == nullptr) {
    CreateAudioRenderer(sampleRate, mode);
  }
  if (audioCapturer == nullptr) {
    CreateAudioCapturer(sampleRate, mode);
  }
  if (audioRenderer != nullptr) {
    OH_AudioRenderer_Start(audioRenderer);
  }
  if (audioCapturer != nullptr) {
    OH_AudioCapturer_Start(audioCapturer);
  }
}

void Stop() {
  if (audioRenderer != nullptr) {
    OH_AudioRenderer_Stop(audioRenderer);
    OH_AudioRenderer_Release(audioRenderer);
    audioRenderer = nullptr;
  }
  if (audioCapturer != nullptr) {
    OH_AudioCapturer_Stop(audioCapturer);
    OH_AudioCapturer_Release(audioCapturer);
    audioCapturer = nullptr;
  }
}

int32_t GetAppLatency() { return rc.GetLatencyMs(); }
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 音频播放相关
97浏览 • 1回复 待解决
HarmonyOS音频播放问题
530浏览 • 1回复 待解决
HarmonyOS 播放音频示例
106浏览 • 1回复 待解决
HarmonyOS 希望提供napi版音频采集demo
352浏览 • 1回复 待解决
HarmonyOS 音频录制与播放
816浏览 • 1回复 待解决
HarmonyOS 音频播放帧率控制
319浏览 • 1回复 待解决
SoundPool实现音频播放功能
1526浏览 • 1回复 待解决
HarmonyOS 音频播放设备切换
478浏览 • 1回复 待解决
HarmonyOS 音频播放相关问题咨询
327浏览 • 1回复 待解决
HarmonyOS 如何播放rtmp协议音频
15浏览 • 1回复 待解决
AVplayer开发音频播放功能
1331浏览 • 1回复 待解决