public void Test16BitSingleChannelRoundTrip() { const int samplesPerSecond = 44_100; const double delta = 1.0 / samplesPerSecond; Func <double, double> func = (double t) => Math.Cos(2.0 * Math.PI * t); short[] data = new short[samplesPerSecond]; for (int i = 0; i < samplesPerSecond; ++i) { data[i] = (short)(short.MaxValue * func(i * delta)); } using var ms = new MemoryStream(); var writer = new WaveWriter(ms, samplesPerSecond); writer.Write(data.Length, data); ms.Position = 0; var reader = new WaveReader(ms); Assert.AreEqual(reader.Format.Channels, 1); Assert.AreEqual(reader.Format.SamplesPerSecond, samplesPerSecond); Assert.AreEqual(reader.Format.BitsPerSample, 16); var channel = reader.GetChannelInt16(0); int h = 0; var e = channel.GetEnumerator(); while (e.MoveNext()) { Assert.AreEqual(data[h++], e.Current, $"Index: {h}"); } }
public WaveFormView2(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } InitializeComponent(); _Reader = new WaveReader(stream); for (short i = 0; i < _Reader.Format.Channels; ++i) { switch (_Reader.Format.BitsPerSample) { case 8: this.wavePlot.plt.PlotSignalConst(_Reader.GetChannelInt8(i).Select(x => (float)x).ToArray()); break; case 16: this.wavePlot.plt.PlotSignalConst(_Reader.GetChannelInt16(i).Select(x => (float)x).ToArray()); break; case 32: this.wavePlot.plt.PlotSignalConst(_Reader.GetChannelFloat(i).ToArray()); break; default: throw new NotSupportedException($"Invalid sample size: {_Reader.Format.BitsPerSample}."); } ; } this.wavePlot.MouseWheel += WavePlot_MouseWheel; this.wavePlot.plt.AxisAuto(horizontalMargin: 0, verticalMargin: 0); }