質問編集履歴

5

文章の、修正。

2020/09/26 20:15

投稿

akirasada1972
akirasada1972

スコア41

test CHANGED
@@ -1 +1 @@
1
- Javaで音を鳴らそうとしているのですが、振幅を0にしても音が消えません。
1
+ Javaで音を鳴らそうとしているのですが、振幅を0にしても音が消えません。
test CHANGED
File without changes

4

解決しました。

2020/09/26 20:15

投稿

akirasada1972
akirasada1972

スコア41

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ```java
14
14
 
15
- wave_data[i]= (byte)(110*(1-i/wave_data.length)*Math.sin((i*l1)*Math.PI*2)
15
+ wave_data[i]= (byte)(110*(wave_data.length-(i+1))/wave_data.length*Math.sin((i*l1)*Math.PI*2)
16
16
 
17
17
  *Math.sin((i*l2)*Math.PI*2));
18
18
 
@@ -116,7 +116,7 @@
116
116
 
117
117
 
118
118
 
119
- byte[] wave_data;
119
+ static byte[] wave_data = new byte[44100];
120
120
 
121
121
 
122
122
 
@@ -228,22 +228,18 @@
228
228
 
229
229
  try{
230
230
 
231
- wave_data = new byte[44100];
232
-
233
231
  double l1 = j*slider1.getValue()/44100.0;
234
232
 
235
233
  double l2 = j*slider2.getValue()/44100.0;
236
234
 
237
235
  for(int i=0;i<wave_data.length;i++){
238
236
 
239
- wave_data[i]= (byte)(110*(1-(i+1)/wave_data.length)*Math.sin((i*l1)*Math.PI*2)
237
+ wave_data[i]= (byte)(110*(wave_data.length-(i+1))/wave_data.length*Math.sin((i*l1)*Math.PI*2)
240
238
 
241
239
  *Math.sin((i*l2)*Math.PI*2));
242
240
 
243
241
  }
244
242
 
245
- WaveData.wave_data = wave_data;
246
-
247
243
  AudioFormat frmt= new AudioFormat(44100,8,1,true,false);
248
244
 
249
245
  DataLine.Info info= new DataLine.Info(Clip.class,frmt);
@@ -274,75 +270,75 @@
274
270
 
275
271
 
276
272
 
277
- class DrawCurve extends JPanel implements Runnable {
273
+ class DrawCurve extends JPanel {
278
-
279
-
280
-
281
- byte[] wave_data;
282
-
283
- Thread thread;
284
274
 
285
275
 
286
276
 
287
277
  JButton b1;
288
278
 
279
+ Paint paint;
280
+
289
281
  JPanel p1;
290
282
 
291
- JPanel p2;
292
-
293
283
 
294
284
 
295
285
  DrawCurve() {
296
286
 
297
287
 
298
288
 
299
- p1 = new JPanel();
289
+ p1 = new JPanel();
300
-
301
- JPanel p2 = new JPanel();
290
+
302
-
303
- b1 = new JButton ("PUSH");
291
+ b1 = new JButton ("PUSH");
292
+
304
-
293
+ b1.setPreferredSize(new Dimension(100, 30));
294
+
295
+ paint = new Paint();
296
+
297
+ paint.setPreferredSize(new Dimension(600, 200));
298
+
299
+ p1.setPreferredSize(new Dimension(600, 300));
300
+
305
- p2.add(b1);
301
+ p1.add(b1);
306
-
302
+
307
- p2.add(p1);
303
+ p1.add(paint);
308
-
304
+
309
- add(p2);
305
+ add(p1);
310
-
311
-
312
-
306
+
307
+
308
+
313
- wave_data = new byte[44100];
309
+ b1.addActionListener(new ActionListener() {
310
+
314
-
311
+ @Override
312
+
313
+ public void actionPerformed(ActionEvent e) {
314
+
315
- p1.repaint();
315
+ paint.repaint();
316
-
317
-
318
-
316
+
319
- thread = new Thread(this);
317
+ }
320
-
318
+
321
- thread.start();
319
+ });
322
320
 
323
321
  }
324
322
 
325
-
326
-
327
- public void run() {
328
-
329
- b1.addActionListener(new ActionListener() {
330
-
331
- @Override
332
-
333
- public void actionPerformed(ActionEvent e) {
334
-
335
- wave_data = WaveData.wave_data;
336
-
337
- p1.repaint();
338
-
339
- }
323
+ }
324
+
325
+
326
+
340
-
327
+ class Paint extends JPanel {
328
+
329
+
330
+
331
+ byte[] wave_data = SynthSlider.wave_data;
332
+
333
+
334
+
335
+ public Paint() {
336
+
341
- });
337
+ super();
342
-
338
+
343
- }
339
+ }
344
-
345
-
340
+
341
+
346
342
 
347
343
  public void paintComponent(Graphics g) {
348
344
 
@@ -350,28 +346,14 @@
350
346
 
351
347
  g.setColor(Color.RED);
352
348
 
353
- for(int i = 0; i < (wave_data.length-1); i++) {
349
+ for(int i = 0; i < (wave_data.length/200-1); i++) {
354
-
350
+
355
- g.drawLine(i/100,128-wave_data[i],(i+1)/100,128-wave_data[i+1]);
351
+ g.drawLine(i*2,128-wave_data[i*200],(i+1)*2,128-wave_data[(i+1)*200]);
356
-
352
+
357
- }
353
+ }
358
354
 
359
355
  }
360
356
 
361
357
  }
362
358
 
363
-
364
-
365
- class WaveData {
366
-
367
-
368
-
369
- static byte[] wave_data;
370
-
371
-
372
-
373
- }
374
-
375
-
376
-
377
359
  ```

