// Draw the waveform in a graphics // TODO: handle other bit depths than 16 bit. private void DrawWaveform(Graphics g, AudioBlock block) { PCMFormatInfo format = this.audio.getPCMFormat(); if (format.getBitDepth() != 16) { throw new Exception("Cannot deal with bitdepth others than 16."); } ushort channels = format.getNumberOfChannels(); ushort frameSize = format.getBlockAlign(); int samplesPerPixel = (int)Math.Ceiling(this.audio.getPCMLength() / (float)frameSize / Width * channels); int bytesPerPixel = samplesPerPixel * frameSize / channels; byte[] bytes = new byte[bytesPerPixel]; short[] samples = new short[samplesPerPixel]; System.IO.Stream au = this.audio.getAudioData(); for (int x = 0; x < Width; ++x) { int read = au.Read(bytes, 0, bytesPerPixel); Buffer.BlockCopy(bytes, 0, samples, 0, read); DrawChannel(g, block.Highlighted ? block.Colors.WaveformChannelSelectedPen : channels == 1 ? block.Colors.WaveformMonoPen : block.Colors.WaveformChannel1Pen, samples, x, read, frameSize, 0, channels); if (channels == 2) { DrawChannel(g, block.Selected ? block.Colors.WaveformChannelSelectedPen : block.Colors.WaveformChannel2Pen, samples, x, read, frameSize, 1, channels); } } au.Close(); }