UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ADPCMAudioInfo.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 ADPCMAudioInfo.h: Unreal audio ADPCM decompression interface object.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10#include "Audio.h"
11#include "AudioDecompress.h"
12#include "Sound/SoundWave.h"
13#include "ContentStreaming.h"
14
15#define NUM_ADAPTATION_TABLE 16
16#define NUM_ADAPTATION_COEFF 7
17
18#define WAVE_FORMAT_LPCM 1
19#ifndef WAVE_FORMAT_ADPCM
20#define WAVE_FORMAT_ADPCM 2
21#endif
22
23#if PLATFORM_SUPPORTS_PRAGMA_PACK
24#pragma pack(push, 2)
25#endif
27 {
28 uint16 wFormatTag; // Format type: 1 = PCM, 2 = ADPCM
29 uint16 nChannels; // Number of channels (i.e. mono, stereo...).
30 uint32 nSamplesPerSec; // Sample rate. 44100 or 22050 or 11025 Hz.
31 uint32 nAvgBytesPerSec; // For buffer estimation = sample rate * BlockAlign.
32 uint16 nBlockAlign; // Block size of data = Channels times BYTES per sample.
33 uint16 wBitsPerSample; // Number of bits per sample of mono data.
34 uint16 cbSize; // The count in bytes of the size of extra information (after cbSize).
35 };
36#if PLATFORM_SUPPORTS_PRAGMA_PACK
37#pragma pack(pop)
38#endif
39
40namespace ADPCM
41{
42
43 template <typename T>
45 {
46 // Magic values as specified by standard
47 static T AdaptationCoefficient1[] =
48 {
49 256, 512, 0, 192, 240, 460, 392
50 };
51 static T AdaptationCoefficient2[] =
52 {
53 0, -256, 0, 64, 0, -208, -232
54 };
55
56 FMemory::Memcpy(&OutAdaptationCoefficient1, AdaptationCoefficient1, sizeof(AdaptationCoefficient1));
57 FMemory::Memcpy(&OutAdaptationCoefficient2, AdaptationCoefficient2, sizeof(AdaptationCoefficient2));
58 }
59
60#if PLATFORM_SUPPORTS_PRAGMA_PACK
61#pragma pack(push, 2)
62#endif
64 {
69 uint32 SamplesPerChannel; // This is the exact samples per channel for sample precise looping
70
72 {
74 int16 AdaptationCoefficient1[NUM_ADAPTATION_COEFF];
75 int16 AdaptationCoefficient2[NUM_ADAPTATION_COEFF];
76 GetAdaptationCoefficients(AdaptationCoefficient1, AdaptationCoefficient2);
77
78 // Interlace the coefficients as pairs
79 for (int32 Coeff = 0, WriteIndex = 0; Coeff < NUM_ADAPTATION_COEFF; Coeff++)
80 {
81 aCoef[WriteIndex++] = AdaptationCoefficient1[Coeff];
82 aCoef[WriteIndex++] = AdaptationCoefficient2[Coeff];
83 }
84 }
85 };
86#if PLATFORM_SUPPORTS_PRAGMA_PACK
87#pragma pack(pop)
88#endif
89
90};
91
93{
94public:
97 // ICompressedAudioInfo Interface
99 ADPCMAUDIODECODER_API virtual bool ReadCompressedData(uint8* Destination, bool bLooping, uint32 BufferSize);
100 ADPCMAUDIODECODER_API virtual void SeekToTime(const float SeekTime);
102 ADPCMAUDIODECODER_API virtual void ExpandFile(uint8* DstBuffer, struct FSoundQualityInfo* QualityInfo);
103 virtual void EnableHalfRate(bool HalfRate) {};
104 virtual uint32 GetSourceBufferSize() const { return SrcBufferDataSize; }
105 virtual bool UsesVorbisChannelOrdering() const { return false; }
106 virtual int GetStreamBufferSize() const;
107
108 // Additional overrides for streaming
109 virtual bool SupportsStreaming() const override {return true;}
110 virtual bool StreamCompressedInfoInternal(const FSoundWaveProxyPtr& InWaveProxy, struct FSoundQualityInfo* QualityInfo) override;
111 virtual bool StreamCompressedData(uint8* Destination, bool bLooping, uint32 BufferSize, int32& OutNumBytesStreamed) override;
112 virtual int32 GetCurrentChunkIndex() const override
113 {
114 return CurrentChunkIndex;
115 }
116 virtual int32 GetCurrentChunkOffset() const override {return CurrentChunkBufferOffset;}
117 virtual bool ReleaseStreamChunk(bool bBlockUntilReleased) override;
118 // End of ICompressedAudioInfo Interface
119
121 {
122 if (WaveInfo.pFormatTag)
123 {
124 return *WaveInfo.pFormatTag;
125 }
126 else
127 {
128 return 0;
129 }
130 }
131
133 {
134 return NumChannels;
135 }
136
137private:
138
139 // Wrapper function that returns a pointer to the currently used compressed data.
140 // If a non-zero chunk is requested, this function also aquires a reference to that chunk
141 // until we move on to a different chunk.
142 const uint8* GetLoadedChunk(const FSoundWaveProxyPtr& InSoundWave, uint32 ChunkIndex, uint32& OutChunkSize);
143
144 int32 NumConsecutiveReadFailiures;
145
146 FWaveModInfo WaveInfo;
147 const uint8* SrcBufferData;
148 uint32 SrcBufferDataSize;
149
150 uint32 UncompressedBlockSize;
151 uint32 CompressedBlockSize;
152 uint32 BlockSize;
153 int32 StreamBufferSize;
154 uint32 TotalDecodedSize;
155 int32 NumChannels;
156 int32 Format;
157
158 uint32 PreviouslyRequestedChunkIndex;
159
160 uint8* UncompressedBlockData; // This holds the current block of compressed data for all channels
161 uint32 CurrentUncompressedBlockSampleIndex; // This is the sample index within the current uncompressed block data
162 uint32 CurrentChunkIndex; // This is the index that is currently being used, needed by streaming engine to make sure it stays loaded and the next chunk gets preloaded
163 uint32 CurrentChunkBufferOffset; // This is this byte offset within the current chunk, used by streaming engine to prioritize a load if more then half way through current chunk
164 uint32 CurrentChunkDataSize; // The size of the current chunk, the first chunk is bigger to accommodate the header info
165 uint32 TotalSamplesStreamed; // The number of samples streamed so far (per channel)
166 uint32 TotalSamplesPerChannel; // Number of samples per channel, used to detect when an audio waveform has ended
167 uint32 SamplesPerBlock; // The number of samples per block
168 uint32 FirstChunkSampleDataOffset; // The size of the header in the first chunk, used to skip over it when looping or starting the sample over
169 uint32 FirstChunkSampleDataIndex;
170 const uint8* CurCompressedChunkData; // A pointer to the current chunk of data
171 FAudioChunkHandle CurCompressedChunkHandle; // Shared reference to the current chunk of data.
172 FCriticalSection CurCompressedChunkHandleCriticalSection;
173 bool bDecompressorReleased; // When the chunk is released, we raise this to true, then early exit on future decodes.
174
175 uint32 CurrentCompressedBlockIndex; // For non disk streaming - the current compressed block in the compressed source data
176 uint32 TotalCompressedBlocksPerChannel; // For non disk streaming - the total number of compressed blocks per channel
177 uint8 bNewSeekRequest : 1; // Whether or not there is a new seek request to be processed
178 uint8 bSeekPendingRead : 1; // Whether or not a requested seek is pending a read
179 uint8 bSeekedFowardToNextChunk : 1; // If this is true, we have already seeked forward to the next chunk while waiting for the current chunk of audio to load.
180
181private:
182 void ProcessSeekRequest();
183 void SeekToFrameInternal(const uint32 InSeekFrame);
184 void ResetSeekState();
185
186 std::atomic<uint32> TargetSeekFrame;
187
188 FCriticalSection StreamSeekCriticalSection;
189};
#define NUM_ADAPTATION_COEFF
Definition ADPCMAudioInfo.h:16
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ADPCMAudioInfo.h:93
virtual bool ReleaseStreamChunk(bool bBlockUntilReleased) override
Definition ADPCMAudioInfo.cpp:978
uint16 GetFormatTag()
Definition ADPCMAudioInfo.h:120
virtual ADPCMAUDIODECODER_API void ExpandFile(uint8 *DstBuffer, struct FSoundQualityInfo *QualityInfo)
Definition ADPCMAudioInfo.cpp:456
virtual void EnableHalfRate(bool HalfRate)
Definition ADPCMAudioInfo.h:103
virtual int32 GetCurrentChunkIndex() const override
Definition ADPCMAudioInfo.h:112
virtual uint32 GetSourceBufferSize() const
Definition ADPCMAudioInfo.h:104
virtual bool SupportsStreaming() const override
Definition ADPCMAudioInfo.h:109
virtual ADPCMAUDIODECODER_API bool ReadCompressedInfo(const uint8 *InSrcBufferData, uint32 InSrcBufferDataSize, struct FSoundQualityInfo *QualityInfo)
Definition ADPCMAudioInfo.cpp:234
virtual ADPCMAUDIODECODER_API void SeekToTime(const float SeekTime)
Definition ADPCMAudioInfo.cpp:74
virtual int GetStreamBufferSize() const
Definition ADPCMAudioInfo.cpp:463
virtual ADPCMAUDIODECODER_API ~FADPCMAudioInfo(void)
Definition ADPCMAudioInfo.cpp:65
ADPCMAUDIODECODER_API FADPCMAudioInfo(void)
Definition ADPCMAudioInfo.cpp:43
virtual ADPCMAUDIODECODER_API bool ReadCompressedData(uint8 *Destination, bool bLooping, uint32 BufferSize)
Definition ADPCMAudioInfo.cpp:317
virtual int32 GetCurrentChunkOffset() const override
Definition ADPCMAudioInfo.h:116
virtual bool UsesVorbisChannelOrdering() const
Definition ADPCMAudioInfo.h:105
virtual bool StreamCompressedInfoInternal(const FSoundWaveProxyPtr &InWaveProxy, struct FSoundQualityInfo *QualityInfo) override
Definition ADPCMAudioInfo.cpp:482
int32 GetNumChannels()
Definition ADPCMAudioInfo.h:132
virtual ADPCMAUDIODECODER_API void SeekToFrame(const uint32 SeekFrame)
Definition ADPCMAudioInfo.cpp:86
virtual bool StreamCompressedData(uint8 *Destination, bool bLooping, uint32 BufferSize, int32 &OutNumBytesStreamed) override
Definition ADPCMAudioInfo.cpp:594
Definition ContentStreaming.h:110
Definition Audio.h:938
uint16 * pFormatTag
Definition Audio.h:954
Definition AudioDecompress.h:31
Definition Adpcm.cpp:7
Definition ADPCMAudioInfo.h:64
uint16 wSamplesPerBlock
Definition ADPCMAudioInfo.h:66
WaveFormatHeader BaseFormat
Definition ADPCMAudioInfo.h:65
int16 aCoef[2 *NUM_ADAPTATION_COEFF]
Definition ADPCMAudioInfo.h:68
uint32 SamplesPerChannel
Definition ADPCMAudioInfo.h:69
uint16 wNumCoef
Definition ADPCMAudioInfo.h:67
ADPCMFormatHeader()
Definition ADPCMAudioInfo.h:71
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition UnrealMemory.h:131
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
Definition ADPCMAudioInfo.h:27
uint16 nChannels
Definition ADPCMAudioInfo.h:29
uint16 wFormatTag
Definition ADPCMAudioInfo.h:28
uint32 nSamplesPerSec
Definition ADPCMAudioInfo.h:30
uint16 cbSize
Definition ADPCMAudioInfo.h:34
uint16 wBitsPerSample
Definition ADPCMAudioInfo.h:33
uint32 nAvgBytesPerSec
Definition ADPCMAudioInfo.h:31
uint16 nBlockAlign
Definition ADPCMAudioInfo.h:32