function [] = freqplay() % sampling frequency Fs = 48000; % A above middle C Freq = 440; step = 2*pi*Freq/Fs; xmax = 2*pi*2*Freq; % where we want to sample the signal x = 0:step:xmax; % the signal - a sine wave with frequency 440 y = sin(x)'; plot(x(1:110),y(1:110)); soundsc(y,Fs); % by changing the frequency of our signal, we can play different notes % middle C Freq = 523.25; step = 2*pi*Freq/Fs; xmax = 2*pi*2*Freq; x = 0:step:xmax; y2 = sin(x)'; soundsc(y2,Fs); % E above middle C Freq = 659.26; step = 2*pi*Freq/Fs; xmax = 2*pi*2*Freq; x = 0:step:xmax; y3 = sin(x)'; soundsc(y3,Fs); % an octave about the A above middle C - as we talked about an octave corresponds to doubling the % frequency Freq = 880; step = 2*pi*Freq/Fs; % to change the length of a note, you would modify the xmax to be shorter/longer % lets make this one longer xmax = 2*2*pi*2*Freq; x = 0:step:xmax; y4 = sin(x)'; soundsc(y4,Fs); % so we can easily create individual notes, or chords by playing or adding sine waves together % play notes or add them together ytotal = [y; y2; y3; y+y2+y3; y4]; %% Play it soundsc(ytotal,Fs); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % you can also record you own sounds and add them to your composition %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% r = audiorecorder(Fs, 16, 1); record(r); % speak into microphone... pause(r); p = play(r); % listen resume(r); % speak again stop(r); p = play(r); % listen to complete recording mySpeech = getaudiodata(r, 'int16'); % get data as int16 array mySpeechd = double(mySpeech); mySpeechd = mySpeechd/max(mySpeechd); soundsc(mySpeechd,Fs); r2 = audiorecorder(Fs, 16, 1); record(r2); % speak into microphone... pause(r2); mySpeech2 = getaudiodata(r2, 'int16'); % get data as int16 array mySpeechd2 = double(mySpeech2); mySpeechd2 = mySpeechd2/max(mySpeechd2); soundsc(mySpeechd2,Fs); totalSpeech = zeros(300000,1); totalSpeech(1:length(mySpeechd)) = mySpeechd; totalSpeech(30000:29999+length(mySpeechd2)) = totalSpeech(30000:29999+length(mySpeechd2))+mySpeechd2; soundsc(totalSpeech,Fs); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % or import bits of audio files %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [cow, f] = wavread('cow.wav'); % make sure and resample your sample to be at the same frequency you've been working with f cow2 = resample(cow,Fs,f); whos cow2 % if it's in stereo just take one chanel if size(cow2,2)~=1 cow2 = cow2(:,1); end alltotal = [y+y2+y3; cow2; totalSpeech]; soundsc(alltotal,Fs) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % you can put them all together wherever you like %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% bigsound = zeros(300000,1); bigsound(1:length(y)) = bigsound(1:length(y))+y+y2+y3; bigsound(35000:34999+length(mySpeechd2)) = bigsound(35000:34999+length(mySpeechd2)) + mySpeechd2; bigsound(50000:49999+length(mySpeechd)) = bigsound(50000:49999+length(mySpeechd)) + mySpeechd; bigsound(100000:99999+length(cow2)) = bigsound(100000:99999+length(cow2)) + cow2; soundsc(bigsound,Fs); composition = bigsound; wavwrite(composition,Fs,16,'composition.wav');