-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCodeToWavGUI.java
More file actions
222 lines (196 loc) · 9.19 KB
/
Copy pathMorseCodeToWavGUI.java
File metadata and controls
222 lines (196 loc) · 9.19 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package morse_wav;
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class MorseCodeToWavGUI {
// 和文モールス符号表
private static final String[] MORSE_CODE = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
// 和文モールス符号表
private static final Map<Character, String> JAPANESE_MORSE = new HashMap<>();
static {
JAPANESE_MORSE.put('い', ".-");
JAPANESE_MORSE.put('ろ', ".-.-");
JAPANESE_MORSE.put('は', "-...");
JAPANESE_MORSE.put('に', "-.-.");
JAPANESE_MORSE.put('ほ', "-..");
JAPANESE_MORSE.put('へ', ".");
JAPANESE_MORSE.put('と', "..-..");
JAPANESE_MORSE.put('ち', "..-.");
JAPANESE_MORSE.put('り', "--.");
JAPANESE_MORSE.put('ぬ', "....");
JAPANESE_MORSE.put('る', "-.--.");
JAPANESE_MORSE.put('を', ".---");
JAPANESE_MORSE.put('わ', "-.-");
JAPANESE_MORSE.put('か', ".-..");
JAPANESE_MORSE.put('よ', "--");
JAPANESE_MORSE.put('た', "-.");
JAPANESE_MORSE.put('れ', "---");
JAPANESE_MORSE.put('そ', "---.");
JAPANESE_MORSE.put('つ', ".--.");
JAPANESE_MORSE.put('ね', "--.-");
JAPANESE_MORSE.put('な', ".-.");
JAPANESE_MORSE.put('ら', "...");
JAPANESE_MORSE.put('む', "-");
JAPANESE_MORSE.put('う', "..-");
JAPANESE_MORSE.put('ゐ', ".-..-");
JAPANESE_MORSE.put('の', "..--");
JAPANESE_MORSE.put('お', ".-...");
JAPANESE_MORSE.put('く', "...-");
JAPANESE_MORSE.put('や', ".--");
JAPANESE_MORSE.put('ま', "-..-");
JAPANESE_MORSE.put('け', "-.--");
JAPANESE_MORSE.put('ふ', "--..");
JAPANESE_MORSE.put('こ', "----");
JAPANESE_MORSE.put('え', "-.---");
JAPANESE_MORSE.put('て', ".-.--");
JAPANESE_MORSE.put('あ', "--.--");
JAPANESE_MORSE.put('さ', "-.-.-");
JAPANESE_MORSE.put('き', "-.-..");
JAPANESE_MORSE.put('ゆ', "-..--");
JAPANESE_MORSE.put('め', "-...-");
JAPANESE_MORSE.put('み', "..-.-");
JAPANESE_MORSE.put('し', "--.-.");
JAPANESE_MORSE.put('ゑ', ".--..");
JAPANESE_MORSE.put('ひ', "--..-");
JAPANESE_MORSE.put('も', "-..-.");
JAPANESE_MORSE.put('せ', ".---.");
JAPANESE_MORSE.put('す', "---.-");
JAPANESE_MORSE.put('ん', ".-.-.");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(MorseCodeToWavGUI::createAndShowGUI);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("モールス符号をWAV出力");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
JLabel inputLabel = new JLabel("文字を入力してください:");
JTextField inputField = new JTextField(20);
JLabel frequencyLabel = new JLabel("発信周波数 (Hz):");
JSpinner frequencySpinner = new JSpinner(new SpinnerNumberModel(440, 100, 1000, 10));
JLabel speedLabel = new JLabel("速度 (CPM):");
JSpinner speedSpinner = new JSpinner(new SpinnerNumberModel(20, 20, 150, 5));
JButton generateButton = new JButton("WAV生成");
JLabel statusLabel = new JLabel("準備完了");
generateButton.addActionListener(e -> {
String input = inputField.getText().toLowerCase(); // 小文字も処理可能
int frequency = (int) frequencySpinner.getValue();
int cpm = (int) speedSpinner.getValue();
try {
String morse = textToMorse(input);
byte[] audioData = generateMorseAudio(morse, frequency, cpm);
saveWavFile(audioData, cpm, frequency, input, morse);
statusLabel.setText("WAVファイルを生成しました");
} catch (Exception ex) {
statusLabel.setText("エラー: " + ex.getMessage());
}
});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
panel.add(inputLabel);
panel.add(inputField);
panel.add(frequencyLabel);
panel.add(frequencySpinner);
panel.add(speedLabel);
panel.add(speedSpinner);
panel.add(generateButton);
frame.add(panel, BorderLayout.CENTER);
frame.add(statusLabel, BorderLayout.SOUTH);
frame.setVisible(true);
}
private static String textToMorse(String text) {
StringBuilder morseBuilder = new StringBuilder();
for (char c : text.toCharArray()) {
if (c >= 'a' && c <= 'z') {
morseBuilder.append(MORSE_CODE[c - 'a']).append(" ");
} else if (JAPANESE_MORSE.containsKey(c)) {
morseBuilder.append(JAPANESE_MORSE.get(c)).append(" ");
} else if (c == ' ') {
morseBuilder.append("/ ");
}
}
return morseBuilder.toString().trim();
}
private static byte[] generateMorseAudio(String morse, int frequency, int cpm) throws IOException {
int unitTime = 60000 / (cpm * 5); // 1単位時間(ミリ秒)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
byte[] dotSound = createBeep(frequency, unitTime, format); // ドット(短点)の音
byte[] dashSound = createBeep(frequency, unitTime * 3, format); // ダッシュ(長点)の音
byte[] shortGap = createSilence(unitTime, format); // 短点・長点の間隔(1単位)
byte[] mediumGap = createSilence(unitTime * 3, format); // 文字間の間隔(3単位)
byte[] longGap = createSilence(unitTime * 7, format); // 単語間の間隔(7単位)
for (char c : morse.toCharArray()) {
switch (c) {
case '.':
outputStream.write(dotSound);
outputStream.write(shortGap);
break;
case '-':
outputStream.write(dashSound);
outputStream.write(shortGap);
break;
case ' ':
outputStream.write(mediumGap);
break;
case '/':
outputStream.write(longGap);
break;
}
}
return outputStream.toByteArray();
}
private static byte[] createBeep(int frequency, int lengthMs, AudioFormat format) {
int sampleRate = (int) format.getSampleRate();
int length = sampleRate * lengthMs / 1000;
byte[] buffer = new byte[length * 2];
for (int i = 0; i < length; i++) {
double angle = 2.0 * Math.PI * frequency * i / sampleRate;
short value = (short) (Math.sin(angle) * 32767);
buffer[i * 2] = (byte) (value & 0xff);
buffer[i * 2 + 1] = (byte) ((value >> 8) & 0xff);
}
return buffer;
}
private static byte[] createSilence(int durationMs, AudioFormat format) {
int sampleRate = (int) format.getSampleRate();
int length = sampleRate * durationMs / 1000; // ミリ秒をサンプル数に変換
return new byte[length * 2]; // 16ビット(2バイト)で無音データを生成
}
private static void saveWavFile(byte[] audioData, int cpm, int frequency, String originalText, String morseText) throws IOException {
// 日付と時刻を基にした動的なファイル名を生成
String timestamp = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
String dynamicFileName = String.format("output_%s_CPM%d_Hz%d.wav", timestamp, cpm, frequency);
AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
try (AudioInputStream ais = new AudioInputStream(
new ByteArrayInputStream(audioData), format, audioData.length / format.getFrameSize());
FileOutputStream fos = new FileOutputStream(new File(dynamicFileName))) {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, fos);
// 出力情報をコンソールに表示
System.out.println("=== WAVファイル生成情報 ===");
System.out.println("ファイル名: " + dynamicFileName);
System.out.println("元の文字列: " + originalText);
System.out.println("モールス符号: " + morseText);
System.out.println("速度 (CPM): " + cpm);
System.out.println("周波数 (Hz): " + frequency);
System.out.println("=========================");
}
}
}