From d40a22a0b5fe61ccbbdabba9db583e81ccef1630 Mon Sep 17 00:00:00 2001 From: Darcy Wilson <20220392@murdoch.edu.au> Date: Thu, 22 May 2025 08:55:19 +0800 Subject: [PATCH 1/5] Update WindowsMicrophoneStream.cs NOTES: - Added lines to set the state of the various Booleans used to keep track of the initialisation, streaming and recording of the microphone stream. The way it was set up before, in most instances these Booleans would not change, and portions of the code would never fire off as they relied on them to do so. --- .../UnityAddon/WindowsMicrophoneStream.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs index 795e8416..bfd9a68c 100644 --- a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs +++ b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs @@ -102,7 +102,9 @@ public WindowsMicrophoneStreamErrorCode Initialize(WindowsMicrophoneStreamType s return WindowsMicrophoneStreamErrorCode.Success; } - return (WindowsMicrophoneStreamErrorCode)MicInitializeCustomRate((int)streamType, AudioSettings.outputSampleRate); + initialized = true; + + return (WindowsMicrophoneStreamErrorCode)MicInitializeCustomRate((int)streamType, AudioSettings.outputSampleRate); } private bool paused = false; @@ -122,6 +124,8 @@ public WindowsMicrophoneStreamErrorCode Pause() return WindowsMicrophoneStreamErrorCode.Success; } + paused = true; + return (WindowsMicrophoneStreamErrorCode)MicPause(); } @@ -154,6 +158,8 @@ public WindowsMicrophoneStreamErrorCode Resume() return WindowsMicrophoneStreamErrorCode.Success; } + paused = false; + return (WindowsMicrophoneStreamErrorCode)MicResume(); } @@ -182,6 +188,8 @@ public WindowsMicrophoneStreamErrorCode StartRecording(string fileName, bool pre return WindowsMicrophoneStreamErrorCode.Success; } + recording = true; + return (WindowsMicrophoneStreamErrorCode)MicStartRecording(fileName, preview); } @@ -215,6 +223,8 @@ public WindowsMicrophoneStreamErrorCode StartStream(bool keepData, bool preview) return WindowsMicrophoneStreamErrorCode.Success; } + streaming = true; + return (WindowsMicrophoneStreamErrorCode)MicStartStream(keepData, preview); } @@ -252,6 +262,8 @@ public WindowsMicrophoneStreamErrorCode StopStream() return WindowsMicrophoneStreamErrorCode.Success; } + streaming = false; + return (WindowsMicrophoneStreamErrorCode)MicStopStream(); } @@ -270,6 +282,8 @@ public WindowsMicrophoneStreamErrorCode Uninitialize() return WindowsMicrophoneStreamErrorCode.Success; } + initialized = false; + return (WindowsMicrophoneStreamErrorCode)MicDestroy(); } @@ -312,4 +326,4 @@ public WindowsMicrophoneStreamErrorCode Uninitialize() } #endif // UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_EDITOR_WIN -} +} \ No newline at end of file From 31f3323b65410da66ee37122f0497ba82e3cb101 Mon Sep 17 00:00:00 2001 From: Darcy Wilson <20220392@murdoch.edu.au> Date: Thu, 22 May 2025 09:22:04 +0800 Subject: [PATCH 2/5] Update WindowsMicrophoneStream.cs NOTES: - Changed the `WindowsMicrophoneStreamErrorCode` return types from `Success` to `AlreadyRunning` for `Initialize()` and `AlreadyRecording` for `StartRecording()` if the Booleans tracking those states are already true. Thought it to be more descript than setting everything to `Success` which is technically true as it is working, but it doesn't let the developer know that these states were already running and may cause them to add code where not needed. --- Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs index bfd9a68c..07e6780d 100644 --- a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs +++ b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs @@ -99,7 +99,7 @@ public WindowsMicrophoneStreamErrorCode Initialize(WindowsMicrophoneStreamType s if (initialized) { // The microphone stream is already initialized, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.AlreadyRunning; } initialized = true; @@ -185,7 +185,7 @@ public WindowsMicrophoneStreamErrorCode StartRecording(string fileName, bool pre if (recording) { // The microphone stream is already recording, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.AlreadyRecording; } recording = true; From 319f2b60e19bf243625522b0e0e8d8c8bb6f97de Mon Sep 17 00:00:00 2001 From: Darcy Wilson <20220392@murdoch.edu.au> Date: Thu, 22 May 2025 10:06:09 +0800 Subject: [PATCH 3/5] Added New Microphone Stream Error Codes NOTES: - Added new stream error codes to better indicate to the developer that a microphone stream state has or has not already started. Similar reasoning to the previous push, setting everything as `Success` may put the developer in a false sense of security that their code is doing what they are thinking, and causes them to add extra lines where it may not be needed in some instances. This makes it more clear as to what is happening and doesn't necessarily stop their code from running, but allows them to better debug and bug fix. - Added the new enums in at lower values than what was already there. Wanted to have them in order of how they appear in `WindowsMicrophoneStream.cs` but didn't want to change said order and end up breaking people's code that may be relying on the actual integer values rather than just the enums. It may not be that likely, but this at least keeps compatibility for those people in those niche cases - Added new enums to `ErrorCodes` in `MicStreamSelector.cpp` and Booleans to allow for this new error code data to be properly captured by Unity. Different functions will output these new integer values given by the enums and set Booleans to keep track of these states. - Added XML comments for documentation --- .../Source/MicStreamSelector.cpp | 35 ++++++++++++++++++- .../UnityAddon/WindowsMicrophoneStream.cs | 10 +++--- .../WindowsMicrophoneStreamErrorCodes.cs | 27 +++++++++++++- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/Input/MicStreamSelector/Source/MicStreamSelector.cpp b/Input/MicStreamSelector/Source/MicStreamSelector.cpp index 38f0a9ff..a28c8df4 100644 --- a/Input/MicStreamSelector/Source/MicStreamSelector.cpp +++ b/Input/MicStreamSelector/Source/MicStreamSelector.cpp @@ -47,9 +47,12 @@ static char filepath_char[MAX_PATH]; // stores the fu static std::queue audioqueue; // stores our microphone data to hand back to the app // error codes to hand back to engine with nice printed output -private enum ErrorCodes { ALREADY_RUNNING = -10, NO_AUDIO_DEVICE, NO_INPUT_DEVICE, ALREADY_RECORDING, GRAPH_NOT_EXIST, CHANNEL_COUNT_MISMATCH, FILE_CREATION_PERMISSION_ERROR, NOT_ENOUGH_DATA, NEED_ENABLED_MIC_CAPABILITY }; +private enum ErrorCodes { ALREADY_PAUSED = -15, ALREADY_RESUMED = -14, ALREADY_STREAMING = -13, NOT_STREAMING = -12, NOT_RUNNING = -11, ALREADY_RUNNING = -10, NO_AUDIO_DEVICE, NO_INPUT_DEVICE, ALREADY_RECORDING, GRAPH_NOT_EXIST, CHANNEL_COUNT_MISMATCH, FILE_CREATION_PERMISSION_ERROR, NOT_ENOUGH_DATA, NEED_ENABLED_MIC_CAPABILITY }; +bool initialized; bool recording; +bool streaming; +bool paused; int appExpectedBufferLength; int dataInBuffer; int samplesPerQuantum; // preferred sample size per chunk of data from the audio driver. usually you want system default. @@ -57,7 +60,10 @@ int numChannels; int indexInFrame; // keeps track of copying data out of the plug-in and into the application void resetToDefaults() { // used on init to reset state of the plug-in + initialized = false; recording = false; + streaming = false; + paused = false; appExpectedBufferLength = -1; // By making negative, we won't output data until we get at least one app call. dataInBuffer = 0; samplesPerQuantum = 256; @@ -204,6 +210,8 @@ extern "C" // Make a callback at the end of every frame to store our data until the app wants it. graph->QuantumProcessed += ref new Windows::Foundation::TypedEventHandler(&OnQuantumProcessed); + initialized = true; + return 0; } @@ -224,6 +232,10 @@ extern "C" API int MicStartStream(bool keepData, bool previewOnDevice, CallbackIntoHost cb) { + if (streaming) + { + return ErrorCodes::ALREADY_STREAMING; + } if (!graph) { int err = MicInitializeDefault(0); @@ -243,17 +255,23 @@ extern "C" } hostCallback = cb; graph->Start(); + streaming = true; return 0; } API int MicStopStream() { + if (!streaming) + { + return ErrorCodes::NOT_STREAMING; + } if (!graph) { return ErrorCodes::GRAPH_NOT_EXIST; } graph->Stop(); hostCallback = nullptr; + streaming = false; return 0; } @@ -384,20 +402,30 @@ extern "C" } API int MicPause() { + if (paused) + { + return ErrorCodes::ALREADY_PAUSED; + } if (!graph) { return ErrorCodes::GRAPH_NOT_EXIST; } graph->Stop(); + paused = true; return 0; } API int MicResume() { + if (!paused) + { + return ErrorCodes::ALREADY_RESUMED; + } if (!graph) { return ErrorCodes::GRAPH_NOT_EXIST; } graph->Start(); + paused = false; return 0; } @@ -415,6 +443,10 @@ extern "C" API int MicDestroy() { + if (!initialized) + { + return ErrorCodes::NOT_RUNNING; + } if (!graph) // If there isn't a graph, there is nothing to stop, so just return { return ErrorCodes::GRAPH_NOT_EXIST; @@ -443,6 +475,7 @@ extern "C" #ifdef MEMORYLEAKDETECT _CrtDumpMemoryLeaks(); // output our memory stats if desired #endif // MEMORYLEAKDETECT + initialized = false; return 0; } diff --git a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs index 07e6780d..04c0f0e5 100644 --- a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs +++ b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs @@ -121,7 +121,7 @@ public WindowsMicrophoneStreamErrorCode Pause() if (paused) { // The microphone stream is already paused, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.AlreadyPaused; } paused = true; @@ -155,7 +155,7 @@ public WindowsMicrophoneStreamErrorCode Resume() if (!paused) { // The microphone stream is already resumed, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.AlreadyResumed; } paused = false; @@ -220,7 +220,7 @@ public WindowsMicrophoneStreamErrorCode StartStream(bool keepData, bool preview) if (streaming) { // The microphone stream is already streaming, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.NotStreaming; } streaming = true; @@ -259,7 +259,7 @@ public WindowsMicrophoneStreamErrorCode StopStream() if (!streaming) { // The microphone stream is already stopped, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.AlreadyStoppedStream; } streaming = false; @@ -279,7 +279,7 @@ public WindowsMicrophoneStreamErrorCode Uninitialize() if (!initialized) { // The microphone stream is not initialized, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.Success; + return WindowsMicrophoneStreamErrorCode.NotRunning; } initialized = false; diff --git a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStreamErrorCodes.cs b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStreamErrorCodes.cs index 91e08b50..e72081d7 100644 --- a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStreamErrorCodes.cs +++ b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStreamErrorCodes.cs @@ -13,6 +13,31 @@ public enum WindowsMicrophoneStreamErrorCode /// Success = 0, + /// + /// The microphone stream has already been paused. + /// + AlreadyPaused = -15, + + /// + /// The microphone stream has already been resumed. + /// + AlreadyResumed = -14, + + /// + /// The microphone stream has already been started. + /// + AlreadyStreaming = -13, + + /// + /// The microphone stream has already been stopped. + /// + NotStreaming = -12, + + /// + /// The microphone is already uninitialized. + /// + NotRunning = -11, + /// /// The microphone has already been initialized. /// @@ -58,4 +83,4 @@ public enum WindowsMicrophoneStreamErrorCode /// NeedMicCapabilityEnabled } -} +} \ No newline at end of file From 95faef5800b2ef08b6847b04a87f52cd11419991 Mon Sep 17 00:00:00 2001 From: Darcy Wilson <20220392@murdoch.edu.au> Date: Thu, 22 May 2025 10:22:28 +0800 Subject: [PATCH 4/5] Fixed Incorrect Error Codes NOTES: - Error codes were using old names, or were incorrect. Have been corrected to reflect their current names and to use the error code appropriate the state it is reporting on --- Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs index 04c0f0e5..b95519da 100644 --- a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs +++ b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs @@ -220,7 +220,7 @@ public WindowsMicrophoneStreamErrorCode StartStream(bool keepData, bool preview) if (streaming) { // The microphone stream is already streaming, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.NotStreaming; + return WindowsMicrophoneStreamErrorCode.AlreadyStreaming; } streaming = true; @@ -259,7 +259,7 @@ public WindowsMicrophoneStreamErrorCode StopStream() if (!streaming) { // The microphone stream is already stopped, no need to alarm the calling code. - return WindowsMicrophoneStreamErrorCode.AlreadyStoppedStream; + return WindowsMicrophoneStreamErrorCode.NotStreaming; } streaming = false; From 2ae076c4c3b1c7033ba4a25d95e39476d433d452 Mon Sep 17 00:00:00 2001 From: Darcy Wilson <20220392@murdoch.edu.au> Date: Mon, 26 May 2025 09:08:13 +0800 Subject: [PATCH 5/5] Removed + Updated Comments NOTES: - Removed the old comments from the if-statement checks in each of the methods that had them as they are no longer just returning `Success` error codes - Updated the XML comments for the methods with the if-statement checks that had their comments removed. Noted that the `WindowsMicrophoneStreamErrorCode` can either indicate success, failure, or a reason why it did not progress past the if-statement check --- .../UnityAddon/WindowsMicrophoneStream.cs | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs index b95519da..acb0e644 100644 --- a/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs +++ b/Input/MicStreamSelector/UnityAddon/WindowsMicrophoneStream.cs @@ -91,14 +91,14 @@ public float Gain /// Initializes the microphone stream. /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// public WindowsMicrophoneStreamErrorCode Initialize(WindowsMicrophoneStreamType streamType) { if (initialized) { - // The microphone stream is already initialized, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.AlreadyRunning; } @@ -113,14 +113,14 @@ public WindowsMicrophoneStreamErrorCode Initialize(WindowsMicrophoneStreamType s /// Pauses the microphone stream. /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// public WindowsMicrophoneStreamErrorCode Pause() { if (paused) { - // The microphone stream is already paused, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.AlreadyPaused; } @@ -135,8 +135,9 @@ public WindowsMicrophoneStreamErrorCode Pause() /// The buffer in which to plce the data. /// The number of audio channels to read. /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// public WindowsMicrophoneStreamErrorCode ReadAudioFrame(float[] buffer, int numChannels) { @@ -147,14 +148,14 @@ public WindowsMicrophoneStreamErrorCode ReadAudioFrame(float[] buffer, int numCh /// Resumes the microphone stream /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// public WindowsMicrophoneStreamErrorCode Resume() { if (!paused) { - // The microphone stream is already resumed, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.AlreadyResumed; } @@ -174,8 +175,9 @@ public WindowsMicrophoneStreamErrorCode Resume() /// audio device. /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// /// /// Files are created in the Music Library folder. @@ -184,7 +186,6 @@ public WindowsMicrophoneStreamErrorCode StartRecording(string fileName, bool pre { if (recording) { - // The microphone stream is already recording, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.AlreadyRecording; } @@ -207,8 +208,9 @@ public WindowsMicrophoneStreamErrorCode StartRecording(string fileName, bool pre /// audio device. /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// /// /// When keepData is set to false, the application will always receive the latest @@ -219,7 +221,6 @@ public WindowsMicrophoneStreamErrorCode StartStream(bool keepData, bool preview) { if (streaming) { - // The microphone stream is already streaming, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.AlreadyStreaming; } @@ -251,14 +252,14 @@ public string StopRecording() /// Stops the microphone stream. /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// public WindowsMicrophoneStreamErrorCode StopStream() { if (!streaming) { - // The microphone stream is already stopped, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.NotStreaming; } @@ -271,14 +272,14 @@ public WindowsMicrophoneStreamErrorCode StopStream() /// Uninitializes the microphone stream. /// /// - /// A value indicating success or the - /// reason that the call failed. + /// A value indicating success, the + /// reason that the call failed or the reason why the method did not continue past + /// the if-statement check. /// public WindowsMicrophoneStreamErrorCode Uninitialize() { if (!initialized) { - // The microphone stream is not initialized, no need to alarm the calling code. return WindowsMicrophoneStreamErrorCode.NotRunning; }