Thursday, July 7, 2016

JFugue 4. Saving a MIDI file

A four-note file is saved to a midi file.


Notes C, D, E and F are saved. They are in octave 6, and have velocity of 71, 80, 90, 101 respectively. The first 2 are quarter-notes and the next 2 are half-notes.


package jfugue4;

import java.io.File;
import java.io.IOException;
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.midi.MidiFileManager;
import org.jfugue.pattern.Pattern;
import org.jfugue.player.Player;

public class JFugue4 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("Save MIDI");
                
        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 4. Saving a MIDI file");
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void example() {
        String patt = "T100 V0 C6qa71 D6qa80 E6ha90 F6ha101";
        Pattern pattern = new Pattern(patt);
        Player player = new Player();
        try {
            MidiFileManager
                    .savePatternToMidi(pattern, new File("JFugue4.mid"));
        } catch (IOException ex) {
        }
        text.appendText("Pattern:" + patt
                        + "\nSaved to JFugue4.mid");
        player.play(pattern);
    }
}

This is the output:


2 comments: