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

ue4 Convert audio from 48 stereo to 16 mono

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

因此我认为这应该只适用于 C++。

我用 C++ 做到了

文件 -> 新 C++ class -> VoiceCharacter -> Public

将您的角色更改为将“VoiceCharacter”作为父角色。可以在角色BP的Class设置中找到。

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

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中添加这个,这样你就可以使用它了。

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