Unreal的Audio::IAudioCaptureStream在Android中录制数据异常

server/2024/12/16 18:53:00/

修改OpenAudioCaptureStream启动参数为PCM_32,在PC上正常,在Android系统,读取的的数据计算出的音量值在0.4-0.6之间跳动,数据异常。

    Audio::FAudioCaptureDeviceParams Params;/** 设置声卡不支持的采样数和通道数开始音频流不会成功,这里不能修改* Params.NumInputChannels = 1;* Params.SampleRate = 16000;* * 可以修改PCM数据格式,默认是32位浮点数FLOATING_POINT_32* 我这里修改为32位整数PCM_32*/Params.PCMAudioEncoding = Audio::EPCMAudioEncoding::PCM_32;// 使用 TFunction 包装成员函数Audio::FOnAudioCaptureFunction OnCapture = [this](const void* InAudio, int32 NumFrames, int32 NumChannels, int32 SampleRate, double StreamTime, bool bOverFlow){this->OnAudioCapture(InAudio, NumFrames, NumChannels, SampleRate, StreamTime, bOverFlow);};bool r = AudioCapture->OpenAudioCaptureStream(Params, MoveTemp(OnCapture), 4800);

修改为FLOATING_POINT_32,按照float值读取数据则是正常的。

    Audio::FAudioCaptureDeviceParams Params;/** 设置声卡不支持的采样数和通道数开始音频流不会成功,这里不能修改* Params.NumInputChannels = 1;* Params.SampleRate = 16000;* * 可以修改PCM数据格式,默认是32位浮点数FLOATING_POINT_32* 修改为32位整数PCM_32,在Android系统有问题,还是修改为FLOATING_POINT_32*/Params.PCMAudioEncoding = Audio::EPCMAudioEncoding::FLOATING_POINT_32;// 使用 TFunction 包装成员函数Audio::FOnAudioCaptureFunction OnCapture = [this](const void* InAudio, int32 NumFrames, int32 NumChannels, int32 SampleRate, double StreamTime, bool bOverFlow){this->OnAudioCapture(InAudio, NumFrames, NumChannels, SampleRate, StreamTime, bOverFlow);};bool r = AudioCapture->OpenAudioCaptureStream(Params, MoveTemp(OnCapture), 1920); // 48000采样率可以在重采样是整除3

全部代码
FxAudioCaptureComponent.h

#pragma once#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "AudioCaptureDeviceInterface.h"
#include "HAL/ThreadSafeCounter.h"
#include "HAL/Thread.h"
#include "FxAudioCaptureComponent.generated.h"UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class UFxAudioCaptureComponent : public UActorComponent
{GENERATED_BODY()public:UFxAudioCaptureComponent();UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "XML")int ID;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "XML")float Intensity;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "XML")bool bMobile;protected:virtual void BeginPlay() override;virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);// Called every framevirtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;TArray<float> ResampleAndConvert16KHzMono16Bit(const float* inputData, int inputNumSamples, int inputChannels, int inputSampleRate);public:UFUNCTION(BlueprintPure, Category = "FxAudioCapture")int GetAudioDataSize();UFUNCTION(BlueprintCallable, Category = "FxAudioCapture")TArray<uint8> CopyAudioData(int Length, bool bRemove = true);UFUNCTION(BlueprintCallable, Category = "FxAudioCapture")bool StartRecord(float seconds = 10.0f);UFUNCTION(BlueprintCallable, Category = "FxAudioCapture")void StopRecord();UFUNCTION(BlueprintPure, Category = "FxAudioCapture")bool IsRecording();private:FCriticalSection Mutex;TArray<float> m_audioData;bool bRecording;float RecordSeconds;TUniquePtr<Audio::IAudioCaptureStream> AudioCapture;void OnAudioCapture(const void* InAudio, int32 NumFrames, int32 NumChannels, int32 SampleRate, double StreamTime, bool bOverFlow);
};

FxAudioCaptureComponent.cpp

