From 4afca3814830e1421f9d845e7d6dbe36f43ef373 Mon Sep 17 00:00:00 2001 From: Martin Pluisch <31780571+mapluisch@users.noreply.github.com> Date: Mon, 4 Dec 2023 16:22:39 +0100 Subject: [PATCH] Code refactoring --- Assets/Prefabs/AudioPlayer.prefab | 78 ++++++++------------------ Assets/Prefabs/TTSManager.prefab | 12 +++- Assets/Scenes/OpenAI-TTS-Example.unity | 4 -- Assets/Scripts/AudioPlayer.cs | 14 ++++- Assets/Scripts/OpenAIWrapper.cs | 10 ++-- package.json | 17 ++++++ 6 files changed, 65 insertions(+), 70 deletions(-) create mode 100644 package.json diff --git a/Assets/Prefabs/AudioPlayer.prefab b/Assets/Prefabs/AudioPlayer.prefab index aff578e..d569706 100644 --- a/Assets/Prefabs/AudioPlayer.prefab +++ b/Assets/Prefabs/AudioPlayer.prefab @@ -1,6 +1,6 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: ---- !u!1 &2921076125238579620 +--- !u!1 &3037125951106697557 GameObject: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -8,37 +8,51 @@ GameObject: m_PrefabAsset: {fileID: 0} serializedVersion: 6 m_Component: - - component: {fileID: 6297796333276587174} - - component: {fileID: 370320316426281004} + - component: {fileID: 1538069456119997744} + - component: {fileID: 6876963316446148506} + - component: {fileID: 2775129636596995968} m_Layer: 0 - m_Name: Audio Source + m_Name: AudioPlayer m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 ---- !u!4 &6297796333276587174 +--- !u!4 &1538069456119997744 Transform: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2921076125238579620} + m_GameObject: {fileID: 3037125951106697557} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 1538069456119997744} + m_Father: {fileID: 0} m_RootOrder: -1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!82 &370320316426281004 +--- !u!114 &6876963316446148506 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3037125951106697557} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b7696d676533d4bf5b038e939d083a07, type: 3} + m_Name: + m_EditorClassIdentifier: + audioSource: {fileID: 0} +--- !u!82 &2775129636596995968 AudioSource: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2921076125238579620} + m_GameObject: {fileID: 3037125951106697557} m_Enabled: 1 serializedVersion: 4 OutputAudioMixerGroup: {fileID: 0} @@ -128,49 +142,3 @@ AudioSource: m_PreInfinity: 2 m_PostInfinity: 2 m_RotationOrder: 4 ---- !u!1 &3037125951106697557 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1538069456119997744} - - component: {fileID: 6876963316446148506} - m_Layer: 0 - m_Name: AudioPlayer - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &1538069456119997744 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 3037125951106697557} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: - - {fileID: 6297796333276587174} - m_Father: {fileID: 0} - m_RootOrder: -1 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &6876963316446148506 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 3037125951106697557} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b7696d676533d4bf5b038e939d083a07, type: 3} - m_Name: - m_EditorClassIdentifier: - audioSource: {fileID: 370320316426281004} diff --git a/Assets/Prefabs/TTSManager.prefab b/Assets/Prefabs/TTSManager.prefab index c4c512b..57900f7 100644 --- a/Assets/Prefabs/TTSManager.prefab +++ b/Assets/Prefabs/TTSManager.prefab @@ -45,9 +45,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: cfaf23f70fae74149aff3b371587a856, type: 3} m_Name: m_EditorClassIdentifier: - openAIWrapper: {fileID: 0} audioPlayer: {fileID: 733171135368295313} - text: Hello World + model: 0 + voice: 0 + speed: 1 --- !u!1001 &6143858701567017995 PrefabInstance: m_ObjectHideFlags: 0 @@ -104,8 +105,13 @@ PrefabInstance: propertyPath: m_Name value: AudioPlayer objectReference: {fileID: 0} + - target: {fileID: 6876963316446148506, guid: 41327cb76b77f4560baa48841392390d, type: 3} + propertyPath: audioSource + value: + objectReference: {fileID: 0} m_RemovedComponents: [] - m_RemovedGameObjects: [] + m_RemovedGameObjects: + - {fileID: 2921076125238579620, guid: 41327cb76b77f4560baa48841392390d, type: 3} m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 41327cb76b77f4560baa48841392390d, type: 3} diff --git a/Assets/Scenes/OpenAI-TTS-Example.unity b/Assets/Scenes/OpenAI-TTS-Example.unity index c430b96..950d408 100644 --- a/Assets/Scenes/OpenAI-TTS-Example.unity +++ b/Assets/Scenes/OpenAI-TTS-Example.unity @@ -8558,10 +8558,6 @@ PrefabInstance: propertyPath: m_Name value: OpenAI objectReference: {fileID: 0} - - target: {fileID: 8996109759252394209, guid: b54076d3a1b3342448a41fd8732dd04a, type: 3} - propertyPath: openAIKey - value: - objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] diff --git a/Assets/Scripts/AudioPlayer.cs b/Assets/Scripts/AudioPlayer.cs index 5932cfe..25afd3d 100644 --- a/Assets/Scripts/AudioPlayer.cs +++ b/Assets/Scripts/AudioPlayer.cs @@ -1,11 +1,19 @@ +using System; using System.IO; using UnityEngine; -using UnityEngine.Networking; using System.Collections; +using UnityEngine.Networking; +[RequireComponent(typeof(AudioSource))] public class AudioPlayer : MonoBehaviour { - [SerializeField] private AudioSource audioSource; + private AudioSource audioSource; + private bool deleteCachedFile = true; + + private void OnEnable() + { + this.audioSource = GetComponent(); + } public void ProcessAudioBytes(byte[] audioData) { @@ -30,5 +38,7 @@ private IEnumerator LoadAndPlayAudio(string filePath) { Debug.LogError("Audio file loading error: " + www.error); } + + if (deleteCachedFile) File.Delete(filePath); } } \ No newline at end of file diff --git a/Assets/Scripts/OpenAIWrapper.cs b/Assets/Scripts/OpenAIWrapper.cs index f26fa83..18184d3 100644 --- a/Assets/Scripts/OpenAIWrapper.cs +++ b/Assets/Scripts/OpenAIWrapper.cs @@ -1,11 +1,9 @@ -using System.Reflection; -using System.ComponentModel; using System.Text; +using UnityEngine; using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading.Tasks; using Newtonsoft.Json; -using UnityEngine; +using System.Threading.Tasks; +using System.Net.Http.Headers; public class OpenAIWrapper : MonoBehaviour { @@ -42,7 +40,7 @@ public async Task RequestTextToSpeech(string text) { return response; } - Debug.Log("Error: " + httpResponse.StatusCode.ToString()); + Debug.Log("Error: " + httpResponse.StatusCode); return null; } diff --git a/package.json b/package.json new file mode 100644 index 0000000..01845f2 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "com.martinpluisch.openai-text-to-speech", + "version": "1.0.0", + "displayName": "OpenAI Text To Speech", + "description": "A Unity package for integrating OpenAI's text-to-speech capabilities", + "unity": "2022.3.4f1", + "dependencies": { + }, + "keywords": ["text-to-speech", "openai", "audio"], + "author": { + "name": "Martin Pluisch", + "email": "hello@martinpluisch.com", + "url": "https://martinpluisch.com" + }, + "category": "Audio" + } + \ No newline at end of file