From b8351ccbba306f662015489298ba024504fae1f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20Sch=C3=A4fer?=
 <pschaefer@ITA.AKUSTIK.RWTH-AACHEN.DE>
Date: Fri, 13 Jan 2023 16:46:31 +0100
Subject: [PATCH] Adding signal source representing input channel of the audio
 device: - added respective signal source and sound source class - signal
 source class allows specifying input channel - added VA function to get
 signal source ID based on input channel - TODO: Check if input channel really
 exists

---
 .../VAAudioInputSignalSource.cpp              | 26 +++++++++++++++++
 .../VAAudioInputSourceComponent.cpp           | 12 ++++++++
 Source/VAPlugin/Private/VAPlugin.cpp          | 19 +++++++++++++
 Source/VAPlugin/Private/VAPlugin.h            |  3 ++
 .../SignalSources/VAAudioInputSignalSource.h  | 28 +++++++++++++++++++
 .../SoundSource/VAAudioInputSourceComponent.h | 20 +++++++++++++
 6 files changed, 108 insertions(+)
 create mode 100644 Source/VAPlugin/Private/SignalSources/VAAudioInputSignalSource.cpp
 create mode 100644 Source/VAPlugin/Private/SoundSource/VAAudioInputSourceComponent.cpp
 create mode 100644 Source/VAPlugin/Public/SignalSources/VAAudioInputSignalSource.h
 create mode 100644 Source/VAPlugin/Public/SoundSource/VAAudioInputSourceComponent.h

diff --git a/Source/VAPlugin/Private/SignalSources/VAAudioInputSignalSource.cpp b/Source/VAPlugin/Private/SignalSources/VAAudioInputSignalSource.cpp
new file mode 100644
index 0000000..cc23896
--- /dev/null
+++ b/Source/VAPlugin/Private/SignalSources/VAAudioInputSignalSource.cpp
@@ -0,0 +1,26 @@
+// Fill out your copyright notice in the Description page of Project Settings.
+
+
+#include "SignalSources/VAAudioInputSignalSource.h"
+
+#include "VAUtils.h"
+#include "VAPlugin.h"
+
+void UVAAudioInputSignalSource::Initialize()
+{
+	if (bInitialized)
+	{
+		FVAUtils::LogStuff("[UVAAudioInputSignalSource::Initialize()]: Signal source is already initialized, aborting...", true);
+		return;
+	}
+
+
+	ID = FVAPlugin::GetAudioInputSignalSourceID(Channel);
+	if (!IsValidID(ID))
+	{
+		FVAUtils::LogStuff("[UVAAudioInputSignalSource::Initialize()]: Error initializing Audio Input Signal Source", true);
+		return;
+	}
+
+	bInitialized = true;
+}
diff --git a/Source/VAPlugin/Private/SoundSource/VAAudioInputSourceComponent.cpp b/Source/VAPlugin/Private/SoundSource/VAAudioInputSourceComponent.cpp
new file mode 100644
index 0000000..9624a50
--- /dev/null
+++ b/Source/VAPlugin/Private/SoundSource/VAAudioInputSourceComponent.cpp
@@ -0,0 +1,12 @@
+// Fill out your copyright notice in the Description page of Project Settings.
+
+
+#include "SoundSource/VAAudioInputSourceComponent.h"
+
+#include "SignalSources/VAAudioInputSignalSource.h"
+
+
+UVAAudioInputSourceComponent::UVAAudioInputSourceComponent() : Super()
+{
+	SignalSource = CreateDefaultSubobject<UVAAudioInputSignalSource>("AudioInputSignalSource");
+}
diff --git a/Source/VAPlugin/Private/VAPlugin.cpp b/Source/VAPlugin/Private/VAPlugin.cpp
index 87eeda9..11da190 100644
--- a/Source/VAPlugin/Private/VAPlugin.cpp
+++ b/Source/VAPlugin/Private/VAPlugin.cpp
@@ -557,6 +557,25 @@ bool FVAPlugin::SetSignalSourceBufferLooping(const std::string& SignalSourceID,
 	}
 }
 
