Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mapluisch committed Dec 4, 2023
1 parent d3b5bc0 commit 4afca38
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 70 deletions.
78 changes: 23 additions & 55 deletions Assets/Prefabs/AudioPlayer.prefab
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2921076125238579620
--- !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: 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}
Expand Down Expand Up @@ -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}
12 changes: 9 additions & 3 deletions Assets/Prefabs/TTSManager.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 0 additions & 4 deletions Assets/Scenes/OpenAI-TTS-Example.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
Expand Down
14 changes: 12 additions & 2 deletions Assets/Scripts/AudioPlayer.cs
Original file line number Diff line number Diff line change
@@ -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<AudioSource>();
}

public void ProcessAudioBytes(byte[] audioData)
{
Expand All @@ -30,5 +38,7 @@ private IEnumerator LoadAndPlayAudio(string filePath)
{
Debug.LogError("Audio file loading error: " + www.error);
}

if (deleteCachedFile) File.Delete(filePath);
}
}
10 changes: 4 additions & 6 deletions Assets/Scripts/OpenAIWrapper.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -42,7 +40,7 @@ public async Task<byte[]> RequestTextToSpeech(string text)
{
return response;
}
Debug.Log("Error: " + httpResponse.StatusCode.ToString());
Debug.Log("Error: " + httpResponse.StatusCode);
return null;
}

Expand Down
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"url": "https://martinpluisch.com"
},
"category": "Audio"
}

0 comments on commit 4afca38

Please sign in to comment.