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