We can save a pattern to a text file or read a pattern from a text file.
The file JFugue13.staccato is saved to the Project folder. If you open it, it will have the comment indicated below.
Because it is a text file it is easy to view and modify in text editor. Further it is possible to use another language such as Python to create the text file.
package jfugue13;
import java.io.File;
import java.io.IOException;
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.pattern.Pattern;
import org.jfugue.player.Player;
public class JFugue13 extends Application {
public static void main(String[] args) {
launch(args);
}
TextArea text;
Button button;
@Override
public void start(Stage primaryStage) throws Exception {
button = new Button("Save and Load\n Pattern");
button.setOnAction(e->example());
button.setPrefWidth(250);
button.setFont(Font.font("Verdana", 20));
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 13. Saving and Loading Pattern");
primaryStage.setScene(scene);
primaryStage.show();
}
private void example() {
Player player = new Player();
String patt = "C5q Rq D5q Rq E5h Rq C4h";
Pattern pattern1 = new Pattern(patt);
try {
pattern1.save(new File("JFugue13.staccato"), "Test pattern");
} catch (IOException ex) {
}
text.setText("\nSaved Pattern:\n\n" + pattern1);
Pattern pattern2 = null;
try {
pattern2 = Pattern.load(new File("JFugue13.staccato"));
} catch (IOException ex) {
}
text.appendText("\n\nLoaded Pattern:\n\n" + pattern2);
Pattern pattern = new Pattern();
pattern.add(pattern1.setVoice(0));
pattern.add(pattern2.setVoice(1).setInstrument("TRUMPET"));
text.appendText("\n\nCombined Pattern:\n\n" + pattern);
player.play(pattern);
}
}
This is the output. The loaded file, containing same notes, as the saved one, is played as Trumpet alongside the default Piano.
No comments:
Post a Comment