adrenak / univoice-mirror-network

A Mirror based implementation for UniVoice voice network
MIT License
6 stars 0 forks source link

Playing sound from the desired location #3

Open anomal3 opened 5 months ago

anomal3 commented 5 months ago

Hello @adrenak :)

I decided to delve deeper into the code and figured it out a little.

  1. Sample code was created in NetworkManager
 public ChatroomAgent agent;

public override void Start()
{
    base.Start();
    InitializeAgent();
}

void InitializeAgent()
{
    var cac = new CircularAudioClip(8000, 1, 200, segCount: 10);
    agent = new ChatroomAgent(
        new UniVoiceMirrorNetwork(),
        new VoiceChatMic(0, 8000, 25),
        new CustomFactory(10, 5, cac)
    );
}
  1. I slightly rewrote IAudioInput, otherwise on a server where there is no microphone an error occurred that did not allow me to go further
public class VoiceChatMic : IAudioInput
{
 //do do do
 public VoiceChatMic(int deviceIndex = 0, int frequency = 16000, int sampleLen = 100)
 {
     if (Mic.Instance.Devices.Count > 0)
     {
         Mic.Instance.SetDeviceIndex(deviceIndex);
         Mic.Instance.StartRecording(frequency, sampleLen);
         Debug.Log("UniVoiceUniMicInput: Start recording.");
         Mic.Instance.OnSampleReady += Mic_OnSampleReady;
     }

     else Debug.LogWarning("Must have recording devices for Microphone input");
 }
}
  1. From the tests, the best option is 8000 kHz and 25 ms with Telepathy transport; the rest are just terrible sound. Something needs to be done with the packet size. Size 1194 clearly doesn't fit

CustomFactory

 public class CustomFactory : IAudioOutputFactory
 {
     public int BufferSegCount { get; private set; }
     public int MinSegCount { get; private set; }

     public CircularAudioClip cac { get; private  set; }
     public AudioSource audioSource { get; set; }

     public CustomFactory(int bufferSegCount, int minSegCount, CircularAudioClip _cac)
     {
         BufferSegCount = bufferSegCount;
         MinSegCount = minSegCount;
         cac = _cac;
     }

     public IAudioOutput Create(int samplingRate, int channelCount, int segmentLength)
     {
         audioSource = new GameObject($"UniVoiceAudioSourceOutput").AddComponent<AudioSource>();
         audioSource.minDistance = 20;
         audioSource.maxDistance = 100;
         audioSource.spatialBlend = 0.85f;
         return UniVoiceAudioSourceOutput.New(cac, audioSource, MinSegCount);
     }

 }

public override void OnStartServer()
{
    base.OnStartServer();

    var audio = FindObjectOfType<UniVoiceAudioSourceOutput>().transform;

    if (audio != null)
    {
        audio.parent = transform;
        audio.position = transform.position;
    }
    else
    {
        Debug.LogError("audio not found");
    }
}

Now, after all the manipulations, the audio source is created and placed as a child of the player on the server. Everything is amazing. But I still haven’t been able to implement it so that I can only hear nearby players. I can still hear everyone

anomal3 commented 5 months ago

@adrenak Can I count on help?