Wednesday, July 6, 2016

JFugue 2. Play Notes

This program will play a note in the range from MIDI_START to MIDI_STOP, each time the Play button is clicked.


The play button will have the text for the next note to be played. The program will keep on recycling between MIDI_START and MIDI_STOP.


package jfugue2;

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.Note;

public class JFugue2 extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    
    final static byte MIDI_START = 54;
    final static byte MIDI_STOP = 66;
    static byte currentNote = MIDI_START;
    TextArea text;
    Button button;
    Player player;

    @Override
    public void start(Stage primaryStage) throws Exception {
        
        button = new Button("Play " + Note.getToneString(currentNote));
                
        button.setOnAction(e->example());
        
        player = new Player();
                
        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 2. Play Notes");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        Note note = new Note();
        note.setValue(currentNote);
        player.play(note);
        text.appendText("\nPlayed " + Note.getToneString(currentNote));
        if (currentNote < MIDI_STOP) currentNote ++;
        else currentNote = MIDI_START;
        button.setText("Play " + Note.getToneString(currentNote));
    }
}

This is the output after MIDI_STOP has been reached and the program has recycled back to MIDI_START:


No comments:

Post a Comment