public override void Write(byte[] buffer, int offset, int count) { int[] samples = new int[count * 8 / bitsPerSample]; if (bitsPerSample == 8) { for (int i = 0; i < count; i++) { samples[i] = buffer[offset + i]; } } else { unsafe { if (offset + count > buffer.Length) throw new IndexOutOfRangeException(); fixed(byte *rawdata = buffer) { if (bitsPerSample == 16) { short *data = (short *)(rawdata + offset); for (int i = 0; i < count / 2; i++) { samples[i] = data[i]; } } else if (bitsPerSample == 32) { int *data = (int *)(rawdata + offset); for (int i = 0; i < count / 4; i++) { samples[i] = data[i]; } } } } } var streaminfo = new FlacBox.FlacStreaminfo(); streaminfo.ChannelsCount = 1; streaminfo.SampleRate = 44100; streaminfo.BitsPerSample = 16; streaminfo.MinBlockSize = 4608; streaminfo.MaxBlockSize = 4608; writer.StartStreamNoHeader(streaminfo); writer.WriteSamples(samples); writer.EndStream(); }
private void WaveDataAvailable(object sender, WaveInEventArgs e) { lock (flacLock) { if (buffer == null || buffer.Length != e.Buffer.Length / 2) { // Reuse the buffer whenever we can. buffer = new int[e.Buffer.Length / 2]; } for (int i = 0; i < e.Buffer.Length; i += 2) { buffer[i / 2] = BitConverter.ToInt16(e.Buffer, i); } MaybeCreateFlacWriter(); flacWriter.WriteSamples(buffer); } if (audioAsr != null) { audioAsr.AddSamples(e.Buffer, e.BytesRecorded); } }