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.

103 lines
4.2 KiB

  1. # Using RawSourceWaveStream
  2. `RawSourceWaveStream` is useful when you have some raw audio, which might be PCM or compressed, but it is not contained within a file format. `RawSourceWaveStream` allows you to specify the `WaveFormat` associated with the raw audio. Let's see some examples.
  3. ## Playing from a Byte array
  4. Suppose we have a byte array containing raw 16 bit mono PCM, and want to play it.
  5. For demo purposes, let's create a 5 second sawtooth wave into the `raw`. Obviously `SignalGenerator` would be a better way to do this, but we are simulating getting a byte array from somewhere else, maybe received over the network.
  6. ```c#
  7. var sampleRate = 16000;
  8. var frequency = 500;
  9. var amplitude = 0.2;
  10. var seconds = 5;
  11. var raw = new byte[sampleRate * seconds * 2];
  12. var multiple = 2.0*frequency/sampleRate;
  13. for (int n = 0; n < sampleRate * seconds; n++)
  14. {
  15. var sampleSaw = ((n*multiple)%2) - 1;
  16. var sampleValue = sampleSaw > 0 ? amplitude : -amplitude;
  17. var sample = (short)(sampleValue * Int16.MaxValue);
  18. var bytes = BitConverter.GetBytes(sample);
  19. raw[n*2] = bytes[0];
  20. raw[n*2 + 1] = bytes[1];
  21. }
  22. ```
  23. `RawSourceWaveStream` takes a `Stream` and a `WaveFormat`. The `WaveFormat` in this instance is 16 bit mono PCM. The stream we can use `MemoryStream` for, passing in our byte array.
  24. ```c#
  25. var ms = new MemoryStream(raw);
  26. var rs = new RawSourceWaveStream(ms, new WaveFormat(sampleRate, 16, 1));
  27. ```
  28. And now we can play the `RawSourceWaveStream` just like it was any other `WaveStream`:
  29. ```c#
  30. var wo = new WaveOutEvent();
  31. wo.Init(rs);
  32. wo.Play();
  33. while (wo.PlaybackState == PlaybackState.Playing)
  34. {
  35. Thread.Sleep(500);
  36. }
  37. wo.Dispose();
  38. ```
  39. ## Turning a raw file into WAV
  40. Suppose we have a raw audio file and we know the wave format of the audio in it. Let's say its 8kHz 16 bit mono. We can just open the file with `File.OpenRead` and pass it into a `RawSourceWaveStream`. Then we can convert it to a WAV file with `WaveFileWriter.CreateWaveFile`.
  41. ```c#
  42. var path = "example.pcm";
  43. var s = new RawSourceWaveStream(File.OpenRead(path), new WaveFormat(8000,1));
  44. var outpath = "example.wav";
  45. WaveFileWriter.CreateWaveFile(outpath, s);
  46. ```
  47. Note that WAV files can contain compressed audio, so as long as you know the correct `WaveFormat` structure you can use that. Let's look at a compressed audio example next.
  48. ## Converting G.729 audio into a PCM WAV
  49. Suppose we have a `.g729` file containing raw audio compressed with G.729. G.729 isn't actually a built-in `WaveFormat` in NAudio (some other common ones like mu and a-law are). But we can use `WaveFormat.CreateCustomFormat` or even derive from `WaveFormat` to define the correct format.
  50. Now in the previous example we saw how we could create a WAV file that contains the G.729 audio still encoded. But if we wanted it to be PCM, we'd need to use `WaveFormatConversionStream.CreatePcmStream` to look for an ACM codec that understands the incoming `WaveFormat` and can turn it into PCM.
  51. Please note that this won't always be possible. If your version of Windows doesn't have a suitable decoder, this will fail.
  52. But here's how we would convert that raw G.729 file into a PCM WAV file if we did have a suitable decoder:
  53. ```c#
  54. var inFile = @"c:\users\mheath\desktop\chirpg729.g729";
  55. var outFile = @"c:\users\mheath\desktop\chirpg729.wav";
  56. var inFileFormat = WaveFormat.CreateCustomFormat(
  57. WaveFormatEncoding.G729,
  58. 8000, // sample rate
  59. 1, // channels
  60. 1000, // average bytes per second
  61. 10, // block align
  62. 1); // bits per sample
  63. using(var inStream = File.OpenRead(inFile))
  64. using(var reader = new RawSourceWaveStream(inStream, inFileFormat))
  65. using(var converter = WaveFormatConversionStream.CreatePcmStream(reader))
  66. {
  67. WaveFileWriter.CreateWaveFile(outFile, converter);
  68. }
  69. ```
  70. If it was a format that NAudio has built-in support for like G.711 a-law, then we'd do it like this:
  71. ```c#
  72. var inFile = @"c:\users\mheath\desktop\alaw.bin";
  73. var outFile = @"c:\users\mheath\desktop\alaw.wav";
  74. var inFileFormat = WaveFormat.CreateALawFormat(8000,1);
  75. using(var inStream = File.OpenRead(inFile))
  76. using(var reader = new RawSourceWaveStream(inStream, inFileFormat))
  77. using(var converter = WaveFormatConversionStream.CreatePcmStream(reader))
  78. {
  79. WaveFileWriter.CreateWaveFile(outFile, converter);
  80. }
  81. ```