-
Notifications
You must be signed in to change notification settings - Fork 0
/
AkOdinInputComponent.cpp
101 lines (82 loc) · 2.76 KB
/
AkOdinInputComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Fill out your copyright notice in the Description page of Project Settings.
#include "AkOdinInputComponent.h"
#include "OdinFunctionLibrary.h"
#include "OdinMediaSoundGenerator.h"
#include "OdinPlaybackMedia.h"
#include "OdinInitializationSubsystem.h"
UAkOdinInputComponent::UAkOdinInputComponent(const FObjectInitializer& ObjectInitializer) : UAkAudioInputComponent(
ObjectInitializer)
{
PrimaryComponentTick.bCanEverTick = true;
}
void UAkOdinInputComponent::DestroyComponent(bool bPromoteChildren)
{
Super::DestroyComponent(bPromoteChildren);
if (nullptr != Buffer)
{
delete Buffer;
Buffer = nullptr;
BufferSize = 0;
}
}
void UAkOdinInputComponent::AssignOdinMedia(UOdinPlaybackMedia*& Media)
{
if (nullptr == Media)
return;
this->SoundGenerator = MakeShared<OdinMediaSoundGenerator, ESPMode::ThreadSafe>();
this->PlaybackMedia = Media;
SoundGenerator->SetOdinStream(Media->GetMediaHandle());
}
void UAkOdinInputComponent::GetChannelConfig(AkAudioFormat& AudioFormat)
{
int NumChannels = 1;
int SampleRate = 48000;
if (GetWorld() && GetWorld()->GetGameInstance()) {
const UOdinInitializationSubsystem* OdinInitSubsystem =
GetWorld()->GetGameInstance()->GetSubsystem<UOdinInitializationSubsystem>();
if (OdinInitSubsystem) {
NumChannels = OdinInitSubsystem->GetChannelCount();
SampleRate = OdinInitSubsystem->GetSampleRate();
}
}
AkChannelConfig ChannelConfig;
ChannelConfig.SetStandard(AK::ChannelMaskFromNumChannels(NumChannels));
UE_LOG(LogTemp, Warning, TEXT("Initializing Ak Odin Input Component with %i channels and Sample Rate of %i"), NumChannels, SampleRate);
// set audio format
AudioFormat.SetAll(
SampleRate, // Sample rate
ChannelConfig, // \ref AkChannelConfig
8 * sizeof(float), // Bits per samples
sizeof(float), // Block Align = 4 Bytes? Shouldn't it be 2*4=8 Bytes, because of two channels?
AK_FLOAT, // feeding floats
AK_NONINTERLEAVED
);
}
bool UAkOdinInputComponent::FillSamplesBuffer(uint32 NumChannels, uint32 NumSamples, float** BufferToFill)
{
if (!SoundGenerator || !PlaybackMedia)
return false;
const int32 RequestedTotalSamples = NumChannels * NumSamples;
if (BufferSize != RequestedTotalSamples)
{
if (nullptr != Buffer)
delete Buffer;
Buffer = new float[RequestedTotalSamples];
BufferSize = RequestedTotalSamples;
}
const uint32 Result = SoundGenerator->OnGenerateAudio(Buffer, RequestedTotalSamples);
if (odin_is_error(Result))
{
FString ErrorString = UOdinFunctionLibrary::FormatError(Result, true);
UE_LOG(LogTemp, Error, TEXT("UAkOdinInputComponent: Error during FillSamplesBuffer: %s"), *ErrorString);
return false;
}
for (uint32 s = 0; s < NumSamples; ++s)
{
for (uint32 c = 0; c < NumChannels; ++c)
{
BufferToFill[c][s] = Buffer[s * NumChannels + c];
}
}
return true;
}