#include "FxAudioCaptureComponent.h"
#include "AudioCaptureDeviceInterface.h"
#include "AudioCaptureCore.h"
#include "AudioMixer.h"UFxAudioCaptureComponent::UFxAudioCaptureComponent(): bRecording(false)
{PrimaryComponentTick.bCanEverTick = true;
}void UFxAudioCaptureComponent::BeginPlay()
{Super::BeginPlay();IModularFeatures::Get().LockModularFeatureList();TArray<Audio::IAudioCaptureFactory*> AudioCaptureStreamFactories = IModularFeatures::Get().GetModularFeatureImplementations<Audio::IAudioCaptureFactory>(Audio::IAudioCaptureFactory::GetModularFeatureName());IModularFeatures::Get().UnlockModularFeatureList();// For now, just return the first audio capture stream implemented. We can make this configurable at a later point.if (AudioCaptureStreamFactories.Num() > 0 && AudioCaptureStreamFactories[0] != nullptr){AudioCapture = AudioCaptureStreamFactories[0]->CreateNewAudioCaptureStream();if (!AudioCapture.IsValid()){GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("CreateNewAudioCaptureStream return null"));}}else {GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("no Audio Capture Stream Factories"));}
}
void UFxAudioCaptureComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{Super::EndPlay(EndPlayReason);StopRecord();
}
void UFxAudioCaptureComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
int UFxAudioCaptureComponent::GetAudioDataSize()
{int len = 0;Mutex.Lock();len = m_audioData.Num();Mutex.Unlock();return len;
}
TArray<uint8> UFxAudioCaptureComponent::CopyAudioData(int numSamples, bool bRemove)
{TArray<uint8> Array;Mutex.Lock();if (0 < numSamples && numSamples <= m_audioData.Num()) {for (int i = 0; i < numSamples; ++i) {int16_t sample16Bit = static_cast<int16_t>(m_audioData[i] * 32767.0f);// 将16位样本存储到Array中Array.Push(static_cast<uint8_t>(sample16Bit & 0xFF));Array.Push(static_cast<uint8_t>((sample16Bit >> 8) & 0xFF));}if (bRemove) {m_audioData.RemoveAt(0, numSamples);}}Mutex.Unlock();return Array;
}
bool UFxAudioCaptureComponent::StartRecord(float seconds)
{StopRecord();RecordSeconds = seconds;if (AudioCapture.IsValid()){Audio::FAudioCaptureDeviceParams Params;/** 设置声卡不支持的采样数和通道数开始音频流不会成功,这里不能修改* Params.NumInputChannels = 1;* Params.SampleRate = 16000;* * 可以修改PCM数据格式,默认是32位浮点数FLOATING_POINT_32* 修改为32位整数PCM_32,在Android系统有问题,还是修改为FLOATING_POINT_32*/Params.PCMAudioEncoding = Audio::EPCMAudioEncoding::FLOATING_POINT_32;// 使用 TFunction 包装成员函数Audio::FOnAudioCaptureFunction OnCapture = [this](const void* InAudio, int32 NumFrames, int32 NumChannels, int32 SampleRate, double StreamTime, bool bOverFlow){this->OnAudioCapture(InAudio, NumFrames, NumChannels, SampleRate, StreamTime, bOverFlow);};bool r = AudioCapture->OpenAudioCaptureStream(Params, MoveTemp(OnCapture), 1920); // 48000采样率可以在重采样是整除3if (r) {r = AudioCapture->StartStream();if (!r) {GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("StartStream return false"));}}else {GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("OpenAudioCaptureStream return false"));}bRecording = r;}return IsRecording();
}
void UFxAudioCaptureComponent::StopRecord()
{if (bRecording && AudioCapture.IsValid()){AudioCapture->StopStream();AudioCapture->CloseStream();}bRecording = false;
}void UFxAudioCaptureComponent::OnAudioCapture(const void* InAudio, int32 NumFrames, int32 NumChannels, int32 SampleRate, double StreamTime, bool bOverFlow)
{// GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("OnAudioCapture - %d,%d,%d,%f,%d"), NumFrames, NumChannels, SampleRate, (float)StreamTime, bOverFlow ? 1 : 0));// 按照16Khz和Mono重采样数据TArray<float> data = ResampleAndConvert16KHzMono16Bit(static_cast<const float*>(InAudio), NumFrames, NumChannels, SampleRate);// 计算强度float total = 0;for (float val : data) {total += FMath::Abs(val);}Intensity = total / (float)(data.Num());// 拷贝数据Mutex.Lock();m_audioData.Append(data.GetData(), data.Num());Mutex.Unlock();
}bool UFxAudioCaptureComponent::IsRecording() {if (!AudioCapture.IsValid()) {return false;}if (!bRecording) {return false;}if (!AudioCapture->IsCapturing()) {return false;}return true;
}
TArray<float> UFxAudioCaptureComponent::ResampleAndConvert16KHzMono16Bit(const float* inputData, int inputNumSamples, int inputChannels, int inputSampleRate)
{int targetSampleRate = 16000;// 计算重采样的步长double resampleRate = static_cast<double>(inputSampleRate) / targetSampleRate;// 临时存储单声道数据std::vector<float> monoSamples;int id = 0;for (int i = 0; i < inputNumSamples; ++i) {float sampleValue = 0;// 如果是多声道,转换为单声道if (inputChannels > 1) {float monoValue = 0;for (int j = 0; j < inputChannels; ++j) {monoValue += inputData[id++];}// 取平均值以避免溢出sampleValue = monoValue / inputChannels;}else {sampleValue = inputData[id++];}monoSamples.push_back(sampleValue);}// 重采样TArray<float> resampledSamples;int targetNumSamples = static_cast<int>(inputNumSamples / resampleRate);for (int i = 0; i < targetNumSamples; ++i) {double srcIndex = i * resampleRate;int srcIndexInt = static_cast<int>(srcIndex);double frac = srcIndex - srcIndexInt;// 线性插值float sample1 = monoSamples[srcIndexInt];float sample2 = monoSamples[std::min(srcIndexInt + 1, inputNumSamples - 1)];float resampledValue = (1.0 - frac) * sample1 + frac * sample2;resampledSamples.Push(resampledValue);}return resampledSamples;
}

http://www.ppmy.cn/server/150700.html

相关文章

iOS 18.2 今天正式推送更新,带来了备受瞩目的 ChatGPT 集成以及更多 Apple Intelligence 工具

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

【C++】sophus : rotation_matrix.hpp 处理旋转矩阵的辅助函数 (二)

这段代码属于Sophus 命名空间&#xff0c;提供了一些处理旋转矩阵的辅助函数&#xff0c;具体功能如下&#xff1a; isOrthogonal函数&#xff1a; 用途&#xff1a;检查一个方阵是否为正交矩阵。实现方法&#xff1a;通过计算矩阵与其转置矩阵相乘后与单位矩阵的差值的范数是否…

家校通小程序实战教程09搭建部门管理APIs

目录 1 创建APIs2 完整代码3 代码解释3.1 获取原始数据3.2 平铺数据3.3 构建树形结构3.4 组装树形结构3.5 数据返回 4 执行测试总结 我们现在已经调用了antd实现了前端的界面&#xff0c;光有界面还是不够的&#xff0c;还需要和数据源进行交互&#xff0c;本节介绍后端API的搭…

Flutter踩坑记录(一)debug运行生成的项目,不能手动点击运行

问题 IOS14设备&#xff0c;切后台划掉&#xff0c;二次启动崩溃。 原因 IOS14以上 flutter 不支持debugger模式下的二次启动 。 要二次启动需要以release方式编译工程安装至手机。 操作步骤 清理项目&#xff1a;在命令行中运行flutter clean来清理之前的构建文件。重新构…

css 权重

发现宝藏 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。【宝藏入口】。 CSS 权重&#xff08;或称为 CSS 优先级&#xff09;决定了当多个 CSS 规则作用于同一元素时&#xff0c;哪一条规则会被应用。…

Web前端技术宝典:期末冲刺指南

本文将为大家整理一份 Web 前端期末复习资料&#xff0c;内容涵盖 HTML、CSS、JavaScript 和常用的前端框架等方面的知识&#xff0c;帮助大家高效复习。 Web前端技术宝典&#xff1a;期末冲刺指南 1. HTML基础2. CSS基础3. JavaScript基础4. 前端框架5. 常见考试题型结语 1. …

Java中的Stream

1. 什么是 Stream&#xff1f; Stream 是 Java 8 引入的一种新方式&#xff0c;目的是帮助我们更简洁、更高效地处理集合&#xff08;如 List、Set、Map 等&#xff09;。你可以把 Stream 想象成一条“流水线”&#xff0c;数据就像是流水线上的原材料&#xff0c;经过流水线的…

第六章 状态模式优化代码

目录 一、场景概述 二、状态模式优化代码 一、场景概述 我们在日常开发过程中一定会遇到下述场景&#xff1a;业务中有类似订单这样需要修改状态的功能模块&#xff0c;订单状态的修改变化为 未支付 -> 已支付 -> 已完成 因此我们每次在修改状态之前&#xff0c;都得先…