<commit_before>package piano;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.MIDIControl;

/**
 * Player of MIDI events.
 *
 * @author <NAME>
 */

public class MIDIPlayer implements NotePlayer
{
    /**
     * MIDI channel, on which the program is playing.
     */
    public static final int CHANNEL = 0;

    /**
     * MIDI event Note On.
     */
    public static final int MIDI_NOTE_ON = 0x90;

    /**
     * MIDI event Note Off.
     */
    public static final int MIDI_NOTE_OFF = 0x80;

    /**
     * MIDI event Control Change.
     */
    public static final int MIDI_PROGRAM_CHANGE = 0xC0;

    private PianoModel model;

    private Player player;

    private MIDIControl control;

    private boolean bankQuerySupported;

    private int[] banks;

    private boolean running;


    /**
     * Constructor.
     *
     * @param model Piano model
     *
     * @throws IOException
     * @throws MediaException
     * @throws ClassNotFoundException
     */
    public MIDIPlayer(PianoModel model) throws IOException, MediaException, ClassNotFoundException
    {
        // Store model
        this.model = model;

        // Create MIDI player
        player = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR);
        player.prefetch();

        // Create MIDI control
        control = (MIDIControl) player.getControl("javax.microedition.media.control.MIDIControl");

        // Check whether MIDI control was instantiated
        if(control == null)
            throw new ClassNotFoundException("MIDIControl not available!");

        // Check whether it is possible to query banks
        bankQuerySupported = control.isBankQuerySupported();

        // If it is...
        if(bankQuerySupported)
        {
            // Get all (=false) available banks
            banks = control.getBankList(false);
        }

        // Run player
        running = true;
    }

    /**
     * Get list of names of all available MIDI programs.
     *
     * @return List with names
     * @throws MediaException
     */
    public String[] getAllProgramNames() throws MediaException
    {
        Vector names = new Vector();
        String[] result;

        // If bank query supported ...
        if(bankQuerySupported)
        {
            // Iterate through banks
            final int banksLength = banks.length;
            for(int i = 0; i < banksLength; i++)
            {
                final int bankNumber = banks[i];
                int[] programs = control.getProgramList(bankNumber);

                // Iterate through programs
                final int programsLength = programs.length;
                for(int j = 0; j < programsLength; j++)
                {
                    final int programNumber = programs[j];

                    // Get name of the program
                    String programName =
                            control.getProgramName(bankNumber, programNumber);

                    // Add name of the program to the vector of names
                    names.addElement(programName);
                }
            }
        }

        // Convert vector to array and return it
        result = new String[names.size()];
        names.copyInto(result);

        return result;
    }

    /**
     * Set program on the first channel.
     *
     * @param index Index in the previously created list of program names
     */
    public void setProgram(int index)
    {
        int bank = banks[index / 127];
        int program = index % 127;

        // Use first channel
        control.setProgram(CHANNEL, bank, program);
    }

    /**
     * Implementation of NotePlayer.
     */
    public void stop()
    {
        running = false;
    }

    /**
     * Implementation of Runnable.
     */
    public void run()
    {
        while(running)
        {
            while(model.hasMoreNoteEvents())
            {
                NoteEvent ev = model.nextNoteEvent();

                switch(ev.getCode())
                {
                    case NoteEvent.NOTE_ON:<commit_msg>Made MIDI player stoppable at once<commit_after>package piano;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.MIDIControl;

/**
 * Player of MIDI events.
 *
 * @author <NAME>
 */

public class MIDIPlayer implements NotePlayer
{
    /**
     * MIDI channel, on which the program is playing.
     */
    public static final int CHANNEL = 0;

    /**
     * MIDI event Note On.
     */
    public static final int MIDI_NOTE_ON = 0x90;

    /**
     * MIDI event Note Off.
     */
    public static final int MIDI_NOTE_OFF = 0x80;

    /**
     * MIDI event Program Change.
     */
    public static final int MIDI_PROGRAM_CHANGE = 0xC0;

    /**
     * MIDI event Control Change.
     */
    public static final int MIDI_CONTROL_CHANGE = 0xB0;

    /**
     * MIDI control event All Notes Off.
     */
    public static final int MIDI_CONTROL_ALL_NOTES_OFF = 0x78;

    private PianoModel model;

    private Player player;

    private MIDIControl control;

    private boolean bankQuerySupported;

    private int[] banks;

    private boolean running;


    /**
     * Constructor.
     *
     * @param model Piano model
     *
     * @throws IOException
     * @throws MediaException
     * @throws ClassNotFoundException
     */
    public MIDIPlayer(PianoModel model) throws IOException, MediaException, ClassNotFoundException
    {
        // Store model
        this.model = model;

        // Create MIDI player
        player = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR);
        player.prefetch();

        // Create MIDI control
        control = (MIDIControl) player.getControl("javax.microedition.media.control.MIDIControl");

        // Check whether MIDI control was instantiated
        if(control == null)
            throw new ClassNotFoundException("MIDIControl not available!");

        // Check whether it is possible to query banks
        bankQuerySupported = control.isBankQuerySupported();

        // If it is...
        if(bankQuerySupported)
        {
            // Get all (=false) available banks
            banks = control.getBankList(false);
        }

        // Run player
        running = true;
    }

    /**
     * Get list of names of all available MIDI programs.
     *
     * @return List with names
     * @throws MediaException
     */
    public String[] getAllProgramNames() throws MediaException
    {
        Vector names = new Vector();
        String[] result;

        // If bank query supported ...
        if(bankQuerySupported)
        {
            // Iterate through banks
            final int banksLength = banks.length;
            for(int i = 0; i < banksLength; i++)
            {
                final int bankNumber = banks[i];
                int[] programs = control.getProgramList(bankNumber);

                // Iterate through programs
                final int programsLength = programs.length;
                for(int j = 0; j < programsLength; j++)
                {
                    final int programNumber = programs[j];

                    // Get name of the program
                    String programName =
                            control.getProgramName(bankNumber, programNumber);

                    // Add name of the program to the vector of names
                    names.addElement(programName);
                }
            }
        }

        // Convert vector to array and return it
        result = new String[names.size()];
        names.copyInto(result);

        return result;
    }

    /**
     * Set program on the first channel.
     *
     * @param index Index in the previously created list of program names
     */
    public void setProgram(int index)
    {
        int bank = banks[index / 127];
        int program = index % 127;

        // Use first channel
        control.setProgram(CHANNEL, bank, program);
    }

    /**
     * Implementation of NotePlayer.
     */
    public void stop()
    {
        // Stop MIDI synth
        control.shortMidiEvent(MIDI_CONTROL_CHANGE | CHANNEL,
                               MIDI_CONTROL_ALL_NOTES_OFF,
                               0);

        // Stop thread
        running = false;
    }

    /**
     * Implementation of Runnable.
     */
    public void run()
    {
        while(running)
        {
            while(model.hasMoreNoteEvents())
            {
                NoteEvent ev = model.nextNoteEvent();

                switch(ev.getCode())
                {
                    case NoteEvent.NOTE_ON:
{'fraction_non_alphanumeric': 0.08540162454873647, 'fraction_numerical': 0.004512635379061372, 'mean_word_length': 1.706043956043956, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3276580', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2252, 'n_words': 669}