+std::string FVAPlugin::GetAudioInputSignalSourceID(const int Channel)
+{
+	if (!ShouldInteractWithServer())
+		return VA_SLAVE_ID_STRING;
+
+	const std::string SignalSourceID = "audioinput" + std::to_string(Channel);
+
+	//TODO: Check if signal source really exist, otherwise return invalid ID
+	
+	//PSC: I was trying to use CVASignalSourceInfo for that. But when the local variable is destroyed, the program crashes.
+	//std::vector<CVASignalSourceInfo> Infos;
+	//VAServer->GetSignalSourceInfos(Infos);
+	//CVASignalSourceInfo Info = VAServer->GetSignalSourceInfo(SignalSourceID);
+	//if (false)
+	//	return VA_INVALID_ID_STRING;
+
+	return SignalSourceID;
+}
+
 std::string FVAPlugin::CreateSignalSourcePrototype(UVAAbstractSignalSource* SignalSource)
 {
 	if (!ShouldInteractWithServer())
diff --git a/Source/VAPlugin/Private/VAPlugin.h b/Source/VAPlugin/Private/VAPlugin.h
index 9fa2bcf..bbd17a7 100644
--- a/Source/VAPlugin/Private/VAPlugin.h
+++ b/Source/VAPlugin/Private/VAPlugin.h
@@ -76,6 +76,9 @@ public:
 	static bool SetSignalSourceBufferPlaybackPosition(const std::string& SignalSourceID, float Time);
 	static bool SetSignalSourceBufferLooping(const std::string& SignalSourceID, bool bLoop);
 
+	// Returns the ID of the signal source referring the given audio input channel. Invalid ID, if channel does not exist
+	static std::string GetAudioInputSignalSourceID(const int Channel);
+
 	static std::string CreateSignalSourcePrototype(UVAAbstractSignalSource* SignalSource);
 	// Deletes a signal source with given ID. Use with great care!
 	static bool DeleteSignalSource(const std::string& SignalSourceID);
diff --git a/Source/VAPlugin/Public/SignalSources/VAAudioInputSignalSource.h b/Source/VAPlugin/Public/SignalSources/VAAudioInputSignalSource.h
new file mode 100644
index 0000000..603e4d1
--- /dev/null
+++ b/Source/VAPlugin/Public/SignalSources/VAAudioInputSignalSource.h
@@ -0,0 +1,28 @@
+// Fill out your copyright notice in the Description page of Project Settings.
+
+#pragma once
+
+#include "CoreMinimal.h"
+#include "SignalSources/VAAbstractSignalSource.h"
+#include "VAAudioInputSignalSource.generated.h"
+
+/**
+ * 
+ */
+UCLASS(ClassGroup = (VA))
+class VAPLUGIN_API UVAAudioInputSignalSource : public UVAAbstractSignalSource
+{
+	GENERATED_BODY()
+
+protected:
+	// Input channel used to stream into signal source
+	UPROPERTY(EditAnywhere, meta = (DisplayName = "Input Channel ID", Category = "Audio Input", ClampMin = "1"))
+		int Channel = 1;
+
+public:
+	UVAAudioInputSignalSource() = default;
+
+	// Creates the signal source in VA and sets the ID accordingly
+	void Initialize() override;
+
+};
diff --git a/Source/VAPlugin/Public/SoundSource/VAAudioInputSourceComponent.h b/Source/VAPlugin/Public/SoundSource/VAAudioInputSourceComponent.h
new file mode 100644
index 0000000..8c97ff8
--- /dev/null
+++ b/Source/VAPlugin/Public/SoundSource/VAAudioInputSourceComponent.h
@@ -0,0 +1,20 @@
+// Fill out your copyright notice in the Description page of Project Settings.
+
+#pragma once
+
+#include "CoreMinimal.h"
+#include "SoundSource/VAAbstractSourceComponent.h"
+#include "VAAudioInputSourceComponent.generated.h"
+
+/**
+ * 
+ */
+UCLASS(ClassGroup = (VA), meta = (BlueprintSpawnableComponent))
+class VAPLUGIN_API UVAAudioInputSourceComponent : public UVAAbstractSourceComponent
+{
+	GENERATED_BODY()
+public:
+
+	UVAAudioInputSourceComponent();
+
+};
-- 
GitLab