diff --git a/Samples~/Whisper/Whisper.cs b/Samples~/Whisper/Whisper.cs index 7c1c48e..3ddfe5b 100644 --- a/Samples~/Whisper/Whisper.cs +++ b/Samples~/Whisper/Whisper.cs @@ -9,10 +9,10 @@ public class Whisper : MonoBehaviour [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; @@ -20,51 +20,60 @@ public class Whisper : MonoBehaviour private void Start() { - #if UNITY_WEBGL && !UNITY_EDITOR + // 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 +#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 +#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 + +#if !UNITY_WEBGL clip = Microphone.Start(dropdown.options[index].text, false, duration, 44100); - #endif +#endif } private async void EndRecording() { message.text = "Transcripting..."; - - #if !UNITY_WEBGL + +#if !UNITY_WEBGL Microphone.End(null); - #endif - +#endif + byte[] data = SaveWav.Save(fileName, clip); - + var req = new CreateAudioTranscriptionsRequest { - FileData = new FileData() {Data = data, Name = "audio.wav"}, + FileData = new FileData() { Data = data, Name = "audio.wav" }, // File = Application.persistentDataPath + "/" + fileName, Model = "whisper-1", Language = "id" @@ -82,7 +91,7 @@ private void Update() { time += Time.deltaTime; progressBar.fillAmount = time / duration; - + if (time >= duration) { time = 0;