Skip to content

Commit 4b9a7ad

Browse files
update samples with com.utilities.audio changes
1 parent c8be826 commit 4b9a7ad

File tree

7 files changed

+88
-234
lines changed

7 files changed

+88
-234
lines changed

OpenAI/Packages/com.openai.unity/Samples~/Assistant/AssistantBehaviour.cs

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OpenAI.Models;
77
using OpenAI.Threads;
88
using System;
9-
using System.Collections.Concurrent;
109
using System.Collections.Generic;
1110
using System.Linq;
1211
using System.Threading;
@@ -23,7 +22,7 @@
2322

2423
namespace OpenAI.Samples.Assistant
2524
{
26-
[RequireComponent(typeof(AudioSource))]
25+
[RequireComponent(typeof(StreamAudioSource))]
2726
public class AssistantBehaviour : MonoBehaviour
2827
{
2928
[SerializeField]
@@ -48,7 +47,7 @@ public class AssistantBehaviour : MonoBehaviour
4847
private ScrollRect scrollView;
4948

5049
[SerializeField]
51-
private AudioSource audioSource;
50+
private StreamAudioSource streamAudioSource;
5251

5352
[SerializeField]
5453
[Obsolete]
@@ -62,12 +61,6 @@ public class AssistantBehaviour : MonoBehaviour
6261
private ThreadResponse thread;
6362
private AssistantResponse assistant;
6463

65-
#if PLATFORM_WEBGL
66-
private readonly ConcurrentQueue<AudioClip> audioQueue = new();
67-
#else
68-
private readonly ConcurrentQueue<float> audioQueue = new();
69-
#endif
70-
7164
#if !UNITY_2022_3_OR_NEWER
7265
private readonly CancellationTokenSource lifetimeCts = new();
7366

@@ -82,9 +75,9 @@ private void OnValidate()
8275
submitButton.Validate();
8376
recordButton.Validate();
8477

85-
if (audioSource == null)
78+
if (streamAudioSource == null)
8679
{
87-
audioSource = GetComponent<AudioSource>();
80+
streamAudioSource = GetComponent<StreamAudioSource>();
8881
}
8982
}
9083

@@ -95,10 +88,7 @@ private async void Awake()
9588
{
9689
EnableDebug = enableDebug
9790
};
98-
//RecordingManager.EnableDebug = enableDebug;
99-
#if PLATFORM_WEBGL
100-
AudioPlaybackLoop();
101-
#endif // PLATFORM_WEBGL
91+
RecordingManager.EnableDebug = enableDebug;
10292

10393
try
10494
{
@@ -171,55 +161,6 @@ private async void Awake()
171161
}
172162
}
173163

