ue4 将音频从 48 立体声转换为 16 单声道
Posted
技术标签:
【中文标题】ue4 将音频从 48 立体声转换为 16 单声道【英文标题】:ue4 Convert audio from 48 stereo to 16 mono 【发布时间】:2021-09-07 09:45:21 【问题描述】:如何在 ue4 的 wav 录音中将采样率从 48000(ue4 默认)更改为 16000 个样本,并将立体声更改为单声道?我在BP中搜索过,但并不缺乏。下图显示了我对 BP 所做的事情。为了使它工作,我必须在 WindowsEngine.ini 中将音频设置更改为 XAudio(参见:https://www.youtube.com/watch?v=BpP1SxxwYIE)
因此,我认为这应该只适用于 C++。
【问题讨论】:
【参考方案1】:我是用 C++ 做的
文件 -> 新 C++ 类 -> VoiceCharacter -> 公共
更改您的角色以将“VoiceCharacter”作为父级。可以在角色 BP 的职业设置中找到。
将此方法添加到 VoiceCharacter C++ 类并构建和播放。
void AVoiceCharacter::StereoToMono(TArray<uint8> stereoWavBytes, TArray<uint8>& monoWavBytes)
if(stereoWavBytes.Num() == 0)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Stereo Bytes is empty");
return;
//Change wav headers
for (int i = 0; i < 44; i++)
//NumChannels starts from 22 to 24
if (i == 22)
short originalChannels = (*(short*)&stereoWavBytes[i]);
short NumChannels = originalChannels / 2;
FString message = FString::FromInt(originalChannels);
GEngine->AddOnScreenDebugMessage(-1, 25.f, FColor::Red, message);
monoWavBytes.Append((uint8*)&NumChannels, sizeof(NumChannels));
i++;
//SamplingRate starts from 24 to 27
else if (i == 24)
int OriginalSamplingRate = (*(int*)&stereoWavBytes[i]);
int SamplingRate = OriginalSamplingRate / 3 ;
GEngine->AddOnScreenDebugMessage(-1, 25.f, FColor::Yellow, FString::FromInt(OriginalSamplingRate));
monoWavBytes.Append((uint8*)&SamplingRate, sizeof(SamplingRate));
i += 3;
//ByteRate starts from 28 to 32
else if (i == 28)
int OriginalByteRate = (*(int*)&stereoWavBytes[i]);
int ByteRate = OriginalByteRate / 6 ;
GEngine->AddOnScreenDebugMessage(-1, 25.f, FColor::Yellow, FString::FromInt(OriginalByteRate));
monoWavBytes.Append((uint8*)&ByteRate, sizeof(ByteRate));
i += 3;
//BlockAlign starts from 32 to 34
else if (i == 32)
short BlockAlign = (*(short*)&stereoWavBytes[i]) / 2;
GEngine->AddOnScreenDebugMessage(-1, 25.f, FColor::White, FString::FromInt(BlockAlign));
monoWavBytes.Append((uint8*)&BlockAlign, sizeof(BlockAlign));
i++;
//SubChunkSize starts from 40 to 44
else if (i == 40)
int SubChunkSize = (*(int*)&stereoWavBytes[i]) / 2;
GEngine->AddOnScreenDebugMessage(-1, 25.f, FColor::Green, FString::FromInt(SubChunkSize));
monoWavBytes.Append((uint8*)&SubChunkSize, sizeof(SubChunkSize));
i += 3;
else
monoWavBytes.Add(stereoWavBytes[i]);
//Copies only the left channel and ignores the right channel
// for (int i = 44; i < stereoWavBytes.Num(); i += 4)
//
// monoWavBytes.Add(stereoWavBytes[i]);
// monoWavBytes.Add(stereoWavBytes[i+1]);
//
//Copies only the left channel and ignores the right channel. Also downsamples by 3,
// i.e. converts Windows 48000 sampling rate of ue4 to 16000
for (int i = 44; i < stereoWavBytes.Num(); i += 12)
monoWavBytes.Add(stereoWavBytes[i]);
monoWavBytes.Add(stereoWavBytes[i+1]);
不要忘记在 VoiceCharacter.h 中添加它,以便您可以使用它的 BP。
UFUNCTION(BlueprintCallable, Category="Audio")
static void StereoToMono(TArray<uint8> stereoWavBytes, TArray<uint8>& monoWavBytes);
这里是如何在 BPs 中使用它
一些有用的插件(CUFile) https://github.com/getnamo/nodejs-ue4 https://github.com/getnamo/socketio-client-ue4
【讨论】:
以上是关于ue4 将音频从 48 立体声转换为 16 单声道的主要内容,如果未能解决你的问题,请参考以下文章