Play sound in .NET using generated waveform data

后端 未结 6 1668
感情败类
感情败类 2021-02-02 03:55

How can I play a sound based on waveform data that my .NET program is generating from user input and mathematical functions?

By \"waveform data\" I mean SPL (sound press

6条回答
  •  隐瞒了意图╮
    2021-02-02 04:31

    How to play from an array of doubles

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

提交回复
热议问题