オーディオのインポート
ランタイムでオーディオをインポートするプロセスはいくつかのステップに分解できます:
- Runtime Audio Importer を作成する。
- 必要なデリゲート(OnProgress と OnResult)にバインドする。
- ファイルまたはバッファからオーディオをインポートする。
- OnResult デリゲートから得られたインポートされた音声波を再生する(詳細はこちら)。
Runtime Audio ImporterとSound Waveのインスタンスが不要にガベージコレクトされないように、UPROPERTY()、TStrongObjectPtr、またはオブジェクトが破壊されるのを防ぐ ための他の方法を使用して、それらにハード参照を保持して、別々の変数に割り当てることをお勧めします。
Blueprints の例
最初に、Runtime Audio Importer オブジェクトを作成する必要があります。これはガーベジコレクタによって強い参照として扱われることを確認してください。これは、Blueprints 内で別々の変数として配置することで達成できます。これにより、オブジェクトの早期破壊を防ぎます。
- Blueprint
- C++
// UPROPERTY() is used here to prevent the object from being prematurely garbage collected
UPROPERTY()
class URuntimeAudioImporterLibrary* Importer;
Importer = URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter();
オーディオデータのインポート進行状況を監視するには、OnProgress
(Blueprints) / OnProgressNative
(C++) デリゲートにバインドすることができます。これにより、進行状況を監視でき、例えばローディング画面を実装できます。このデリゲートにバインドするには、バインドイベントノードからイベントピンを引っ張るだけです。
- Blueprint
- C++
// Assuming Importer is a UE reference to a URuntimeAudioImporterLibrary object
// AddWeakLambda is used just as an example. You can use any other method to bind the delegate, such as AddUObject, AddUFunction, etc.
Importer->OnProgressNative.AddWeakLambda(this, [](int32 Percentage)
{
UE_LOG(LogTemp, Log, TEXT("Import progress: %d"), Percentage);
});
オーディオデータのインポートプロセスが完了したことを通知され、生成された音波の参照にアクセスするには、OnResult
(Blueprints) / OnResultNative
(C++) デリゲートにバインドする必要があります。また、インポートされた音波が不要な早期のガベージコレクションを防ぐために、ガーベージコレクタによって強参照として扱われていることを確認してください。これは、Blueprints でそれを別の変数として配置することで行えます。
- Blueprint
- C++
// Assuming Importer is a UE reference to a URuntimeAudioImporterLibrary object
// AddWeakLambda is used just as an example. You can use any other method to bind the delegate, such as AddUObject, AddUFunction, etc.
Importer->OnResultNative.AddWeakLambda(this, [](URuntimeAudioImporterLibrary* Importer, UImportedSoundWave* ImportedSoundWave, ERuntimeImportStatus Status)
{
UE_LOG(LogTemp, Log, TEXT("Import result: %s"), *UEnum::GetValueAsString(Status));
});
オーディオインポートプロセスを開始するには、圧縮および非圧縮のオーディオデータ形式を扱うことができる 関連機能を呼び出します。
- Blueprint
- C++
// Assuming Importer is a UE reference to a URuntimeAudioImporterLibrary object
// Import audio from a file
Importer->ImportAudioFromFile(TEXT("C:/Folder/AudioFile.mp3"), ERuntimeAudioFormat::Auto);
// Import audio from a buffer
TArray<uint8> AudioData = ...; // Fill the array with your audio data
Importer->ImportAudioFromBuffer(MoveTemp(AudioData), ERuntimeAudioFormat::OggVorbis);
// Import audio from a RAW file
Importer->ImportAudioFromRAWFile(TEXT("C:/Folder/AudioFile.raw"), ERuntimeRAWAudioFormat::Int8, 44100, 2);
// Import audio from a RAW buffer
TArray<uint8> RAWBuffer = ...; // Fill the array with your PCM int 16-bit audio data
Importer->ImportAudioFromRAWBuffer(MoveTemp(RAWBuffer), ERuntimeRAWAudioFormat::Int16, 44100, 2);
インポートプロセスが完了すると、OnResult
デリゲートを通じて結果として得られたインポートされた音波を取得できます。
完全な例
- ブループリント
- C++
これはファイルからオーディオをインポートするための基本的なコード例です。
例は、EXAMPLEMODULE
モジュール内の UImportAudioClassExample
クラスにある ImportAudioExample
関数を使用しています。
注意: この例を正常に実行するには、.Build.cs ファイルの PublicDependencyModuleNames
または PrivateDependencyModuleNames
に RuntimeAudioImporter
モジュールを追加し、プロジェクトの .uproject ファイルにも追加してください。
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "ImportAudioClassExample.generated.h"
UCLASS(BlueprintType)
class EXAMPLEMODULE_API UImportAudioClassExample : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void ImportAudioExample();
private:
// Please pay attention to making the RuntimeAudioImporter a hard reference, such as using UPROPERTY(), to prevent it from being prematurely garbage collected
UPROPERTY()
class URuntimeAudioImporterLibrary* RuntimeAudioImporter;
};
#include "ImportAudioClassExample.h"
#include "RuntimeAudioImporterLibrary.h"
void UImportAudioClassExample::ImportAudioExample()
{
RuntimeAudioImporter = URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter();
if (!IsValid(RuntimeAudioImporter))
{
UE_LOG(LogTemp, Error, TEXT("Failed to create audio importer"));
return;
}
RuntimeAudioImporter->OnProgressNative.AddWeakLambda(this, [](int32 Percentage)
{
UE_LOG(LogTemp, Log, TEXT("Audio importing percentage: %d"), Percentage);
});
RuntimeAudioImporter->OnResultNative.AddWeakLambda(this, [this](URuntimeAudioImporterLibrary* Importer, UImportedSoundWave* ImportedSoundWave, ERuntimeImportStatus Status)
{
if (Status == ERuntimeImportStatus::SuccessfulImport)
{
UE_LOG(LogTemp, Warning, TEXT("Successfully imported audio with sound wave %s"), *ImportedSoundWave->GetName());
// Here you can handle ImportedSoundWave playback, like "UGameplayStatics::PlaySound2D(GetWorld(), ImportedSoundWave);"
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to import audio"));
}
RuntimeAudioImporter = nullptr;
});
RuntimeAudioImporter->ImportAudioFromFile(TEXT("C:/Folder/Example.mp3"), ERuntimeAudioFormat::Auto);
}