Digital Sound & Music: Concepts, Applications, & Science, Chapter 3, last updated 6/25/2013
53
136 /** Listens to the radio buttons. */
137 public void actionPerformed(ActionEvent e) {
138 ChordApp.playChord(e.getActionCommand());
139 }
140 }
Algorithm 3.3 ChordApp class
1
2
3
import javax.sound.sampled.AudioFormat;
4 import javax.sound.sampled.AudioSystem;
5 import javax.sound.sampled.SourceDataLine;
6 import javax.sound.sampled.LineUnavailableException;
7
8 public class Chord {
9 float[] Scale;
10
11 public Chord(float[] scale) {
12 Scale=scale;
13 }
14
15 public void play() {
16 try {
17 makeChord(Scale);
18 } catch (LineUnavailableException lue) {
19 System.out.println(lue);
20 }
21 }
22
23 private void makeChord(float[] scale)
24 throws LineUnavailableException
25 {
26 int freq = 44100;
27 float[] x=new float[(int)(freq)];
28
29 byte[] buf;
30 AudioFormat audioF;
31
32 for(int i=0;ix.length;i++){
33 x[i]=(float)(i+1)/freq;
34 }
35
36 buf = new byte[1];
37 audioF = new AudioFormat(freq,8,1,true,false);
38
39 SourceDataLine sourceDL = AudioSystem.getSourceDataLine(audioF);
40 sourceDL = AudioSystem.getSourceDataLine(audioF);
41 sourceDL.open(audioF);
42 sourceDL.start();
43
44 for (int j=0;jx.length;j++){
45 buf[0]= 0;
46 for(int i=0;iscale.length;i++){
47
buf[0]=(byte)(buf[0]+(Math.sin((2*Math.PI*scale[i])*x[j])*10.0))
48 }
Previous Page Next Page