7
\$\begingroup\$

I am able to Record and Run Audio from my Microphone using this code snippet:

  void OnGUI() {
        if (GUI.Button(new Rect(0, 0, 100, 20), "record")) {
           // Microphone.End(Microphone.devices[0]);
            audsourceAttached.clip = Microphone.Start(Microphone.devices[0], false, 3, 44100);
        }
        if (GUI.Button(new Rect(0, 30, 100, 20), "Stop"))
        {
            //Microphone.End(Microphone.devices[0]);
            audsourceAttached.Play();
        }
    }

Now I am willing to send this audio across Network (on my local network-another instance of the game). How can I do this?

I tried to find this on google but unable to find any useful solution/links.

How can I send audio through microphone like real-time Chat in a game/etc.

\$\endgroup\$
  • 1
    \$\begingroup\$ Unhelpful comment: please don't. Many gamers never want the game to transmit audio. We already have Skype, Steam, Teamspeak, and many many many other solutions for that, and your game will interfere with them. \$\endgroup\$ – Peter - Unban Robert Harvey Mar 15 '17 at 11:50
  • 4
    \$\begingroup\$ I don't see this as a problem, as long as you can turn it off in favour of Skype, Steam, Teamspeak, etc. Also, there are some edge cases where microphone audio gets used in combination with its source location instead of a traditional voice chat, i.e. it's only audible if you're close enough to the speaking player in the game. I think for example DayZ and/or ArmA do this. \$\endgroup\$ – Christian Mar 15 '17 at 15:58
  • \$\begingroup\$ Here may be some code that interests you: answers.unity3d.com/questions/737002/wav-byte-to-audioclip.html \$\endgroup\$ – Lasse Mar 16 '17 at 7:07
  • 2
    \$\begingroup\$ I agree that there are plenty of reasons to integrate voice chat directly into the game instead of relying on 3rd party tools. It allows to connect players to each other ad-hoc when they meet in an open world or in a game lobby. In a team-based game with no respawn you can also prevent dead players from talking to the living. But keep in mind that just getting the waveform and sending it over the network might not give you very good results. It would be better to encode it in an audio codec which is made for voice encoding in realtime over low-bandwidth. \$\endgroup\$ – Philipp Mar 16 '17 at 16:03
  • \$\begingroup\$ @Philipp maybe you are right but i just share my efforts that what i have done so far. Is there anyother details please share your complete answer \$\endgroup\$ – Muhammad Faizan Khan Mar 17 '17 at 5:25
4
\$\begingroup\$

TL;DR: You can't do it natively in Unity

Microphone.Start returns an AudioClip which has 3 load types, none of which fit your use case. All of them (even the .Streaming one) require the entire clip to be on the users computer already. Using AudioClip.GetData() and sending a file over the network (in Unity, using Networking.NetworkTransport.Send()) would be horribly unresponsive and can only actually occur after the recording finishes its clip. This ignores audio codecs, using multiple threads (Unity isn't thread safe), and dealing with the real time nature of audio device data.

What you need is a way to get the audio device data in real time which Unity provides no mechanism for and therefore cannot support RTC natively. This answer comes to the same conclusion

Third party solutions exist on the asset store. Here's one such solution mentioned in the above linked answer: Dissonance Voice Chat. Going through the files list it uses a native .dll, the Opus audio codec, and a bunch of C# files for interfacing with the native code.

Rolling your own, which I do not recommend, would require doing the following:

  • Using a separate C# System.Threading thread to process your audio so that your framerate and audio playback aren't coupled together.
  • Getting the audio data isn't trivial in C#. .NET uses DirectSound which is a Windows things. Mono doesn't support this. This means you either need to implement a cross platform solution yourself natively, or rely on a library that already does.
  • Transporting it over the network requires a socket using .NET sockets. These seem robust and are supported in Mono.
  • Your sockets should transport as a P2P multicast. This allows you to send one audio stream to multiple people all at once. It also skips a setup like Client ==> Server ==> Client which adds extra latency.
  • You would also want a codec to make sure that you're not just sending uncompressed audio willy-nilly. There are many out there which most likely requires interfacing with another native library.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.