function nm = readmidi_java(file_name,add_track,verbose)
% Returns the same matrix that you get from the midi toolbox
%
%   nm = readmidi_java(file_name {, add_track, verbose});
%
% INPUTS:
%   file_name - name of the midi file
%   add_track (optional, default false) - if true, then the 
%     output nm  has an additional column with the track number
%   verbose (optional, default false) - if true, warning 
%     messages generated by the midi parsing will be added
%
% OUTPUTS:
%   nm - the note matrix.  This will have the same format as
%     the midi toolbox's note matrix.  Each row represents
%     a note and the columns are:
%       (1) - note start in beats
%       (2) - note duration in beats
%       (3) - channel
%       (4) - midi pitch (60 --> C4 = middle C)
%       (5) - velocity
%       (6) - note start in seconds
%       (7) - note duration in seconds
%       (8) - track (only present if add_track is true)
%
% NOTES:
%   (1) Channel numbers start at 1, following the midi toolbox
%       convention.  In the same spirit, track numbers also
%       begin at 1.
%
% 2010-05-03 Christine Smit csmit@ee.columbia.edu
% Released under the GNU Public License v. 3



if nargin < 2
    add_track = false;
end
if nargin < 3
    verbose = false;
end

import edu.columbia.ee.csmit.MidiKaraoke.read.*;
import java.io.File;
import javax.sound.midi.*;

midiFile = File(file_name);
if ~midiFile.exists
    error('Unable to find file %s',file_name);
end
seq = MidiSystem.getSequence(midiFile);

% get the number of ticks/quarter note, which I assume is the
% 'beat' in the nm
ticksPerQuarterNote = seq.getResolution();

pianoRoll = PianoRollViewParser.parse(seq,verbose);

notes = pianoRoll.getNotesDoubles;

nm = zeros(size(notes,1),7);

nm(:,1) = notes(:,4)./ticksPerQuarterNote; % start beats
nm(:,2) = notes(:,5)./ticksPerQuarterNote; % duration beats
nm(:,3) = notes(:,3)+1; % channel
nm(:,4) = notes(:,1); % pitch
nm(:,5) = notes(:,2); % velocity
nm(:,6) = notes(:,6); % start seconds
nm(:,7) = notes(:,7); % duration seconds

if add_track
    nm(:,8) = notes(:,8)+1;
end

end





