-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayThatTune.java
28 lines (26 loc) · 936 Bytes
/
PlayThatTune.java
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
import java.io.*;
import java.util.*;
import javax.swing.*;
public class PlayThatTune {
public static void main(String[] args) throws FileNotFoundException {
JFileChooser chooser = new JFileChooser(new File("./play"));
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
Scanner input = new Scanner(chooser.getSelectedFile());
Guitar g = new Guitar37();
while (input.hasNextInt()) {
int pitch = input.nextInt();
double duration = input.nextDouble();
g.playNote(pitch);
advance(duration, g);
}
}
}
public static void advance(double duration, Guitar g) {
int tics = (int) Math.round(duration * StdAudio.SAMPLE_RATE);
for (int i = 0; i < tics; i++) {
StdAudio.play(g.sample());
g.tic();
}
}
}