Ejemplo n.º 1
0
    public void UseMic(bool useMic, MicrophoneQuality qual)
    {
        int samplingRate = 44100;

        if (qual == MicrophoneQuality.VERYLOW)
        {
            samplingRate = 8000;
        }
        else if (qual == MicrophoneQuality.HIGH)
        {
            samplingRate = 44100;
        }
        else if (qual == MicrophoneQuality.VERYHIGH)
        {
            samplingRate = 48000;
        }
        if (useMic)
        {
            this.isPlaying = true;
            source.clip    = Microphone.Start(Microphone.devices[0].ToString(), true, 1, samplingRate);
            //source.loop = true;

            while (!(Microphone.GetPosition(null) > 0))
            {
                source.Play();
                float[] samples = new float[source.clip.samples * source.clip.channels];
                source.clip.GetData(samples, 0);

                for (int i = 0; i < samples.Length; ++i)
                {
                    samples[i] = samples[i] * 0.5f;
                }
                var byteArray = new byte[samples.Length * 4];
                Buffer.BlockCopy(samples, 0, byteArray, 0, byteArray.Length);

                //print(byteArray.Length);

                var protocol = new Protocol();
                var buffer   = protocol.Serialize((byte)PacketId.AudioRequest, _myPlayerId, byteArray.Length, byteArray);
                var packet   = default(Packet);
                packet.Create(buffer);
                _peer.Send(channelID, ref packet);
            }
        }
        else
        {
            this.isPlaying = false;
            source.Stop();
            source.clip = null;
        }
    }
Ejemplo n.º 2
0
    //Method to use the microphone and play the sound
    public void UseMic(bool useMic, MicrophoneQuality qual)
    {
        //Get the sampling rate from an enum value
        int samplingRate = 44100;

        if (qual == MicrophoneQuality.VERYLOW)
        {
            samplingRate = 8000;
        }
        else if (qual == MicrophoneQuality.HIGH)
        {
            samplingRate = 44100;
        }
        else if (qual == MicrophoneQuality.VERYHIGH)
        {
            samplingRate = 48000;
        }
        //Play if we're using our mic
        if (useMic)
        {
            //Tell the script we're using the mic
            this.isPlaying = true;
            //Get the clip from the mic from the default device and continue to 'loop' it while it's played
            source.clip = Microphone.Start(Microphone.devices[0].ToString(), true, 1, samplingRate);
            //Make sure we're looping the source
            source.loop = true;

            //While the microphone is active, play the audioclip
            while (!(Microphone.GetPosition(null) > 0))
            {
                source.Play();
            }
        }
        else
        {
            //If we're not using our mic make sure we're not playing
            this.isPlaying = false;
            //Make sure that the audio clip is stopped
            source.Stop(); //Make sure the clip is null again
            source.clip = null;
        }
    }