Placeholder-Software / Dissonance

Unity Voice Chat Asset
69 stars 5 forks source link

Is it possible to use Dissonance without networking? #236

Closed friuns2 closed 2 years ago

friuns2 commented 2 years ago

Dissonance.zip Is it possible to use Dissonance without its networking? Just for encoding and decoding i want to send packets directly without additional Dissonance networking layer

I tried to make some loopback test, but get errors packet arrived is very late, Can you help me? image


using System;
using System.Collections;
using System.Collections.Generic;
using Dissonance;
using Dissonance.Audio.Capture;
using Dissonance.Audio.Codecs;
using Dissonance.Audio.Codecs.Opus;
using Dissonance.Audio.Playback;
using Dissonance.Networking;
using NAudio.Wave;
using UnityEngine;
[RequireComponent(typeof(SamplePlaybackComponent),typeof(MyBasicMicrophoneCapture))]
public class NewBehaviourScript : MonoBehaviour
{
    private SpeechSessionStream _sessions;
    // Start is called before the first frame update
    private EncoderPipeline enc;
    private MyBasicMicrophoneCapture microphone;
    void Start()
    {
        microphone = GetComponent<MyBasicMicrophoneCapture>();

        WaveFormat waveFormat = microphone.StartCapture(null);
        enc = new EncoderPipeline( waveFormat, new OpusEncoder(AudioQuality.Medium,FrameSize.Medium,true), null);

        // MyDecoderPipeline dev = new MyDecoderPipeline(DecoderFactory.Create(new FrameFormat()), new WaveFormat(), new OpusEncoder(), null);
        microphone.OnReceiveMicrophoneData+= delegate(ArraySegment<float> floats, WaveFormat format)
        {
            enc.ReceiveMicrophoneData(floats, format);
        };
        enc.OnData+= delegate(ArraySegment<byte> bytes)
        {
            _sessions.ReceiveFrame(new VoicePacket("asd", ChannelPriority.Default, 1, false, bytes, 1));
        };

        _sessions = new SpeechSessionStream(new VolumeProvider());
        _sessions.PlayerName = "asd";
        _sessions.StartSession(new FrameFormat(Codec.Opus,waveFormat,960));

        AudioSource = GetComponent<AudioSource>();

        InitAudio();
    }
    private void InitAudio()
    {
        _player = GetComponent<SamplePlaybackComponent>();
        AudioSource.clip = AudioClip.Create("Flatline", 4096, 1, AudioSettings.outputSampleRate, true, buf =>
        {
            for (var i = 0; i < buf.Length; i++)
                buf[i] = 1.0f;
        });

        // Set all of the audio source settings that are not allowed to changed
        AudioSource.loop = true; // Audio must play forever
        AudioSource.pitch = 1; // Pitch has no effect on the audio
        AudioSource.dopplerLevel = 0; // Pitch cannot be changed, so doppler makes no sense
        AudioSource.mute = false; // Muting should be done through the player object, not the source
        AudioSource.priority = 0; // 0 is the **maximum** priority! Dissonance cannot handle...
    }
    private AudioSource AudioSource; 
    private SamplePlaybackComponent _player;
    // Update is called once per frame
    void Update()
    {
        microphone.UpdateSubscribers();
        SpeechSession? s = _sessions.TryDequeueSession();

        if (s.HasValue)
        {
            _player.Play(s.Value);
            AudioSource.Play();
        }
        // else
        // {
        //     // No session was available to start playing. Stop the audio source playing to preserve
        //     // limited "real voices" in the Unity audio mixer.
        //     if (AudioSource.isPlaying)
        //         AudioSource.Stop();
        // }

    }
}
`
martindevans commented 2 years ago

Dissonance isn't really designed to make this easy. Most of the complex things that Dissonance does for you are in the networking system, so I would really strongly advise to use that if at all possible!

If you do want to do this anyway, you can still build a custom network system for Dissonance but instead of using the base classes (BaseServer/BaseClient etc) you can instead implement ICommsNetwork. You can implement this however you like and the rest of Dissonance will work as normal (e.g. you don't need to replace the microphone or playback systems). This isn't a use case I can provide much support for though.

martindevans commented 2 years ago

It's been a while so I'll close this discussion, feel free to re-open it if you have more questions :)