174-
#if PLATFORM_WEBGL
175-
private async void AudioPlaybackLoop()
176-
{
177-
try
178-
{
179-
do
180-
{
181-
if (!audioSource.isPlaying &&
182-
audioQueue.TryDequeue(out var clip))
183-
{
184-
if (enableDebug)
185-
{
186-
Debug.Log($"playing partial clip: {clip.name} | ({audioQueue.Count} remaining)");
187-
}
188-
audioSource.PlayOneShot(clip);
189-
// ReSharper disable once MethodSupportsCancellation
190-
await Task.Delay(TimeSpan.FromSeconds(clip.length)).ConfigureAwait(true);
191-
}
192-
else
193-
{
194-
await Task.Yield();
195-
}
196-
} while (!destroyCancellationToken.IsCancellationRequested);
197-
}
198-
catch (Exception e)
199-
{
200-
Debug.LogException(e);
201-
// restart playback loop
202-
AudioPlaybackLoop();
203-
}
204-
}
205-
#else
206-
private void OnAudioFilterRead(float[] data, int channels)
207-
{
208-
if (audioQueue.IsEmpty) { return; }
209-
210-
for (var i = 0; i < data.Length; i += channels)
211-
{
212-
if (audioQueue.TryDequeue(out var sample))
213-
{
214-
for (var j = 0; j < channels; j++)
215-
{
216-
data[i + j] = sample;
217-
}
218-
}
219-
}
220-
}
221-
#endif // PLATFORM_WEBGL
222-
223164
private void OnDestroy()
224165
{
225166
#if !UNITY_2022_3_OR_NEWER
@@ -374,23 +315,16 @@ private async Task GenerateSpeechAsync(string text, CancellationToken cancellati
374315
#pragma warning restore CS0612 // Type or member is obsolete
375316
var speechClip = await openAI.AudioEndpoint.GetSpeechAsync(request, partialClip =>
376317
{
377-
#if PLATFORM_WEBGL
378-
audioQueue.Enqueue(partialClip);
379-
#else
380-
foreach (var sample in partialClip.AudioSamples)
381-
{
382-
audioQueue.Enqueue(sample);
383-
}
384-
#endif // PLATFORM_WEBGL
318+
streamAudioSource.BufferCallback(partialClip.AudioSamples);
385319
}, cancellationToken);
386320

387321
if (enableDebug)
388322
{
389323
Debug.Log(speechClip.CachePath);
390324
}
391325

392-
await new WaitUntil(() => audioQueue.IsEmpty || cancellationToken.IsCancellationRequested);
393-
audioSource.clip = speechClip.AudioClip;
326+
await new WaitUntil(() => streamAudioSource.IsEmpty || cancellationToken.IsCancellationRequested);
327+
((AudioSource)streamAudioSource).clip = speechClip.AudioClip;
394328
}
395329
finally
396330
{

OpenAI/Packages/com.openai.unity/Samples~/Assistant/OpenAIAssistantSample.unity

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ GameObject:
588588
m_Component:
589589
- component: {fileID: 445122399}
590590
- component: {fileID: 445122398}
591+
- component: {fileID: 445122400}
591592
- component: {fileID: 445122397}
592593
m_Layer: 0
593594
m_Name: AssistantBehaviour
@@ -615,7 +616,7 @@ MonoBehaviour:
615616
inputField: {fileID: 1377121433}
616617
contentArea: {fileID: 250955499}
617618
scrollView: {fileID: 1974642466}
618-
audioSource: {fileID: 445122398}
619+
streamAudioSource: {fileID: 445122400}
619620
voice: 0
620621
systemPrompt: 'You are a helpful assistant.
621622
@@ -738,6 +739,19 @@ Transform:
738739
m_Father: {fileID: 0}
739740
m_RootOrder: 4
740741
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
742+
--- !u!114 &445122400
743+
MonoBehaviour:
744+
m_ObjectHideFlags: 0
745+
m_CorrespondingSourceObject: {fileID: 0}
746+
m_PrefabInstance: {fileID: 0}
747+
m_PrefabAsset: {fileID: 0}
748+
m_GameObject: {fileID: 445122396}
749+
m_Enabled: 1
750+
m_EditorHideFlags: 0
751+
m_Script: {fileID: 11500000, guid: 8b2317b29e6eb934f9f1cd50d682e019, type: 3}
752+
m_Name:
753+
m_EditorClassIdentifier:
754+
audioSource: {fileID: 445122398}
741755
--- !u!1 &530667792
742756
GameObject:
743757
m_ObjectHideFlags: 0
@@ -1647,7 +1661,7 @@ RectTransform:
16471661
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
16481662
m_AnchorMin: {x: 0, y: 0}
16491663
m_AnchorMax: {x: 0, y: 0}
1650-
m_AnchoredPosition: {x: 475.67877, y: 0}
1664+
m_AnchoredPosition: {x: 447.4204, y: 0}
16511665
m_SizeDelta: {x: 64, y: 64}
16521666
m_Pivot: {x: 0.5, y: 0.5}
16531667
--- !u!114 &1143678155

OpenAI/Packages/com.openai.unity/Samples~/Chat/ChatBehaviour.cs

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using OpenAI.Images;
66
using OpenAI.Models;
77
using System;
8-
using System.Collections.Concurrent;
98
using System.Collections.Generic;
109
using System.Threading;
1110
using System.Threading.Tasks;
@@ -21,7 +20,7 @@
2120

2221
namespace OpenAI.Samples.Chat
2322
{
24-
[RequireComponent(typeof(AudioSource))]
23+
[RequireComponent(typeof(StreamAudioSource))]
2524
public class ChatBehaviour : MonoBehaviour
2625
{
2726
[SerializeField]
@@ -46,7 +45,7 @@ public class ChatBehaviour : MonoBehaviour
4645
private ScrollRect scrollView;
4746

4847
[SerializeField]
49-
private AudioSource audioSource;
48+
private StreamAudioSource streamAudioSource;
5049

5150
[Obsolete]
5251
[SerializeField]
@@ -61,12 +60,6 @@ public class ChatBehaviour : MonoBehaviour
6160
private readonly Conversation conversation = new();
6261
private readonly List<Tool> assistantTools = new();
6362

64-
#if PLATFORM_WEBGL
65-
private readonly ConcurrentQueue<AudioClip> audioQueue = new();
66-
#else
67-
private readonly ConcurrentQueue<float> audioQueue = new();
68-
#endif
69-
7063
#if !UNITY_2022_3_OR_NEWER
7164
private readonly CancellationTokenSource lifetimeCts = new();
7265
// ReSharper disable once InconsistentNaming
@@ -80,9 +73,9 @@ private void OnValidate()
8073
submitButton.Validate();
8174
recordButton.Validate();
8275

83-
if (audioSource == null)
76+
if (streamAudioSource == null)
8477
{
85-
audioSource = GetComponent<AudioSource>();
78+
streamAudioSource = GetComponent<StreamAudioSource>();
8679
}
8780
}
8881

@@ -93,10 +86,7 @@ private void Awake()
9386
{
9487
EnableDebug = enableDebug
9588
};
96-
//RecordingManager.EnableDebug = enableDebug;
97-
#if PLATFORM_WEBGL
98-
AudioPlaybackLoop();
99-
#endif // PLATFORM_WEBGL
89+
RecordingManager.EnableDebug = enableDebug;
10090

10191
assistantTools.Add(Tool.GetOrCreateTool(openAI.ImagesEndPoint, nameof(ImagesEndpoint.GenerateImageAsync)));
10292
conversation.AppendMessage(new Message(Role.System, systemPrompt));
@@ -105,55 +95,6 @@ private void Awake()
10595
recordButton.onClick.AddListener(ToggleRecording);
10696
}
10797

108-
#if PLATFORM_WEBGL
109-
private async void AudioPlaybackLoop()
110-
{
111-
try
112-
{
113-
do
114-
{
115-
if (!audioSource.isPlaying &&
116-
audioQueue.TryDequeue(out var clip))
117-
{
118-
if (enableDebug)
119-
{
120-
Debug.Log($"playing partial clip: {clip.name} | ({audioQueue.Count} remaining)");
121-
}
122-
audioSource.PlayOneShot(clip);
123-
// ReSharper disable once MethodSupportsCancellation
124-
await Task.Delay(TimeSpan.FromSeconds(clip.length)).ConfigureAwait(true);
125-
}
126-
else
127-
{
128-
await Task.Yield();
129-
}
130-
} while (!destroyCancellationToken.IsCancellationRequested);
131-
}
132-
catch (Exception e)
133-
{
134-
Debug.LogException(e);
135-
// restart playback loop
136-
AudioPlaybackLoop();
137-
}
138-
}
139-
#else
140-
private void OnAudioFilterRead(float[] data, int channels)
141-
{
142-
if (audioQueue.Count <= 0) { return; }
143-
144-
for (var i = 0; i < data.Length; i += channels)
145-
{
146-
if (audioQueue.TryDequeue(out var sample))
147-
{
148-
for (var j = 0; j < channels; j++)
149-
{
150-
data[i + j] = sample;
151-
}
152-
}
153-
}
154-
}
155-
#endif // PLATFORM_WEBGL
156-
15798
#if !UNITY_2022_3_OR_NEWER
15899
private void OnDestroy()
159100
{
@@ -304,23 +245,16 @@ private async Task GenerateSpeechAsync(string text, CancellationToken cancellati
304245
#pragma warning restore CS0612 // Type or member is obsolete
305246
var speechClip = await openAI.AudioEndpoint.GetSpeechAsync(request, partialClip =>
306247
{
307-
#if PLATFORM_WEBGL
308-
audioQueue.Enqueue(partialClip);
309-
#else
310-
foreach (var sample in partialClip.AudioSamples)
311-
{
312-
audioQueue.Enqueue(sample);
313-
}
314-
#endif // PLATFORM_WEBGL
248+
streamAudioSource.BufferCallback(partialClip.AudioSamples);
315249
}, destroyCancellationToken);
316250

317251
if (enableDebug)
318252
{
319253
Debug.Log(speechClip.CachePath);
320254
}
321255

322-
await new WaitUntil(() => audioQueue.IsEmpty || cancellationToken.IsCancellationRequested);
323-
audioSource.clip = speechClip.AudioClip;
256+
await new WaitUntil(() => streamAudioSource.IsEmpty || cancellationToken.IsCancellationRequested);
257+
((AudioSource)streamAudioSource).clip = speechClip.AudioClip;
324258
}
325259

326260
private TextMeshProUGUI AddNewTextMessageContent(Role role)

OpenAI/Packages/com.openai.unity/Samples~/Chat/OpenAIChatSample.unity

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ RectTransform:
14871487
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
14881488
m_AnchorMin: {x: 0, y: 0}
14891489
m_AnchorMax: {x: 0, y: 0}
1490-
m_AnchoredPosition: {x: 475.67877, y: 0}
1490+
m_AnchoredPosition: {x: 447.4204, y: 0}
14911491
m_SizeDelta: {x: 64, y: 64}
14921492
m_Pivot: {x: 0.5, y: 0.5}
14931493
--- !u!114 &1143678155
@@ -2039,6 +2039,7 @@ GameObject:
20392039
m_Component:
20402040
- component: {fileID: 1411251223}
20412041
- component: {fileID: 1411251222}
2042+
- component: {fileID: 1411251224}
20422043
- component: {fileID: 1411251221}
20432044
m_Layer: 0
20442045
m_Name: ChatBehaviour
@@ -2066,7 +2067,7 @@ MonoBehaviour:
20662067
inputField: {fileID: 1377121433}
20672068
contentArea: {fileID: 250955499}
20682069
scrollView: {fileID: 1974642466}
2069-
audioSource: {fileID: 1411251222}
2070+
streamAudioSource: {fileID: 1411251224}
20702071
voice: 0
20712072
systemPrompt: 'You are a helpful assistant.
20722073
@@ -2189,6 +2190,19 @@ Transform:
21892190
m_Father: {fileID: 0}
21902191
m_RootOrder: 4
21912192
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
2193+
--- !u!114 &1411251224
2194+
MonoBehaviour:
2195+
m_ObjectHideFlags: 0
2196+
m_CorrespondingSourceObject: {fileID: 0}
2197+
m_PrefabInstance: {fileID: 0}
2198+
m_PrefabAsset: {fileID: 0}
2199+
m_GameObject: {fileID: 1411251220}
2200+
m_Enabled: 1
2201+
m_EditorHideFlags: 0
2202+
m_Script: {fileID: 11500000, guid: 8b2317b29e6eb934f9f1cd50d682e019, type: 3}
2203+
m_Name:
2204+
m_EditorClassIdentifier:
2205+
audioSource: {fileID: 1411251222}
21922206
--- !u!1 &1466169038
21932207
GameObject:
21942208
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)