Digital Sound & Music: Concepts, Applications, & Science, Chapter 2, last updated 6/25/2013
57
58 sourceDL.stop();
59 sourceDL.close();
60 }
61
62 protected void paintComponent(Graphics g) {
63 super.paintComponent(g);
64 Graphics2D g2 = (Graphics2D)g;
65 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
66 RenderingHints.VALUE_ANTIALIAS_ON);
67
68 int pointsToDraw=4000;
69 double max=sines[0];
70 for(int i=1;ipointsToDraw;i++) if (maxsines[i]) max=sines[i];
71 int border=10;
72 int w = getWidth();
73 int h = (2*border+(int)max);
74
75 double xInc = 0.5;
76
77 //Draw x and y axes
78 g2.draw(new Line2D.Double(border, border, border,
2*(max+border)));
79 g2.draw(new Line2D.Double(border, (h-sines[0]), w-border, (h-
sines[0])));
80
81 g2.setPaint(Color.red);
82
83 for(int i = 0; i pointsToDraw; i++) {
84 double x = border + i*xInc;
85 double y = (h-sines[i]);
86 g2.fill(new Ellipse2D.Double(x-2, y-2, 2, 2));
87 }
88
}
89 }
Program 2.6 Visualizing the sound waves in a Java program
If we increase the value of the frequency in line 18 to 400 Hz, we can notice how the
number of cycles increases, as shown in Figure 2.57. On the other hand, by increasing the
volume, we obtain a higher amplitude for each frequency.
Previous Page Next Page