Files
UnrealEngineUWP/Engine/Plugins/Runtime/WaveTable/Source/WaveTableEditor/Private/WaveTableFileUtilities.cpp
Rob Gay bd158319e3 WaveTable Checkpoint
- Migrate/Create base WaveTable functionality & editor bits from Modulation plugin so it can be leveraged for other assets & tools
- Add WaveTableImporter/Sampler classes for use by WaveTable editor and MetaSound nodes (and potentially other systems)
#rb helen.yang
#rb phil.popp
#jira UE-156632
#rnx
#preflight 62aa5076634e82e5d1000229

[CL 20677783 by Rob Gay in ue5-main branch]
2022-06-15 18:13:28 -04:00

61 lines
1.9 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "WaveTableFileUtilities.h"
#include "Factories/SoundFactory.h"
#include "HAL/Platform.h"
#include "Sound/SoundWave.h"
#include "UObject/SoftObjectPath.h"
#include "UObject/Object.h"
#include "UObject/ObjectMacros.h"
#include "UObject/Package.h"
#include "Utils.h"
#include "WaveTableSampler.h"
namespace WaveTable
{
namespace Editor
{
namespace FileUtilities
{
void LoadPCMChannel(const FString& InFilePath, int32 InChannelIndex, TArray<float>& OutPCMData)
{
OutPCMData.Empty();
if (!InFilePath.IsEmpty())
{
if (USoundFactory* SoundWaveFactory = NewObject<USoundFactory>())
{
// Setup sane defaults for importing localized sound waves
SoundWaveFactory->bAutoCreateCue = false;
SoundWaveFactory->SuppressImportDialogs();
UPackage* TempPackage = CreatePackage(TEXT("/Temp/WaveTable"));
if (USoundWave* SoundWave = ImportObject<USoundWave>(TempPackage, "ImportedWaveTable", RF_Public | RF_Standalone, *InFilePath, nullptr, SoundWaveFactory))
{
TArray<uint8> RawPCMData;
uint32 SampleRate = 0;
uint16 NumChannels = 0;
SoundWave->GetImportedSoundWaveData(RawPCMData, SampleRate, NumChannels);
if (NumChannels > 0)
{
const int32 NumFrames = RawPCMData.Num() / NumChannels / sizeof(int16);
OutPCMData.Empty();
OutPCMData.AddZeroed(NumFrames);
const int16* RawDataPtr = (const int16*)(RawPCMData.GetData());
const int32 ChannelOffset = (InChannelIndex % NumChannels) * sizeof(int16);
constexpr float Max16BitAsFloat = static_cast<float>(TNumericLimits<int16>::Max());
for (int32 i = 0; i < OutPCMData.Num(); ++i)
{
OutPCMData[i] = (RawDataPtr[(i * NumChannels) + ChannelOffset]) / Max16BitAsFloat;
}
}
}
}
}
}
} // namespace FileUtilities
} // namespace Editor
} // namespace WaveTable