Monday, July 25, 2016

JFugue 24. First Note, Rest or Harmonic

One of the important actions, on patterns, is transposition up and down semitones. There are 12 semitones to an octave.


JFugue parsers allows us modifications to a pattern. There are two classes in the jfugue24 package. Below is for the JFugue24a class in JFugue24a.java file. Here each note is queried, whether it is rest, a lone note (only first note), start of chord (first note) or part of chord (harmonic with the first note). Since this will be used later (for the actual transposition) a very simple case is presented here.


package jfugue24;

import org.jfugue.parser.ParserListenerAdapter;
import org.jfugue.theory.Note;

class JFugue24a extends ParserListenerAdapter {
    private StringBuilder sb = new StringBuilder();
    
    @Override
    public void onNoteParsed(Note note) {
        sb.append("\nNote ");
        sb.append(note);
        String str;
        if (note.isRest()) str = " is Rest";
        else if (note.isFirstNote()) str =" is First Note";
        else if (note.isHarmonicNote()) str = " is Harmonic Note";
        else str = "It is not rest, first note or harmonic note";
        sb.append(str);
    }
    
    public String getString() {
        return sb.toString();
    }
}

In the main class for the JFugue24 class, we create a listener object of JFugue24a that parses a pattern string, with the different conditionals.


In the text output, we can find the status of the various notes.



package jfugue24;

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.realtime.RealtimePlayer;
import org.staccato.StaccatoParser;

public class JFugue24 extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    TextArea text;
    Button button;
    RealtimePlayer rp;
    
    @Override
    public void stop() {
        rp.close();
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        
        rp = new RealtimePlayer();
        
        button = new Button("Kind of notes");
                
        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 24. First Note or Harmonic");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        
        Pattern pattern = new Pattern("T85 C4q Rq C4+D4q Rh "
                + "E5q. Rs. C5+D5+E5q");
        text.appendText("\nPattern:\n" + pattern + "\n\n");
        
        StaccatoParser parser = new StaccatoParser();
        JFugue24a listener = new JFugue24a();
        parser.addParserListener(listener);
        parser.parse(pattern);
        
        text.appendText("Types of notes:\n");
        text.appendText(listener.getString());
        
        
        rp.play(pattern);
    }
}

This is the output:


No comments:

Post a Comment