Audio and MIDI library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
4.1 KiB

  1. # Render an Audio Wave Form to PNG
  2. NAudio does not include any visualization code in the core library, but it does provid access to the raw audio samples which you need to render wave-forms.
  3. NAudio does however provide a sample project at GitHub: [NAudio.WaveFormRenderer](https://github.com/naudio/NAudio.WaveFormRenderer) which makes use of `NAudio` and `System.Drawing` to render waveforms in a variety of styles.
  4. ![Orange Blocks](https://cloud.githubusercontent.com/assets/147668/18606778/5a9516ac-7cb1-11e6-8660-a0a80d72fe26.png)
  5. ## WaveFormRendererLib
  6. The `WaveFormRendererLib` project contains a customizable waveform rendering algorithm, allowing you to
  7. The waveform rendering algorithm is customizable:
  8. - Supports several peak calculation strategies (max, average, sampled, RMS, decibels)
  9. - Supports different colors or gradients for the top and bottom half
  10. - Supports different sizes for top and bottom half
  11. - Overall image size and background can be customized
  12. - Transparent backgrounds
  13. - Support for SoundCloud style bars
  14. - Several built-in rendering styles
  15. ## WaveFormRenderer
  16. The `WaveFormRenderer` class allows easy rendering of files. We need to create some configuration options first.
  17. The peak provider decides how peaks are calculated. There are four built in options you can choose from. `MaxPeakProvider` simply picks out the maximum sample value in the timeblock that each bar represents. `RmsPeakProvider` calculates the root mean square of each sample and returns the maximum value found in a specified blcok. The `SamplingPeakProvider` simply samples the samples, and you pass in a sample interval.Finally the `AveragePeakProvider` averages the sample values and takes a scale parameter to multiply the average by as it tends to produce lower values.
  18. ```c#
  19. var maxPeakProvider = new MaxPeakProvider();
  20. var rmsPeakProvider = new RmsPeakProvider(blockSize); // e.g. 200
  21. var samplingPeakProvider = new SamplingPeakProvider(sampleInterval); // e.g. 200
  22. var averagePeakProvider = new AveragePeakProvider(scaleFactor); // e.g. 4
  23. ```
  24. Next we need to provide the rendering settings. This is an instance of `WaveFormRendererSettings` which specifies:
  25. - **Width** - the width of the rendered image in pixels
  26. - **TopHeight** - height of the top half of the waveform in pixels
  27. - **BottomHeight** - height of the bottom half of the waveform in pixels. Normally set to the same as `TopHeight` but can be 0 or smaller for asymmetric waveforms
  28. - **PixelsPerPeak** - allows for wider bars to represent each peak. Usually set to 1.
  29. - **SpacerPixels** - allows blank spaces to be inserted between vertical bars. Usually 0 unless when wide bars are used.
  30. - **TopPeakPen** - Pen to draw the top bars with
  31. - **TopSpacerPen** - Pen to draw the top spacer bars with
  32. - **BottomPeakPen** - Pen to draw the bottom bars with
  33. - **BottomSpacerPen** - Pen to draw the bottom spacer bars with
  34. - **DecibelScale** - if true, convert values to decibels for a logarithmic waveform
  35. - **BackgroundColor** - background color (used if no `BackgroundImage` is specified)
  36. - **BackgroundImage** - background image (alternative to solid color)
  37. To simplify setting up an instance of `WaveFormRendererSettings` several derived types are supplied including
  38. `StandardWaveFormRendererSettings`, `SoundCloudOriginalSettings` and `SoundCloudBlockWaveFormSettings`. The latter two mimic rendering styles that have been used by SoundCloud in the past.
  39. ```c#
  40. var myRendererSettings = new StandardWaveFormRendererSettings();
  41. myRendererSettings.Width = 640;
  42. myRendererSettings.TopHeight = 32;
  43. myRendererSettings.BottomHeight = 32;
  44. ```
  45. Now we just need to create our `WaveFormRenderer` and give it a path to the file we want to render, and pass in the peak provider we've chosen and the renderer settings:
  46. ```C#
  47. var renderer = new WaveFormRenderer();
  48. var audioFilePath = "myfile.mp3";
  49. var image = renderer.Render(audioFilePath, myPeakProvider, myRendererSettings);
  50. ```
  51. With that image we could render it to a WinForms picturebox:
  52. ```c#
  53. pictureBox1.Image = image;
  54. ```
  55. Or we could save it to a PNG file which you'd want to do if you were rendering on a web server for example:
  56. ```c#
  57. image.Save("myfile.png", ImageFormat.Png);
  58. ```