Mercurial > hg > smallbox
comparison util/matlab_midi/synth.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=synth(freq,dur,amp,Fs,type) | |
2 % y=synth(freq,dur,amp,Fs,type) | |
3 % | |
4 % Synthesize a single note | |
5 % | |
6 % Inputs: | |
7 % freq - frequency in Hz | |
8 % dur - duration in seconds | |
9 % amp - Amplitude in range [0,1] | |
10 % Fs - sampling frequency in Hz | |
11 % type - string to select synthesis type | |
12 % current options: 'fm', 'sine', or 'saw' | |
13 | |
14 % Copyright (c) 2009 Ken Schutte | |
15 % more info at: http://www.kenschutte.com/midi | |
16 | |
17 if nargin<5 | |
18 error('Five arguments required for synth()'); | |
19 end | |
20 | |
21 N = floor(dur*Fs); | |
22 | |
23 if N == 0 | |
24 warning('Note with zero duration.'); | |
25 y = []; | |
26 return; | |
27 | |
28 elseif N < 0 | |
29 warning('Note with negative duration. Skipping.'); | |
30 y = []; | |
31 return; | |
32 end | |
33 | |
34 n=0:N-1; | |
35 if (strcmp(type,'sine')) | |
36 y = amp.*sin(2*pi*n*freq/Fs); | |
37 | |
38 elseif (strcmp(type,'saw')) | |
39 | |
40 T = (1/freq)*Fs; % period in fractional samples | |
41 ramp = (0:(N-1))/T; | |
42 y = ramp-fix(ramp); | |
43 y = amp.*y; | |
44 y = y - mean(y); | |
45 | |
46 elseif (strcmp(type,'fm')) | |
47 | |
48 t = 0:(1/Fs):dur; | |
49 envel = interp1([0 dur/6 dur/3 dur/5 dur], [0 1 .75 .6 0], 0:(1/Fs):dur); | |
50 I_env = 5.*envel; | |
51 y = envel.*sin(2.*pi.*freq.*t + I_env.*sin(2.*pi.*freq.*t)); | |
52 | |
53 else | |
54 error('Unknown synthesis type'); | |
55 end | |
56 | |
57 % smooth edges w/ 10ms ramp | |
58 if (dur > .02) | |
59 L = 2*fix(.01*Fs)+1; % L odd | |
60 ramp = bartlett(L)'; % odd length | |
61 L = ceil(L/2); | |
62 y(1:L) = y(1:L) .* ramp(1:L); | |
63 y(end-L+1:end) = y(end-L+1:end) .* ramp(end-L+1:end); | |
64 end |