This is similar to JFugue5 example, except now the Realtime player is used.
This leads to change of only a few lines but now the program should be more interactive as it will display text as audio is being played, rather than after audio is finished. Further the audio should play more smoothly.
package jfugue9;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.jfugue.realtime.RealtimePlayer;
import org.jfugue.theory.Chord;
public class JFugue9 extends Application {
static int currentChord = 0;
public static void main(String[] args) {
launch(args);
}
TextArea text;
Button button;
String[] names;
RealtimePlayer player;
@Override
public void stop() {
player.close();
}
@Override
public void start(Stage primaryStage) throws Exception {
player = new RealtimePlayer();
names = Chord.getChordNames();
button = new Button("Play C" + names[currentChord].toLowerCase());
button.setOnAction(e->example());
VBox examples = new VBox(10, button);
examples.setPadding(new Insets(10));
text = new TextArea();
text.setPrefRowCount(20);
text.setPrefColumnCount(32);
text.setFont(Font.font("Verdana", 20));
text.setEditable(false);
text.setWrapText(true);
HBox root = new HBox(50,examples,text);
Scene scene = new Scene(root, 900, 600);
primaryStage.setTitle("JFugue 9. Realtime Playing chords");
primaryStage.setScene(scene);
primaryStage.show();
}
private void example() {
String chordString = "C" + names[currentChord].toLowerCase();
Chord chord = new Chord(chordString);
int notes = chord.getNotes().length;
text.appendText(String.format("\n\n%d. %s\n\t%d Notes (%s)",
currentChord, chordString, notes,
chord.getPatternWithNotes()));
player.play(chord.getPattern());
currentChord ++;
currentChord %= names.length;
button.setText("Play C" + names[currentChord].toLowerCase());
}
}
This is the output after a few chords have been played.:
No comments:
Post a Comment