我试图通过DirectX Sound Secondary Buffer生成摩尔斯电码并播放它。我已经浏览了不同的链接并生成了莫尔斯代码,但是我的应用程序生成的摩尔斯电码与链接中找不到的代码不匹配。
这是编写的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MorseCodegenerator
{
class MorseCodeLibrary
{
Char[] Letters = new Char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8',
'9','0', '.',',',':','?','\\', '-' , '/', '(', ')', '"','@','=',' '};
String[] MorseCode = new String[] {".-","-...","-.-.","-..",".","..-.","--.","....","..",".-- -",
"-.-",".-..", "--","-.","---",".--.","--.-",".-.","...", "-","..-","...-",".--","-..-","-.-- ","--..",".----",
"..---", "...--","....-",".....","-....","--...","---..","----.","-----",".-.-.-","--..--",
"---...","..--..",".----.","-....-","-..-.","-.--.-", "-.--.-",".-..-.",".--.- .", "-...-" ,"/"};
public String ConvertTextToMorse(String text)
{
text = text.ToUpper();
String result = "";
int index = -1;
for (int i = 0; i <= text.Length - 1; i++)
{
index = Array.IndexOf(Letters, text[i]);
if (index != -1)
result += MorseCode[index] + " ";
}
return result;
}
public String ConvertMorseToText(String text)
{
text = "@" + text.Replace(" ", "@@") + "@";
int index = -1;
foreach (Char c in Letters)
{
index = Array.IndexOf(Letters, c);
text = text.Replace("@" + MorseCode[index] + "@", "@" + c.ToString() + "@");
}
return text.Replace("@@@@", " ").Replace("@", "");
}
}
}
我在这个函数中调用了上面的类方法
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX.DirectSound;
using System.Collections.Generic;
using MorseCodegenerator;
namespace Noisey
{
/// <summary>
/// Very simple test form
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonWhiteNoise;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
Device applicationDevice;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// Add any constructor code after InitializeComponent call
//
applicationDevice = new Device();
applicationDevice.SetCooperativeLevel(this, CooperativeLevel.Normal);
DevicesCollection devList = new DevicesCollection();
for (int i = 0; i < devList.Count; i++)
{
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonWhiteNoise = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonWhiteNoise
//
this.buttonWhiteNoise.Location = new System.Drawing.Point(140, 42);
this.buttonWhiteNoise.Name = "buttonWhiteNoise";
this.buttonWhiteNoise.Size = new System.Drawing.Size(87, 20);
this.buttonWhiteNoise.TabIndex = 0;
this.buttonWhiteNoise.Text = "WhiteNoise";
this.buttonWhiteNoise.Click += new System.EventHandler(this.buttonWhiteNoise_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(140, 119);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(140, 188);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(28, 119);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(37, 42);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 4;
this.button4.Text = "button4";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 260);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.buttonWhiteNoise);
this.Name = "MainForm";
this.Text = "Noisey";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void buttonWhiteNoise_Click(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
static string morse = string.Empty;
static int wpm = 15, fwpm = 15;
static int freq = 700;
private void PlayMorse()
{
try
{
WaveFormat format = new WaveFormat();
format.BitsPerSample = 16;
format.Channels = 1;
format.BlockAlign = (short)((format.Channels * format.BitsPerSample) / 8);
format.FormatTag = WaveFormatTag.Pcm;
format.SamplesPerSecond = 44100; //sampling frequency of your data;
format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign;
// buffer description
BufferDescription desc = new BufferDescription(format);
desc.DeferLocation = true;
desc.BufferBytes = 4 * format.AverageBytesPerSecond;
desc.ControlPan = true;
desc.ControlFrequency = true;
desc.ControlVolume = true;
SecondaryBuffer secondaryBuffer = new SecondaryBuffer(desc, applicationDevice);
var timings = morse2timingsFarnsworth(morse, wpm, fwpm);
List<double> sample;
double[] rawsamples = new double[44100];
if (timings.Count > 0)
{
timings.Add(1000);
sample = makeSample(timings, freq);
for (int i = 0; i < sample.Count; i++)
{
rawsamples[i] = sample[i];
}
}
//load audio samples to secondary buffer
secondaryBuffer.Pan = 0;
//secondaryBuffer.Frequency = 44100;
//int fre = secondaryBuffer.Frequency;
secondaryBuffer.Volume = (int)-2000;
secondaryBuffer.Write(0, rawsamples, LockFlag.EntireBuffer);
//play audio buffer
secondaryBuffer.Play(0, BufferPlayFlags.Looping);
}
catch (Exception ex)
{
}
}
static int DITS_PER_WORD = 50; // based on "PARIS "
static int SAMPLE_RATE = 8000;
static List<int> morse2timingsFarnsworth(string morse, int wpm, int farnsworth)
{
var dit = 60000 / (DITS_PER_WORD * wpm);
var r = wpm / farnsworth; // slow down the spaces by this ratio
return morse2timingsGeneral(morse, dit, 3 * dit, dit, 3 * dit * r, 7 * dit * r);
}
static List<int> morse2timingsGeneral(string morse, int dit, int dah,int ditSpace,int
charSpace, int wordSpace)
{
List<int> times = new List<int>();
char c;
for (var i = 0; i < morse.Length; i++)
{
c = morse[i];
if (c == '.' || c == '-')
{
if (c == '.')
{
times.Add(dit);
}
else
{
times.Add(dah);
}
times.Add(ditSpace);
}
else if (c == ' ')
{
times.Reverse();
times.RemoveAt(0);
times.Reverse();
times.Add(charSpace);
}
else if (c == '/')
{
times.Reverse();
times.RemoveAt(0);
times.Reverse();
times.Add(wordSpace);
}
}
times.RemoveAt(times.Count-1); // take off the last ditSpace
return times;
}
static List<double> makeSample(List<int> timings, int freq)
{
List<double> buf = new List<double>();
double[] timing1 = new double[timings.Count];
for (int i = 0; i < timings.Count; i++)
{
timing1[i] = timings[i];
}
var counterIncrementAmount = Math.PI * 2 * freq / SAMPLE_RATE;
var on = 1;
for (var t = 0; t < timing1.Length; t += 1)
{
var duration = SAMPLE_RATE * timing1[t] / 1000;
for (var i = 0; i < duration; i += 1)
{
buf.Add(on * Math.Sin(i * counterIncrementAmount) );
}
on = 1 - on;
}
//double dKoef = 2 * 3.1416 / 44100 * 1000;
//for (int i = 0; i < 4410; i++)
// buf.Add((Math.Cos(i * dKoef) * 125 + 127));
return buf;
}
const double _2PI = 2.0 * 3.14159265358979323846;
private void button4_Click(object sender, EventArgs e)
{
MorseCodeLibrary lib = new MorseCodeLibrary();
morse = lib.ConvertTextToMorse("BAN");
PlayMorse();
}
}
}
我无法识别摩尔斯电码生成中的问题,所以请帮我解决问题。