/*
 * Copyright (c) 2016 Christian Micocci. This file is part of Concerto.
 *
 * Concerto is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Concerto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Concerto.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.n_micocci.concerto.filters;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.ShortMessage;

public class FilterVelocity extends AbstractMidiFilter {
    private int velocity;

    public FilterVelocity(int velocity) {
        this.velocity = velocity;
    }

    @Override
    public void send(MidiMessage midiMessage, long l) {
        int status = midiMessage.getStatus();
        if (status / 16 == 0x8 || status / 16 == 0x9) {
            byte[] message = midiMessage.getMessage();
            int note = message[1];
            try {
                midiMessage = new ShortMessage(status, note, message[2] != 0 ? velocity : 0);
            } catch (InvalidMidiDataException e) {
                e.printStackTrace();
            }
        }
        getReceiver().send(midiMessage, l);
    }

}
