UnrealVoxta 0.1.1
 
Loading...
Searching...
No Matches
AudioCaptureHandler.h
Go to the documentation of this file.
1// Copyright(c) 2024 grrimgrriefer & DZnnah, see LICENSE for details.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Misc/ScopeLock.h"
7#include "VoiceRunnerThread.h"
8#include "VoxtaDefines.h"
9
10class AudioWebSocket;
11class IVoiceCapture;
12
18class VOXTAAUDIOUTILITY_API AudioCaptureHandler
19{
20#pragma region public API
21public:
28 void RegisterSocket(TWeakPtr<AudioWebSocket> socket, int bufferMillisecondSize);
29
40 bool TryInitializeVoiceCapture(int sampleRate = 16000, int numChannels = 1);
41
49 void ConfigureSilenceThresholds(float micNoiseGateThreshold, float silenceDetectionThreshold, float micInputGain);
50
58
60 void StopCapture();
61
63 void ShutDown(bool alsoDestroyCaptureDevice = false);
64
66 bool IsInputSilent() const;
67
69 float GetDecibels() const;
70
72 const FString& GetDeviceName() const;
73
74 void SetIsTestMode(bool isTestMode);
75#pragma endregion
76
77#pragma region data
78private:
79 TArray<uint8> m_socketDataBuffer;
80
81 static constexpr float DEFAULT_SILENCE_DECIBELS = -144.f;
82
84 friend class FVoiceRunnerThread;
85
86 FString m_deviceName = EMPTY_STRING;
87 bool m_isCapturing = false;
88 int m_bufferMillisecondSize = 0;
89
90 float m_decibels = DEFAULT_SILENCE_DECIBELS;
91 bool m_isTestMode = false;
92 FDateTime m_lastVoiceTimestamp = FDateTime();
93
94 mutable FCriticalSection m_captureGuard;
95 TUniquePtr<FVoiceRunnerThread> m_voiceRunnerThread;
96 TWeakPtr<AudioWebSocket> m_webSocket;
97 TSharedPtr<IVoiceCapture> m_voiceCaptureDevice;
98#pragma endregion
99
100#pragma region private API
102 void CaptureAndSendVoiceData();
103
110 bool CaptureVoiceInternal(TArray<uint8>& voiceDataBuffer, float& decibels) const;
111
117 void SendInternal(const TArray<uint8>& rawData) const;
118
127 float AnalyseDecibels(const TArray<uint8>& voiceInputData, uint32 dataSize) const;
128
130 bool IsInputSilentInternal() const;
131#pragma endregion
132};
AudioCaptureHandler.
Definition AudioCaptureHandler.h:19
bool TryInitializeVoiceCapture(int sampleRate=16000, int numChannels=1)
Tries to create a voice capture instance (IVoiceCapture).
Definition AudioCaptureHandler.cpp:18
void RegisterSocket(TWeakPtr< AudioWebSocket > socket, int bufferMillisecondSize)
Store a pointer to the websocket that will receive the parsed data from the FVoiceModule.
Definition AudioCaptureHandler.cpp:12
bool TryStartVoiceCapture()
Tries to start that IVoiceCapture and also start the background thread that will forward any captured...
Definition AudioCaptureHandler.cpp:82
void ShutDown(bool alsoDestroyCaptureDevice=false)
Stops the capture, permanently clearing the reserved memory for the background thread.
Definition AudioCaptureHandler.cpp:141
friend class FVoiceRunnerThread
The background thread needs access to our private functions.
Definition AudioCaptureHandler.h:84
void ConfigureSilenceThresholds(float micNoiseGateThreshold, float silenceDetectionThreshold, float micInputGain)
Configure values to help the microphone to pick up voice without background noise.
Definition AudioCaptureHandler.cpp:63
float GetDecibels() const
Definition AudioCaptureHandler.cpp:230
const FString & GetDeviceName() const
Definition AudioCaptureHandler.cpp:236
void StopCapture()
Stop the voice capture, reset the buffers and stops the background thread.
Definition AudioCaptureHandler.cpp:122
bool IsInputSilent() const
Definition AudioCaptureHandler.cpp:185
void SetIsTestMode(bool isTestMode)
Definition AudioCaptureHandler.cpp:241
AudioWebSocket Main low level API to communicate with the audio streaming socket of VoxtaServer.
Definition AudioWebSocket.h:16