We can parse a MIDI file, a Staccato String, etc.
Here we parse a Staccato String (converted to Pattern). Thus we use a StaccatoListener. We can always use a StaccatoListener as we can always convert a MIDI file (or another format) into a Pattern.
This program has two Java files. This is JFugue12a, which is a subclass of ParserListenerAdapter. We only override its onNoteParsed which gets individual notes. Here we count the number of notes belonging to a family such as "C", any octave. To find instrument information, etc. we can override another method. All the methods have empty implementations in the superclass.
package jfugue12;
import org.jfugue.parser.ParserListenerAdapter;
import org.jfugue.theory.Note;
class JFugue12a extends ParserListenerAdapter {
private int[] counter = new int[12];
@Override
public void onNoteParsed(Note note) {
if (note.isRest()) return;
counter[note.getPositionInOctave()]++;
}
String getCounter() {
StringBuilder sb = new StringBuilder();
for (byte i = 0; i<counter.length; i++) {
sb.append("\n");
sb.append(Note.getToneStringWithoutOctave(i));
sb.append(":\t");
sb.append(counter[i]);
}
return sb.toString();
}
}
This is the main file which parses the Pattern.
Once the Pattern is parsed, we call getCounter() method of JFugue12a, to get String to display listing the counts.
package jfugue12;
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.realtime.RealtimePlayer;
import org.staccato.StaccatoParser;
public class JFugue12 extends Application {
public static void main(String[] args) {
launch(args);
}
RealtimePlayer player;
TextArea text;
Button button;
@Override
public void stop() {
player.close();
}
@Override
public void start(Stage primaryStage) throws Exception {
button = new Button("Parse");
button.setPrefSize(150, 100);
button.setOnAction(e->example());
button.setFont(Font.font("Verdana", 20));
player = new RealtimePlayer();
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 12. Parser");
primaryStage.setScene(scene);
primaryStage.show();
}
private void example() {
String patt = "C5q Rq D5q Rq E5h Rq C4h";
Pattern pattern = new Pattern(patt);
StaccatoParser parser = new StaccatoParser();
JFugue12a listener = new JFugue12a();
parser.addParserListener(listener);
parser.parse(pattern);
text.setText("\nWe have these Notes:\n\n");
text.appendText(listener.getCounter());
player.play(pattern);
}
}
This is the output:
No comments:
Post a Comment