Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

microphone permission problem solved on whisper.cs #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions Samples~/Whisper/Whisper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,71 @@ 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;
private OpenAIApi openai = new OpenAIApi();

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"
Expand All @@ -82,7 +91,7 @@ private void Update()
{
time += Time.deltaTime;
progressBar.fillAmount = time / duration;

if (time >= duration)
{
time = 0;
Expand Down