回答編集履歴

2

minimバージョン

2019/12/21 09:56

投稿

TN8001
TN8001

スコア9855

test CHANGED
@@ -22,15 +22,13 @@
22
22
 
23
23
  void setup() {
24
24
 
25
- size(600, 400);
25
+ size(600, 400);
26
-
27
-
28
-
26
+
29
- MidiBus.list();
27
+ MidiBus.list();
30
-
28
+
31
- myBus = new MidiBus(this, -1, 1); // 注2
29
+ myBus = new MidiBus(this, -1, 1); // 注2
32
-
30
+
33
- myBus.sendMessage(192, 3); // 注3
31
+ myBus.sendMessage(192, 3); // 注3
34
32
 
35
33
  }
36
34
 
@@ -44,19 +42,17 @@
44
42
 
45
43
  void keyPressed() {
46
44
 
47
- String pitchName = getPitchName();
45
+ String pitchName = getPitchName();
46
+
48
-
47
+ if (pitchName == null) return;
49
-
50
-
48
+
51
- if (pitchName != "" && !pressedKeys.getOrDefault(pitchName, false)) {
49
+ if (pressedKeys.getOrDefault(pitchName, false)) return;
52
-
50
+
51
+
52
+
53
- myBus.sendNoteOn(0, (int) Frequency.ofPitch(pitchName).asMidiNote(), 127);
53
+ myBus.sendNoteOn(0, (int) Frequency.ofPitch(pitchName).asMidiNote(), 127);
54
-
55
- }
54
+
56
-
57
-
58
-
59
- pressedKeys.put(pitchName, true);
55
+ pressedKeys.put(pitchName, true);
60
56
 
61
57
  }
62
58
 
@@ -64,13 +60,15 @@
64
60
 
65
61
  void keyReleased() {
66
62
 
67
- String pitchName = getPitchName();
63
+ String pitchName = getPitchName();
64
+
68
-
65
+ if (pitchName == null) return;
69
-
70
-
66
+
67
+
68
+
71
- myBus.sendNoteOff(0, (int) Frequency.ofPitch(pitchName).asMidiNote(), 127);
69
+ myBus.sendNoteOff(0, (int) Frequency.ofPitch(pitchName).asMidiNote(), 127);
72
-
70
+
73
- pressedKeys.put(pitchName, false);
71
+ pressedKeys.put(pitchName, false);
74
72
 
75
73
  }
76
74
 
@@ -78,35 +76,37 @@
78
76
 
79
77
  String getPitchName() {
80
78
 
81
- switch (key) {
79
+ switch (key) {
82
-
80
+
83
- case 'z': return "C4";
81
+ case 'z': return "C4";
84
-
82
+
85
- case 's': return "C#4";
83
+ case 's': return "C#4";
86
-
84
+
87
- case 'x': return "D4";
85
+ case 'x': return "D4";
88
-
86
+
89
- case 'd': return "D#4";
87
+ case 'd': return "D#4";
90
-
88
+
91
- case 'c': return "E4";
89
+ case 'c': return "E4";
92
-
90
+
93
- case 'v': return "F4";
91
+ case 'v': return "F4";
94
-
92
+
95
- case 'g': return "F#4";
93
+ case 'g': return "F#4";
96
-
94
+
97
- case 'b': return "G4";
95
+ case 'b': return "G4";
98
-
96
+
99
- case 'h': return "G#4";
97
+ case 'h': return "G#4";
100
-
98
+
101
- case 'n': return "A4";
99
+ case 'n': return "A4";
102
-
100
+
103
- case 'j': return "A#4";
101
+ case 'j': return "A#4";
104
-
102
+
105
- case 'm': return "B4";
103
+ case 'm': return "B4";
106
-
104
+
107
- }
105
+ }
108
-
106
+
107
+
108
+
109
- return "";
109
+ return null;
110
110
 
111
111
  }
112
112
 
@@ -155,3 +155,179 @@
155
155
  `sendMessage(192, 3)`の`3`は音色です(`192`は気にしないでいいです)
156
156
 
