Skip to content

Commit 4afca38

Browse files
committed
Code refactoring
1 parent d3b5bc0 commit 4afca38

File tree

6 files changed

+65
-70
lines changed

6 files changed

+65
-70
lines changed

Assets/Prefabs/AudioPlayer.prefab

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
%YAML 1.1
22
%TAG !u! tag:unity3d.com,2011:
3-
--- !u!1 &2921076125238579620
3+
--- !u!1 &3037125951106697557
44
GameObject:
55
m_ObjectHideFlags: 0
66
m_CorrespondingSourceObject: {fileID: 0}
77
m_PrefabInstance: {fileID: 0}
88
m_PrefabAsset: {fileID: 0}
99
serializedVersion: 6
1010
m_Component:
11-
- component: {fileID: 6297796333276587174}
12-
- component: {fileID: 370320316426281004}
11+
- component: {fileID: 1538069456119997744}
12+
- component: {fileID: 6876963316446148506}
13+
- component: {fileID: 2775129636596995968}
1314
m_Layer: 0
14-
m_Name: Audio Source
15+
m_Name: AudioPlayer
1516
m_TagString: Untagged
1617
m_Icon: {fileID: 0}
1718
m_NavMeshLayer: 0
1819
m_StaticEditorFlags: 0
1920
m_IsActive: 1
20-
--- !u!4 &6297796333276587174
21+
--- !u!4 &1538069456119997744
2122
Transform:
2223
m_ObjectHideFlags: 0
2324
m_CorrespondingSourceObject: {fileID: 0}
2425
m_PrefabInstance: {fileID: 0}
2526
m_PrefabAsset: {fileID: 0}
26-
m_GameObject: {fileID: 2921076125238579620}
27+
m_GameObject: {fileID: 3037125951106697557}
2728
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
2829
m_LocalPosition: {x: 0, y: 0, z: 0}
2930
m_LocalScale: {x: 1, y: 1, z: 1}
3031
m_ConstrainProportionsScale: 0
3132
m_Children: []
32-
m_Father: {fileID: 1538069456119997744}
33+
m_Father: {fileID: 0}
3334
m_RootOrder: -1
3435
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
35-
--- !u!82 &370320316426281004
36+
--- !u!114 &6876963316446148506
37+
MonoBehaviour:
38+
m_ObjectHideFlags: 0
39+
m_CorrespondingSourceObject: {fileID: 0}
40+
m_PrefabInstance: {fileID: 0}
41+
m_PrefabAsset: {fileID: 0}
42+
m_GameObject: {fileID: 3037125951106697557}
43+
m_Enabled: 1
44+
m_EditorHideFlags: 0
45+
m_Script: {fileID: 11500000, guid: b7696d676533d4bf5b038e939d083a07, type: 3}
46+
m_Name:
47+
m_EditorClassIdentifier:
48+
audioSource: {fileID: 0}
49+
--- !u!82 &2775129636596995968
3650
AudioSource:
3751
m_ObjectHideFlags: 0
3852
m_CorrespondingSourceObject: {fileID: 0}
3953
m_PrefabInstance: {fileID: 0}
4054
m_PrefabAsset: {fileID: 0}
41-
m_GameObject: {fileID: 2921076125238579620}
55+
m_GameObject: {fileID: 3037125951106697557}
4256
m_Enabled: 1
4357
serializedVersion: 4
4458
OutputAudioMixerGroup: {fileID: 0}
@@ -128,49 +142,3 @@ AudioSource:
128142
m_PreInfinity: 2
129143
m_PostInfinity: 2
130144
m_RotationOrder: 4
131-
--- !u!1 &3037125951106697557
132-
GameObject:
133-
m_ObjectHideFlags: 0
134-
m_CorrespondingSourceObject: {fileID: 0}
135-
m_PrefabInstance: {fileID: 0}
136-
m_PrefabAsset: {fileID: 0}
137-
serializedVersion: 6
138-
m_Component:
139-
- component: {fileID: 1538069456119997744}
140-
- component: {fileID: 6876963316446148506}
141-
m_Layer: 0
142-
m_Name: AudioPlayer
143-
m_TagString: Untagged
144-
m_Icon: {fileID: 0}
145-
m_NavMeshLayer: 0
146-
m_StaticEditorFlags: 0
147-
m_IsActive: 1
148-
--- !u!4 &1538069456119997744
149-
Transform:
150-
m_ObjectHideFlags: 0
151-
m_CorrespondingSourceObject: {fileID: 0}
152-
m_PrefabInstance: {fileID: 0}
153-
m_PrefabAsset: {fileID: 0}
154-
m_GameObject: {fileID: 3037125951106697557}
155-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
156-
m_LocalPosition: {x: 0, y: 0, z: 0}
157-
m_LocalScale: {x: 1, y: 1, z: 1}
158-
m_ConstrainProportionsScale: 0
159-
m_Children:
160-
- {fileID: 6297796333276587174}
161-
m_Father: {fileID: 0}
162-
m_RootOrder: -1
163-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
164-
--- !u!114 &6876963316446148506
165-
MonoBehaviour:
166-
m_ObjectHideFlags: 0
167-
m_CorrespondingSourceObject: {fileID: 0}
168-
m_PrefabInstance: {fileID: 0}
169-
m_PrefabAsset: {fileID: 0}
170-
m_GameObject: {fileID: 3037125951106697557}
171-
m_Enabled: 1
172-
m_EditorHideFlags: 0
173-
m_Script: {fileID: 11500000, guid: b7696d676533d4bf5b038e939d083a07, type: 3}
174-
m_Name:
175-
m_EditorClassIdentifier:
176-
audioSource: {fileID: 370320316426281004}

