what difference betweeen that 2 scripts?? using UnityEngine; using FishNet.Object; using…
- User
what difference betweeen that 2 scripts??
using UnityEngine; using FishNet.Object; using FishNet.Object.Synchronizing;
public class VoiceDataHandler : NetworkBehaviour { [SerializeField] private VoiceRecorder voiceRecorder; [SerializeField] private VoiceStream voiceStream;
private void Start() { if (voiceRecorder != null) { voiceRecorder.evtVoiceStream.AddListener(SendVoiceData); } else { Debug.LogError(“VoiceRecorder is not assigned.”); }
if (voiceStream == null) { Debug.LogError(“VoiceStream is not assigned.”); } }
// Method to be called by VoiceRecorder when data is ready to be sent private void SendVoiceData(byte[] data) { if (IsOwner) { SendVoiceDataToServer(data); } }
[ServerRpc(RequireOwnership = false, Channel = FishNet.Transporting.Channel.Unreliable)] private void SendVoiceDataToServer(byte[] data) { ReceiveVoiceDataOnClients(data); }
[ObserversRpc(ExcludeOwner = false, Channel = FishNet.Transporting.Channel.Unreliable)] private void ReceiveVoiceDataOnClients(byte[] data) { if (!IsOwner && voiceStream != null) { voiceStream.PlayVoiceData(data); } } }
and taht scirrptt
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Networking;
using TMPro;
using UnityEngine; using FishNet.Object; using FishNet.Object.Synchronizing;
public class VoiceDataHandler : NetworkBehaviour { [SerializeField] private VoiceRecorder voiceRecorder; [SerializeField] private VoiceStream voiceStream;
public override void OnStartClient() { if (voiceRecorder != null) { voiceRecorder.evtVoiceStream.AddListener(SendVoiceData); } else { Debug.LogError(“VoiceRecorder is not assigned.”); }
if (voiceStream == null) { Debug.LogError(“VoiceStream is not assigned.”); } }
// Method to be called by VoiceRecorder when data is ready to be sent public void SendVoiceData(byte[] data) { // if (IsOwner) // { SendVoiceDataToServer(data); //} }
[ServerRpc(RequireOwnership = false, Channel = FishNet.Transporting.Channel.Unreliable)] public void SendVoiceDataToServer(byte[] data) { ReceiveVoiceDataOnClients(data); }
[ObserversRpc(ExcludeOwner = true, Channel = FishNet.Transporting.Channel.Unreliable)] public void ReceiveVoiceDataOnClients(byte[] data) { //if (!IsOwner && voiceStream != null) // { voiceStream.PlayVoiceData(data); // } } }
- Assistant
- User
u added sampleRate
but i have anothe scriptt named VoiceStream Meant to be attached to a game object representing non-local players. It can be used to playback the audio streamed in from those "other" players.
its also have sampleRate
AudioSource
outputSource
The audio source to play audio through
sampleRateMethod
The method by which to calculate samplerate
uint
customSampleRate
If using a custom sample rate, specify that rate here.
double
encodingTime
The max length of the current streaming clip time.
This can be useful for debugg
also tht scriptt i have scriptt VocieREcord
Meant to be attached to a game object representing the local player. This componenet helps capture voice data from the local user and prepare it for transmission.
- Assistant
- User
whats difference here ? prefomance?
codepublic void PlayVoiceData(byte[] buffer) { float[] floatBuffer = new float[buffer.Length / 2]; System.Buffer.BlockCopy(buffer, 0, floatBuffer, 0, buffer.Length); audioBuffer.AddRange(floatBuffer); if (outputSource.isPlaying) { outputSource.Stop(); } // Prevent the audio buffer from exceeding the clip length if (audioBuffer.Count > audioClip.samples) { audioBuffer.RemoveRange(0, audioBuffer.Count - audioClip.samples); } audioClip.SetData(audioBuffer.ToArray(), 0); outputSource.Play(); }and there
public void PlayVoiceData(byte[] data) { // Convert byte[] data to float[] for audio playback float[] floatData = new float[data.Length / 2]; for (int i = 0; i < floatData.Length; i++) { floatData[i] = BitConverter.ToInt16(data, i * 2) / 32768f; // Assuming 16-bit PCM }
- Assistant
- User
I WANNA IN VOID VoceRecord that 2 voids
Methods Start Recording public void StartRecording(); Starts the recording process.
Stop Recording public void StopRecording();
- Assistant
- User
Improved Combined Approach
Here’s an improved approach combining the best of both methods:
public void PlayVoiceData(byte[] data) { // Efficiently convert byte[] to float[] float[] floatData = new float[data.Length / 2]; Buffer.BlockCopy(data, 0, floatData, 0, data.Length);
code// Enqueue audio data for playback audioQueue.Enqueue(floatData); ProcessQueue();}
private void ProcessQueue() { // If not already playing and there’s data in the queue, play the next clip if (!outputSource.isPlaying && audioQueue.Count > 0) { PlayNextClip(); } }
private void PlayNextClip() { if (audioQueue.Count > 0) { float[] clipData = audioQueue.Dequeue(); if (clipData.Length > 0) { outputSource.clip = AudioClip.Create(“VoiceStreamClip”, clipData.Length, 1, sampleRate, false); outputSource.clip.SetData(clipData, 0); outputSource.Play(); } } }
can u write here your betterr version of VoiceStream?
- Assistant