This program uses the Transpose class and the Parse Listener.
This plays Smoke On the Water by Deep Purple. For voice 1, the pattern of voice0 is lowered by octave and we start at beat 8. One beat is a quarter note. Both voice 0 and voice 1 are the same instument (OVERDRIVEN_GUITAR). We also have ELECTRIC_BASS_FINGER and percussion (ACOUSTIC_SNARE).
package jfugue27;
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.midi.MidiFileManager;
import org.jfugue.pattern.Pattern;
import org.jfugue.player.Player;
public class JFugue27 extends Application {
public static void main(String[] args) {
launch(args);
}
TextArea text;
Button button;
int interval = 2;
@Override
public void start(Stage primaryStage) throws Exception {
button = new Button("Play Deep Purple\nSmoke On the\n\tWater");
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 27. Smoke On the Water" +
" by Deep Purple.");
primaryStage.setScene(scene);
primaryStage.show();
}
private void example() {
Player player = new Player();
String g1 = "G3q A#3q C4q. G3q A#3q C#4i C4h G3q A#3q C4q. A#3q "
+ "G3ih.";
Pattern guitar1 = new Pattern(g1);
Transpose transpose = new Transpose(-12); // octave lower
Pattern guitar2 = transpose.transposeFromPattern(guitar1);
guitar1.repeat(8);
guitar1.setInstrument("OVERDRIVEN_GUITAR").setVoice(0);
guitar2.repeat(6);
guitar2.prepend("R/8.0"); // start at 8 beat
guitar2.setInstrument("OVERDRIVEN_GUITAR").setVoice(1);
Pattern bass1 =
new Pattern("G3i G3i G3i G3i G3i G3i G3i G3i G3i G3i");
Pattern bass2 =
new Pattern("G3i G3i G3i G3i G3i G3i G3i G3i");
Pattern bass3 =
new Pattern("A#3i A#3i C4i C4i C4i A#3i A#3i G3i G3i");
Pattern bass4 =
new Pattern("G3i G3i G3i G3i G3i");
Pattern bass = new Pattern(bass1, bass2, bass3, bass4);
bass.repeat(4);
bass.prepend("R/16.0"); // start at 16 beat
bass.setInstrument("ELECTRIC_BASS_FINGER").setVoice(2);
Pattern drum = new Pattern("Rq [ACOUSTIC_SNARE]q").repeat(16);
drum.prepend("R/24.0"); // start at beat 24
drum.setVoice(9); // percussion
Pattern pattern =
new Pattern(guitar1, guitar2, bass, drum);
pattern.setTempo(110);
text.appendText("\n\nPattern:\n" + pattern + "\n\n");
try {
MidiFileManager.savePatternToMidi(pattern,
new File("DeepPurple.mid"));
} catch (IOException ex) {
}
player.play(pattern);
}
}
This is the output. You have to scroll to see the entire pattern. The midi file is 70 seconds.
No comments:
Post a Comment