何の役にも立たないけど、こんなものを作ってみた。
PCの(仮想)COMポートに繋いだパドルでエレキーみたいに短点、長点の音を出せるソフト。
ソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using NAudio.Wave; using System; using System.Threading.Tasks; using System.Windows.Forms; //serialPortのDtrEnableをtrue、PortNameをCOM3(デスクトップPC)に設定しておく namespace elekey2024_1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool execute; private WaveOut waveOut; private Task dot, dash; private void btn_start_Click(object sender, EventArgs e) { if (!execute) { execute = true; btn_start.Text = "停止"; var sineWaveProvider = new SineWaveProvider32(); sineWaveProvider.Frequency = 800; sineWaveProvider.Amplitude = 0.5f; waveOut = new WaveOut(); waveOut.Init(sineWaveProvider); waveOut.Play(); } else if(execute) { execute = false; btn_start.Text = "開始"; waveOut.Stop(); waveOut.Dispose(); waveOut = null; doton = false; dashon = false; } btn_start.Refresh(); while(execute) { if(serialPort1.CtsHolding) { if(!dashon) dot = dashproc(); label1.Text = "Cts"; } else if(serialPort1.DsrHolding) { if(!doton) dash = dotproc(); label1.Text = "Dsr"; } else { label1.Text = "None"; } label1.Refresh(); Application.DoEvents(); } } private void Form1_Load(object sender, EventArgs e) { try { if (serialPort1.IsOpen == true) { return; } serialPort1.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } execute = false; label1.Text = "None"; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { serialPort1.Close(); } } } |
コメントは、自宅PC用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System.Threading.Tasks; namespace elekey2024_1 { public partial class Form1 { private void soundonoff(bool done) { if (done) waveOut.Volume = 0.25f; else if (!done) waveOut.Volume = 0.0f; } bool doton, dashon; private async Task dotproc() { doton = true; soundonoff(true); await Task.Delay(100); soundonoff(false); await Task.Delay(100); doton = false; } private async Task dashproc() { dashon = true; soundonoff(true); await Task.Delay(300); soundonoff(false); await Task.Delay(100); dashon = false; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using NAudio.Wave; using System; namespace elekey2024_1 { public partial class Form1 { public abstract class WaveProvider32 : IWaveProvider { private WaveFormat waveFormat; public WaveProvider32() : this(44100, 1) { } public WaveProvider32(int sampleRate, int channels) { SetWaveFormat(sampleRate, channels); } public void SetWaveFormat(int sampleRate, int channels) { this.waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels); } public int Read(byte[] buffer, int offset, int count) { WaveBuffer waveBuffer = new WaveBuffer(buffer); int samplesRequired = count / 4; int samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired); return samplesRead * 4; } public abstract int Read(float[] buffer, int offset, int sampleCount); public WaveFormat WaveFormat { get { return waveFormat; } } } public class SineWaveProvider32 : WaveProvider32 { int sample; public SineWaveProvider32() { Frequency = 1000; Amplitude = 0.0f; // let's not hurt our ears } public float Frequency { get; set; } public float Amplitude { get; set; } public override int Read(float[] buffer, int offset, int sampleCount) { int sampleRate = WaveFormat.SampleRate; for (int n = 0; n < sampleCount; n++) { buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * Frequency) / sampleRate)); sample++; if (sample >= sampleRate) sample = 0; } return sampleCount; } } } } |
ファイルを分けているけど、全部Form1クラス。
スクイズキー機能とか長短点メモリーとかないので、すごく打ちづらい。
時間待ちに精度が悪いといわれているTask.Delay()を使ってみたけど、そんなに違和感はない。