■ RawSourceWaveStream 클래스를 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
using System; using System.IO; using System.Threading; using NAudio.Wave; int sampleRate = 16000; int frequency = 500; double amplitude = 0.2; int secondCount = 5; byte[] bufferByteArray = new byte[sampleRate * secondCount * 2]; double multiple = 2.0 * frequency / sampleRate; for(int i = 0; i < sampleRate * secondCount; i++) { double sampleSaw = ((i * multiple) % 2) - 1; double sampleValueDouble = sampleSaw > 0 ? amplitude : -amplitude; short sampleValueShort = (short)(sampleValueDouble * short.MaxValue); byte[] sampleValueByteArray = BitConverter.GetBytes(sampleValueShort); bufferByteArray[i * 2 ] = sampleValueByteArray[0]; bufferByteArray[i * 2 + 1] = sampleValueByteArray[1]; } MemoryStream memoryStream = new MemoryStream(bufferByteArray); RawSourceWaveStream rawSourceWaveStream = new RawSourceWaveStream ( memoryStream, new WaveFormat(sampleRate, 16, 1) ); WaveOutEvent waveOutEvent = new WaveOutEvent(); waveOutEvent.Init(rawSourceWaveStream); waveOutEvent.Play(); while(waveOutEvent.PlaybackState == PlaybackState.Playing) { Thread.Sleep(500); } waveOutEvent.Dispose(); |