Tuesday, July 26, 2016

JFugue 31. Pattern Reverse and Pattern Duration in main class

This file demonstrates two more methods that will be later added to Mod class.


They are reverse (to get reverse of pattern) and duration (to get the total length of a pattern).


package jfugue31;

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.pattern.Token;
import org.jfugue.pattern.Token.TokenType;
import org.jfugue.player.Player;
import org.jfugue.theory.Note;

public class JFugue31 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("Find Reverse\n and Duration");
                
        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 31. Pattern Reverse and Pattern"
                + " Duration in main class.");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        
        Player player = new Player();
        
        Pattern pattern = new Pattern("C4q Rq C4+D4+G5ha50 Rh G4h.");
        
        Pattern reversePattern = reverse(pattern);
        
        text.appendText("\n\nPattern:\n" + pattern + "\n");
        text.appendText("\nReverse:\n" + reversePattern + "\n");
        
        text.appendText("\nDuration of pattern: " + 
                duration(pattern));
        text.appendText("\nDuration of reverse pattern: " + 
                duration(reversePattern));
        player.play(pattern, reversePattern);
        
    }
    
    public Pattern reverse(Pattern pattern) {
        Pattern transpose = new Pattern();
        for (Token token: pattern.getTokens()) 
            transpose.prepend(token);
        return transpose;
    }
    
    public double duration(Pattern pattern) {
        double duration = 0.0;
        for (Token token: pattern.getTokens()) {
            String val = token.toString();
            if (token.getType() == TokenType.NOTE) {
                if (val.contains("+")) {
                    Note n = new Note(val.replaceAll(".+[+]", ""));
                    duration += n.getDuration();
                } else {
                    Note n = new Note(val);
                    duration += n.getDuration();
                }
            }
        }
        return duration;
    }
}

This is the output:


No comments:

Post a Comment