Esempio n. 1
0
        private static void ApplyFlatAttenuation(ArraySegment <float> frame, float volume)
        {
            if (frame.Array == null)
            {
                throw new ArgumentNullException("frame");
            }

            //Early exit in the (very common) case that volume is exactly 1
            // ReSharper disable once CompareOfFloatsByEqualityOperator (Justification: exact float equality is what we want)
            if (volume == 1)
            {
                return;
            }

            //Just clear the entire array in the (relatively common) case that volume is 0
            // ReSharper disable once CompareOfFloatsByEqualityOperator (Justification: exact float equality is what we want)
            if (volume == 0)
            {
                frame.Clear();
                return;
            }

            //Apply the attenuation to the entire frame
            for (int i = 0; i < frame.Count; i++)
            {
                frame.Array[frame.Offset + i] *= volume;
            }
        }
        public int Decode(EncodedBuffer input, ArraySegment <float> output)
        {
            //Clear output buffer so that it just contains silence
            output.Clear();

            //Output the length of a frame
            return(_frameSize);
        }
Esempio n. 3
0
        public void IList_NotSupported()
        {
            var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };

            IList <long> s = new ArraySegment <long> (array, 2, 3);

            try {
                s.Add(1);
                Assert.Fail("#1");
            } catch (NotSupportedException) {
            }

            try {
                s.Clear();
                Assert.Fail("#2");
            } catch (NotSupportedException) {
            }

            try {
                s.Remove(3);
                Assert.Fail("#3");
            } catch (NotSupportedException) {
            }

            try {
                s.RemoveAt(3);
                Assert.Fail("#4");
            } catch (NotSupportedException) {
            }

            try {
                s.Insert(2, 3);
                Assert.Fail("#5");
            } catch (NotSupportedException) {
            }
        }
        public bool Read(ArraySegment <float> samples)
        {
            var offset = samples.Offset;
            var count  = samples.Count;

            Log.Trace("{0} samples requested", samples.Count);

            while (count > 0)
            {
                // if we have data already buffered..
                if (_firstSample < _lastSample)
                {
                    // copy out what we need
                    var c = Math.Min(count, _lastSample - _firstSample);
                    Log.Trace("Transferring {0} buffered samples from previous read", c);
                    // ReSharper disable once AssignNullToNotNullAttribute (Justification: Array segment cannot be null)
                    Buffer.BlockCopy(_temp, _firstSample * sizeof(float), samples.Array, offset * sizeof(float), c * sizeof(float));

                    offset       += c;
                    count        -= c;
                    _firstSample += c;

                    // if that was the final frame, and we have read all of it
                    if (_upstreamComplete && _firstSample == _lastSample)
                    {
                        // pad the remainder with 0s if we didnt have enough
                        for (var i = offset; i < samples.Offset + samples.Count; i++)
                        {
                            samples.Array[i] = 0;
                        }

                        Log.Trace("Request satisfied ({0} samples provided with {1} samples 0-padded)", offset - samples.Offset, samples.Count - (offset - samples.Offset));

                        // return that we are complete
                        return(true);
                    }
                }

                // break if we have read enough
                if (count == 0)
                {
                    break;
                }

                // if we get here, then we need to read another frame
                _firstSample = 0;
                _lastSample  = _temp.Length;

                //If the upstream has already indicated that it is complete there's not a lot we can do!
                //Clear the buffer and return zeroes
                if (_upstreamComplete)
                {
                    Log.Warn(Log.PossibleBugMessage("Attempting to read from a stream which has already finished", "C88903DE-17D4-4341-9AC6-28EB50BCFC8A"));

                    samples.Clear();
                    return(true);
                }

                Log.Trace("Reading frame ({0} samples)", _temp.Length);
                _upstreamComplete = _source.Read(new ArraySegment <float>(_temp));
            }

            Log.Trace("Request satisfied ({0} samples provided)", offset - samples.Offset);

            // return that there is more available
            return(false);
        }
Esempio n. 5
0
		public void IList_NotSupported ()
		{
			var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };

			IList<long> s = new ArraySegment<long> (array, 2, 3);

			try {
				s.Add (1);
				Assert.Fail ("#1");
			} catch (NotSupportedException) {
			}

			try {
				s.Clear ();
				Assert.Fail ("#2");
			} catch (NotSupportedException) {
			}

			try {
				s.Remove (3);
				Assert.Fail ("#3");
			} catch (NotSupportedException) {
			}

			try {
				s.RemoveAt (3);
				Assert.Fail ("#4");
			} catch (NotSupportedException) {
			}

			try {
				s.Insert (2, 3);
				Assert.Fail ("#5");
			} catch (NotSupportedException) {
			}
		}