| 1 | //#define VERBOSE_LOGGING |
| 2 | using UnityEngine; |
| 3 | using System.Collections; |
| 4 | using System; |
| 5 | |
| 6 | using Oculus.Platform; |
| 7 | |
| 8 | public class BufferedAudioStream { |
| 9 | const bool VerboseLogging = false; |
| 10 | AudioSource audio; |
| 11 | |
| 12 | float[] audioBuffer; |
| 13 | int writePos; |
| 14 | |
| 15 | const float bufferLengthSeconds = 0.25f; |
| 16 | const int sampleRate = 48000; |
| 17 | const int bufferSize = (int)(sampleRate * bufferLengthSeconds); |
| 18 | const float playbackDelayTimeSeconds = 0.05f; |
| 19 | |
| 20 | float playbackDelayRemaining; |
| 21 | float remainingBufferTime; |
| 22 | |
| 23 | public BufferedAudioStream(AudioSource audio) { |
| 24 | audioBuffer = new float[bufferSize]; |
| 25 | this.audio = audio; |
| 26 | |
| 27 | audio.loop = true; |
| 28 | audio.clip = AudioClip.Create("", bufferSize, 1, sampleRate, false); |
| 29 | |
| 30 | Stop(); |
| 31 | } |
| 32 | |
| 33 | public void Update () { |
| 34 | |
| 35 | if(remainingBufferTime > 0) |
| 36 | { |
| 37 | #if VERBOSE_LOGGING |
| 38 | Debug.Log(string.Format("current time: {0}, remainingBufferTime: {1}", Time.time, remainingBufferTime)); |
| 39 | #endif |
| 40 | |
| 41 | if (!audio.isPlaying && remainingBufferTime > playbackDelayTimeSeconds) |
| 42 | { |
| 43 | playbackDelayRemaining -= Time.deltaTime; |
| 44 | if (playbackDelayRemaining <= 0) |
| 45 | { |
| 46 | #if VERBOSE_LOGGING |
| 47 | Debug.Log("Starting playback"); |
| 48 | #endif |
| 49 | audio.Play(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (audio.isPlaying) |
| 54 | { |
| 55 | remainingBufferTime -= Time.deltaTime; |
| 56 | if (remainingBufferTime < 0) |
| 57 | { |
| 58 | remainingBufferTime = 0; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (remainingBufferTime <= 0) |
| 64 | { |
| 65 | if (audio.isPlaying) |
| 66 | { |
| 67 | Debug.Log("Buffer empty, stopping " + DateTime.Now); |
| 68 | Stop(); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | if (writePos != 0) |
| 73 | { |
| 74 | Debug.LogError("writePos non zero while not playing, how did this happen?"); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void Stop() |
| 81 | { |
| 82 | audio.Stop(); |
| 83 | audio.time = 0; |
| 84 | writePos = 0; |
| 85 | playbackDelayRemaining = playbackDelayTimeSeconds; |
| 86 | } |
| 87 | |
| 88 | public void AddData(float[] samples) { |
| 89 | int remainingWriteLength = samples.Length; |
| 90 | |
| 91 | if(writePos > audioBuffer.Length) { |
| 92 | throw new Exception(); |
| 93 | } |
| 94 | |
| 95 | do { |
| 96 | int writeLength = remainingWriteLength; |
| 97 | int remainingSpace = audioBuffer.Length - writePos; |
| 98 | |
| 99 | if(writeLength > remainingSpace) { |
| 100 | writeLength = remainingSpace; |
| 101 | } |
| 102 | |
| 103 | Array.Copy(samples, 0, audioBuffer, writePos, writeLength); |
| 104 | |
| 105 | remainingWriteLength -= writeLength; |
| 106 | writePos += writeLength; |
| 107 | if(writePos > audioBuffer.Length) { |
| 108 | throw new Exception(); |
| 109 | } |
| 110 | if(writePos == audioBuffer.Length) { |
| 111 | writePos = 0; |
| 112 | } |
| 113 | } while(remainingWriteLength > 0); |
| 114 | |
| 115 | #if VERBOSE_LOGGING |
| 116 | float prev = remainingBufferTime; |
| 117 | #endif |
| 118 | remainingBufferTime += (float)samples.Length / sampleRate; |
| 119 | #if VERBOSE_LOGGING |
| 120 | Debug.Log(string.Format("previous remaining: {0}, new remaining: {1}, added {2} samples", prev, remainingBufferTime, samples.Length)); |
| 121 | #endif |
| 122 | audio.clip.SetData(audioBuffer, 0); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | } |