Wednesday, July 13, 2016

JFugue 15. Absolute Time 2

The same 3 notes are played by 3 different patterns. The notes are C (whole), E (3 quarters), and G (3 quarters). C starts at beginning of measure 1, E at middle of 1, and G 3/4 inside measure 1. The three notes are overlapping in time.


In the first pattern the durations are written numerically, h = /0.5, q = /0.25, etc.


In the second patterns, the durations are written using letters, /0.75 = qqq = hq.


In the third pattern, we use three channels to play each note individually. We also save the files as midi, to check visually, in the piano roll, the notes are identical in a DAW.


package jfugue15;

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 JFugue15 extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    TextArea text;
    String[] patts = 
        {"C/1.0 @0.5 E/0.75 @0.75 G/0.75",
         "Cw @0.5 Ehq @0.75 Ghq",
         "V0 Cw V1 Rh Ehq V2 Rhq Ghq"};
    Button[] buttons = new Button[patts.length];
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        
        int w = 250, h = 100;
        for (int i = 0; i<buttons.length; i++){
            String str = String.valueOf(i);
            buttons[i] = new Button("Pattern " + i);
            buttons[i].setOnAction( e-> example(str));
            buttons[i].setPrefSize(w,h);
            buttons[i].setFont(Font.font("Verdana", 16));
        }
        
        VBox examples = new VBox(10, buttons);
        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 15. Absolute Time 2");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example(String string) {
        
        int i = Integer.valueOf(string);
        Player player = new Player();
        Pattern pattern = new Pattern(patts[i]);
        text.appendText("\n" + (i+1) +". Pattern:\t" + pattern);
        try {
            MidiFileManager
                    .savePatternToMidi(pattern,
                            new File("JFugue15_" + string + ".mid"));
        } catch (IOException ex) {
        }
        player.play(pattern);
    }
}

This is the output after the 3 patterns are played:


No comments:

Post a Comment