using System; using UnityEngine; public class SoundFileOgg : IDisposableBase { public SoundFileOgg(AFileSystemBase fileSystem, float amp_late = 1f) { this.reader_ = new SoundReaderOGG(fileSystem); this.AmpRate = amp_late; } public void Open(string file_name, bool threeD, bool stream) { this.reader_.OpenFile(file_name); if (!this.reader_.IsOpenFile()) { return; } UnityEngine.Object.DestroyImmediate(this.clip_); this.clip_ = null; int sample_length = (int)this.reader_.sample_length; this.clip_ = AudioClip.Create(file_name, sample_length, this.reader_.channels, this.reader_.sample_rate, stream, new AudioClip.PCMReaderCallback(this.OnAudioRead), new AudioClip.PCMSetPositionCallback(this.OnAudioSetPosition)); } public float GetLength() { return (float)(this.reader_.sample_length / (double)this.reader_.sample_rate); } public void Close() { this.reader_.CloseFile(); } public bool IsOpen() { return this.reader_.IsOpenFile(); } public void Seek(int new_pos) { this.reader_.Seek(new_pos); } private void OnAudioRead(float[] data) { for (int i = 0; i < data.Length; i++) { data[i] = 0f; } this.reader_.Read(data); if (this.AmpRate != 1f) { for (int j = 0; j < data.Length; j++) { data[j] *= this.AmpRate; } } this.position_ += data.Length; } private void OnAudioSetPosition(int newPosition) { this.reader_.Seek(newPosition); this.position_ = newPosition; } protected override void DisposeEvent() { this.reader_.Dispose(); this.reader_ = null; UnityEngine.Object.DestroyImmediate(this.clip_, true); this.clip_ = null; } public float AmpRate { get; set; } public AudioClip clip { get { return this.clip_; } } public int position { get { return this.position_; } } private AudioClip clip_; private SoundReaderOGG reader_; private int position_; }