首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ue4将音频从48立体声转换为16单声道

问ue4将音频从48立体声转换为16单声道
EN

Stack Overflow用户
提问于 2021-09-07 09:45:22
回答 1查看 22关注 0票数 0

如何在ue4中将采样率从48000 (ue4默认值)更改为16000采样,并将立体声更改为单声道?我已经在BPs中搜索过了,但并不缺乏。下图显示了我对BPs所做的工作。为了让它起作用,我必须在WindowsEngine.ini中将音频设置更改为XAudio (参见:https://www.youtube.com/watch?v=BpP1SxxwYIE)

​

​

因此,我假设只有使用C++才能做到这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-07 09:48:36

我和C++一起做的

File ->新公共类-> VoiceCharacter -> ->

将角色更改为"VoiceCharacter“作为父角色。它可以在角色BP的类设置中找到。

将此方法添加到VoiceCharacter C++类中并构建并播放。

代码语言:javascript
复制
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中,以便您可以使用它BPs。

代码语言:javascript
复制
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的一些有用插件

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69085916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档