using NVorbis; using System; using System.IO; using UnityEngine; namespace SanAndreasUnity.Audio { /// /// Audio stream class /// public class AudioStream : IDisposable { /// /// Reader /// private VorbisReader reader; /// /// Audio clip /// private AudioClip audioClip; /// /// Audio clip /// public AudioClip AudioClip { get { return audioClip; } } /// /// Constructor /// /// Stream /// Audio clip name /// Close stream on dispose public AudioStream(Stream stream, string audioClipName, bool closeStreamOnDispose) { reader = new VorbisReader(stream, closeStreamOnDispose); audioClip = AudioClip.Create(audioClipName, (int)(reader.TotalSamples), reader.Channels, reader.SampleRate, true, (data) => { if (data != null) { if (data.Length > 0) { reader.ReadSamples(data, 0, Mathf.Min(data.Length, Mathf.Max(0, (int)(reader.TotalSamples - reader.DecodedPosition)))); } } }, (newPosition) => { reader.DecodedPosition = newPosition; }); } /// /// Dispose /// public void Dispose() { if (reader != null) { reader.Dispose(); reader = null; } } } }