在.NET(C#或VB.NET)中生成摩尔斯电码(或任何音频),无需第三方依赖,无需使用音频文件

时间:2013-10-30 02:08:54

标签: c# .net vb.net winforms audio

那么在.NET(WinForms)中播放简单生成的莫尔斯代码的最佳方法是什么,不需要任何外部文件,也没有任何第三方库?我只想使用CLR,没有不必要的依赖。欢迎使用C#或VB.NET代码。我是多语言的。 ; P

(我真的不在乎产生什么音频。一个简单的单频音就​​好了。)

3 个答案:

答案 0 :(得分:2)

有很多方法可以实现你想要的......

我认为对你来说最简单的就是播放一些MIDI音符。

请参阅此文章:http://msdn.microsoft.com/en-us/magazine/ee336028.aspx

NAudio库是开源的,如果你真的很不开心'你可以将额外的DLL复制粘贴到你的项目中。

答案 1 :(得分:1)

我在其他网站上找到了一半的在线帮助,大约一半我想出了自己的方法。这是我所需要的几乎理想的解决方案: 1:创建一个MemoryStream, 2:将WAV文件的字节写入MemoryStream(永远不会保存到磁盘,但会像WAV文件一样播放), 3:寻找MemoryStream的开头, 4:使用System.Media.SoundPlayer(memoryStream).Play()播放MemoryStream。 而已。最困难的部分是创建WAV格式的字节流......除非你只是复制像我这样的人的代码。 :P 这是一个.NET方法,它将播放没有外部DLL或.NET外部任何东西的声音:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)
{
    var mStrm = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(mStrm);

    const double TAU = 2 * Math.PI;
    int formatChunkSize = 16;
    int headerSize = 8;
    short formatType = 1;
    short tracks = 1;
    int samplesPerSecond = 44100;
    short bitsPerSample = 16;
    short frameSize = (short)(tracks * ((bitsPerSample + 7) / 8));
    int bytesPerSecond = samplesPerSecond * frameSize;
    int waveSize = 4;
    int samples = (int)((decimal)samplesPerSecond * msDuration / 1000);
    int dataChunkSize = samples * frameSize;
    int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
    // var encoding = new System.Text.UTF8Encoding();
    writer.Write(0x46464952); // = encoding.GetBytes("RIFF")
    writer.Write(fileSize);
    writer.Write(0x45564157); // = encoding.GetBytes("WAVE")
    writer.Write(0x20746D66); // = encoding.GetBytes("fmt ")
    writer.Write(formatChunkSize);
    writer.Write(formatType);
    writer.Write(tracks);
    writer.Write(samplesPerSecond);
    writer.Write(bytesPerSecond);
    writer.Write(frameSize);
    writer.Write(bitsPerSample);
    writer.Write(0x61746164); // = encoding.GetBytes("data")
    writer.Write(dataChunkSize);
    {
        double theta = frequency * TAU / (double)samplesPerSecond;
        // 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
        // we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
        double amp = volume >> 2; // so we simply set amp = volume / 2
        for (int step = 0; step < samples; step++)
        {
            short s = (short)(amp * Math.Sin(theta * (double)step));
            writer.Write(s);
        }
    }

    mStrm.Seek(0, SeekOrigin.Begin);
    new System.Media.SoundPlayer(mStrm).Play();
    writer.Close();
    mStrm.Close();
} // public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)

快乐的编码!
-Humilulo&LT;&GT;&LT;

答案 2 :(得分:0)

使用Console.Beep(frequency, duration)

private static void Main(string[] args)
{
    int freq = 500;
    int duration = 500;

    Console.Beep(freq, duration); //S
    Console.Beep(freq, duration);
    Console.Beep(freq, duration);

    Console.Beep(freq, duration * 2); //O
    Console.Beep(freq, duration * 2);
    Console.Beep(freq, duration * 2);

    Console.Beep(freq, duration); //S
    Console.Beep(freq, duration);
    Console.Beep(freq, duration);

}