Skip to content

Commit 4c34270

Browse files
author
Pablo Fernandez
committed
Midi player to play tones
important
1 parent c2f8443 commit 4c34270

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

src/scaleplayer/ScalePlayer.java

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
public class ScalePlayer extends Application {
2929

3030
private static int startingNote;
31-
31+
private MidiPlayer sequence = new MidiPlayer(2, 60);
32+
3233
@Override
3334
public void start(Stage primaryStage) {
3435

36+
//create menu bar
3537
MenuBar menuBar = new MenuBar();
3638
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
3739
Menu file = new Menu("File");
@@ -40,6 +42,7 @@ public void start(Stage primaryStage) {
4042
file.getItems().add(exitMenuItem);
4143
exitMenuItem.setOnAction(actionEvent -> Platform.exit());
4244

45+
//play button
4346
Button playBtn = new Button();
4447
playBtn.setText("Play Scale");
4548
playBtn.setStyle("-fx-base: #b6e7c9;");
@@ -51,34 +54,40 @@ public void handle(ActionEvent event) {
5154
dialog.setTitle("Starting Note");
5255
dialog.setHeaderText("Please enter a note (0-155)");
5356

57+
//get result, parse it into an int in a roundabout way, then play scale once its had
5458
Optional<String> result = dialog.showAndWait();
5559
if (result.isPresent()){
56-
System.out.println("Entered Note: " + result.toString());
5760
String stringResult = result.toString();
5861
stringResult = stringResult.substring(9, stringResult.length()-1);
5962
startingNote = Integer.parseInt(stringResult);
60-
//playScale(startingNote);
63+
stopScale(sequence);
64+
clearScale(sequence);
65+
playScale(sequence, startingNote);
6166
}
6267
}
6368
});
6469

70+
//stop button
6571
Button stopBtn = new Button();
6672
stopBtn.setText("Stop Playing");
6773
stopBtn.setStyle("-fx-base: #eda6a6;");
6874
stopBtn.setOnAction(new EventHandler<ActionEvent>() {
6975

7076
@Override
7177
public void handle(ActionEvent event) {
72-
System.out.println("Hello World!");
78+
stopScale(sequence);
79+
clearScale(sequence);
7380
}
7481
});
7582

83+
//create layout
7684
HBox hbox = new HBox();
7785
hbox.getChildren().addAll(playBtn, stopBtn);
7886
hbox.setSpacing(10);
7987
hbox.setCenterShape(true);
8088
hbox.setAlignment(Pos.CENTER);
8189

90+
//Overhead for showing screen
8291
BorderPane root = new BorderPane();
8392

8493
root.setTop(menuBar);
@@ -91,9 +100,15 @@ public void handle(ActionEvent event) {
91100
primaryStage.show();
92101
}
93102

94-
public void playScale(int startingNote) {
95-
96-
MidiPlayer sequence = new MidiPlayer(2, 60);
103+
public void stopScale(MidiPlayer sequence) {
104+
sequence.stop();
105+
}
106+
107+
public void clearScale(MidiPlayer sequence) {
108+
sequence.clear();
109+
}
110+
111+
public void playScale(MidiPlayer sequence, int startingNote) {
97112
/**
98113
*
99114
* A convenience method for adding notes to the composition.
@@ -109,40 +124,22 @@ public void playScale(int startingNote) {
109124
* pitch, volume, startTick, duration,
110125
channel, trackIndex
111126
*/
112-
sequence.addNote(50, 100, 1, 1,
113-
1, 1);
114-
sequence.addNote(51, 100, 2, 1,
115-
1, 1);
116-
sequence.addNote(52, 100, 3, 1,
117-
1, 1);
118-
sequence.addNote(53, 100, 4, 1,
119-
1, 1);
120-
sequence.addNote(54, 100, 5, 1,
127+
128+
for(int i=1; i<9; i++)
129+
{
130+
sequence.addNote(startingNote, 100, i, 1,
121131
1, 1);
122-
sequence.addNote(55, 100, 6, 1,
123-
1, 1);
124-
sequence.addNote(56, 100, 7, 1,
125-
1, 1);
126-
sequence.addNote(57, 100, 8, 1,
127-
1, 1);
128-
129-
sequence.addNote(57, 100, 10, 1,
130-
1, 1);
131-
sequence.addNote(56, 100, 11, 1,
132-
1, 1);
133-
sequence.addNote(55, 100, 12, 1,
134-
1, 1);
135-
sequence.addNote(54, 100, 13, 1,
136-
1, 1);
137-
sequence.addNote(53, 100, 14, 1,
138-
1, 1);
139-
sequence.addNote(52, 100, 15, 1,
140-
1, 1);
141-
sequence.addNote(51, 100, 16, 1,
142-
1, 1);
143-
sequence.addNote(50, 100, 17, 1,
132+
startingNote = startingNote + 1;
133+
}
134+
135+
startingNote = startingNote - 1;
136+
for(int i=10; i<18; i++)
137+
{
138+
sequence.addNote(startingNote, 100, i, 1,
144139
1, 1);
145-
sequence.play();
140+
startingNote = startingNote - 1;
141+
}
142+
sequence.play();
146143
}
147144

148145
/**

0 commit comments

Comments
 (0)