Wednesday, July 27, 2016

JFugue 33. J.S. Bach's Trias Harmonica (BWV 1072)

This is the only Java file used, as Mod functions are not needed.


Two different related set of patterns are used on different instruments. The first 4 midi channels get one instrument and next 4 midi channels get another instrument. Of course most DAWs will allow you to change the instruments and usually have better sound fonts, and you can install extra ones.


package jfugue33;

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 JFugue33 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 J.S.\nBach's Trias\nHarmonica");
                
        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 33. J.S. Bach's"
                + " Trias Harmonica (BWV 1072)");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        
        Player player = new Player();
        
        Pattern voice0 = new Pattern();
        voice0.add("C5q. D5i E5q. F5i G5q. F5i E5q. D4i")
                .repeat(8).add("C5q.");
        
        Pattern voice1 = new Pattern(voice0);
        voice1.prepend("V1 I[CHOIR_AAHS] Rh"); // half note later
        
        Pattern voice2 = new Pattern(voice0);
        voice2.prepend("V2 I[CHOIR_AAHS] Rhh"); // 2 half note later
        
        Pattern voice3 = new Pattern(voice0);
        voice3.prepend("V3 I[CHOIR_AAHS] Rhhh"); // 3 half note later
        
        Pattern voice4 = new Pattern();
        voice4.add("G5q. F5i E5q. D5i C5q. D5i E5q. F5i")
                .repeat(8).add("G5q.");
        
        Pattern voice5 = new Pattern(voice4);
        voice5.prepend("V5 I[VOICE_OOHS] Rh"); // half note later
        
        Pattern voice6 = new Pattern(voice4);
        voice6.prepend("V6 I[VOICE_OOHS] Rhh"); // 2 half note later
        
        Pattern voice7 = new Pattern(voice4);
        voice7.prepend("V7 I[VOICE_OOHS] Rhhh"); // 3 half note later
        
        voice0.prepend("V0 I[CHOIR_AAHS]");
        
        Pattern choir0 = new Pattern(voice0, voice1, voice2, voice3);
        
        voice4.prepend("V4 I[VOICE_OOHS]");
        Pattern choir1 = new Pattern(voice4, voice5, voice6, voice7);
        
        Pattern pattern = new Pattern();
        pattern.add(choir0, choir1);
        pattern.setTempo(100);

        text.appendText("\n\nPattern:\n" + pattern + "\n");
        
        try {
            MidiFileManager.savePatternToMidi(pattern,
                    new File ("BWV1072.mid"));
        } catch (IOException ex) {
        }
        
        player.play(pattern);
    }
}

This is the output:



The saved midi file is opened in LMMS. This shows the 8 repeats for each channel as well as the different time offsets for the four voices in each of the two choirs.


No comments:

Post a Comment