从多个正弦波创建.wav并立即播放

时间:2017-12-15 22:29:21

标签: c# wav memorystream binarywriter

我想知道是否有办法调整here显示的代码,这样我基本上可以发出一连串的哔哔声并一次播放它们。

我尝试使用全局MemoryStream和BinaryWriter,提前计算所有内容的大小但是它一次比一哔还慢,并且似乎没有用。

之后,我尝试将MemoryStream写入字节数组,但这与为流/写入器制作全局变量相同。

如何编辑代码以使MemoryStream保持多次哔声并一次播放?

1 个答案:

答案 0 :(得分:0)

以下是从哔哔声中创建歌曲的修改:

void Main() {
    var beeps = new[] {
        new Song.Beep(500, 1000, 200),
        new Song.Beep(1000, 1000, 300),
        new Song.Beep(300, 2000, 150),
        new Song.Beep(500, 500, 200),
    };

    var song = new Song(beeps);
    song.PlaySong();
}

public class Song {
    public struct Beep {
        readonly public int Amplitude;
        readonly public int Frequency;
        readonly public int Duration;

        public Beep(int a, int f, int d) {
            Amplitude = a;
            Frequency = f;
            Duration = d;
        }
    }

    MemoryStream MS;

    public Song(IEnumerable<Beep> beeps) {
        int Bytes = beeps.Sum(b => 4 * (441 * b.Duration / 10));

        int[] Hdr = { 0X46464952, 36 + Bytes, 0X45564157, 0X20746D66, 16, 0X20001, 44100, 176400, 0X100004, 0X61746164, Bytes };

        MS = new MemoryStream(44 + Bytes);
        using (var BW = new BinaryWriter(MS, Encoding.UTF8, true)) {
            for (int I = 0; I < Hdr.Length; ++I)
                BW.Write(Hdr[I]);

            foreach (var beep in beeps) {
                double A = ((beep.Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
                double DeltaFT = 2 * Math.PI * beep.Frequency / 44100.0;

                int Samples = 441 * beep.Duration / 10;
                for (int T = 0; T < Samples; ++T) {
                    short Sample = System.Convert.ToInt16(A * Math.Sin(DeltaFT * T));
                    BW.Write(Sample); // left channel
                    BW.Write(Sample); // right channel
                }
            }
        }
    }

    public void PlaySong() {
        MS.Seek(0, SeekOrigin.Begin);
        using (var SP = new SoundPlayer(MS)) {
            SP.PlaySync();
        }
    }
}