/// <summary>Reads processed data to the provided buffer.</summary> /// <param name="buffer">The buffer.</param> /// <param name="offset">The offset to the first value.</param> /// <param name="count">The number of the values to read.</param> /// <returns>The number of read values.</returns> public int Read(float[] buffer, int offset, int count) { int read = resampler.Read(buffer, offset, count); SamplesAvailable?.Invoke(this, new SamplesAvailableEventArgs(buffer, offset, read)); return(read); }
public Afedri() { settings.SamplingRate = DEFAULT_SAMPLING_RATE; Format = new SignalFormat(DEFAULT_SAMPLING_RATE, true, settings.IsSync(), settings.ChannelCount(), -(int)(DEFAULT_SAMPLING_RATE * 0.47), (int)(DEFAULT_SAMPLING_RATE * 0.47), 0); buffer.SamplesAvailable += (o, e) => SamplesAvailable?.Invoke(this, e); }
/// <summary>Writes floating point values to the specified buffer.</summary> /// <param name="buffer">The buffer.</param> /// <param name="offset">The offset to the first value.</param> /// <param name="count">The number of values.</param> public void Write(float[] buffer, int offset, int count) { if (count > ringBuffer.Length) { //throw new ArgumentException($"Too many values to write: {count}."); Resize(count); } lock (lockObject) { int spaceAvailable = ringBuffer.Length - Count; int count1 = Math.Min(count, ringBuffer.Length - writePos); int count2 = count - count1; if (count1 > 0) { Array.Copy(buffer, offset, ringBuffer, writePos, count1); writePos += count1; if (writePos == ringBuffer.Length) { writePos = 0; } } if (count2 > 0) { Array.Copy(buffer, offset + count1, ringBuffer, 0, count2); writePos = count2; } Count += count; //buffer overflow if (count > spaceAvailable) { Count = ringBuffer.Length; readPos = writePos; } } SamplesAvailable?.Invoke(this, new SamplesAvailableEventArgs((float[])buffer.Clone(), offset, count)); }
/// <summary> /// Ensure that registered delegates receive the SamplesAvailable event. /// </summary> /// <param name="e">Event argument.</param> private void OnSamplesAvailable(SamplesAvailableEventArgs e) { // If there are subscriber(s), raise event. SamplesAvailable?.Invoke(this, e); }