playmidi.task.MidiPlayTask.java Source code

Java tutorial

Introduction

Here is the source code for playmidi.task.MidiPlayTask.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package playmidi.task;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.NoOpLog;

/**
 * ??????????
 *
 * @author normal
 */
public final class MidiPlayTask implements Runnable, AutoCloseable {

    /**
     * false?????????????
     */
    public static final boolean CLASS_LOG_OUTPUT_MODE = true;

    private static final Log LOG;

    static {
        if (CLASS_LOG_OUTPUT_MODE == true) {
            LOG = LogFactory.getLog(new Throwable().getStackTrace()[1].getClassName());
        } else {
            LOG = new NoOpLog();
            System.out.println(
                    new Throwable().getStackTrace()[1].getClassName() + "????");
        }
    }
    private final File midiFile;
    private final int loopCount;
    private final Sequencer sequencer;

    public MidiPlayTask(File midiFile, int count)
            throws FileNotFoundException, IOException, MidiUnavailableException, InvalidMidiDataException {

        this.midiFile = midiFile;
        this.loopCount = count;

        if (this.midiFile == null) {
            throw new NullPointerException("??????");
        }
        if (!midiFile.exists()) {
            MessageFormat msg1 = new MessageFormat("?????File={0}");
            Object[] parameters1 = { this.midiFile.getAbsolutePath() };
            throw new FileNotFoundException(msg1.format(parameters1));
        }
        if (!midiFile.isFile()) {
            MessageFormat msg1 = new MessageFormat("?????File={0}");
            Object[] parameters1 = { this.midiFile.getAbsolutePath() };
            throw new IllegalArgumentException(msg1.format(parameters1));
        }
        if (!midiFile.canRead()) {
            MessageFormat msg1 = new MessageFormat("?????File={0}");
            Object[] parameters1 = { this.midiFile.getAbsolutePath() };
            throw new IllegalArgumentException(msg1.format(parameters1));
        }
        MessageFormat msg1 = new MessageFormat("File={0},LoopCount={1}");
        Object[] parameters1 = { this.midiFile.getAbsolutePath(), loopCount };
        LOG.info(msg1.format(parameters1));

        LOG.trace("?");
        sequencer = MidiSystem.getSequencer();
        sequencer.setLoopEndPoint(-1L);
        sequencer.setLoopCount(loopCount);
        sequencer.open();
        LOG.trace("?");

        LOG.trace("??");
        try (FileInputStream in = new FileInputStream(this.midiFile)) {
            Sequence sequence = MidiSystem.getSequence(in);
            sequencer.setLoopCount(count);
            sequencer.setSequence(sequence);
        }
        LOG.trace("??");
    }

    public int getLoopCount() {
        return loopCount;
    }

    public synchronized long getMicrosecondLength() {
        return sequencer.getMicrosecondLength();
    }

    public synchronized long getMicrosecondPosition() {
        return sequencer.getMicrosecondPosition();
    }

    @Override
    public synchronized void run() {
        try {
            this.play(loopCount);
        } catch (InterruptedException ex) {
            this.close();
            LOG.error(ex);
        }
    }

    /**
     * midi???
     */
    private synchronized void play(int count) throws InterruptedException {
        sequencer.setLoopCount(count);
        MessageFormat msg1 = new MessageFormat("?File={0},LoopCount={1},Length(MicroSecond)={2}");
        Object[] parameters1 = { midiFile.getAbsolutePath(), count, sequencer.getMicrosecondLength() };
        LOG.info(msg1.format(parameters1));
        sequencer.start();
    }

    /**
     * ???
     */
    public synchronized void stop() {
        if (sequencer.isRunning()) {
            sequencer.stop();
            LOG.info("??");
        }
    }

    //?
    @Override
    public synchronized void close() throws IllegalStateException {
        stop();//?
        sequencer.close();
        LOG.info("");
    }

}