Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by A_munim · May 15, 2018 at 04:55 PM · networkingmicrophone

Networking Mic

H!!
As always I have tried debugging my code and everything but after so many days still no progress but I remember at some point the audio just produced like no signal TV sound zzzz
I'll post my code and define some things to not waste your time only figuering out what just happened

 using System.Collections.Generic;
 using System.Collections;
 using System.Text;
 using System.Linq;
 using UnityEngine;
 using System;
 
 [RequireComponent(typeof(AudioSource))]
 /// <summary>
 /// Source :  http://forum.unity3d.com/threads/microphone-network-test.123776/
 /// </summary>
 public class MicInput : MonoBehaviour
 {
 
     AudioClip c;
     AudioSource audio;
     int lastSample;
     int FREQUENCY = 44100;
     bool _started;
 
     void Start()
     {
         audio = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         if (_started)
         {
             int pos = Microphone.GetPosition(null);
             int diff = pos - lastSample;
             if (diff > 0)
             {
                 float[] samples = new float[diff * c.channels];
                 c.GetData(samples, lastSample);
                 SendData(samples);
 
             }
             lastSample = pos;
         }
     }
     
     public void StartGame()
     {
         if (true)
         {
             c = Microphone.Start(null, true, 100, FREQUENCY);
             while (Microphone.GetPosition(null) < 0) { } // HACK from Riro
             FindObjectOfType<Client>().AudioReceivers += ReceiveAudioData;
             _started = true;
         }
     }
 
     public void Receive(byte[] ba, int chan)
     {
         float[] f = ToFloatArray(ba);
 #pragma warning disable CS0618 // Type or member is obsolete
         audio.clip = AudioClip.Create("test", f.Length, chan, FREQUENCY, true, true);
 #pragma warning restore CS0618 // Type or member is obsolete
         audio.clip.SetData(f, 0);
         /*if (!audio.isPlaying)*/ audio.Play();
     }
 
     public void SendData(float[] data)
     {
         if (_started)
         {
             byte[] d = ToByteArray(data);
             string strData = Convert(d);
             print("data length : " + strData.Length);
             FindObjectOfType<Client>().Send("CAUDIO|" + strData);
         }
     }
 
     private void ReceiveAudioData(string data)
     {
         byte[] aData = Convert(data);
         print("bytes length : " + aData.Length);
         Receive(aData, c.channels);
     }
 
     public List<int> Factor(int number)
     {
         List<int> factors = new List<int>();
         int max = (int)Math.Sqrt(number);  //round down
         for (int factor = 1; factor <= max; ++factor)
         { //test from 1 to the square root, or the int below it, inclusive.
             if (number % factor == 0)
             {
                 factors.Add(factor);
                 if (factor != number / factor)
                 { // Don't add the square root twice!  Thanks Jon
                     factors.Add(number / factor);
                 }
             }
         }
         return factors;
     }
 
     public byte[] ToByteArray(float[] floatArray)
     {
         int len = floatArray.Length * 4;
         byte[] byteArray = new byte[len];
         int pos = 0;
         foreach (float f in floatArray)
         {
             byte[] data = System.BitConverter.GetBytes(f);
             System.Array.Copy(data, 0, byteArray, pos, 4);
             pos += 4;
         }
         return byteArray;
     }
 
     public float[] ToFloatArray(byte[] byteArray)
     {
         int magicNumber = Factor(byteArray.Length)[1];
         int len = byteArray.Length / magicNumber;
         float[] floatArray = new float[len];
         for (int i = 0; i < byteArray.Length; i += magicNumber)
         {
             floatArray[i / magicNumber] = System.BitConverter.ToSingle(byteArray, i);
         }
         return floatArray;
     }
 
     /// <summary>
     /// Convert Bytes(byte array) to string
     /// </summary>
     /// <param name="data"></param>
     /// <returns></returns>
     string Convert(byte[] data)
     {
         char[] characters = data.Select(b => (char)b).ToArray();
         return new string(characters);
     }
 
     /// <summary>
     /// Convert string to bytes(byte array)
     /// </summary>
     /// <param name="data"></param>
     /// <returns></returns>
     byte[] Convert(string data)
     {
         return data.ToCharArray().Select(c => (byte)c).ToArray();
     }
 }


most is self explainatory but the magicNumber helps when you have a string of 3 then you convert it to float array so you get 3 bytes now if you increment i by 4 then it won't work for some reason ok?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Integrating fmod with AR\VR VoIP chat networked mic stream 0 Answers

transmitting voice via internet 1 Answer

Unity networking tutorial? 6 Answers

Voice Chat Prototype 0 Answers

Is there a way to tell a client not to destroy NetworkBehaviour after server disconnection? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges