0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

生成AI(Copilot)を使って、モールス信号を出力するJavaScript文を作成した。codepenで動かす。

Posted at

はじめに

 無性にモースル信号音を聞きたくなった。「こっちのけんと」さんの曲「はいよろこんで→ギリギリダンス」とか、天空の城ラピュタのムスカ様とか、タイタニックとか、戦争ゲームとか、NHKスペシャルの戦時中特集で流れているトントン・ツーツーのあれだ。

 「トン」は単音「・」
 「ツー」は長音「ー」

となる。0と1の2進法でデジタル的信号を電波に乗せて長距離通信が可能となる古い技術だ。詳細はWikipedia参照として、文字を入力して、モールス信号に変換して聞きたくなった。しかし、プログラムを一から作成するのもメンドクサイので、生成AIさんに頼んで作成してもらうことにした。

条件

作成してもらうプログラム言語:HTMLとJavaScript

手順

1 厳格モードで、プロンプト(命令文)をCopilotに入力
2 出力されたプログラムをコピペしHTMLファイルにコピペして実行
3 うまく以下なったら1のプロンプトを編集

生成Aiに入力したプロンプト(命令文)

 最終的に以下のプロンプトで比較的満足したHTMLとJavaScriptを出力が出来た。
 尚、生成AIの性質上、以下のプロンプトを入力しても、同じ結果が必ずも出力されるわけではのでご注意下さい。

アルファベットと、ひらがなと、濁音と、半濁音と、数値を入力するとモールス信号の波形に変更できるJavaScriptのサンプルプログラムを教えてください。
ひらがなは50音全部入力できるようにしてください。
合成音でモールス音を鳴らすようにしてください。

生成プログラム(HTML+JavaScript)の出力結果

 CodePen利用しています。以下のテキストボックスに文字(例SOS)を入力して「変換」ボタンを押すと動きます。★!音の大きさに注意してください!★

See the Pen モールス信号 by F.M (@kobe2018) on CodePen.

生成プログラム(HTML+JavaScript)【追記・手直後】

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>アルファベットとひらがなをモールス信号に変換</title>
</head>
<body>
<h1>アルファベット・ひらがな・カタカナ・数値をモールス信号で出力</h1>
<input type="text" id="inputText" placeholder="入力してください">
<button onclick="convertToMorse()">変換</button>
<p id="output"></p>

    <script>
        const morseCodeMap = {
"":"--.--","":".-","":"..-","":"-.---","":".-...",
"":".-..","":"-.-..","":"...-","":"-.--","":"----",
"":"-.-.-","":"--.-.","":"---.-","":".---.","":"---.",
"":"-.","":"..-.","":".--.","":".-.--","":"..-..",
"":".-.","":"-.-.","":"....","":"--.-","":"..--",
"":"-...","":"--..-","":"--..","":".","":"-..",
"":"-..-","":"..-.-","":"-","":"-...-","":"-..-.",
"":".--","":"-..--","":"--",
"":"...","":"--.","":"-.--.","":"---","":".-.-",
"":"-.-","":".-..-","":".--..","":".---","":".-.-.",

            '': '..',//濁点
            '': '..--.', //半濁点 
"":".--.-",//長音
"":".-.-.-",//区切点
"":"-.--.-",//括弧
"":".-..-.",//括弧閉


"":"--.--","":".-","":"..-","":"-.---","":".-...",
"":".-..","":"-.-..","":"...-","":"-.--","":"----",
"":"-.-.-","":"--.-.","":"---.-","":".---.","":"---.",
"":"-.","":"..-.","":".--.","":".-.--","":"..-..",
"":".-.","":"-.-.","":"....","":"--.-","":"..--",
"":"-...","":"--..-","":"--..","":".","":"-..",
"":"-..-","":"..-.-","":"-","":"-...-","":"-..-.",
"":".--","":"-..--","":"- ",
"":"...","":"--.","":"-.--.","":"---","":".-.-",
"":"-.-","":".-..-","":".--..","":".---","":".-.-.",


            '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': '--..',
            
            '.': '.-.-.-', ',': '--..--', ':': '---...', '?': '..--..',

            '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
            '': '-----', '': '.----', '': '..---', '': '...--', '': '....-', '': '.....', '': '-....', '': '--...', '': '---..', '': '----.',

            ' ': '       ', ' ': '       ', // 半角全角の空白文字  
        };
        const replacements = [
            ["", ""],["", ""],["", ""],["", ""],["", ""],["", ""],
            ["", ""],["", ""],["", ""],["", ""],["", ""],["", ""],
            ["", "か゛"],["", "き゛"],["", "く゛"],["", "け゛"],["", "こ゛"],
            ["", "さ゛"],["", "し゛"],["", "す゛"],["", "せ゛"],["", "そ゛"],
            ["", "た゛"],["", "ち゛"],["", "つ゛"],["", "て゛"],["", "と゛"],
            ["", "は゛"],["", "ひ゛"],["", "ふ゛"],["", "へ゛"],["", "ほ゛"],
    
            ["", "カ゛"],["", "キ゛"],["", "ク゛"],["", "ケ゛"],["", "コ゛"],
            ["", "サ゛"],["", "シ゛"],["", "ス゛"],["", "セ゛"],["", "ソ゛"],
            ["", "タ゛"],["", "チ゛"],["", "ツ゛"],["", "テ゛"],["", "ト゛"],
            ["", "ハ゛"],["", "ビ゛"],["", "フ゛"],["", "ヘ゛"],["", "ホ゛"],
            ["", "は゜"],["", "ビ゜"],["", "フ゜"],["", "ヘ゜"],["", "ホ゜"],
            
        ];

        function playMorseSound(morseCode) {
            const audioContext = new (window.AudioContext || window.webkitAudioContext)();
            const dotDuration = 0.07; // ドットの長さ(秒)
            
            //const dashDuration = dotDuration * 1.8; // ダッシュの長さ(秒)
            const dashDuration = dotDuration * 3; // ダッシュの長さは三倍(秒)
            
            const gapDuration = dotDuration; // 各信号間のギャップ(秒)

            let currentTime = audioContext.currentTime;

            for (let symbol of morseCode) {
                if (symbol === '.') {
                    playTone(audioContext, currentTime, dotDuration);
                    currentTime += dotDuration + gapDuration;
                } else if (symbol === '-') {
                    playTone(audioContext, currentTime, dashDuration);
                    currentTime += dashDuration + gapDuration;
                } else {
                    currentTime += gapDuration; // スペースの場合
                }
            }
        }

        function playTone(audioContext, startTime, duration) {
            const oscillator = audioContext.createOscillator();
            oscillator.type = 'sine';
            oscillator.frequency.setValueAtTime(600, audioContext.currentTime); // 周波数600Hz
            oscillator.connect(audioContext.destination);
            oscillator.start(startTime);
            oscillator.stop(startTime + duration);
        }

        function convertToMorse() {
            const inputText = multiReplace(document.getElementById('inputText').value.toLowerCase(),replacements);
            let morseCode = '';
            for (let char of inputText) {
                if (morseCodeMap[char]) {
                    morseCode += morseCodeMap[char] + ' ';
                } else {
                    morseCode += '? ';
                }
            }
            document.getElementById('output').innerText = morseCode;
            playMorseSound(morseCode.trim());
        }

    function multiReplace(str, replacements) {
        replacements.forEach(replacement => {
            const [target, replacementText] = replacement;
            const regex = new RegExp(target, 'g');
            str = str.replace(regex, replacementText);
        });
        return str;
    }
    </script>
</body>
</html>

考察&感想

 出力されたプログラムは100%完璧なものとは言えなくて、手直しは必要だったが、普段なら休日を1日つぶす仕事量なのに、生成AIを使ったら30分ほどで結果を得られた。
ああ、末恐ろしい。

参考

和文モールス符号

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?