view util/matlab_midi/midi2audio.m @ 218:c38d965b5a1d luisf_dev

Moved CVX_add_const_Audio_declipping.m from solvers to the examples/AudioInpainting folder.
author Aris Gretsistas <aris.gretsistas@eecs.qmul.ac.uk>
date Thu, 22 Mar 2012 15:37:45 +0000
parents a30e8bd6d948
children
line wrap: on
line source
function [y,Fs]=midi2audio(input,Fs,synthtype)
% y = midi2audio(input, Fs, synthtype)
% y = midi2audio(input, Fs)
% y = midi2audio(input)
%
% Convert midi structure to a digital waveform
%
% Inputs:
%  input - can be one of:
%    a structure: matlab midi structure (created by readmidi.m)
%    a string: a midi filename
%    other: a 'Notes' matrix (as ouput by midiInfo.m)
%
%  synthtype - string to choose synthesis method
%      passed to synth function in synth.m
%      current choices are: 'fm', 'sine' or 'saw'
%      default='fm'
%
%  Fs - sampling frequency in Hz (beware of aliasing!)
%       default =  44.1e3

% Copyright (c) 2009 Ken Schutte
% more info at: http://www.kenschutte.com/midi

if (nargin<2)
  Fs=44.1e3;
end
if (nargin<3)
  synthtype='fm';
end

endtime = -1;
if (isstruct(input))
  [Notes,endtime] = midiInfo(input,0);
elseif (ischar(input))
  [Notes,endtime] = midiInfo(readmidi(input), 0);
else
  Notes = input;
end

% t2 = 6th col
if (endtime == -1)
  endtime = max(Notes(:,6));
end
if (length(endtime)>1)
  endtime = max(endtime);
end


y = zeros(1,ceil(endtime*Fs));

for i=1:size(Notes,1)

  f = midi2freq(Notes(i,3));
  dur = Notes(i,6) - Notes(i,5);
  amp = Notes(i,4)/127;

  yt = synth(f, dur, amp, Fs, synthtype);

  n1 = floor(Notes(i,5)*Fs)+1;
  N = length(yt);  

  % ensure yt is [1,N]:
  y(n1:n1+N-1) = y(n1:n1+N-1) + reshape(yt,1,[]);

end