Created
October 18, 2013 04:41
-
-
Save DevJohnC/7036575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Author: John Carruthers (johnc@frag-labs.com) | |
| // | |
| // Copyright (C) 2013 John Carruthers | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining | |
| // a copy of this software and associated documentation files (the | |
| // "Software"), to deal in the Software without restriction, including | |
| // without limitation the rights to use, copy, modify, merge, publish, | |
| // distribute, sublicense, and/or sell copies of the Software, and to | |
| // permit persons to whom the Software is furnished to do so, subject to | |
| // the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be | |
| // included in all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| using System; | |
| using System.Threading; | |
| using FragLabs.Adjutant.Network; | |
| using FragLabs.Adjutant.Network.Interfaces; | |
| using FragLabs.AdjutantOS.API.Modules.Domains; | |
| using FragLabs.AdjutantOS.API.Network; | |
| namespace FragLabs.AdjutantOS.Modules.RainyMood.Wave | |
| { | |
| public class WaveStreamer | |
| { | |
| private readonly IClient _client; | |
| private readonly WaveStream _stream; | |
| private readonly byte[] _buffer; | |
| private Timer _timer; | |
| private Guid _streamId; | |
| private TimeSpan _timeDrift; | |
| private DateTime _lastCall; | |
| public WaveStreamer(IClient client, WaveStream stream) | |
| { | |
| _client = client; | |
| _stream = stream; | |
| var bufferSize = (stream.Channels * (stream.SampleRate * (stream.BitDepth / 8))) / 2; | |
| _buffer = new byte[bufferSize]; | |
| } | |
| private void SendSegment(object state) | |
| { | |
| if (_lastCall != DateTime.MinValue) | |
| { | |
| var timeSinceLastCall = DateTime.Now - _lastCall; | |
| var drift = timeSinceLastCall - TimeSpan.FromSeconds(0.5); | |
| _timeDrift += drift; | |
| if (_timeDrift >= TimeSpan.FromSeconds(0.5)) | |
| { | |
| _timeDrift -= TimeSpan.FromSeconds(0.5); | |
| PerformSend(); | |
| } | |
| } | |
| _lastCall = DateTime.Now; | |
| PerformSend(); | |
| } | |
| private void PerformSend() | |
| { | |
| var readCount = _buffer.Length; | |
| if (readCount > _stream.Length - _stream.Position) | |
| readCount = _stream.Length - _stream.Position; | |
| _stream.Read(_buffer, 0, readCount); | |
| if (_stream.Length == _stream.Position) | |
| { | |
| _stream.Rewind(); | |
| if (readCount < _buffer.Length) | |
| { | |
| _stream.Read(_buffer, readCount, _buffer.Length - readCount); | |
| } | |
| } | |
| _client.Send(new Message | |
| { | |
| IsFromServer = true, | |
| IsResponse = false, | |
| Words = new[] | |
| { | |
| new [] { (byte)Command.SpeechPlaybackPacket }, | |
| _streamId.ToByteArray(), | |
| _buffer | |
| } | |
| }, new DomainAction<IClient, Message>((client, message) => { }).Callback); | |
| } | |
| public void BeginSend() | |
| { | |
| _streamId = Guid.NewGuid(); | |
| _timeDrift = TimeSpan.FromSeconds(0); | |
| _lastCall = DateTime.MinValue; | |
| _client.Send(new Message | |
| { | |
| IsFromServer = true, | |
| IsResponse = false, | |
| Words = new[] | |
| { | |
| new[] { (byte)Command.SpeechPlaybackStart }, | |
| _streamId.ToByteArray(), | |
| BitConverter.GetBytes((uint)_stream.SampleRate), | |
| BitConverter.GetBytes((short)_stream.Channels), | |
| BitConverter.GetBytes((ushort)_stream.BitDepth) | |
| } | |
| }, new DomainAction<IClient, Message>((client, message) => | |
| { | |
| _timer = new Timer(SendSegment, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0.5)); | |
| PerformSend(); | |
| Thread.Sleep(TimeSpan.FromSeconds(0.2)); | |
| PerformSend(); | |
| }).Callback); | |
| } | |
| public void Stop() | |
| { | |
| _client.Send(new Message | |
| { | |
| IsFromServer = true, | |
| IsResponse = false, | |
| Words = new[] | |
| { | |
| new [] { (byte)Command.SpeechPlaybackEnd }, | |
| _streamId.ToByteArray() | |
| } | |
| }, new DomainAction<IClient, Message>((client1, message) => { }).Callback); | |
| _timer.Dispose(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment