Skip to content

Commit

Permalink
Endless loop microphone and memory leak fix (Macoron#55)
Browse files Browse the repository at this point in the history
* Started working on a loop microphone

* Clean up

* Fixed memory leak

* Comments

* Stuff

* Some params changes

* Updated chunk logic

* Minor changes
  • Loading branch information
Macoron authored Sep 14, 2023
1 parent 8725359 commit a64d182
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 78 deletions.
19 changes: 10 additions & 9 deletions Assets/Samples/2 - Microphone/2 - Microphone.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0.000030517578, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &720715772
Expand Down Expand Up @@ -3102,22 +3102,23 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3bc03a4c19604ea394e364f8fc632928, type: 3}
m_Name:
m_EditorClassIdentifier:
maxLengthSec: 30
loop: 1
maxLengthSec: 60
frequency: 16000
chunksLengthSec: 0.5
echo: 0
echo: 1
useVad: 1
vadUpdateRateSec: 0.1
vadContextSec: 30
vadLastSec: 1.25
vadThd: 0.6
vadThd: 1
vadFreqThd: 100
vadIndicatorImage: {fileID: 1756544758}
vadStop: 0
dropVadPart: 1
vadStopTime: 1.5
vadStopTime: 3
microphoneDropdown: {fileID: 948466958}
microphoneDefaultLabel: Default mic
microphoneDefaultLabel: Default microphone
--- !u!1 &1345287211
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -3201,7 +3202,7 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 967652833}
m_HandleRect: {fileID: 967652832}
m_Direction: 2
m_Value: 1
m_Value: 0
m_Size: 1
m_NumberOfSteps: 0
m_OnValueChanged:
Expand Down Expand Up @@ -4167,7 +4168,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0.000030517578, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1603953686
Expand Down Expand Up @@ -4253,7 +4254,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -0.00012207031, y: 0}
m_AnchoredPosition: {x: -0.00024414062, y: 0}
m_SizeDelta: {x: 55.6282, y: 55.6282}
m_Pivot: {x: 1, y: 1}
--- !u!114 &1756544758
Expand Down
6 changes: 3 additions & 3 deletions Assets/Samples/2 - Microphone/MicrophoneDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ private void OnButtonPressed()
}
}

private async void OnRecordStop(float[] data, int frequency, int channels, float length)
private async void OnRecordStop(AudioChunk recordedAudio)
{
buttonText.text = "Record";
_buffer = "";

var sw = new Stopwatch();
sw.Start();

var res = await whisper.GetTextAsync(data, frequency, channels);
var res = await whisper.GetTextAsync(recordedAudio.Data, recordedAudio.Frequency, recordedAudio.Channels);
if (res == null || !outputText)
return;

var time = sw.ElapsedMilliseconds;
var rate = length / (time * 0.001f);
var rate = recordedAudio.Length / (time * 0.001f);
timeText.text = $"Time: {time} ms\nRate: {rate:F1}x";

var text = res.Result;
Expand Down
13 changes: 7 additions & 6 deletions Assets/Samples/5 - Streaming/5 - Streaming.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1874,22 +1874,23 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3bc03a4c19604ea394e364f8fc632928, type: 3}
m_Name:
m_EditorClassIdentifier:
maxLengthSec: 240
maxLengthSec: 60
loop: 1
frequency: 16000
chunksLengthSec: 0.2
echo: 1
chunksLengthSec: 0.5
echo: 0
useVad: 1
vadUpdateRateSec: 0.1
vadContextSec: 30
vadLastSec: 1.25
vadThd: 0.6
vadThd: 1
vadFreqThd: 100
vadIndicatorImage: {fileID: 1303962929}
vadStop: 0
dropVadPart: 1
vadStopTime: 3
microphoneDropdown: {fileID: 948466958}
microphoneDefaultLabel: Default mic
microphoneDefaultLabel: Default microphone
--- !u!114 &1337591124
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1991,7 +1992,7 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 967652833}
m_HandleRect: {fileID: 967652832}
m_Direction: 2
m_Value: 1
m_Value: 0
m_Size: 1
m_NumberOfSteps: 0
m_OnValueChanged:
Expand Down
2 changes: 1 addition & 1 deletion Assets/Samples/5 - Streaming/StreamingSampleMic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private void OnButtonPressed()
buttonText.text = microphoneRecord.IsRecording ? "Stop" : "Record";
}

private void OnRecordStop(float[] data, int frequency, int channels, float length)
private void OnRecordStop(AudioChunk recordedAudio)
{
buttonText.text = "Record";
}
Expand Down
Loading

0 comments on commit a64d182

Please sign in to comment.