7
7
using System . Threading ;
8
8
using System . Threading . Tasks ;
9
9
using UnityEngine ;
10
- using UnityEngine . Networking ;
11
10
using Utilities . WebRequestRest ;
12
11
13
12
namespace OpenAI . Audio
@@ -27,25 +26,29 @@ internal AudioEndpoint(OpenAIClient client) : base(client) { }
27
26
28
27
private static readonly object mutex = new ( ) ;
29
28
30
- /// <summary>
31
- /// Generates audio from the input text.
32
- /// </summary>
33
- /// <param name="request"><see cref="SpeechRequest"/>.</param>
34
- /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
35
- /// <returns><see cref="AudioClip"/> and the cached path.</returns>
36
- [ Function ( "Generates audio from the input text." ) ]
29
+ [ Obsolete ( "use GetSpeechAsync" ) ]
37
30
public async Task < Tuple < string , AudioClip > > CreateSpeechAsync ( SpeechRequest request , CancellationToken cancellationToken = default )
38
31
=> await CreateSpeechStreamAsync ( request , null , cancellationToken ) ;
39
32
33
+ [ Obsolete ( "use GetSpeechAsync" ) ]
34
+ public async Task < Tuple < string , AudioClip > > CreateSpeechStreamAsync ( SpeechRequest request , Action < AudioClip > partialClipCallback , CancellationToken cancellationToken = default )
35
+ {
36
+ var result = await GetSpeechAsync ( request , speechClip =>
37
+ {
38
+ partialClipCallback . Invoke ( speechClip . AudioClip ) ;
39
+ } , cancellationToken ) ;
40
+ return Tuple . Create ( result . CachePath , result . AudioClip ) ;
41
+ }
42
+
40
43
/// <summary>
41
- /// Generates streaming audio from the input text.
44
+ /// Generates audio from the input text.
42
45
/// </summary>
43
46
/// <param name="request"><see cref="SpeechRequest"/>.</param>
44
- /// <param name="partialClipCallback">Optional, partial <see cref="AudioClip "/> callback used to stream audio.</param>
47
+ /// <param name="partialClipCallback">Optional, partial <see cref="SpeechClip "/> callback used to stream audio.</param>
45
48
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
46
- /// <returns><see cref="AudioClip "/> and the cached path. </returns>
47
- [ Function ( "Generates streaming audio from the input text." ) ]
48
- public async Task < Tuple < string , AudioClip > > CreateSpeechStreamAsync ( SpeechRequest request , Action < AudioClip > partialClipCallback , CancellationToken cancellationToken = default )
49
+ /// <returns><see cref="SpeechClip "/></returns>
50
+ [ Function ( "Generates audio from the input text." ) ]
51
+ public async Task < SpeechClip > GetSpeechAsync ( SpeechRequest request , Action < SpeechClip > partialClipCallback = null , CancellationToken cancellationToken = default )
49
52
{
50
53
if ( partialClipCallback != null && request . ResponseFormat != SpeechResponseFormat . PCM )
51
54
{
@@ -70,52 +73,16 @@ public async Task<Tuple<string, AudioClip>> CreateSpeechStreamAsync(SpeechReques
70
73
71
74
Rest . TryGetDownloadCacheItem ( clipName , out var cachedPath ) ;
72
75
73
- if ( request . ResponseFormat == SpeechResponseFormat . PCM )
74
- {
75
- var part = 0 ;
76
- var response = await Rest . PostAsync (
77
- GetUrl ( "/speech" ) ,
78
- payload ,
79
- StreamCallback ,
80
- eventChunkSize : 8192 ,
81
- new RestParameters ( client . DefaultRequestHeaders ) ,
82
- cancellationToken ) ;
83
- response . Validate ( EnableDebug ) ;
84
- var samples = Utilities . Audio . PCMEncoder . Decode ( response . Data ) ;
85
- await File . WriteAllBytesAsync ( cachedPath , response . Data , cancellationToken ) . ConfigureAwait ( true ) ;
86
- return new Tuple < string , AudioClip > ( cachedPath , AudioClip . Create ( clipName , samples . Length , 1 , 24000 , false ) ) ;
87
-
88
- void StreamCallback ( Response partialResponse )
89
- {
90
- var chunk = Utilities . Audio . PCMEncoder . Decode ( partialResponse . Data ) ;
91
- var partialClip = AudioClip . Create ( $ "{ clipName } _{ ++ part } ", chunk . Length , 1 , 24000 , false ) ;
92
-
93
- if ( ! partialClip . SetData ( chunk , 0 ) )
94
- {
95
- Debug . LogError ( "Failed to set pcm data to partial clip." ) ;
96
- return ;
97
- }
98
-
99
- partialClipCallback ? . Invoke ( partialClip ) ;
100
- }
101
- }
76
+ var part = 0 ;
77
+ var pcmResponse = await Rest . PostAsync ( GetUrl ( "/speech" ) , payload , StreamCallback , 8192 , new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
78
+ pcmResponse . Validate ( EnableDebug ) ;
79
+ await File . WriteAllBytesAsync ( cachedPath , pcmResponse . Data , cancellationToken ) . ConfigureAwait ( true ) ;
80
+ return new SpeechClip ( clipName , cachedPath , new ReadOnlyMemory < byte > ( pcmResponse . Data ) ) ;
102
81
103
- var audioFormat = request . ResponseFormat switch
82
+ void StreamCallback ( Response partialResponse )
104
83
{
105
- SpeechResponseFormat . MP3 => AudioType . MPEG ,
106
- SpeechResponseFormat . WAV => AudioType . WAV ,
107
- _ => throw new NotSupportedException ( request . ResponseFormat . ToString ( ) )
108
- } ;
109
-
110
- var clip = await Rest . DownloadAudioClipAsync (
111
- GetUrl ( "/speech" ) ,
112
- audioFormat ,
113
- UnityWebRequest . kHttpVerbPOST ,
114
- clipName ,
115
- payload ,
116
- parameters : new RestParameters ( client . DefaultRequestHeaders , debug : EnableDebug ) ,
117
- cancellationToken : cancellationToken ) ;
118
- return new Tuple < string , AudioClip > ( cachedPath , clip ) ;
84
+ partialClipCallback ? . Invoke ( new SpeechClip ( $ "{ clipName } _{ ++ part } ", null , partialResponse . Data ) ) ;
85
+ }
119
86
}
120
87
121
88
/// <summary>
0 commit comments