Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Runtime/Scripts/AudioStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class AudioStream : IDisposable
private readonly AudioSource _audioSource;
private RingBuffer _buffer;
private short[] _tempBuffer;
private short[] _upmixBuffer;
private uint _numChannels;
private uint _sampleRate;
private AudioResampler _resampler = new AudioResampler();
Expand Down Expand Up @@ -106,11 +107,35 @@ private void OnAudioStreamEvent(AudioStreamEvent e)
unsafe
{
var uFrame = _resampler.RemixAndResample(frame, _numChannels, _sampleRate);
if (uFrame != null)
if (uFrame != null && uFrame.Length > 0)
{
var data = new Span<byte>(uFrame.Data.ToPointer(), uFrame.Length);
_buffer?.Write(data);
}
else if (frame.Length > 0 && frame.NumChannels == 1 && _numChannels == 2 && frame.SampleRate == _sampleRate)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to up-mix from mono to stereo

{
int monoSamples = (int)frame.SamplesPerChannel;
int stereoSamples = monoSamples * 2;
if (_upmixBuffer == null || _upmixBuffer.Length < stereoSamples)
_upmixBuffer = new short[stereoSamples];

var input = new ReadOnlySpan<short>(frame.Data.ToPointer(), monoSamples);
for (int i = 0; i < monoSamples; i++)
{
short sample = input[i];
int idx = i * 2;
_upmixBuffer[idx] = sample;
_upmixBuffer[idx + 1] = sample;
}

var upmixed = MemoryMarshal.Cast<short, byte>(_upmixBuffer.AsSpan(0, stereoSamples));
_buffer?.Write(upmixed);
}
else if (frame.Length > 0 && frame.NumChannels == _numChannels && frame.SampleRate == _sampleRate)
{
var raw = new Span<byte>(frame.Data.ToPointer(), frame.Length);
_buffer?.Write(raw);
}

}
}
Expand Down
Loading