3

わかりにくい点が、あったので。

2020/09/26 20:05

投稿

akirasada1972
akirasada1972

スコア41

test CHANGED
File without changes
test CHANGED
@@ -52,16 +52,6 @@
52
52
 
53
53
 
54
54
 
55
- /*
56
-
57
- <applet code="SynthAkira.class" width="600" height="600">
58
-
59
- </applet>
60
-
61
- */
62
-
63
-
64
-
65
55
  public class SynthAkira extends JFrame {
66
56
 
67
57
 

2

全体のコードに、グラフをつけようとしたのですが、そちらもうまく行っていません。

2020/09/25 11:31

投稿

akirasada1972
akirasada1972

スコア41

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,19 @@
30
30
 
31
31
  import java.awt.event.ActionListener;
32
32
 
33
-
33
+ import java.awt.Color;
34
+
35
+ import java.awt.Component;
36
+
37
+ import java.awt.Container;
38
+
39
+ import java.awt.Graphics;
40
+
41
+ import java.awt.Dimension;
42
+
43
+
44
+
45
+ import java.lang.*;
34
46
 
35
47
  import javax.swing.*;
36
48
 
@@ -40,7 +52,55 @@
40
52
 
41
53
 
42
54
 
55
+ /*
56
+
57
+ <applet code="SynthAkira.class" width="600" height="600">
58
+
59
+ </applet>
60
+
61
+ */
62
+
63
+
64
+
65
+ public class SynthAkira extends JFrame {
66
+
67
+
68
+
69
+ public static void main(String[] args){
70
+
71
+
72
+
73
+ SynthAkira akira = new SynthAkira("Synth Akira");
74
+
75
+ akira.setVisible(true);
76
+
77
+ }
78
+
79
+
80
+
81
+ SynthAkira(String title){
82
+
83
+ setTitle(title);
84
+
85
+ setBounds(100, 100, 600, 600);
86
+
87
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
88
+
89
+
90
+
91
+ Container contentPane = getContentPane();
92
+
93
+ contentPane.add(new SynthSlider(), BorderLayout.PAGE_START);
94
+
95
+ contentPane.add(new DrawCurve(), BorderLayout.CENTER);
96
+
97
+ }
98
+
99
+ }
100
+
101
+
102
+
43
- public class SynthAkira extends JFrame implements ChangeListener{
103
+ class SynthSlider extends JPanel implements ChangeListener {
44
104
 
45
105
 
46
106
 
@@ -52,37 +112,29 @@
52
112
 
53
113
  JPanel p2;
54
114
 
115
+ JPanel p3;
116
+
117
+ JPanel p4;
118
+
55
- JButton b;
119
+ JButton b1;
120
+
121
+ JButton b2;
56
122
 
57
123
  JLabel label1;
58
124
 
59
125
  JLabel label2;
60
126
 
61
-
62
-
63
- public static void main(String[] args){
127
+
64
-
65
- SynthAkira frame = new SynthAkira();
128
+
66
-
67
-
68
-
69
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70
-
71
- frame.setBounds(10, 10, 600, 200);
72
-
73
- frame.setTitle("SYNTH AKIRA");
74
-
75
- frame.setVisible(true);
129
+ byte[] wave_data;
76
-
77
- }
130
+
78
-
79
-
80
-
131
+
132
+
81
- SynthAkira(){
133
+ SynthSlider() {
82
-
83
-
84
-
134
+
135
+
136
+
85
- slider1 = new JSlider(0, 1000);
137
+ slider1 = new JSlider(0, 1000);
86
138
 
87
139
  slider1.setValue(100);
88
140
 
@@ -104,9 +156,19 @@
104
156
 
105
157
 
106
158
 
159
+ b1 = new JButton("DOWN");
160
+
107
- b = new JButton("PUSH");
161
+ b2 = new JButton("UP");
162
+
163
+
164
+
108
-
165
+ p2 = new JPanel();
166
+
109
-
167
+ p2.add(b1);
168
+
169
+ p2.add(b2);
170
+
171
+
110
172
 
111
173
  label1 = new JLabel();
112
174
 
@@ -118,57 +180,79 @@
118
180
 
119
181
 
120
182
 
121
- p2 = new JPanel();
183
+ p3 = new JPanel();
122
-
184
+
123
- p2.add(label1);
185
+ p3.add(label1);
124
-
186
+
125
- p2.add(label2);
187
+ p3.add(label2);
188
+
189
+
190
+
126
-
191
+ p4 = new JPanel();
192
+
127
-
193
+ // p4.setPreferredSize(new Dimension(600, 400));
194
+
128
-
195
+ p4.setLayout(new BorderLayout());
196
+
129
- getContentPane().add(p1, BorderLayout.PAGE_START);
197
+ p4.add(p1, BorderLayout.PAGE_START);
130
-
198
+
131
- getContentPane().add(b, BorderLayout.CENTER);
199
+ p4.add(p2, BorderLayout.CENTER);
132
-
200
+
133
- getContentPane().add(p2, BorderLayout.PAGE_END);
201
+ p4.add(p3, BorderLayout.PAGE_END);
202
+
203
+
204
+
134
-
205
+ add(p4);
135
-
136
-
206
+
207
+
208
+
137
- b.addActionListener(new ActionListener() {
209
+ b1.addActionListener(new ActionListener() {
138
210
 
139
211
  @Override
140
212
 
141
213
  public void actionPerformed(ActionEvent e) {
142
214
 
143
- setSound();
215
+ setSound(1);
144
216
 
145
217
  }
146
218
 
147
219
  });
148
220
 
221
+ b2.addActionListener(new ActionListener() {
222
+
223
+ @Override
224
+
225
+ public void actionPerformed(ActionEvent e) {
226
+
227
+ setSound(2);
228
+
149
- }
229
+ }
230
+
150
-
231
+ });
232
+
151
-
233
+ }
152
-
234
+
235
+
236
+
153
- void setSound(){
237
+ void setSound(int j){
154
238
 
155
239
  try{
156
240
 
157
- byte[] wave_data= new byte[44100*2];
241
+ wave_data = new byte[44100];
158
-
242
+
159
- double l1 = slider1.getValue()/44100.0;
243
+ double l1 = j*slider1.getValue()/44100.0;
160
-
244
+
161
- double l2 = slider2.getValue()/44100.0;
245
+ double l2 = j*slider2.getValue()/44100.0;
162
246
 
163
247
  for(int i=0;i<wave_data.length;i++){
164
248
 
165
- wave_data[i]= (byte)(110*(1-i/wave_data.length)*Math.sin((i*l1)*Math.PI*2)
249
+ wave_data[i]= (byte)(110*(1-(i+1)/wave_data.length)*Math.sin((i*l1)*Math.PI*2)
166
250
 
167
251
  *Math.sin((i*l2)*Math.PI*2));
168
252
 
169
- }
253
+ }
254
+
170
-
255
+ WaveData.wave_data = wave_data;
171
-
172
256
 
173
257
  AudioFormat frmt= new AudioFormat(44100,8,1,true,false);
174
258
 
@@ -180,11 +264,7 @@
180
264
 
181
265
  clip.start();
182
266
 
183
-
184
-
185
- Thread.sleep(100);while(clip.isRunning()) {Thread.sleep(100);}
186
-
187
- }
267
+ }
188
268
 
189
269
  catch(Exception e){e.printStackTrace(System.err);}
190
270
 
@@ -198,8 +278,110 @@
198
278
 
199
279
  label2.setText("値:" + slider2.getValue());
200
280
 
201
- }
281
+ }
202
282
 
203
283
  }
204
284
 
285
+
286
+
287
+ class DrawCurve extends JPanel implements Runnable {
288
+
289
+
290
+
291
+ byte[] wave_data;
292
+
293
+ Thread thread;
294
+
295
+
296
+
297
+ JButton b1;
298
+
299
+ JPanel p1;
300
+
301
+ JPanel p2;
302
+
303
+
304
+
305
+ DrawCurve() {
306
+
307
+
308
+
309
+ p1 = new JPanel();
310
+
311
+ JPanel p2 = new JPanel();
312
+
313
+ b1 = new JButton ("PUSH");
314
+
315
+ p2.add(b1);
316
+
317
+ p2.add(p1);
318
+
319
+ add(p2);
320
+
321
+
322
+
323
+ wave_data = new byte[44100];
324
+
325
+ p1.repaint();
326
+
327
+
328
+
329
+ thread = new Thread(this);
330
+
331
+ thread.start();
332
+
333
+ }
334
+
335
+
336
+
337
+ public void run() {
338
+
339
+ b1.addActionListener(new ActionListener() {
340
+
341
+ @Override
342
+
343
+ public void actionPerformed(ActionEvent e) {
344
+
345
+ wave_data = WaveData.wave_data;
346
+
347
+ p1.repaint();
348
+
349
+ }
350
+
351
+ });
352
+
353
+ }
354
+
355
+
356
+
357
+ public void paintComponent(Graphics g) {
358
+
359
+ super.paintComponent(g);
360
+
361
+ g.setColor(Color.RED);
362
+
363
+ for(int i = 0; i < (wave_data.length-1); i++) {
364
+
365
+ g.drawLine(i/100,128-wave_data[i],(i+1)/100,128-wave_data[i+1]);
366
+
367
+ }
368
+
369
+ }
370
+
371
+ }
372
+
373
+
374
+
375
+ class WaveData {
376
+
377
+
378
+
379
+ static byte[] wave_data;
380
+
381
+
382
+
383
+ }
384
+
385
+
386
+
205
387
  ```

1

タグの追加

2020/09/25 11:30

投稿

akirasada1972
akirasada1972

スコア41

test CHANGED
File without changes
test CHANGED
File without changes