Skip to content

Instantly share code, notes, and snippets.

@Astroneko404
Created March 21, 2023 07:19
Show Gist options
  • Save Astroneko404/e8118d6cce436f22da1d336205c3c524 to your computer and use it in GitHub Desktop.
Save Astroneko404/e8118d6cce436f22da1d336205c3c524 to your computer and use it in GitHub Desktop.
Audio PCM Retrieving in Unreal Engine
// Fill out your copyright notice in the Description page of Project Settings.
#include "PCMRetrieveActor.h"
#include "EnvironmentQuery/EnvQueryTypes.h"
// Read PCM and save the data in a TArray
static void FilterCallback(
void* OBJ,
CriAtomExPlaybackId id,
CriAtomPcmFormat Format,
CriSint32 NumChannels,
CriSint32 NumSamples,
void* Data[])
{
if (APCMRetrieveActor* Self = static_cast<APCMRetrieveActor*> (OBJ))
{
if (Format == CRIATOM_PCM_FORMAT_FLOAT32)
{
for (CriSint32 i=0; i < NumChannels; i++)
{
CriFloat32 *PCM = static_cast<CriFloat32*>(Data[i]);
for (CriSint32 j=0; j < NumSamples; j++)
{
Self->PCMArray.Add(PCM[j]);
}
}
}
else if (Format == CRIATOM_PCM_FORMAT_SINT16)
{
for (CriSint32 i=0; i < NumChannels; i++)
{
CriSint16 *PCM = static_cast<CriSint16*>(Data[i]);
for (CriSint32 j=0; j < NumSamples; j++)
{
Self->PCMArray.Add(PCM[j]);
}
}
}
}
}
// Sets default values
APCMRetrieveActor::APCMRetrieveActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void APCMRetrieveActor::BeginPlay()
{
Super::BeginPlay();
// Init UAtomComponent
AudioComponent = NewObject<UAtomComponent>();
if (AudioComponent == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Failed to create AtomComponent."));
return;
}
// Get AtomExPlayer
Player = AudioComponent->GetAtomExPlayer();
// Set Cue
SoundCue = Cast<USoundAtomCue>(
StaticLoadObject(USoundAtomCue::StaticClass(), NULL, TEXT("/Game/CueSheet_0_Cue_0_Cue")));
if (SoundCue == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Failed to load SoundAtomCue"));
}
AudioComponent->SetSound(SoundCue);
PCMArray.Init(0.0f, 0);
}
// Called every frame
void APCMRetrieveActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called in bluePrint to play the sound
void APCMRetrieveActor::PlaySound()
{
// Filter Callback
if (Player)
{
FCriWareApi::criAtomExPlayer_SetFilterCallback(Player, FilterCallback, this);
}
// Play Sound
AudioComponent->Play();
}
void APCMRetrieveActor::ClearArray()
{
PCMArray.Empty();
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CriWareApi.h"
#include "AtomComponent.h"
#include "PCMRetrieveActor.generated.h"
UCLASS()
class PCM_TESTING_API APCMRetrieveActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APCMRetrieveActor();
CriAtomExPlayerHn Player;
UPROPERTY(BlueprintReadWrite)
UAtomComponent* AudioComponent;
UPROPERTY(BlueprintReadWrite)
USoundAtomCue* SoundCue;
UPROPERTY(BlueprintReadWrite)
TArray<float> PCMArray;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable)
void PlaySound();
UFUNCTION(BlueprintCallable)
void ClearArray();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment