Mercurial > hg > smallbox
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util/matlab_midi/synth.m Mon Mar 28 17:35:01 2011 +0100 @@ -0,0 +1,64 @@ +function y=synth(freq,dur,amp,Fs,type) +% y=synth(freq,dur,amp,Fs,type) +% +% Synthesize a single note +% +% Inputs: +% freq - frequency in Hz +% dur - duration in seconds +% amp - Amplitude in range [0,1] +% Fs - sampling frequency in Hz +% type - string to select synthesis type +% current options: 'fm', 'sine', or 'saw' + +% Copyright (c) 2009 Ken Schutte +% more info at: http://www.kenschutte.com/midi + +if nargin<5 + error('Five arguments required for synth()'); +end + +N = floor(dur*Fs); + +if N == 0 + warning('Note with zero duration.'); + y = []; + return; + +elseif N < 0 + warning('Note with negative duration. Skipping.'); + y = []; + return; +end + +n=0:N-1; +if (strcmp(type,'sine')) + y = amp.*sin(2*pi*n*freq/Fs); + +elseif (strcmp(type,'saw')) + + T = (1/freq)*Fs; % period in fractional samples + ramp = (0:(N-1))/T; + y = ramp-fix(ramp); + y = amp.*y; + y = y - mean(y); + +elseif (strcmp(type,'fm')) + + t = 0:(1/Fs):dur; + envel = interp1([0 dur/6 dur/3 dur/5 dur], [0 1 .75 .6 0], 0:(1/Fs):dur); + I_env = 5.*envel; + y = envel.*sin(2.*pi.*freq.*t + I_env.*sin(2.*pi.*freq.*t)); + +else + error('Unknown synthesis type'); +end + +% smooth edges w/ 10ms ramp +if (dur > .02) + L = 2*fix(.01*Fs)+1; % L odd + ramp = bartlett(L)'; % odd length + L = ceil(L/2); + y(1:L) = y(1:L) .* ramp(1:L); + y(end-L+1:end) = y(end-L+1:end) .* ramp(end-L+1:end); +end