public int Read(float[] buffer, int offset, int count)
        {
            BufferedData srcBuffer = buffers[bufferIndex];

            if (!srcBuffer.IsValid)
            {
                return(0);
            }

            int samplesRead = srcBuffer.CopySamples(buffer, offset, count);

            while (samplesRead < count)
            {
                if (srcBuffer.EndOfStream)
                {
                    return(samplesRead);
                }

                // A buffer swap will be required
                WaitForBufferTask();

                bufferIndex = (bufferIndex + 1) % buffers.Length;
                srcBuffer   = buffers[bufferIndex];

                if (!srcBuffer.EndOfStream)
                {
                    StartFillBufferTask();
                }

                samplesRead += srcBuffer.CopySamples(buffer, offset + samplesRead, count - samplesRead);
            }

            return(samplesRead);
        }
Example #2
0
        private bool CheckShouldRemove(ISyncable syncable, object instance, BufferedData buffer)
        {
            if (buffer.Sent && Equals(buffer.ToSend, buffer.Actual))
            {
                return(true);
            }

            object currentValue = syncable.Get(instance);

            if (Equals(currentValue, buffer.Actual))
            {
                return(false);
            }

            if (buffer.Sent)
            {
                return(true);
            }

            buffer.Actual = currentValue;
            return(false);
        }
Example #3
0
        private async void BeginPoll()
        {
            try
            {
                if (!this.IsValid)
                {
                    return;
                }
                this.IsPolling = true;
Label_007E:
                if (this.cts.IsCancellationRequested)
                {
                    return;
                }
                this._joystick.Poll();
                BufferedDataCollection bufferedData = this._joystick.GetBufferedData();
                if (bufferedData != null)
                {
                    IEnumerator enumerator = bufferedData.GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            BufferedData current = (BufferedData)enumerator.Current;
                            if (current.ButtonPressedData == 1)
                            {
                                int button = Array.FindIndex <byte>(this.Buttons, b => b != 0);
                                if (button == -1)
                                {
                                    continue;
                                }
                                this.OnButtonPressed(button);
                                goto Label_03F5;
                            }
                            if (current.ButtonPressedData == 0)
                            {
                                if (this.LeftStickX > 0xafc8)
                                {
                                    this.OnDirectionChanged("right");
                                    await Task.Delay(250);
                                }
                                else if (this.LeftStickX < 0x4a38)
                                {
                                    this.OnDirectionChanged("left");
                                    await Task.Delay(250);
                                }
                                if (this.LeftStickY > 0xafc8)
                                {
                                    this.OnDirectionChanged("down");
                                    await Task.Delay(250);

                                    continue;
                                }
                                if (this.LeftStickY < 0x4a38)
                                {
                                    this.OnDirectionChanged("up");
                                    await Task.Delay(250);
                                }
                            }
                        }
                    }
                    finally
                    {
                        IDisposable asyncVariable1;
                        asyncVariable1 = enumerator as IDisposable;
                        if (asyncVariable1 != null)
                        {
                            asyncVariable1.Dispose();
                        }
                    }
                }
Label_03F5:
                if (this._joystick != null)
                {
                    switch (this.DirectionalButtons[0])
                    {
                    case 0:
                        this.OnDirectionChanged("up");
                        await Task.Delay(250);

                        break;

                    case 0x2328:
                        this.OnDirectionChanged("right");
                        await Task.Delay(250);

                        break;

                    case 0x4650:
                        this.OnDirectionChanged("down");
                        await Task.Delay(250);

                        break;

                    case 0x6978:
                        this.OnDirectionChanged("left");
                        await Task.Delay(250);

                        break;
                    }
                }
                goto Label_007E;
            }
            catch (Exception exception)
            {
                this.IsPolling = false;
                Console.WriteLine(exception.Message);
            }
        }
 private static void FillBuffer(IBGCStream stream, BufferedData buffer)
 {
     buffer.Size        = stream.Read(buffer.Samples, 0, buffer.Samples.Length);
     buffer.Offset      = 0;
     buffer.NumChannels = stream.Channels;
 }
        /// <summary>
        /// Copies the double-buffered audio into the destination buffer, a starting new
        /// async buffering task if necessary.
        /// This function calls itself recursively if there is not enough data in the current
        /// buffer index.
        /// </summary>
        /// <param name="dstBuffer">The buffer to copy to. Channel data is interleaved.</param>
        /// <param name="dstBufferOffset">The index into the buffer to begin copying to.</param>
        /// <param name="dstNumChannels">The number of interleaved channels in the buffer. Must be set to 1 or 2.</param>
        private void CopyBufferedAudio(float[] dstBuffer, int dstBufferOffset, int dstNumChannels)
        {
            Debug.Assert(dstNumChannels > 0);

            int dstSamplesToCopyPerChannel = (dstBuffer.Length - dstBufferOffset) / dstNumChannels;

            if (dstSamplesToCopyPerChannel == 0)
            {
                // 0 bytes to copy, so do nothing.
                return;
            }

            if (dstNumChannels != 1 && dstNumChannels != 2)
            {
                Debug.LogError($"Completely unexpected Player channel count: {dstNumChannels}");
                return;
            }

            bool         endOfStream = false;
            BufferedData srcBuffer   = buffers[bufferIndex];

            if (!srcBuffer.IsValid)
            {
                ClearSamples(dstBuffer, dstBufferOffset);
                endOfStream = true;
            }
            else
            {
                int srcNumChannels = srcBuffer.NumChannels;
                if (dstNumChannels != srcNumChannels)
                {
                    Debug.LogError($"Stream/Player channel mismatch: Src channels = {srcNumChannels}, Dst channels = {dstNumChannels}");
                }

                int srcSamplesAvailablePerChannel = srcBuffer.SamplesRemaining / srcNumChannels;
                int numSamplesToCopyPerChannel    = srcSamplesAvailablePerChannel > dstSamplesToCopyPerChannel ? dstSamplesToCopyPerChannel : srcSamplesAvailablePerChannel;
                CopySamples(srcBuffer.Samples, srcBuffer.Offset, srcNumChannels, dstBuffer, dstBufferOffset, dstNumChannels, numSamplesToCopyPerChannel);
                srcBuffer.Offset           += numSamplesToCopyPerChannel * srcNumChannels;
                dstSamplesToCopyPerChannel -= numSamplesToCopyPerChannel;
                dstBufferOffset            += numSamplesToCopyPerChannel * dstNumChannels;

                if (dstSamplesToCopyPerChannel > 0)
                {
                    if (!srcBuffer.EndOfStream)
                    {
                        // A buffer swap will be required

                        // Unless the stream processing speed is slower than the sample rate, the task should
                        // already by completed by now.
                        WaitForBufferTask();

                        bufferIndex = (bufferIndex + 1) % buffers.Length;
                        srcBuffer   = buffers[bufferIndex];

                        if (!srcBuffer.EndOfStream)
                        {
                            StartFillBufferTask();
                        }
                        CopyBufferedAudio(dstBuffer, dstBufferOffset, dstNumChannels);
                    }
                    else
                    {
                        ClearSamples(dstBuffer, dstBufferOffset);
                        endOfStream = true;
                    }
                }
            }

            if (endOfStream)
            {
                currentState = SoundState.Stopped;
                playbackEndedNotifier?.Invoke();
            }
        }
 private void FillBuffer(BufferedData buffer)
 {
     buffer.Size   = internalStream.Read(buffer.Samples, 0, buffer.Samples.Length);
     buffer.Offset = 0;
 }