UnrealVoxta 0.1.1
 
Loading...
Searching...
No Matches
VoxtaAudioInput.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"
8#include "AudioWebSocket.h"
9#include "VoxtaAudioInput.generated.h"
10
11class UVoxtaClient;
12
21UCLASS(Category = "Voxta")
22class UNREALVOXTA_API UVoxtaAudioInput : public UObject
23{
24 GENERATED_BODY()
25
26#pragma region delegate declarations
27public:
29 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FVoxtaAudioInputStateChanged, VoxtaMicrophoneState, newState);
31 DECLARE_MULTICAST_DELEGATE_OneParam(FVoxtaAudioInputStateChangedNative, VoxtaMicrophoneState);
32#pragma endregion
33
34#pragma region events
35public:
37 UPROPERTY(BlueprintAssignable, Category = "Voxta", meta = (IsBindableEvent = "True"))
38 FVoxtaAudioInputStateChanged VoxtaAudioInputStateChangedEvent;
39 FVoxtaAudioInputStateChangedNative VoxtaAudioInputStateChangedEventNative;
40#pragma endregion
41
42#pragma region public API
43public:
53 void InitializeSocket(int bufferMs = 200, int sampleRate = 16000, int inputChannels = 1);
54
58 void Cleanup();
59
61 bool IsInitialized() const;
62
65
67 void DisconnectFromChat();
68
74 UFUNCTION(BlueprintCallable, Category = "Voxta")
75 void StartAudioTesting(int sampleRate = 16000, int inputChannels = 1);
76
78 UFUNCTION(BlueprintCallable, Category = "Voxta")
79 void StopAudioTesting();
80
82 UFUNCTION(BlueprintCallable, Category = "Voxta")
83 void StartStreaming(bool isTestMode = false);
84
86 UFUNCTION(BlueprintCallable, Category = "Voxta")
87 void StopStreaming();
88
90 UFUNCTION(BlueprintCallable, Category = "Voxta")
92
94 UFUNCTION(BlueprintPure, Category = "Voxta")
95 bool IsRecording() const;
96
98 UFUNCTION(BlueprintPure, Category = "Voxta")
99 bool IsInputSilent() const;
100
106 UFUNCTION(BlueprintCallable, Category = "Voxta")
107 float GetInputDecibels() const;
108
110 UFUNCTION(BlueprintCallable, Category = "Voxta")
111 const FString& GetInputDeviceName() const;
112
120 UFUNCTION(BlueprintCallable, Category = "Voxta")
121 void ConfigureSilenceThresholds(float micNoiseGateThreshold, float silenceDetectionThreshold, float micInputGain);
122#pragma endregion
123
124#pragma region UObject overrides
126 virtual void BeginDestroy() override;
127#pragma endregion
128
129#pragma region data
130private:
131 UPROPERTY(BlueprintReadOnly, Category = "Voxta", meta = (AllowPrivateAccess = "true", DisplayName = "Current Mic Noise Gate Threshold"))
132 float m_micNoiseGateThreshold = 0.001f;
133
134 UPROPERTY(BlueprintReadOnly, Category = "Voxta", meta = (AllowPrivateAccess = "true", DisplayName = "Current Silence Detection Threshold"))
135 float m_silenceDetectionThreshold = 0.001f;
136
137 UPROPERTY(BlueprintReadOnly, Category = "Voxta", meta = (AllowPrivateAccess = "true", DisplayName = "Current Mic Input Gain"))
138 float m_micInputGain = 6.f;
139
140 int m_bufferMs = 0;
141 int m_sampleRate = 0;
142 int m_inputChannels = 0;
143
144 UPROPERTY()
145 UVoxtaClient* m_voxtaClient = nullptr;
146
147 AudioCaptureHandler m_audioCaptureDevice;
148 TSharedPtr<AudioWebSocket> m_audioWebSocket = nullptr;
150
151 FDelegateHandle m_connectedHandle;
152 FDelegateHandle m_connectionErrorHandle;
153 FDelegateHandle m_closedHandle;
154#pragma endregion
155
156#pragma region private API
157private:
166 void ChatSessionHandshake();
167
168 void UpdateConnectionState(VoxtaMicrophoneState newState);
169
170#pragma region IWebSocket listeners
171private:
173 void OnSocketConnected();
175 void OnSocketConnectionError(const FString& error);
177 void OnSocketClosed(int statusCode, const FString& reason, bool wasClean);
178#pragma endregion
179#pragma endregion
180};
VoxtaMicrophoneState
VoxtaMicrophoneState To keep track of what's going on with the VoxtaAudioInput internals,...
Definition VoxtaMicrophoneState.h:13
@ Uninitialized
Definition VoxtaMicrophoneState.h:14
AudioCaptureHandler.
Definition AudioCaptureHandler.h:19
AudioWebSocket Main low level API to communicate with the audio streaming socket of VoxtaServer.
Definition AudioWebSocket.h:16
UVoxtaAudioInput Main public-facing class responsible for containing all AudioInput related logic.
Definition VoxtaAudioInput.h:23
bool IsRecording() const
Definition VoxtaAudioInput.cpp:161
bool IsInputSilent() const
Definition VoxtaAudioInput.cpp:166
void StartAudioTesting(int sampleRate=16000, int inputChannels=1)
Starts audio testing mode with the given sample rate and input channels.
Definition VoxtaAudioInput.cpp:105
void InitializeSocket(int bufferMs=200, int sampleRate=16000, int inputChannels=1)
Creates the AudioWebSocket and connects audioSocket input hosted on the VoxtaServer's IP and port.
Definition VoxtaAudioInput.cpp:10
void StopAudioTesting()
Stops audio testing mode.
Definition VoxtaAudioInput.cpp:121
const FString & GetInputDeviceName() const
Definition VoxtaAudioInput.cpp:176
void ConnectToCurrentChat()
Connects the audio input to the current chat session.
Definition VoxtaAudioInput.cpp:75
FVoxtaAudioInputStateChanged VoxtaAudioInputStateChangedEvent
Event fired after both the audiosocket and the capturedevice are initialized.
Definition VoxtaAudioInput.h:38
DECLARE_MULTICAST_DELEGATE_OneParam(FVoxtaAudioInputStateChangedNative, VoxtaMicrophoneState)
Native C++ delegate for audio input state changes.
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FVoxtaAudioInputStateChanged, VoxtaMicrophoneState, newState)
Delegate fired when the audio input state changes.
void StartStreaming(bool isTestMode=false)
Starts streaming audio data to the server.
Definition VoxtaAudioInput.cpp:129
void StopStreaming()
Stops streaming audio data to the server.
Definition VoxtaAudioInput.cpp:153
void ConfigureSilenceThresholds(float micNoiseGateThreshold, float silenceDetectionThreshold, float micInputGain)
Configure values to help the microphone to pick up voice without background noise.
Definition VoxtaAudioInput.cpp:181
bool IsInitialized() const
Definition VoxtaAudioInput.cpp:46
float GetInputDecibels() const
Definition VoxtaAudioInput.cpp:171
FVoxtaAudioInputStateChangedNative VoxtaAudioInputStateChangedEventNative
Definition VoxtaAudioInput.h:39
VoxtaMicrophoneState GetCurrentState() const
Definition VoxtaAudioInput.cpp:100
virtual void BeginDestroy() override
Fallback in case the Cleanup was not called, should not be necessary though, but oh well.
Definition VoxtaAudioInput.cpp:190
void DisconnectFromChat()
Disconnects the audio input from the current chat session.
Definition VoxtaAudioInput.cpp:64
void Cleanup()
Used for cleaning up resources, should only be triggered when shutting down the game.
Definition VoxtaAudioInput.cpp:51
UVoxtaClient Main public-facing subsystem for Voxta integration.
Definition VoxtaClient.h:50