MetaSoul

Loading...
Searching...
No Matches
ESOpusCoder.h
Go to the documentation of this file.
1#pragma once
2#define WITH_OPUS (PLATFORM_WINDOWS || PLATFORM_MAC || PLATFORM_UNIX || PLATFORM_ANDROID)
3#include "CoreMinimal.h"
4
5#if WITH_OPUS
6#include "ThirdParty/libOpus/opus-1.1/include/opus.h"
7//#include "ogg/ogg.h"
8#endif
9
10//Bare minimum struct for transferring opus bytes.
11 struct FEmoShapeOpusMinimalStream
12{
13 TArray<int16> PacketSizes; // Maximum packet size is 32768 (much larger than typical packet)
14 TArray<uint8> CompressedBytes;
15};
16
17
23 class FESOpusCoder
24{
25 public:
26 FESOpusCoder();
27 ~FESOpusCoder();
28
30 void SetSampleRate(int32 InSampleRate);
31 void SetChannels(int32 Channels);
32 void SetBitrate(int32 Bitrate);
33 void SetFrameSizeMs(int32 FrameSizeInMs);
34
36 bool EncodeStream(const TArray<uint8>& InPCMBytes, FEmoShapeOpusMinimalStream& OutStream);
37 bool DecodeStream(const FEmoShapeOpusMinimalStream& InStream, TArray<uint8>& OutPCMFrame);
38
39
40 //Format: PacketCount, PacketSizes, CompressedOpusBytes. Expects settings set in a different stream
41 bool SerializeMinimal(const FEmoShapeOpusMinimalStream& InStream, TArray<uint8>& OutSerializedBytes);
42 bool DeserializeMinimal(const TArray<uint8>& InSerializedMinimalBytes, FEmoShapeOpusMinimalStream& OutStream);
43
44 //Handle a single frame
45 int32 EncodeFrame(const TArray<uint8>& InPCMFrame, TArray<uint8>& OutCompressed);
46 int32 DecodeFrame(const TArray<uint8>& InCompressedFrame, TArray<uint8>& OutPCMFrame);
47
48 //Todo: add ogg file format wrapper for raw opus bytes
49
50 //Intended to be Read-only
51 int32 Channels;
52 int32 SampleRate;
53
54 //Whether we should reset the coder per stream to keep byte size small. Costs ~0.01ms
55 bool bResetBetweenEncoding;
56 bool bApplicationVoip;
57 bool bLowestPossibleLatency; //trade-off is compression size; ~3ms/sec of audio if on, ~10ms/sec if off. For ~ 33% more bytes.
58
59 void ResetCoderIfInitialized();
60
61 protected:
62 //Call this if some settings need to be reflected (all setters all this)
63 bool InitEncoderIfNeeded();
64 bool InitDecoderIfNeeded();
65
66 private:
67
68#if WITH_OPUS
69 OpusEncoder* Encoder;
70 OpusDecoder* Decoder;
71#endif
72
73 int32 BitRate;
74 int32 MaxPacketSize;
75 int32 FrameSizeMs;
76 int32 FrameSize;
77 int32 MaxFrameSize;
78
79
80 //Debug utilities
81 void DebugLogEncoder();
82 void DebugLogDecoder();
83 void DebugLogFrame(const uint8* PacketData, uint32 PacketLength, uint32 SampleRate, bool bEncode);
84
85};
FESOpusCoder
Definition ESOpusCoder.h:24
FESOpusCoder::SampleRate
int32 SampleRate
Definition ESOpusCoder.h:52
FESOpusCoder::DecodeFrame
int32 DecodeFrame(const TArray< uint8 > &InCompressedFrame, TArray< uint8 > &OutPCMFrame)
Definition ESOpusCoder.cpp:236
FESOpusCoder::InitEncoderIfNeeded
bool InitEncoderIfNeeded()
Definition ESOpusCoder.cpp:262
FESOpusCoder::Channels
int32 Channels
Definition ESOpusCoder.h:51
FESOpusCoder::SetBitrate
void SetBitrate(int32 Bitrate)
Definition ESOpusCoder.cpp:51
FESOpusCoder::SetSampleRate
void SetSampleRate(int32 InSampleRate)
Definition ESOpusCoder.cpp:38
FESOpusCoder::bLowestPossibleLatency
bool bLowestPossibleLatency
Definition ESOpusCoder.h:57
FESOpusCoder::bResetBetweenEncoding
bool bResetBetweenEncoding
Definition ESOpusCoder.h:55
FESOpusCoder::SerializeMinimal
bool SerializeMinimal(const FEmoShapeOpusMinimalStream &InStream, TArray< uint8 > &OutSerializedBytes)
Definition ESOpusCoder.cpp:190
FESOpusCoder::ResetCoderIfInitialized
void ResetCoderIfInitialized()
Definition ESOpusCoder.cpp:244
FESOpusCoder::FESOpusCoder
FESOpusCoder()
Definition ESOpusCoder.cpp:6
FESOpusCoder::bApplicationVoip
bool bApplicationVoip
Definition ESOpusCoder.h:56
FESOpusCoder::SetFrameSizeMs
void SetFrameSizeMs(int32 FrameSizeInMs)
Definition ESOpusCoder.cpp:57
FESOpusCoder::~FESOpusCoder
~FESOpusCoder()
Definition ESOpusCoder.cpp:22
FESOpusCoder::EncodeFrame
int32 EncodeFrame(const TArray< uint8 > &InPCMFrame, TArray< uint8 > &OutCompressed)
Definition ESOpusCoder.cpp:228
FESOpusCoder::SetChannels
void SetChannels(int32 Channels)
Definition ESOpusCoder.cpp:45
FESOpusCoder::DeserializeMinimal
bool DeserializeMinimal(const TArray< uint8 > &InSerializedMinimalBytes, FEmoShapeOpusMinimalStream &OutStream)
Definition ESOpusCoder.cpp:210
FESOpusCoder::EncodeStream
bool EncodeStream(const TArray< uint8 > &InPCMBytes, FEmoShapeOpusMinimalStream &OutStream)
Definition ESOpusCoder.cpp:65
FESOpusCoder::InitDecoderIfNeeded
bool InitDecoderIfNeeded()
Definition ESOpusCoder.cpp:301
FESOpusCoder::DecodeStream
bool DecodeStream(const FEmoShapeOpusMinimalStream &InStream, TArray< uint8 > &OutPCMFrame)
Definition ESOpusCoder.cpp:132
FEmoShapeOpusMinimalStream
Definition ESOpusCoder.h:12
FEmoShapeOpusMinimalStream::PacketSizes
TArray< int16 > PacketSizes
Definition ESOpusCoder.h:13
FEmoShapeOpusMinimalStream::CompressedBytes
TArray< uint8 > CompressedBytes
Definition ESOpusCoder.h:14