스트리밍 사운드 웨이브
음성 활동 감지(VAD)에 대해서는 이 페이지를 참조하세요.
스트리밍 사운드 웨이브는 오디오 데이터를 동적으로 추가할 수 있는 유형의 수입 사운드 웨이브로, 재생 중에도 가능합니다. 이는 수입 사운드 웨이브와 동일한 기능(예: 되감기 등)을 제공하며, SoundCues 등에서 사용할 수 있습니다.
스트리밍 사운드 웨이브 생성
먼저 스트리밍 사운드 웨이브를 생성해야 합니다. 빠른 파괴를 방지하기 위해 강한 참조로 취급해야 한다는 점을 유의하세요(예: Blueprint에서 별도의 변수에 할당하거나 C++에서 UPROPERTY()
사용).
- Blueprint
- C++
UStreamingSoundWave* StreamingSoundWave = UStreamingSoundWave::CreateStreamingSoundWave();
사운드 웨이브 재생
그런 다음 해당 사운드 웨이브를 재생할 수 있습니다. 그러나 지금 바로 할 필요는 없으며, 나중에 사운드 웨이브 재생을 시작할 수 있습니다.
오디오 데이터 미리 할당
선택적으로, 새로운 오디오 데이터가 추가될 때마다 전체 PCM 버퍼를 재할당하는 것을 피하기 위해 오디오 데이터(바이트)를 미리 할당할 수 있습니다.
- Blueprint
- C++
// Assuming StreamingSoundWave is a UE reference to a UStreamingSoundWave object (or its derived type, such as UCapturableSoundWave)
StreamingSoundWave->PreAllocateAudioData(12582912, FOnPreAllocateAudioDataResultNative::CreateWeakLambda(this, [StreamingSoundWave](bool bSucceeded)
{
// Handle the result
}));
오디오 데이터 추가
기존 버퍼의 끝에 오디오 데이터를 추가하려면, 오디오 데이터를 동적으로 추가할 수 있는 적절한 함수를 사용하세요. 재생은 이러한 추가의 큐 순서를 따릅니다.
- Blueprint
- C++
// Assuming StreamingSoundWave is a UE reference to a UStreamingSoundWave object (or its derived type, such as UCapturableSoundWave)
// Example of appending encoded audio data
TArray<uint8> AudioData = ...; // Fill with audio data
StreamingSoundWave->AppendAudioDataFromEncoded(AudioData, ERuntimeAudioFormat::Auto);
// Or, if you have raw audio data
TArray<uint8> RawAudioData = ...; // Fill with raw audio data
StreamingSoundWave->AppendAudioDataFromRAW(RawAudioData, ERuntimeRAWAudioFormat::Float32, 44100, 2);
가속된 오디오 재생 방지
종종 오디오 데이터를 스트리밍하고 동시에 재생하려면, 너무 빠른 버퍼 생성으로 인한 가속된 오디오 재생을 피하기 위해 재생 전에 약간의 지연을 추가해야 합니다. 일반적으로 약 0.5초의 지연이 권장됩니다.
예제 사용법
마지막으로, 당신의 구현은 다음과 같을 수 있습니다:
- Blueprint
- C++
이것은 스트리밍 사운드 웨이브에 오디오 데이터를 추가하는 기본 코드 예제입니다.
이 예제는 EXAMPLEMODULE
모듈 내의 UAppendAudioClassExample
클래스에 위치한 AppendAudioExample
함수를 사용합니다.
참고: 예제를 성공적으로 실행하려면 .Build.cs 파일의 PublicDependencyModuleNames
또는 PrivateDependencyModuleNames
에 RuntimeAudioImporter
모듈을 추가하고, 프로젝트의 .uproject 파일에도 추가하십시오.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "AppendAudioClassExample.generated.h"
UCLASS(BlueprintType)
class EXAMPLEMODULE_API UAppendAudioClassExample : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void AppendAudioExample();
private:
// Please pay attention to making the StreamingSoundWave a hard reference, such as using UPROPERTY(), to prevent it from being prematurely garbage collected
UPROPERTY()
class UStreamingSoundWave* StreamingSoundWave;
};
#include "AppendAudioClassExample.h"
#include "Sound/StreamingSoundWave.h"
void UAppendAudioClassExample::AppendAudioExample()
{
// Create a streaming sound wave
StreamingSoundWave = UStreamingSoundWave::CreateStreamingSoundWave();
// Append audio data
TArray<uint8> StreamedAudioDataToAdd = ...; // Fill with audio data
StreamingSoundWave->AppendAudioDataFromEncoded(StreamedAudioDataToAdd, ERuntimeAudioFormat::Auto);
// Play the sound wave
UGameplayStatics::PlaySound2D(GetWorld(), StreamingSoundWave);
}