Thursday, July 7, 2016

JFugue 5. Playing chords

We recycle through the entire range of chords built-in to JFugue. We get the chord names via Chord.getChordNames().


The base note is C. We use the String method toLowerCase() only for readability. The program will work fine without it.


package jfugue5;

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.player.Player;
import org.jfugue.theory.Chord;

public class JFugue5 extends Application {
    
    static int currentChord = 0;

    public static void main(String[] args) {
        launch(args);
    }
    
    TextArea text;
    Button button;
    String[] names;

    @Override
    public void start(Stage primaryStage) throws Exception {
        
        names = Chord.getChordNames();
        
        button = new Button("Play C" + names[currentChord].toLowerCase());
                
        button.setOnAction(e->example());
        
        
        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 5. Playing chords");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        
        String chordString = "C" + names[currentChord].toLowerCase();
        Chord chord = new Chord(chordString);
        Player player = new Player();
        int notes = chord.getNotes().length;
        text.appendText(String.format("\n\n%d. %s\n\t%d Notes (%s)",
                currentChord, chordString, notes,
                chord.getPatternWithNotes()));
        player.play(chord.getPattern());
        currentChord ++;
        currentChord %= names.length;
        button.setText("Play C" + names[currentChord].toLowerCase());
    }
}

This is the output after a few chords have been played:


No comments:

Post a Comment