簡體   English   中英

在.NET(C#或VB.NET)中生成摩爾斯電碼(或任何音頻),而無需第三方依賴,也無需使用音頻文件

[英]Generate Morse Code (or any audio) in .NET (C# or VB.NET) without 3rd party dependencies, and without needing to use an audio file

那么,在不需要任何外部文件且沒有任何第三方庫的情況下,在.NET(WinForms)中播放簡單的生成的摩爾斯電碼的最佳方法是什么? 我只想使用CLR,沒有不必要的依賴關系。 歡迎使用C#或VB.NET代碼。 我會說多種語言。 ; P

(我真的不在乎會生成什么音頻。簡單的單頻音就​​可以了。)

實現目標的方法有很多。

我認為對您來說最簡單的是播放一些MIDI音符。

請參閱這篇文章: http : //msdn.microsoft.com/en-us/magazine/ee336028.aspx

NAudio庫是開放源代碼,如果您真的不滿意,可以將相關的類復制粘貼到您的項目中。

我在其他站點上找到了在線幫助的一半,大約一半的人想出了解決方法。 這是我需要的幾乎理想的解決方案: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 <> <

使用Console.Beep(頻率,持續時間)

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM