Bad Audio Quality

I'm trying to stream music from one user to the other users. To do this I have 2 classes. An AudioReader, which takes the audio from the audiolistener and fills up a buffer, and a PhotonAudioReader, which is set as the "factory" in my photon voice recorder, and grabs data from the AudioReader buffer when OnRead() is called.

Here is some of my code:
In AudioReader:


In PhotonAudioReader (implemeting IAudioReader):


And here are my Photon Voice settings, which I have been playing around with a bit:


The problem is, the audio quality is super bad. It's all crackly, breaks up, and after a while it seems to cut out completely. Although, I don't seem to get errors (atleast from the listener's side, as that is the side i've been debugging in the editor).

Another thing I am wondering is, does it matter what my PhotonAudioReader class has set for its sample rate? Is the sample rate not already decided in the photon voice settings? I'm honestly not even sure what to set as the sample rate for either of them...

Any advice/suggestions for improving the audio quality, and preventing it cutting out, would be great, thanks!

Comments

  • To update: I figured that I should set the PhotonAudioReader's sample rate to be Unity's AudioSettings.outputSampleRate (48k). I also now clear my audio buffer in the GetData() function, as sometimes it started lagging behind. I also reduced the gain of the audio, which seemed to help a little too.

    Now it doesnt seem to cut out completely, but It does still cut out occasionally, for a second or so at a time. It is also still quite distorted. Aswell as this, the audio sounds detuned, and some layers in my song sound out of key in relation to other layers? Does anyone have any suggestions?
  • vadim
    vadim mod
    edited April 2018
    Maybe the problem is that Read method returns true and some data even there is no enough samples to fill entire buffer. It should return false and keep current samples instead.

    When data stream is driven not by consumer (as in AudioClipWrapper.cs) but by producer as in your case, it may be more convenient to use Voice.IAudioPusher interface. Its instance can be assigned as a factory too. You need to implement SetCallback method only which mainly stores given callback. During streaming, you simply call this callback periodically (e.g. from OnAudioFilterRead) with as many samples as you have. Photon Voice will do all buffering work for you.
    ToneAudioPusher in AudioUtil.cs is a sample of such approach.
  • Thanks so much vadim! That does sound much more suitable for my project!
    I'll implement this tonight and post an update about how it sounds :smiley:
  • Wow @vadmin, that change made it sound sooo much better!

    It still sounds a little bit distorted, and has breaks in the audio from time to time, so are there any further suggestions about these two things?

    It does sound a lot better though so I'm very happy :smiley:
  • shadowsora
    edited April 2018
    @vadim hmm sorry to keep spamming with messages, but I have found that sometimes it does break up, and then cut out completely. Do you know why this might be?

    EDIT: It looks like I had the bitrate too high maybe... I had increased it to 320kbps, but using the default 30kbps seems to create much smoother audio, without dropping out!
  • Good to know that it works.
    It's always better to start with default settings, then adjust them if required.
    If you still experience dropouts, try to increase PlayDelayMs which sets jitter buffer size in milliseconds. Jitter buffer compensates irregularities in incoming packets arrival times.
    Also make sure that you updated Photon Voice to the latest version. It contains the fix which may affect audio quality and connection reliability.