157
157
  [楽器](https://ja.wikipedia.org/wiki/General_MIDI#Melodic_sounds)の`No.`の数字からいろいろ試してみてください。
158
+
159
+
160
+
161
+ ---
162
+
163
+
164
+
165
+ 追記
166
+
167
+
168
+
169
+ やっぱり気になったのでminimバージョンも。無駄にややこしくしてる気もする^^;
170
+
171
+
172
+
173
+ ```Processing
174
+
175
+ import ddf.minim.spi.*;
176
+
177
+ import ddf.minim.signals.*;
178
+
179
+ import ddf.minim.*;
180
+
181
+ import ddf.minim.analysis.*;
182
+
183
+ import ddf.minim.ugens.*;
184
+
185
+ import ddf.minim.effects.*;
186
+
187
+
188
+
189
+ Minim minim;
190
+
191
+ AudioOutput out;
192
+
193
+ HashMap<String, Keyboard> pressedKeys = new HashMap<String, Keyboard>();
194
+
195
+
196
+
197
+ void setup() {
198
+
199
+ size(600, 400);
200
+
201
+ minim = new Minim(this);
202
+
203
+ out = minim.getLineOut();
204
+
205
+ }
206
+
207
+
208
+
209
+ void draw() {
210
+
211
+ }
212
+
213
+
214
+
215
+ void keyPressed() {
216
+
217
+ String pitchName = getPitchName();
218
+
219
+ if (pitchName == null) return;
220
+
221
+
222
+
223
+ Keyboard k = pressedKeys.get(pitchName);
224
+
225
+ if (k == null) {
226
+
227
+ k = new Keyboard(pitchName);
228
+
229
+ pressedKeys.put(pitchName, k);
230
+
231
+ }
232
+
233
+
234
+
235
+ k.noteOn();
236
+
237
+ }
238
+
239
+
240
+
241
+ void keyReleased() {
242
+
243
+ String pitchName = getPitchName();
244
+
245
+ if (pitchName == null) return;
246
+
247
+
248
+
249
+ pressedKeys.get(pitchName).noteOff();
250
+
251
+ }
252
+
253
+
254
+
255
+ String getPitchName() {
256
+
257
+ switch (key) {
258
+
259
+ case 'z': return "C4";
260
+
261
+ case 's': return "C#4";
262
+
263
+ case 'x': return "D4";
264
+
265
+ case 'd': return "D#4";
266
+
267
+ case 'c': return "E4";
268
+
269
+ case 'v': return "F4";
270
+
271
+ case 'g': return "F#4";
272
+
273
+ case 'b': return "G4";
274
+
275
+ case 'h': return "G#4";
276
+
277
+ case 'n': return "A4";
278
+
279
+ case 'j': return "A#4";
280
+
281
+ case 'm': return "B4";
282
+
283
+ }
284
+
285
+
286
+
287
+ return null;
288
+
289
+ }
290
+
291
+
292
+
293
+ class Keyboard {
294
+
295
+ Oscil osc;
296
+
297
+ boolean pressed;
298
+
299
+
300
+
301
+ Keyboard(String pitchName) {
302
+
303
+ osc = new Oscil(Frequency.ofPitch(pitchName), 1.0, Waves.SAW);
304
+
305
+ }
306
+
307
+
308
+
309
+ void noteOn() {
310
+
311
+ if (pressed) return;
312
+
313
+
314
+
315
+ osc.patch(out);
316
+
317
+ pressed = true;
318
+
319
+ }
320
+
321
+
322
+
323
+ void noteOff() {
324
+
325
+ osc.unpatch(out);
326
+
327
+ pressed = false;
328
+
329
+ }
330
+
331
+ }
332
+
333
+ ```

1

誤認

2019/12/21 09:56

投稿

TN8001
TN8001

スコア9855

test CHANGED
@@ -1,4 +1,4 @@
1
- `out.playNote`は鳴らす長さを入れて音を出すみたいなので、任意の長さで鳴らすのはちょっと難しそうですねぇ(何か方法はあるかもしれませんが)
1
+ ~~`out.playNote`は鳴らす長さを入れて音を出すみたいなので、任意の長さで鳴らすのはちょっと難しそうですねぇ(何か方法はあるかもしれませんが)~~
2
2
 
3
3
 
4
4