Пример #1
0
        public void Beep(int frequency, int duration)
        {
            float Tick    = 1f / 44100f;
            int   Samples = (44100 * duration) / 1000;
            int   Bytes   = Samples * sizeof(short);

            int[] Header = new int[] { 0x46464952, 0x24 + Bytes, 0x45564157, 0x20746d66,
                                       0x10, 0x10001, 0xac44, 0x15880, 0x100002, 0x61746164, Bytes };

            using (MemoryStream memoryStream = new MemoryStream(0x24 + Bytes))
            {
                using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
                {
                    SignalGenerator signalGenerator = new SignalGenerator();
                    signalGenerator.Amplitude = short.MaxValue;
                    signalGenerator.Frequency = frequency;

                    for (int i = 0; i <= Header.Length - 1; i++)
                    {
                        binaryWriter.Write(Header[i]);
                    }

                    for (int i = 0; i <= Samples - 1; i++)
                    {
                        binaryWriter.Write(Convert.ToInt16(signalGenerator.GetValue(i * Tick)));
                    }

                    binaryWriter.Flush();

                    memoryStream.Seek(0L, SeekOrigin.Begin);
                    using (SoundPlayer soundPlayer = new SoundPlayer(memoryStream))
                    {
                        soundPlayer.PlaySync();
                    }
                }
            }
        }
Пример #2
0
 public void Synchronize(SignalGenerator instance)
 {
     startTime      = instance.startTime;
     ticksPerSecond = instance.ticksPerSecond;
 }