Skip to content

Commit 50953d9

Browse files
committed
Add TTS API
1 parent f5cb00f commit 50953d9

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Runtime/OpenAITtsApi.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Globalization;
2+
using System.Text;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Serialization;
5+
using OpenAI;
6+
using UnityEngine;
7+
using UnityEngine.Networking;
8+
9+
namespace OpenAI
10+
{
11+
public class CreateTTSRequest {
12+
public string model { get; set; }
13+
public string input { get; set; }
14+
public string voice { get; set; }
15+
}
16+
17+
public class OpenAITtsApi : OpenAIApi
18+
{
19+
private const string BASE_PATH = "https://api.openai.com/v1";
20+
21+
private Configuration configuration;
22+
private Configuration Configuration
23+
{
24+
get
25+
{
26+
if (configuration == null)
27+
{
28+
configuration = new Configuration();
29+
}
30+
31+
return configuration;
32+
}
33+
}
34+
35+
public OpenAIApiTTS(string apiKey = null, string organization = null)
36+
{
37+
if (apiKey != null)
38+
{
39+
configuration = new Configuration(apiKey, organization);
40+
}
41+
}
42+
43+
/// Used for serializing and deserializing PascalCase request object fields into snake_case format for JSON. Ignores null fields when creating JSON strings.
44+
private readonly JsonSerializerSettings jsonSerializerSettings = new()
45+
{
46+
NullValueHandling = NullValueHandling.Ignore,
47+
ContractResolver = new DefaultContractResolver()
48+
{
49+
NamingStrategy = new CustomNamingStrategy()
50+
},
51+
MissingMemberHandling = MissingMemberHandling.Error,
52+
Culture = CultureInfo.InvariantCulture
53+
};
54+
55+
/// <summary>
56+
/// Create byte array payload from the given request object that contains the parameters.
57+
/// </summary>
58+
/// <param name="request">The request object that contains the parameters of the payload.</param>
59+
/// <typeparam name="T">type of the request object.</typeparam>
60+
/// <returns>Byte array payload.</returns>
61+
private byte[] CreatePayload<T>(T request)
62+
{
63+
var json = JsonConvert.SerializeObject(request, jsonSerializerSettings);
64+
return Encoding.UTF8.GetBytes(json);
65+
}
66+
67+
/// <summary>
68+
/// Turn text into spoken audio.
69+
/// </summary>
70+
/// <param name="request">See <see cref="CreateTTSRequest"/></param>
71+
/// <returns>See <see cref="CreateTTSResponse"/></returns>
72+
public UnityWebRequest CreateTextToSpeechRequest(CreateTTSRequest request)
73+
{
74+
var path = $"{BASE_PATH}/audio/speech";
75+
var payload = CreatePayload(request);
76+
77+
UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.MPEG);
78+
req.method = UnityWebRequest.kHttpVerbPOST;
79+
req.uploadHandler = new UploadHandlerRaw(payload);
80+
req.disposeUploadHandlerOnDispose = true;
81+
req.disposeDownloadHandlerOnDispose = true;
82+
req.SetHeaders(Configuration, ContentType.ApplicationJson);
83+
84+
return req;
85+
}
86+
}
87+
}

Runtime/OpenAITtsApi.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)