/// <summary> /// Reads bytes from this WaveProvider /// </summary> public int Read(byte[] buffer, int offset, int count) { int sourceBytesRequired = count * 2; this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(buffer); int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired); int samplesRead = sourceBytesRead / 2; int destOffset = offset / 2; for (int sample = 0; sample < samplesRead; sample += 2) { short left = sourceWaveBuffer.ShortBuffer[sample]; short right = sourceWaveBuffer.ShortBuffer[sample + 1]; float outSample = (left * LeftVolume) + (right * RightVolume); // hard limiting if (outSample > Int16.MaxValue) outSample = Int16.MaxValue; if (outSample < Int16.MinValue) outSample = Int16.MinValue; destWaveBuffer.ShortBuffer[destOffset++] = (short)outSample; } return sourceBytesRead / 2; }
public override int Read(byte[] buffer, int offset, int count) { int sourceBytesRequired = count * 2; WaveBuffer destWaveBuffer = new WaveBuffer(buffer); int samplesRead = FBufferCount / 4; int destOffset = offset / 4; short ch1, ch2, ch3, ch4; short outSample1, outSample2; for (int sample = 0; sample < samplesRead; sample += 4) { ch1 = FBufferIn.ShortBuffer[sample]; ch2 = FBufferIn.ShortBuffer[sample + 1]; ch3 = FBufferIn.ShortBuffer[sample + 2]; ch4 = FBufferIn.ShortBuffer[sample + 3]; outSample1 = ch1;// (ch1 + ch2) / 2; outSample2 = 0;// *ch3;// (ch3 + ch4) / 2; destWaveBuffer.ShortBuffer[destOffset++] = outSample1; destWaveBuffer.ShortBuffer[destOffset++] = outSample2; } return FBufferCount / 2; // bytes returned }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> // Token: 0x06000A8A RID: 2698 RVA: 0x0001F044 File Offset: 0x0001D244 public int Read(byte[] destBuffer, int offset, int numBytes) { int num = numBytes * 2; this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, num); int num2 = this.sourceProvider.Read(this.sourceBuffer, 0, num); WaveBuffer waveBuffer = new WaveBuffer(this.sourceBuffer); WaveBuffer waveBuffer2 = new WaveBuffer(destBuffer); int num3 = num2 / 4; int num4 = offset / 2; for (int i = 0; i < num3; i++) { float num5 = waveBuffer.FloatBuffer[i] * this.volume; if (num5 > 1f) { num5 = 1f; } if (num5 < -1f) { num5 = -1f; } waveBuffer2.ShortBuffer[num4++] = (short)(num5 * 32767f); } return(num3 * 2); }
public int Read(byte[] buffer, int offset, int count) { int num = count * 2; this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, num); WaveBuffer waveBuffer = new WaveBuffer(this.sourceBuffer); WaveBuffer waveBuffer2 = new WaveBuffer(buffer); int num2 = this.sourceProvider.Read(this.sourceBuffer, 0, num); int num3 = num2 / 2; int num4 = offset / 2; for (int i = 0; i < num3; i += 2) { short num5 = waveBuffer.ShortBuffer[i]; short num6 = waveBuffer.ShortBuffer[i + 1]; float num7 = (float)num5 * this.LeftVolume + (float)num6 * this.RightVolume; if (num7 > 32767f) { num7 = 32767f; } if (num7 < -32768f) { num7 = -32768f; } waveBuffer2.ShortBuffer[num4++] = (short)num7; } return(num2 / 2); }
/// <summary> /// Reads bytes from this WaveProvider /// </summary> public int Read(byte[] buffer, int offset, int count) { int sourceBytesRequired = count * 2; sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(buffer); int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired); int samplesRead = sourceBytesRead / 2; int destOffset = offset / 2; for (int sample = 0; sample < samplesRead; sample += 2) { short left = sourceWaveBuffer.ShortBuffer[sample]; short right = sourceWaveBuffer.ShortBuffer[sample + 1]; float outSample = (left * LeftVolume) + (right * RightVolume); // hard limiting if (outSample > Int16.MaxValue) { outSample = Int16.MaxValue; } if (outSample < Int16.MinValue) { outSample = Int16.MinValue; } destWaveBuffer.ShortBuffer[destOffset++] = (short)outSample; } return(sourceBytesRead / 2); }
public int Read(byte[] buffer, int offset, int count) { var waveBuffer = new WaveBuffer(buffer); var samplesRequired = count / 4; var samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired); return samplesRead * 4; }
/// <summary> /// Implements the Read method of IWaveProvider by delegating to the abstract /// Read method taking a short array /// </summary> public int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int samplesRequired = count / 2; int samplesRead = Read(waveBuffer.ShortBuffer, offset / 2, samplesRequired); return samplesRead * 2; }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> public int Read(byte[] destBuffer, int offset, int numBytes) { int sourceBytesRequired = numBytes * 2; byte[] sourceBuffer = GetSourceBuffer(sourceBytesRequired); int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer); int sourceSamples = sourceBytesRead / 4; int destOffset = offset / 2; for (int sample = 0; sample < sourceSamples; sample++) { // adjust volume float sample32 = sourceWaveBuffer.FloatBuffer[sample] * volume; // clip if (sample32 > 1.0f) { sample32 = 1.0f; } if (sample32 < -1.0f) { sample32 = -1.0f; } destWaveBuffer.ShortBuffer[destOffset++] = (short)(sample32 * 32767); } return(sourceSamples * 2); }
public override int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int count2 = count / 4; int num = this.Read(waveBuffer.FloatBuffer, offset / 4, count2); return(num * 4); }
/// <summary> /// Implements the Read method of IWaveProvider by delegating to the abstract /// Read method taking a short array /// </summary> public int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int samplesRequired = count / 2; int samplesRead = Read(waveBuffer.ShortBuffer, offset / 2, samplesRequired); return(samplesRead * 2); }
public int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int sampleCount = count / 4; int num = this.Read(waveBuffer.FloatBuffer, offset / 4, sampleCount); return(num * 4); }
public int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int sampleCount = count / 2; int num = this.Read(waveBuffer.ShortBuffer, offset / 2, sampleCount); return(num * 2); }
public SpeexChatCodec(BandMode bandMode, int sampleRate, string description) { decoder = new SpeexDecoder(bandMode); encoder = new SpeexEncoder(bandMode); recordingFormat = new WaveFormat(sampleRate, 16, 2); this.description = description; encoderInputBuffer = new WaveBuffer(recordingFormat.AverageBytesPerSecond); // more than enough }
/// <summary> /// Implements the Read method of IWaveProvider by delegating to the abstract /// Read method taking a float array /// </summary> public int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int samplesRequired = count / 4; int samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired); return(samplesRead * 4); }
static byte[] PcmFromFloat(float[] pdOutput) { WaveBuffer wavebuffer = new WaveBuffer(pdOutput.Length * 4); for (var i = 0; i < pdOutput.Length; i++) { wavebuffer.FloatBuffer[i] = pdOutput[i]; } return wavebuffer.ByteBuffer; }
public byte[] Decode(byte[] data, int offset, int length) { var outputBufferTemp = new byte[length * 320]; var wb = new WaveBuffer(outputBufferTemp); int samplesDecoded = decoder.Decode(data, offset, length, wb.ShortBuffer, 0, false); int bytesDecoded = samplesDecoded * 2; var decoded = new byte[bytesDecoded]; Array.Copy(outputBufferTemp, 0, decoded, 0, bytesDecoded); return decoded; }
public byte[] Decode(byte[] data, int offset, int length) { var outputBufferTemp = new byte[length * 320]; var wb = new WaveBuffer(outputBufferTemp); int samplesDecoded = decoder.Decode(data, offset, length, wb.ShortBuffer, 0, false); int bytesDecoded = samplesDecoded * 2; var decoded = new byte[bytesDecoded]; Array.Copy(outputBufferTemp, 0, decoded, 0, bytesDecoded); //Debug.WriteLine(String.Format("NSpeex: In {0} bytes, decoded {1} bytes [dec frame size = {2}]", length, bytesDecoded, decoder.FrameSize)); return decoded; }
public byte[] Encode(byte[] data, int offset, int length) { if (offset != 0) { throw new ArgumentException("G722 does not yet support non-zero offsets"); } WaveBuffer wb = new WaveBuffer(data); int encodedLength = length / 4; byte[] outputBuffer = new byte[encodedLength]; _codec.Encode(_encoderState, outputBuffer, wb.ShortBuffer, length / 2); return outputBuffer; }
public byte[] Decode(byte[] data, int offset, int length) { if (offset != 0) { throw new ArgumentException("G722 does not yet support non-zero offsets"); } int decodedLength = length * 4; byte[] outputBuffer = new byte[decodedLength]; WaveBuffer wb = new WaveBuffer(outputBuffer); _codec.Decode(_decoderState, wb.ShortBuffer, data, length); return outputBuffer; }
protected override byte[] ProcessRecordedData(byte[] recorded, int offset, int count) { lock (lockObject) { int sourceSamples = count / 4; int outSamples = sourceSamples; byte[] sourceBuffer = new byte[sourceSamples * 4]; Buffer.BlockCopy(recorded, offset, sourceBuffer, 0, sourceSamples * 4); byte[] outBuffer = new byte[outSamples * 2]; WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(outBuffer); int destOffset = 0; for (int sample = 0; sample < sourceSamples; sample++) { #region 32 bit float to 16bit short, same channels float sample32 = sourceWaveBuffer.FloatBuffer[sample] * 1.0f; // clip if (sample32 > 1.0f) sample32 = 1.0f; if (sample32 < -1.0f) sample32 = -1.0f; destWaveBuffer.ShortBuffer[destOffset++] = (short)(sample32 * Int16.MaxValue); #endregion #region 32 bit float to 16bit short, combine left and right channels // adjust volume //float leftSample32 = sourceWaveBuffer.FloatBuffer[sample] * 1.0f; // clip //if (leftSample32 > 1.0f) // leftSample32 = 1.0f; //if (leftSample32 < -1.0f) // leftSample32 = -1.0f; //float rightSample32 = sourceWaveBuffer.FloatBuffer[sample + 1] * 1.0f; //if (rightSample32 > 1.0f) // rightSample32 = 1.0f; //if (rightSample32 < -1.0f) // rightSample32 = -1.0f; //short spl = (short)(((leftSample32 * Int16.MaxValue) + (int)(rightSample32 * Int16.MaxValue)) / 2); //destWaveBuffer.ShortBuffer[destOffset++] = spl; #endregion } Array.Copy(destWaveBuffer.ByteBuffer, convertionStream.SourceBuffer, outSamples * 2); int converted = convertionStream.Convert(outSamples * 2); converted -= converted % 4; byte[] result = new byte[converted]; Array.Copy(convertionStream.DestBuffer, result, converted); return base.ProcessRecordedData(result, 0, converted); } }
public byte[] Decode(byte[] data, int offset, int length) { if (offset != 0) { throw new ArgumentException("G722 does not yet support non-zero offsets"); } int decodedLength = length * 4; var outputBuffer = new byte[decodedLength]; var wb = new WaveBuffer(outputBuffer); int decoded = codec.Decode(decoderState, wb.ShortBuffer, data, length); Debug.Assert(decodedLength == decoded * 2); // because decoded is a number of samples return outputBuffer; }
public VoipCodecSpeex(BandMode bandMode, int sampleRate, string description, VoipCodecMode mode = VoipCodecMode.Both) { _bandMode = bandMode; _recordingFormat = new WaveFormat(sampleRate, 16, 1); _description = description; if (mode.HasFlag(VoipCodecMode.Decode)) _decoder = new SpeexDecoder(bandMode); if (mode.HasFlag(VoipCodecMode.Encode)) { _encoder = new SpeexEncoder(bandMode); _outputBufferTemp = new byte[_recordingFormat.AverageBytesPerSecond]; _encoderInputBuffer = new WaveBuffer(_recordingFormat.AverageBytesPerSecond); // more than enough } }
public AutoTuneWaveProvider(IWaveProvider source, AutoTuneSettings autoTuneSettings) { this.autoTuneSettings = autoTuneSettings; if (source.WaveFormat.SampleRate != 44100) throw new ArgumentException("AutoTune only works at 44.1kHz"); if (source.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) throw new ArgumentException("AutoTune only works on IEEE floating point audio data"); if (source.WaveFormat.Channels != 1) throw new ArgumentException("AutoTune only works on mono input sources"); this.source = source; this.pitchDetector = new AutoCorrelator(source.WaveFormat.SampleRate); this.pitchShifter = new SmbPitchShifter(Settings); this.waveBuffer = new WaveBuffer(8192); }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> public override int Read(byte[] destBuffer, int offset, int numBytes) { lock (lockObject) { int bytesWritten = 0; WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer); // 1. fill with silence if (position < 0) { bytesWritten = (int)Math.Min(numBytes, 0 - position); for (int n = 0; n < bytesWritten; n++) { destBuffer[n + offset] = 0; } } if (bytesWritten < numBytes) { this.sampleProvider.LoadNextChunk(sourceStream, (numBytes - bytesWritten) / 8); float left, right; int outIndex = (offset / 4) + bytesWritten / 4; while (this.sampleProvider.GetNextSample(out left, out right) && bytesWritten < numBytes) { // implement better panning laws. left = (pan <= 0) ? left : (left * (1 - pan) / 2.0f); right = (pan >= 0) ? right : (right * (pan + 1) / 2.0f); left *= volume; right *= volume; destWaveBuffer.FloatBuffer[outIndex++] = left; destWaveBuffer.FloatBuffer[outIndex++] = right; bytesWritten += 8; if (Sample != null) { RaiseSample(left, right); } } } // 3. Fill out with zeroes if (PadWithZeroes && bytesWritten < numBytes) { Array.Clear(destBuffer, offset + bytesWritten, numBytes - bytesWritten); bytesWritten = numBytes; } position += bytesWritten; return(bytesWritten); } }
public int Read(byte[] buffer, int offset, int count) { if (waveBuffer == null || waveBuffer.MaxSize < count) { waveBuffer = new WaveBuffer(count); } int bytesRead = source.Read(waveBuffer, 0, count); //Debug.Assert(bytesRead == count); // the last bit sometimes needs to be rounded up: if (bytesRead > 0) bytesRead = count; //pitchsource->getPitches(); int frames = bytesRead / sizeof(float); // MRH: was count float pitch = pitchDetector.DetectPitch(waveBuffer.FloatBuffer, frames); if(pitch != 0 && Frequency == 0) Frequency = pitch; // MRH: an attempt to make it less "warbly" by holding onto the pitch for at least one more buffer if (pitch == 0f && release < maxHold) { pitch = previousPitch; release++; } else { this.previousPitch = pitch; release = 0; } //int midiNoteNumber = 40; //float targetPitch = (float)(8.175 * Math.Pow(1.05946309, midiNoteNumber)); //WaveBuffer outBuffer = new WaveBuffer(buffer); //pitchShifter.ShiftPitch(waveBuffer.FloatBuffer, pitch, targetPitch, outBuffer.FloatBuffer, frames); //if (frames > 0) // iteration++; //else //{ // Frequency /= (float)iteration; //} return frames * 4; }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> // Token: 0x06000B83 RID: 2947 RVA: 0x000225E8 File Offset: 0x000207E8 public override int Read(byte[] destBuffer, int offset, int numBytes) { int result; lock (this.lockObject) { int num = 0; WaveBuffer waveBuffer = new WaveBuffer(destBuffer); if (this.position < 0L) { num = (int)Math.Min((long)numBytes, -this.position); for (int i = 0; i < num; i++) { destBuffer[i + offset] = 0; } } if (num < numBytes) { this.sampleProvider.LoadNextChunk(this.sourceStream, (numBytes - num) / 8); int num2 = offset / 4 + num / 4; float num3; float num4; while (this.sampleProvider.GetNextSample(out num3, out num4) && num < numBytes) { num3 = ((this.pan <= 0f) ? num3 : (num3 * (1f - this.pan) / 2f)); num4 = ((this.pan >= 0f) ? num4 : (num4 * (this.pan + 1f) / 2f)); num3 *= this.volume; num4 *= this.volume; waveBuffer.FloatBuffer[num2++] = num3; waveBuffer.FloatBuffer[num2++] = num4; num += 8; if (this.Sample != null) { this.RaiseSample(num3, num4); } } } if (this.PadWithZeroes && num < numBytes) { Array.Clear(destBuffer, offset + num, numBytes - num); num = numBytes; } this.position += (long)num; result = num; } return(result); }
public int Read(byte[] destBuffer, int offset, int numBytes) { int num = numBytes / 2; this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, num); int num2 = this.sourceProvider.Read(this.sourceBuffer, offset, num); WaveBuffer waveBuffer = new WaveBuffer(this.sourceBuffer); WaveBuffer waveBuffer2 = new WaveBuffer(destBuffer); int num3 = num2 / 2; int num4 = offset / 4; for (int i = 0; i < num3; i++) { waveBuffer2.FloatBuffer[num4++] = (float)waveBuffer.ShortBuffer[i] / 32768f * this.volume; } return(num3 * 4); }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> public int Read(byte[] destBuffer, int offset, int numBytes) { int sourceBytesRequired = numBytes / 2; byte[] sourceBuffer = GetSourceBuffer(sourceBytesRequired); int sourceBytesRead = sourceProvider.Read(sourceBuffer, offset, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer); int sourceSamples = sourceBytesRead / 2; int destOffset = offset / 4; for (int sample = 0; sample < sourceSamples; sample++) { destWaveBuffer.FloatBuffer[destOffset++] = (sourceWaveBuffer.ShortBuffer[sample] / 32768f) * volume; } return sourceSamples * 4; }
public int Read(byte[] buffer, int offset, int count) { _readBuffer = NAudio.Utils.BufferHelpers.Ensure(_readBuffer, count); _outBuffer = NAudio.Utils.BufferHelpers.Ensure(_outBuffer, count); Array.Clear(_outBuffer, 0, _outBuffer.Length); WaveBuffer outBuffer = new WaveBuffer(_outBuffer); foreach (IWaveProvider prov in _providers) { int ReadCnt = (prov.Read(_readBuffer, 0, _outBuffer.Length) / 2); WaveBuffer readBuff = new WaveBuffer(_readBuffer); for (int i = 0; i < ReadCnt; i++) { outBuffer.ShortBuffer[i] += readBuff.ShortBuffer[i]; } } Array.Copy(_outBuffer, 0, buffer, offset, count); return count; }
/// <summary> /// Reads bytes from this WaveProvider /// </summary> public int Read(byte[] buffer, int offset, int count) { int sourceBytesRequired = count / 2; this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(buffer); int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired); int samplesRead = sourceBytesRead / 2; int destOffset = offset / 2; for (int sample = 0; sample < samplesRead; sample++) { short sampleVal = sourceWaveBuffer.ShortBuffer[sample]; destWaveBuffer.ShortBuffer[destOffset++] = (short)(LeftVolume * sampleVal); destWaveBuffer.ShortBuffer[destOffset++] = (short)(RightVolume * sampleVal); } return samplesRead * 4; }
public void RightChannelOnly() { IWaveProvider stereoStream = new TestStereoProvider(); StereoToMonoProvider16 mono = new StereoToMonoProvider16(stereoStream); mono.LeftVolume = 0.0f; mono.RightVolume = 1.0f; int samples = 1000; byte[] buffer = new byte[samples * 2]; int read = mono.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, read, "bytes read"); WaveBuffer waveBuffer = new WaveBuffer(buffer); short expected = 0; for (int sample = 0; sample < samples; sample++) { short sampleVal = waveBuffer.ShortBuffer[sample]; Assert.AreEqual(expected--, sampleVal, "sample #" + sample.ToString()); } }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> public int Read(byte[] destBuffer, int offset, int numBytes) { int sourceBytesRequired = numBytes / 2; sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired); int sourceBytesRead = sourceProvider.Read(sourceBuffer, offset, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer); int sourceSamples = sourceBytesRead / 2; int destOffset = offset / 4; for (int sample = 0; sample < sourceSamples; sample++) { destWaveBuffer.FloatBuffer[destOffset++] = (sourceWaveBuffer.ShortBuffer[sample] / 32768f) * volume; } return(sourceSamples * 4); }
public int Read(byte[] buffer, int offset, int count) { int num = count / 2; this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, num); WaveBuffer waveBuffer = new WaveBuffer(this.sourceBuffer); WaveBuffer waveBuffer2 = new WaveBuffer(buffer); int num2 = this.sourceProvider.Read(this.sourceBuffer, 0, num); int num3 = num2 / 2; int num4 = offset / 2; for (int i = 0; i < num3; i++) { short num5 = waveBuffer.ShortBuffer[i]; waveBuffer2.ShortBuffer[num4++] = (short)(this.LeftVolume * (float)num5); waveBuffer2.ShortBuffer[num4++] = (short)(this.RightVolume * (float)num5); } return(num3 * 4); }
private static int WaveFloatTo16(byte[] sourceBuffer, int numBytes, byte[] destBuffer) { var sourceWaveBuffer = new WaveBuffer(sourceBuffer); var destWaveBuffer = new WaveBuffer(destBuffer); int sourceSamples = numBytes/4; int destOffset = 0; for (int sample = 0; sample < sourceSamples; sample++) { float sample32 = sourceWaveBuffer.FloatBuffer[sample]; // clip if (sample32 > 1.0f) sample32 = 1.0f; if (sample32 < -1.0f) sample32 = -1.0f; destWaveBuffer.ShortBuffer[destOffset++] = (short) (sample32*32767); } return sourceSamples*2; }
public void LeftChannelOnly() { IWaveProvider monoStream = new TestMonoProvider(); MonoToStereoProvider16 stereo = new MonoToStereoProvider16(monoStream); stereo.LeftVolume = 1.0f; stereo.RightVolume = 0.0f; int samples = 1000; byte[] buffer = new byte[samples * 2]; int read = stereo.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, read, "bytes read"); WaveBuffer waveBuffer = new WaveBuffer(buffer); short expected = 0; for (int sample = 0; sample < samples; sample+=2) { short sampleLeft = waveBuffer.ShortBuffer[sample]; short sampleRight = waveBuffer.ShortBuffer[sample+1]; Assert.AreEqual(expected++, sampleLeft, "sample left"); Assert.AreEqual(0, sampleRight, "sample right"); } }
/// <summary> /// Reads bytes from this WaveProvider /// </summary> public int Read(byte[] buffer, int offset, int count) { var sourceBytesRequired = count / 2; sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, sourceBytesRequired); var sourceWaveBuffer = new WaveBuffer(sourceBuffer); var destWaveBuffer = new WaveBuffer(buffer); var sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired); var samplesRead = sourceBytesRead / 2; var destOffset = offset / 2; for (var sample = 0; sample < samplesRead; sample++) { short sampleVal = sourceWaveBuffer.ShortBuffer[sample]; destWaveBuffer.ShortBuffer[destOffset++] = (short)(LeftVolume * sampleVal); destWaveBuffer.ShortBuffer[destOffset++] = (short)(RightVolume * sampleVal); } return(samplesRead * 4); }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="buffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="bytesCount">Number of bytes read</param> /// <returns>Number of bytes read.</returns> public int Read(byte[] buffer, int offset, int bytesCount) { // How many [SourceBitsPerSample] samples I need to read for reaching [TargetBitsPerSample] bytesCount const int ratio = SourceBitsPerSample / TargetBitsPerSample; var samplesRequired = bytesCount / ratio; _sourceBuffer = BufferHelpers.Ensure(_sourceBuffer, samplesRequired); var sourceSamples = _sourceProvider.Read(_sourceBuffer, 0, samplesRequired); var destWaveBuffer = new WaveBuffer(buffer); var destOffset = offset / ratio; for (var index = 0; index < sourceSamples; index++) { var sample = _sourceBuffer[index]; // sample fits in [-1, 1] so we first add 1 to make it [0, 2]; var normalizedSample = (sample + 1); // multiplying by sbyte.MaxValue to obtain the most significant bits destWaveBuffer.ByteBuffer[destOffset++] = (byte)(normalizedSample * sbyte.MaxValue); } return(sourceSamples); }
/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <param name="offset">Offset into the destination buffer</param> /// <param name="numBytes">Number of bytes read</param> /// <returns>Number of bytes read.</returns> public int Read(byte[] destBuffer, int offset, int numBytes) { int sourceBytesRequired = numBytes * 2; byte[] sourceBuffer = GetSourceBuffer(sourceBytesRequired); int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired); WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer); WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer); int sourceSamples = sourceBytesRead / 4; int destOffset = offset / 2; for (int sample = 0; sample < sourceSamples; sample++) { // adjust volume float sample32 = sourceWaveBuffer.FloatBuffer[sample] * volume; // clip if (sample32 > 1.0f) sample32 = 1.0f; if (sample32 < -1.0f) sample32 = -1.0f; destWaveBuffer.ShortBuffer[destOffset++] = (short)(sample32 * 32767); } return sourceSamples * 2; }
private static byte[] DecodeG722(byte[] g722Data, int sampleRate) { var codec = new G722Codec(); var state = new G722CodecState(sampleRate, G722Flags.SampleRate8000); var decodedLength = g722Data.Length * 2; var outputBuffer = new byte[decodedLength]; var wb = new WaveBuffer(outputBuffer); var length = codec.Decode(state, wb.ShortBuffer, g722Data, g722Data.Length) * 2; if (length != outputBuffer.Length) { var outputBuffer2 = new byte[length]; Buffer.BlockCopy(outputBuffer, 0, outputBuffer2, 0, length); outputBuffer = outputBuffer2; } return outputBuffer; }
public void Write(byte[] buffer, int count) { FBufferIn = new WaveBuffer(buffer); FBufferCount = count; }
/// <summary> /// Reads from this wave stream /// </summary> /// <param name="buffer">Audio buffer</param> /// <param name="offset">Offset into buffer</param> /// <param name="count">Number of bytes required</param> /// <returns>Number of bytes read</returns> public override int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int samplesRequired = count / 4; int samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired); return samplesRead * 4; }
public byte[] Encode(byte[] data, int offset, int length) { if (offset != 0) { throw new ArgumentException("G722 does not yet support non-zero offsets"); } WaveBuffer wb = new WaveBuffer(data); int encodedLength = length / 4 + (length & 0x01); byte[] outputBuffer = new byte[encodedLength]; int encoded = this.codec.Encode(this.encoderState, outputBuffer, wb.ShortBuffer, encodedLength << 1); Debug.Assert(encodedLength == encoded); return outputBuffer; }
private static byte[] EncodeG722(byte[] pcmMonoData, int sampleRate) { var codec = new G722Codec(); var state = new G722CodecState(sampleRate, G722Flags.SampleRate8000); var wb = new WaveBuffer(pcmMonoData); var encodedLength = pcmMonoData.Length / 2; var outputBuffer = new byte[encodedLength]; var length = codec.Encode(state, outputBuffer, wb.ShortBuffer, pcmMonoData.Length / 2); if (length != outputBuffer.Length) { var outputBuffer2 = new byte[length]; Buffer.BlockCopy(outputBuffer, 0, outputBuffer2, 0, length); outputBuffer = outputBuffer2; } return outputBuffer; }
private void RenderFile() { SampleAggregator.RaiseRestart(); using (WaveFileReader reader = new WaveFileReader(this.voiceRecorderState.ActiveFile)) { this.samplesPerSecond = reader.WaveFormat.SampleRate; SampleAggregator.NotificationCount = reader.WaveFormat.SampleRate/10; byte[] buffer = new byte[1024]; WaveBuffer waveBuffer = new WaveBuffer(buffer); waveBuffer.ByteBufferCount = buffer.Length; int bytesRead; do { bytesRead = reader.Read(waveBuffer, 0, buffer.Length); int samples = bytesRead / 2; for (int sample = 0; sample < samples; sample++) { if (bytesRead > 0) { sampleAggregator.Add(waveBuffer.ShortBuffer[sample] / 32768f); } } } while (bytesRead > 0); int totalSamples = (int)reader.Length / 2; TotalWaveFormSamples = totalSamples / sampleAggregator.NotificationCount; SelectAll(); } audioPlayer.LoadFile(this.voiceRecorderState.ActiveFile); }
public void LoadNextChunk(IWaveProvider source, int samplePairsRequired) { int sourceBytesRequired = samplePairsRequired * 8; sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired); sourceWaveBuffer = new WaveBuffer(sourceBuffer); sourceSamples = source.Read(sourceBuffer, 0, sourceBytesRequired) / 4; sourceSample = 0; }
/// <summary> /// Reads from this wave stream /// </summary> /// <param name="buffer">Audio buffer</param> /// <param name="offset">Offset into buffer</param> /// <param name="count">Number of bytes required</param> /// <returns>Number of bytes read</returns> public override int Read(byte[] destBuffer, int offset, int numBytes) { /* var waveBuffer = new WaveBuffer(buffer); int samplesRequired = count / 4; int samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired); return samplesRead * 4; */ lock (lockObject) { int bytesWritten = 0; WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer); // 1. fill with silence if (this.Position < 0) { bytesWritten = (int)Math.Min(numBytes, 0 - this.Position); for (int n = 0; n < bytesWritten; n++) destBuffer[n + offset] = 0; } if (bytesWritten < numBytes) { this.sampleProvider.LoadNextChunk(readerStream, (numBytes - bytesWritten) / 8); float left, right; int outIndex = (offset / 4) + bytesWritten / 4; while (this.sampleProvider.GetNextSample(out left, out right) && bytesWritten < numBytes) { // implement better panning laws. left = (pan <= 0) ? left : (left * (1 - pan) / 2.0f); right = (pan >= 0) ? right : (right * (pan + 1) / 2.0f); left *= this.Volume; right *= this.Volume; destWaveBuffer.FloatBuffer[outIndex++] = left; destWaveBuffer.FloatBuffer[outIndex++] = right; bytesWritten += 8; if (Sample != null) RaiseSample(left, right); } } // 3. Fill out with zeroes if (PadWithZeroes && bytesWritten < numBytes) { Array.Clear(destBuffer, offset + bytesWritten, numBytes - bytesWritten); bytesWritten = numBytes; } this.Position += bytesWritten; return bytesWritten; } }