Tuesday, July 26, 2016

JFugue28. Transpose in main class

The Transpose code has been cleaned up (not requiring a listener as well) and put in the main code so this is the only file.


We send a pattern, as well as interval, to a method called transposeFromPattern. It converts each token using String and Note function into individual notes, and calls getNote as needed to get the String output.


Later we will put these two methods into their own class (Mod) and turn them static so we can use them as, transposePattern = Mod.transposeFromPattern(pattern,-12) to get pattern octave below.


In the split function for the chord case, we put '+' is [ ], so it is treated as a literal, and not as part of a regular expression.


package jfugue28;

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 JFugue28 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 Transpose");
                
        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 28. Transpose in main class.");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        
        Player player = new Player();
        
        Pattern pattern = new Pattern("T85 C4q Rq C4+D4ha50 Rh "
                + "E5q.a100 Rs. C5+D5+E5q Ri C3+E4+F3+G4 rh.");
        
        Pattern transpose1 = transposeFromPattern(pattern, 1);
        Pattern transpose2 = transposeFromPattern(pattern, 2);
        Pattern transpose3 = transposeFromPattern(pattern, 3);
        Pattern transpose4 = transposeFromPattern(pattern, 4);
        
        text.appendText("\n\nPattern:\n" + pattern + "\n");
        text.appendText("\nTranspose by 1:\n" + transpose1 + "\n");
        text.appendText("\nTranspose by 2:\n" + transpose2 + "\n");
        text.appendText("\nTranspose by 3:\n" + transpose3 + "\n");
        text.appendText("\nTranspose by 4:\n" + transpose4 + "\n");
        
        player.play(pattern, 
                transpose1, transpose2, transpose3, transpose4);
        
    }
    
    public Pattern transposeFromPattern(Pattern pattern, int nt) {
        Pattern transpose = new Pattern();
        for (Token token: pattern.getTokens()) {
            if (token.getType() != TokenType.NOTE) {
                transpose.add(token.getPattern());
            }
            else {
                String val = token.toString();
                if (val.substring(0,1).equalsIgnoreCase("r")) { // Rest 
                    transpose.add(token.toString());
                }
                else if (val.contains("+")) { // Chord (C+E+G format)
                    String newValue = "";
                    String[] notes = val.split("[+]");
                    int i;
                    for (i = 0; i<(notes.length-1); i++) {
                        newValue += (getNote(notes[i], 0, nt) + "+");
                    }
                    i = notes.length-1;
                    newValue += getNote(notes[i], 1, nt);
                    transpose.add(newValue);
                    
                }
                else { // Single note
                    transpose.add(getNote(token.toString(), 1, nt));
                }
            }
        }
        return transpose;
    }
    
    public String getNote(String noteString, int flag, int interval) {
        Note note = new Note(noteString);
        byte value = note.changeValue(interval).getValue();
        String tone = Note.getToneString(value);
        if (flag==0) return tone;
        else {
            String dur = Note.getDurationString(note.getDuration());
            String vel = note.getVelocityString();
            return tone + dur + vel;
        }
    }
}

This is the output:


No comments:

Post a Comment