从多个正弦波创建.wav并一次播放 [英] Create .wav from multiple sine waves and play all at once

查看:115
本文介绍了从多个正弦波创建.wav并一次播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以适应

I'm wondering if there is a way to adapt the code shown here so that I can basically make an array of beeps and play them all at once.

我尝试使用全局MemoryStream和BinaryWriter,提前计算所有内容的大小,但是一次甚至比1声哔声还慢,并且似乎不起作用.

I tried making global MemoryStream and BinaryWriter to use, calculating everything ahead of time for the size but it was even slower than 1 beep at a time and didn't seem to work.

在那之后,我尝试将MemoryStream写入字节数组,但这与为流/写入器创建全局变量一样.

After that, I tried writing the MemoryStream to a byte array but that did the same as making globals for the stream/writer.

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

How can I edit the code to get the MemoryStream to hold multiple beeps and play them all at once?

推荐答案

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

Here is a modification to create the song from beeps:

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();
        }
    }
}

这篇关于从多个正弦波创建.wav并一次播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