void _Begin() { if (playing) { Cleanup(); } dataleft = true; playing = true; var bytes = ArrayPool <byte> .Shared.Rent(POOL_BUFFER_SIZE); for (int i = 0; i < 3; i++) { var b = manager.Buffers.Dequeue(); int read = Read(bytes, sound.Data); if (read != 0) { try { Al.BufferData(b, sound.Format, bytes, read, sound.Frequency); } catch (Exception) { FLLog.Error("AL", $"Error in source {info}"); throw; } Al.alSourceQueueBuffers(ID, 1, ref b); } else { manager.Buffers.Enqueue(b); } if (read < POOL_BUFFER_SIZE) { if (!looping) { dataleft = false; break; } else { sound.Data.Seek(0, SeekOrigin.Begin); } } } ArrayPool <byte> .Shared.Return(bytes); Al.alSourcef(ID, Al.AL_GAIN, ALUtils.ClampVolume(_gain)); Al.alSourcePlay(ID); manager.activeStreamers.Add(this); }
void _Begin() { if (playing) { Cleanup(); } dataleft = true; playing = true; var bytes = BufferAllocator.AllocateBytes(); for (int i = 0; i < 3; i++) { var b = manager.Buffers.Dequeue(); int read = sound.Data.Read(bytes, 0, bytes.Length); if (read != 0) { Al.BufferData(b, sound.Format, bytes, read, sound.Frequency); Al.CheckErrors(); Al.alSourceQueueBuffers(ID, 1, ref b); Al.CheckErrors(); } else { manager.Buffers.Enqueue(b); } if (read < bytes.Length) { if (!looping) { dataleft = false; break; } else { sound.Data.Seek(0, SeekOrigin.Begin); } } } BufferAllocator.Free(bytes); Al.alSourcef(ID, Al.AL_GAIN, ALUtils.ClampVolume(_gain)); Al.alSourcePlay(ID); Al.CheckErrors(); manager.activeStreamers.Add(this); }
public void LoadStream(Stream stream) { using (var snd = SoundLoader.Open(stream)) { byte[] data; if (snd.Size != -1) { data = new byte[snd.Size]; System.Diagnostics.Trace.Assert(snd.Data.Read(data, 0, snd.Size) == snd.Size); } else { using (var mem = new MemoryStream()) { snd.Data.CopyTo(mem); data = mem.GetBuffer(); } } Al.BufferData(ID, snd.Format, data, data.Length, snd.Frequency); } }
public bool Update() { bool hadData = dataleft; //Do things if (dataleft) { int processed; Al.alGetSourcei(ID, Al.AL_BUFFERS_PROCESSED, out processed); Al.CheckErrors(); var bytes = BufferAllocator.AllocateBytes(); for (int i = 0; i < processed; i++) { uint buf = 0; Al.alSourceUnqueueBuffers(ID, 1, ref buf); int read = sound.Data.Read(bytes, 0, bytes.Length); if (read != 0) { Al.BufferData(buf, sound.Format, bytes, read, sound.Frequency); Al.CheckErrors(); Al.alSourceQueueBuffers(ID, 1, ref buf); Al.CheckErrors(); if (read < bytes.Length) { if (looping) { sound.Data.Seek(0, SeekOrigin.Begin); } else { dataleft = false; } } } else { if (looping) { sound.Data.Seek(0, SeekOrigin.Begin); read = sound.Data.Read(bytes, 0, bytes.Length); Al.BufferData(buf, sound.Format, bytes, read, sound.Frequency); Al.CheckErrors(); Al.alSourceQueueBuffers(ID, 1, ref buf); Al.CheckErrors(); } else { dataleft = false; manager.Buffers.Enqueue(buf); break; } } } BufferAllocator.Free(bytes); } //Return buffers int val; Al.alGetSourcei(ID, Al.AL_SOURCE_STATE, out val); Al.CheckErrors(); if (val != Al.AL_PLAYING && val != Al.AL_PAUSED) { if (hadData) { FLLog.Warning("Audio", "Buffer underrun"); Al.alSourcePlay(ID); Al.CheckErrors(); } else { CleanupDelayed(); return(false); } } return(true); }
public bool Update() { bool hadData = dataleft; //Do things if (dataleft) { int processed; Al.alGetSourcei(ID, Al.AL_BUFFERS_PROCESSED, out processed); var bytes = ArrayPool <byte> .Shared.Rent(POOL_BUFFER_SIZE); for (int i = 0; i < processed; i++) { uint buf = 0; Al.alSourceUnqueueBuffers(ID, 1, ref buf); int read = Read(bytes, sound.Data); if (read != 0) { try { Al.BufferData(buf, sound.Format, bytes, read, sound.Frequency); } catch (Exception) { FLLog.Error("AL", $"Error in source {info}"); throw; } Al.alSourceQueueBuffers(ID, 1, ref buf); if (read < POOL_BUFFER_SIZE) { if (looping) { sound.Data.Seek(0, SeekOrigin.Begin); } else { dataleft = false; } } } else { if (looping) { sound.Data.Seek(0, SeekOrigin.Begin); read = sound.Data.Read(bytes, 0, POOL_BUFFER_SIZE); Al.BufferData(buf, sound.Format, bytes, read, sound.Frequency); Al.alSourceQueueBuffers(ID, 1, ref buf); } else { dataleft = false; manager.Buffers.Enqueue(buf); break; } } } ArrayPool <byte> .Shared.Return(bytes); } //Return buffers int val; Al.alGetSourcei(ID, Al.AL_SOURCE_STATE, out val); if (val != Al.AL_PLAYING && val != Al.AL_PAUSED) { if (hadData) { FLLog.Warning("Audio", "Buffer underrun"); Al.alSourcePlay(ID); } else { CleanupDelayed(); return(false); } } return(true); }