첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
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();
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요