-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathWhisper.cs
More file actions
104 lines (86 loc) · 2.92 KB
/
Whisper.cs
File metadata and controls
104 lines (86 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
using UnityEngine.UI;
namespace OpenAI
{
public class Whisper : MonoBehaviour
{
[SerializeField] private Button recordButton;
[SerializeField] private Image progressBar;
[SerializeField] private Text message;
[SerializeField] private Dropdown dropdown;
private readonly string fileName = "output.wav";
private readonly int duration = 5;
private AudioClip clip;
private bool isRecording;
private float time;
private OpenAIApi openai = new OpenAIApi();
private void Start()
{
// If you get permission problem
/*
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
Permission.RequestUserPermission(Permission.Microphone);
}
*/
#if UNITY_WEBGL && !UNITY_EDITOR
dropdown.options.Add(new Dropdown.OptionData("Microphone not supported on WebGL"));
#else
foreach (var device in Microphone.devices)
{
dropdown.options.Add(new Dropdown.OptionData(device));
}
recordButton.onClick.AddListener(StartRecording);
dropdown.onValueChanged.AddListener(ChangeMicrophone);
var index = PlayerPrefs.GetInt("user-mic-device-index");
dropdown.SetValueWithoutNotify(index);
#endif
}
private void ChangeMicrophone(int index)
{
PlayerPrefs.SetInt("user-mic-device-index", index);
}
private void StartRecording()
{
isRecording = true;
recordButton.enabled = false;
var index = PlayerPrefs.GetInt("user-mic-device-index");
#if !UNITY_WEBGL
clip = Microphone.Start(dropdown.options[index].text, false, duration, 44100);
#endif
}
private async void EndRecording()
{
message.text = "Transcripting...";
#if !UNITY_WEBGL
Microphone.End(null);
#endif
byte[] data = SaveWav.Save(fileName, clip);
var req = new CreateAudioTranscriptionsRequest
{
FileData = new FileData() { Data = data, Name = "audio.wav" },
// File = Application.persistentDataPath + "/" + fileName,
Model = "whisper-1",
Language = "id"
};
var res = await openai.CreateAudioTranscription(req);
progressBar.fillAmount = 0;
message.text = res.Text;
recordButton.enabled = true;
}
private void Update()
{
if (isRecording)
{
time += Time.deltaTime;
progressBar.fillAmount = time / duration;
if (time >= duration)
{
time = 0;
isRecording = false;
EndRecording();
}
}
}
}
}