% program chordplay % S. Scott Moor September 2006, modified April 2008 % This script illustrates several principles about creating music and % sound, particularly for developing chords. % % EXERCISE 1: Create a Major chord % Play this script as is by typing "chordplay" at the command % line or simply push the "run" icon on the editor toolbar. % set up initial values: sampling rate, basic frequency clear sf = 20000; f = 523.25; % the ratio of frequencies for adjacent notes is the twelfth root of two b = 2^(1/12); % Look at the frequencies created: disp(['The first frequency is ',num2str(f)]) disp(['The second frequency is ' num2str(f*b^4)]) disp(['The third frequency is ', num2str(f*b^7)]) % Notes in a major chord are 4 and 7 half steps apart respectively % these frequencies can be calculated as shown above. % 1. What are the names for these three notes? % 2. What advantage can you see to calculating them this way instead of just % typing in the frequencies? % 3. How are the three note series combined into one series/chord? % You will have to look later in this program to answer this question. % here we create the three notes for a major chord first = makesquare2(f, sf, 1, 2); third = makesquare2(f*b^4, sf, .8, 2); fifth = makesquare2(f*b^7, sf, .6, 2); % EXERCISE 2: Add a delay % Uncomment the four lines at the end of this section, run the % script again and notice the delays that result. Look over these lines and % see how the delay is created. pad = zeros(1,5000); first = [first, pad, pad]; third = [pad, third, pad]; fifth = [pad, pad, fifth]; % 4. How is the delay created? % 5. Why is the pad added at the end of the first note twice? % 6. What are the dimensions of the chord array (the # of rows and columns)? % Use the size command or add a size column to your workspace window. % here we combine the notes and divide by 3 so that the maximum series % value does not exceed the sound card limit of 1. chord = (first + third + fifth)/3; % EXERCISE 3: Play two notes in stereo % You can also create multiple sounds by sending one to the right channel % and one to the left channel. For this approach we can only use two % notes. To accomplish this we change our sound vectors from row to % column vectors and concatenate them together. % Uncomment the line below and rerun the script to try this variation. % chord = [first', fifth']; % If you play this on a computer with stereo speakers or with headphones, % you should be able to hear the first not start in the left speaker and % the pick up the fifth in the right speaker. % 7. Look at your workspace again. What are the dimensions of the chord % array now? % % now we can play the chord with the sound command sound(chord,sf)