comparison util/matlab_midi/midi2audio.m @ 81:a30e8bd6d948

matlab_midi scripts
author Ivan <ivan.damnjanovic@eecs.qmul.ac.uk>
date Mon, 28 Mar 2011 17:35:01 +0100
parents
children
comparison
equal deleted inserted replaced
80:16df822019f1 81:a30e8bd6d948
1 function [y,Fs]=midi2audio(input,Fs,synthtype)
2 % y = midi2audio(input, Fs, synthtype)
3 % y = midi2audio(input, Fs)
4 % y = midi2audio(input)
5 %
6 % Convert midi structure to a digital waveform
7 %
8 % Inputs:
9 % input - can be one of:
10 % a structure: matlab midi structure (created by readmidi.m)
11 % a string: a midi filename
12 % other: a 'Notes' matrix (as ouput by midiInfo.m)
13 %
14 % synthtype - string to choose synthesis method
15 % passed to synth function in synth.m
16 % current choices are: 'fm', 'sine' or 'saw'
17 % default='fm'
18 %
19 % Fs - sampling frequency in Hz (beware of aliasing!)
20 % default = 44.1e3
21
22 % Copyright (c) 2009 Ken Schutte
23 % more info at: http://www.kenschutte.com/midi
24
25 if (nargin<2)
26 Fs=44.1e3;
27 end
28 if (nargin<3)
29 synthtype='fm';
30 end
31
32 endtime = -1;
33 if (isstruct(input))
34 [Notes,endtime] = midiInfo(input,0);
35 elseif (ischar(input))
36 [Notes,endtime] = midiInfo(readmidi(input), 0);
37 else
38 Notes = input;
39 end
40
41 % t2 = 6th col
42 if (endtime == -1)
43 endtime = max(Notes(:,6));
44 end
45 if (length(endtime)>1)
46 endtime = max(endtime);
47 end
48
49
50 y = zeros(1,ceil(endtime*Fs));
51
52 for i=1:size(Notes,1)
53
54 f = midi2freq(Notes(i,3));
55 dur = Notes(i,6) - Notes(i,5);
56 amp = Notes(i,4)/127;
57
58 yt = synth(f, dur, amp, Fs, synthtype);
59
60 n1 = floor(Notes(i,5)*Fs)+1;
61 N = length(yt);
62
63 % ensure yt is [1,N]:
64 y(n1:n1+N-1) = y(n1:n1+N-1) + reshape(yt,1,[]);
65
66 end