사용 NAudio 도서관.
class NotesWaveProvider : WaveProvider32
{
public NotesWaveProvider(Queue<Note> notes)
{
this.Notes = notes;
}
public readonly Queue<Note> Notes;
int sample = 0;
Note NextNote()
{
for (; ;)
{
if (Notes.Count == 0)
return null;
var note = Notes.Peek();
if (sample < note.Duration.TotalSeconds * WaveFormat.SampleRate)
return note;
Notes.Dequeue();
sample = 0;
}
}
public override int Read(float[] buffer, int offset, int sampleCount)
{
int sampleRate = WaveFormat.SampleRate;
for (int n = 0; n < sampleCount; n++)
{
var note = NextNote();
if (note == null)
buffer[n + offset] = 0;
else
buffer[n + offset] = (float)(note.Amplitude * Math.Sin((2 * Math.PI * sample * note.Frequency)/sampleRate));
sample++;
}
return sampleCount;
}
}
class Note
{
public float Frequency;
public float Amplitude = 1.0f;
public TimeSpan Duration = TimeSpan.FromMilliseconds(50);
}
시작 플레이 :
WaveOut waveOut;
this.Notes = new Queue<Note>(new[] { new Note { Frequency = 1000 }, new Note { Frequency = 1100 } });
var waveProvider = new NotesWaveProvider(Notes);
waveProvider.SetWaveFormat(16000, 1); // 16kHz mono
waveOut = new WaveOut();
waveOut.Init(waveProvider);
waveOut.Play();
새로운 메모를 추가 :이 코드는 생각입니다
void Timer_Tick(...)
{
if (Notes.Count < 10)
Notes.Add(new Note{Frecuency = 900});
}
PS
노트 파 제공자를 확인합니다. 진짜를 사용하여 추가 mt- 잠금 등
노트의 순서를 시작하는 아주 좋은 작품. 그러나 , 초기 "멜로디"가 끝난 후 대기열에 더 많은 음을 추가하면 제대로 작동하지 않습니다. 새 음표가 즉시 시작되기를 기대합니다. 위 코드를 사용하면 명령문에 샘플 카운터를 재설정하여 삽입 할 수 있습니다 if (이 경우, 다음 노트는 즉시 시작됩니다. (Notes.Count == 0) {sample = 0; return null;} 몇 초 후에 시작되며, for (;;)는 NextNote에서 필요하지 않습니다. – Goodies