생성 된 파형 데이터를 사용하여 .NET에서 사운드를 재생합니다
문제
내 .NET 프로그램이 사용자 입력 및 수학적 함수에서 생성하는 파형 데이터를 기반으로 사운드를 어떻게 재생할 수 있습니까?
"파형 데이터"에 의해 고정 된 간격 시차 (아마도 44.1 kHz)에서 SPL (음압 레벨) 값을 의미합니다. 나는 이것이 일종의 스트리밍 버퍼 배열이 필요하다고 가정합니다.
이것은 라이브/실시간이어야하므로 .wav 파일을 만들면 충분하지 않은 재생. vb.net이 선호되지만 C#도 허용됩니다.
명확히하기 위해 : 내가 찾고있는 것은 간단한 작업 코드 예제입니다.
해결책
당신은 이것을 사용하여 할 수 있습니다 나우 디오. Wavestream에서 파생 된 스트림을 생성하고 재정의 읽기 방법에서는 샘플을 반환하여 즉시 생성 할 수 있습니다. 사운드 카드가 사용하는 버퍼의 크기를 제어하여 대기 시간을 제어 할 수 있습니다.
다른 팁
PlayerEx pl = new PlayerEx();
private static void PlayArray(PlayerEx pl)
{
double fs = 8000; // sample freq
double freq = 1000; // desired tone
short[] mySound = new short[4000];
for (int i = 0; i < 4000; i++)
{
double t = (double)i / fs; // current time
mySound[i] = (short)(Math.Cos(t * freq) * (short.MaxValue));
}
IntPtr format = AudioCompressionManager.GetPcmFormat(1, 16, (int)fs);
pl.OpenPlayer(format);
byte[] mySoundByte = new byte[mySound.Length * 2];
Buffer.BlockCopy(mySound, 0, mySoundByte, 0, mySoundByte.Length);
pl.AddData(mySoundByte);
pl.StartPlay();
}
체크 아웃 이 스레드 로드시 DirectSound 임의의 데이터가있는 버퍼 및 재생.
의견 당 : 예, 알고 있습니다. C ++를 C# 또는 vb.net으로 변환해야합니다. 그러나 개념은 중요한 것입니다. 보조 DirectSound 버퍼를 생성 한 다음이를 사용하여 기본 버퍼로 스트리밍하고 재생합니다.
나는 당신이 사용해야한다고 생각합니다 DirectSound (Directx API). 생성 된 데이터로 채울 수있는 버퍼에서 작동합니다.
이 코드가 있지만 메모리에서 WAV 파일을 생성하려면 코드가 있어야합니다.
Option Strict Off
Option Explicit On
Imports Microsoft.DirectX.DirectSound
Imports Microsoft.DirectX
Imports System.Threading
Public Class Form1
Const SRATE As Integer = 44100
Const FREQ As Integer = 440
Const DUR As Integer = 1
Private dsDesc As BufferDescription
Private wvFormat As WaveFormat
Private DS As Device
Dim SecondaryBuffer As Microsoft.DirectX.DirectSound.SecondaryBuffer
Dim BufferDescription As Microsoft.DirectX.DirectSound.BufferDescription
Dim DXFormat As Microsoft.DirectX.DirectSound.WaveFormat
Dim sbuf(DUR * SRATE) As Short
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Show()
DS = New Microsoft.DirectX.DirectSound.Device
DS.SetCooperativeLevel(Me, CooperativeLevel.Normal)
wvFormat.FormatTag = WaveFormatTag.Pcm
wvFormat.Channels = 1
wvFormat.SamplesPerSecond = SRATE
wvFormat.BitsPerSample = 16
wvFormat.AverageBytesPerSecond = 2 * SRATE
wvFormat.BlockAlign = 2
dsDesc = New BufferDescription(wvFormat)
dsDesc.BufferBytes = 2 * DUR * SRATE
dsDesc.Flags = 0
Dim buff1 = PlayWave(400)
Dim buff2 = PlayWave(600)
buff1 = PlayWave(400)
buff1.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default)
Thread.Sleep(1000)
buff1 = PlayWave(600)
buff1.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default)
' End
End Sub
Function PlayWave(FREQ As Integer) As SecondaryBuffer
' create a buffer
Dim dsBuffer As SecondaryBuffer
dsBuffer = New SecondaryBuffer(dsDesc, DS)
Dim sbuf(DUR * SRATE) As Short
' create tone
For i As Integer = 0 To DUR * SRATE
sbuf(i) = CShort(10000 * Math.Sin(2 * Math.PI * FREQ * i / SRATE))
Next
' copy to buffer
dsBuffer.Write(0, sbuf, LockFlag.EntireBuffer)
Return dsBuffer
End Function
제휴하지 않습니다 StackOverflow