Friday, July 8, 2016

JFugue 8. Realtime Player

Most of the time we will use the Realtime Player.


We will use this player, in most cases, rather than the one from Player class since it leads to smoother interactions, as audio playing does not block the JavaFX thread. Notice the text immediately follows start of audio, rather than the end of audio. Also the audio start and end should be smoother (no clicks).


We override the stop() method to close the player when the program is closed.


package jfugue8;

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.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.jfugue.realtime.RealtimePlayer;
import org.jfugue.theory.Note;

public class JFugue8 extends Application {
    
    RealtimePlayer player;
    
    public static void main(String[] args) {
        
        launch(args);
    }
    
    TextArea text;
    Button[] buttons;
    
    @Override
    public void stop() {
        player.close();
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        
        player = new RealtimePlayer();
        buttons = new Button[13];
        
        for (int i = 0; i < buttons.length; i++) {
            String noteString = Note.getToneString((byte) (60+i));
            buttons[i] = new Button(noteString);
            buttons[i].setOnAction(e->example(noteString));
            buttons[i].setPrefWidth(100);
            buttons[i].setTextFill(Color.BLUE);
        }
                        
        VBox examples = new VBox(10, buttons);
        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 8. Realtime Player");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example(String noteValue) {
        player.play(noteValue);
        text.appendText("\n Playing note " + noteValue );
    }
}

This is the output after all 13 notes have been played:


2 comments:

  1. Can this realtime player recieve midi input from a midi controller???

    Like a keyboard: playing a note plays a note in the ifugue synth.

    How can I do this?

    Thank you

    ReplyDelete
  2. I am talking about midi in. Which jfugue commands connect to midi in port or equavelent?

    ReplyDelete