Instead of using Strings or the more efficient StringBuilder, we can use simple Patterns to create more complicated Patterns. Patterns use StringBuilder behind the scene.
We added 4 measures of bass, snare, and hihat patterns. They were put in separate layers to sound in harmony and finally added to MIDI channel 10 (or voice 9) - the Percussion channel.
The different Strings inside [ ] for dictionary lookup, can be found in the reference mentioned earlier. It is possible to use the numbers instead since MIDI format standards can be found on different MIDI sites.
package jfugue23;
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 JFugue23 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("Play Drum\nPattern");
button.setOnAction(e->example());
button.setFont(Font.font("Verdana", 16));
button.setPrefSize(200, 100);
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 23. Drum Pattern over 4 measures");
primaryStage.setScene(scene);
primaryStage.show();
}
private void example() {
Player player = new Player();
text.appendText("Played Drum Pattern\n");
// 2 measures of bass, snare, hihat
Pattern bassDrum = new Pattern("[BASS_DRUM]q Rq");
bassDrum.repeat(4);
bassDrum.prepend("L0");
Pattern snareDrum = new Pattern("Rq [ACOUSTIC_SNARE]q");
snareDrum.repeat(4);
snareDrum.prepend("L1");
Pattern hihatDrum = new Pattern("[CLOSED_HI_HAT]i");
hihatDrum.repeat(15);
hihatDrum.add("[OPEN_HI_HAT]i");
hihatDrum.prepend("L2");
Pattern pattern = new Pattern(bassDrum, snareDrum, hihatDrum);
pattern.repeat(2); // 4 measures in total
pattern.setVoice(9); // Percussion channel
pattern.setTempo(125);
text.appendText("Pattern:\n" + pattern + "\n\n");
player.play(pattern);
}
}
This is the output after playing once:
No comments:
Post a Comment