% Musical Notes for ECE333 design project
% Winter 2001-02

% Parameters:
% f0    -   reference frequency (Hz)
f0 = 440;
% fclk  -   master clock frequency for frequency divider (Hz)
fclk = 50e3;
% nlo   -   lowest semitone number
nlo = -12;
% nhi   -   highest semitone number
nhi = 24;
% N     -   bits of resolution for frequency divider
res = 8;

% Sampling frequency for audio
fs = 10e3;

% Time duration for each note
dur=0.2;


% Semitones
n = nlo:nhi;

% Frequencies (in Hz)
f = f0*2.^(n/12);

% Convert frequencies to frequency divider constants
N = round(fclk./f);

% Determine actual frequencies when using N
fac = fclk./N;

% Report min and max values of N
max(N)
min(N)


% Render audio using actual frequencies
tt=0:1/fs:dur-1/fs;
y=[];
for k=1:length(f)
    y=[y square(2*pi*fac(k)*tt)];
end

% Play the audio
soundsc(y,fs)