Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Help streaming microphone audio with websockets

Discussion in 'Audio & Video' started by misomeshi, Nov 8, 2023.

  1. misomeshi

    misomeshi

    Joined:
    Sep 24, 2023
    Posts:
    1
    Hey guys! I am trying to stream my microphone audio in byte[] form for real time speech to text transcription but I am having trouble.
    Disclaimer, I am new to Unity and websockets and am trying to do everything manually at the moment, but am very happy to switch to an easier option with helpful packages or assets from the store. I would happily pay for a service if it would make things run smoothly.

    Here is a snippet of code showing how I am currently trying to stream audio but I am sure there must be an easier way?

    Code (CSharp):
    1.  
    2. this.audioClip = Microphone.Start(microphoneDevice, true, recordingTime, SAMPLE_RATE);
    3. audioData = ConvertToByte(audioClip);
    4.  
    5. // waiting for microphone to start recording
    6. while (Microphone.GetPosition(null) <= 0)
    7. {
    8. }
    9.  
    10. audioPos = 0;
    11. while (audioPos < audioData.Length)
    12. {
    13.  
    14. int chunkSize = Mathf.Min(CHUNK_SIZE, audioData.Length - audioPos);
    15. byte[] chunkData = new byte[chunkSize];
    16. System.Buffer.BlockCopy(audioData, audioPos, chunkData, 0, chunkSize);
    17. wsClient.SendAudio(chunkData);
    18.  
    19. if (chunkSize == audioData.Length - audioPos) //reset to beginning of audioClip
    20. {
    21. audioPos = 0;
    22. }
    23. else
    24. {
    25. audioPos += chunkSize;
    26. }
    27. }
    Thanks a lot!
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    430
    Hi @youngnamh !

    What kind of problems are you seeing? At first glance, what you have there seems to be ok.