Assets/Prefabs/TTSManager.prefab

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ MonoBehaviour:
4545
m_Script: {fileID: 11500000, guid: cfaf23f70fae74149aff3b371587a856, type: 3}
4646
m_Name:
4747
m_EditorClassIdentifier:
48-
openAIWrapper: {fileID: 0}
4948
audioPlayer: {fileID: 733171135368295313}
50-
text: Hello World
49+
model: 0
50+
voice: 0
51+
speed: 1
5152
--- !u!1001 &6143858701567017995
5253
PrefabInstance:
5354
m_ObjectHideFlags: 0
@@ -104,8 +105,13 @@ PrefabInstance:
104105
propertyPath: m_Name
105106
value: AudioPlayer
106107
objectReference: {fileID: 0}
108+
- target: {fileID: 6876963316446148506, guid: 41327cb76b77f4560baa48841392390d, type: 3}
109+
propertyPath: audioSource
110+
value:
111+
objectReference: {fileID: 0}
107112
m_RemovedComponents: []
108-
m_RemovedGameObjects: []
113+
m_RemovedGameObjects:
114+
- {fileID: 2921076125238579620, guid: 41327cb76b77f4560baa48841392390d, type: 3}
109115
m_AddedGameObjects: []
110116
m_AddedComponents: []
111117
m_SourcePrefab: {fileID: 100100000, guid: 41327cb76b77f4560baa48841392390d, type: 3}

Assets/Scenes/OpenAI-TTS-Example.unity

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8558,10 +8558,6 @@ PrefabInstance:
85588558
propertyPath: m_Name
85598559
value: OpenAI
85608560
objectReference: {fileID: 0}
8561-
- target: {fileID: 8996109759252394209, guid: b54076d3a1b3342448a41fd8732dd04a, type: 3}
8562-
propertyPath: openAIKey
8563-
value:
8564-
objectReference: {fileID: 0}
85658561
m_RemovedComponents: []
85668562
m_RemovedGameObjects: []
85678563
m_AddedGameObjects: []

Assets/Scripts/AudioPlayer.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
using System;
12
using System.IO;
23
using UnityEngine;
3-
using UnityEngine.Networking;
44
using System.Collections;
5+
using UnityEngine.Networking;
56

7+
[RequireComponent(typeof(AudioSource))]
68
public class AudioPlayer : MonoBehaviour
79
{
8-
[SerializeField] private AudioSource audioSource;
10+
private AudioSource audioSource;
11+
private bool deleteCachedFile = true;
12+
13+
private void OnEnable()
14+
{
15+
this.audioSource = GetComponent<AudioSource>();
16+
}
917

1018
public void ProcessAudioBytes(byte[] audioData)
1119
{
@@ -30,5 +38,7 @@ private IEnumerator LoadAndPlayAudio(string filePath)
3038
{
3139
Debug.LogError("Audio file loading error: " + www.error);
3240
}
41+
42+
if (deleteCachedFile) File.Delete(filePath);
3343
}
3444
}

Assets/Scripts/OpenAIWrapper.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
using System.Reflection;
2-
using System.ComponentModel;
31
using System.Text;
2+
using UnityEngine;
43
using System.Net.Http;
5-
using System.Net.Http.Headers;
6-
using System.Threading.Tasks;
74
using Newtonsoft.Json;
8-
using UnityEngine;
5+
using System.Threading.Tasks;
6+
using System.Net.Http.Headers;
97

108
public class OpenAIWrapper : MonoBehaviour
119
{
@@ -42,7 +40,7 @@ public async Task<byte[]> RequestTextToSpeech(string text)
4240
{
4341
return response;
4442
}
45-
Debug.Log("Error: " + httpResponse.StatusCode.ToString());
43+
Debug.Log("Error: " + httpResponse.StatusCode);
4644
return null;
4745
}
4846

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "com.martinpluisch.openai-text-to-speech",
3+
"version": "1.0.0",
4+
"displayName": "OpenAI Text To Speech",
5+
"description": "A Unity package for integrating OpenAI's text-to-speech capabilities",
6+
"unity": "2022.3.4f1",
7+
"dependencies": {
8+
},
9+
"keywords": ["text-to-speech", "openai", "audio"],
10+
"author": {
11+
"name": "Martin Pluisch",
12+
"email": "hello@martinpluisch.com",
13+
"url": "https://martinpluisch.com"
14+
},
15+
"category": "Audio"
16+
}
17+

0 commit comments

Comments
 (0)