ivan@81: function [PR,t,nn] = piano_roll(Notes,vel,ts)
ivan@81: %
ivan@81: % Inputs:
ivan@81: %  Notes: A 'notes' matrix as returned from midiInfo.m
ivan@81: %  vel:   (optional) if vel==1, set value to note velocity instead of 1. (default 0)
ivan@81: %  ts:    (optional) time step of one 'pixel' in seconds (default 0.01)
ivan@81: %
ivan@81: % Outputs:
ivan@81: %  PR:    PR(ni,ti): value at note index ni, time index ti
ivan@81: %  t:     t(ti):  time value in seconds at time index ti
ivan@81: %  nn:    nn(ni): note number at note index ti
ivan@81: %
ivan@81: %   (i.e. t and nn provide 'real-world units' for PR)
ivan@81: %
ivan@81: 
ivan@81: % Copyright (c) 2009 Ken Schutte
ivan@81: % more info at: http://www.kenschutte.com/midi
ivan@81: 
ivan@81: if nargin < 2
ivan@81:   vel = 0;
ivan@81: end
ivan@81: if nargin < 3
ivan@81:   ts = 0.01;
ivan@81: end
ivan@81: 
ivan@81: Nnotes = size(Notes,1);
ivan@81: 
ivan@81: n1 = round(Notes(:,5)/ts)+1;
ivan@81: n2 = round(Notes(:,6)/ts)+1;
ivan@81: 
ivan@81: if vel == 0
ivan@81:   vals = ones(Nnotes,1);
ivan@81: else
ivan@81:   vals = Notes(:,4); % velocity
ivan@81: end
ivan@81: 
ivan@81: for i=1:Nnotes
ivan@81:   PR(Notes(i,3), n1(i):n2(i)) = vals(i);
ivan@81: end
ivan@81: 
ivan@81: % create quantized time axis:
ivan@81: t = linspace(0,max(Notes(:,6)),size(PR,2));
ivan@81: % note axis:
ivan@81: nn = min(Notes(:,3)):max(Notes(:,3));  
ivan@81: % truncate to notes used:
ivan@81: PR = PR(nn,:);