function miniSynth(Name,Freq,Amp) % simple waves synth % Gavin Bell % Science of Music IL016 % January 2016 % % Name is full filename e.g. 'miniSynth_1.wav' % Freq is list of frquencies starting with fundamental % e.g. [440 880 2750] % Amp is amplitudes of those frequencies % e.g. [1 0.2 0.1] %---basic parameters Fs = 32768; % sample rate Dur = 5; % duration in s Vol = 0.8; % volume Tcyc = 1/Freq(1); % time for 1 cycle Ncyc = round(Fs*Tcyc); % number of samples in 1 cycle plotCyc = 5; % number of cycles to plot % t = linspace(1/Fs, Dur, Dur*Fs); % time y = zeros(1,Dur*Fs); % initialize sound data for j=1:length(Freq); % loop to add up frequencies y = y + Amp(j)*sin(2*pi*Freq(j)*t); end y = Vol*y/max(y); % normalize to Vol audiowrite(Name,y,Fs); % write WAV file % yPlot = y(1:Ncyc*plotCyc); % plot plotCyc cycles to see beats tPlot = t(1:Ncyc*plotCyc); subplot(2,1,1), plot(tPlot,yPlot), xlabel('time (s)') Y = abs(fft(y)); % FFT of y for plot P = Y(1:(Fs*Dur/2)+1)/(Fs*Dur); % scale power (?) f = (1:(Fs*Dur/2)+1)/Dur; % frequency subplot(2,1,2), plot(f,P), axis([0 max(f) 0 max(P)*1.1]), xlabel('frequency (Hz)')