You are on page 1of 7

DSP2

Lab1

Lab 1: Elementary Music Synthesis Students are divided into group of two persons to do this lab assignment. Please wrap the e-file of your report & MATLAB files in a compressed file and then e-submit this file via TA email address by the due day. Thank you. 1. Goals The goal of this lab is to construct physically meaningful signals mathematically in MATLAB using your knowledge of signals. You will gain some understanding of the physical meaning of the signals you construct by using audio playback. We also hope that you have fun! 2. Lab resource - PC with Matlab - Sound card installed in PC - Headphone set 2. Background In this section, we explore how to use simple tones of various frequencies to compose a segment of music. In addition, you will work on improving the perceived quality of the sound you created. Each musical note can be simply represented by a sinusoid whose frequency depends on the note pitch. In this lab, you will use a sampling rate of 8 kHz. There are seven natural notes: A, B, C, D, E, F and G. After G, we begin again with A. Music is written on a "staff" consisting of five lines with four spaces between the lines. The notes on the staff are written in alphabetical order, the first line is E as shown in Figure 1. Notes can extend above and below the staff. When they do, ledger lines are added.

Figure 1. Natural notes Musical notes are arranged in groups of twelve notes called octaves. The notes that we'll be using for Scarborough Fair are in the octave containing frequencies from 220 Hz to 440 Hz. The twelve notes in each octave are logarithmically spaced in

DSP2

Lab1

frequency, with each note being of a frequency 21/12 times the frequency of the note of lower frequency. Thus, a 1-octave pitch shift corresponds to a doubling of the frequencies of the notes in the original octave. Table 1 shows the ordering of notes in the 220-440 octave to be used to synthesize the song, as well as the fundamental frequencies for these notes. Table 1. Notes in the 220 440 Hz octave Note A A#, Bb B C C#, Db D D#, Eb E F F#, Gb G G#, Ab A Frequency 220 220 21/12 220 22/12 220 23/12 220 24/12 220 25/12 220 26/12 220 27/12 220 28/12 220 29/12 220 210/12 220 211/12 440

A musical score is essentially a plot of frequencies (notes) on the vertical scale versus time (measures) on the horizontal scale. The musical sequence of notes for the piece you will synthesize is given in Figure 2. The following discussion identifies how musical scores can be mapped to tones of specific pitch and duration.

Figure 2. Musical score for Scarborough Fair 3. Note Frequency In the simplest case, each note may be represented by a burst of a sinusoid followed by a shorter period of silence (a pause). The pauses allow us to distinguish between separate notes of the same pitch. The duration of each note burst is determined by whether the note is a whole note, half note, fourth note, or an eight note (see Figure 3). Obviously, a fourth note has twice the duration of an eighth note, and so on.

DSP2

Lab1

In this Lab, use a duration of 4,000 samples for 1 count. Therefore, your whole notes should be four times the duration of your fourth notes. The short pause you use to follow each note should be of the same duration regardless of the length of the note. Longer periods of silence that are part of the musical score are indicated by one of more rest symbols.
whole note. it lasts 4 counts half note. it lasts 2 counts fourth note. it lasts 1 count

Figure 3. Types of notes Note that A-G only yields seven notes; the additional notes are due to changes in pitch called sharps (denoted by the symbol #) or flats (denoted by the symbol b) that follows a given note. A sharp increases the pitch by 21/12 and a flat decreases the pitch by 21/12. In the musical score in Figure 2, the first half note and fourth note are both A. The next three fourth notes are all E and so on. You can get the fundamental frequencies for these notes from Table 1. 4. Improving perceived quality There are many ways of improving the perceived quality of a synthesized sound. Here you will learn about two methods: varying the note volume and overlapping individual tones. 4.1 Volume variations Typically, when a note is played (e.g., using a piano), the volume rises quickly from zero and then decays over time, depending on how hard the key is struck and how long it is depressed. The variation of the volume over time can be divided into four segments: Attack, Decay, Sustain, and Release (ADSR). For a given note, volume changes can be achieved by multiplying a sinusoid by another function called a windowing function. An example of such function simulating the ADSR effects is shown in Figure 4.

volume

R t

DSP2

Lab1

Figure 4. An ADSR Envelope 4.2 Overlapping tones Another improvement in perceived quality can be achieved by overlapping some notes as done by advanced piano players. As the volume of one note is decaying, another note is played. Mathematically, this can be accomplished by allowing the time regions occupied by subsequent sinusoids to overlap, hence removing the pause. This will yield a much smoother, less staccato-sounding piece. 5. Assignments 5.1. Music synthesis (25%) - Synthesize the piece of music appearing in Figure 2 using only information from Sections 2 and 3. - Play it back using the SOUND command in Matlab. Type HELP SOUND for more information. Please specify the sampling rate = 8k Hz in the playback. - Save the entire music synthesis in an .m file. Include this .m file in your E-Submit. M=4000; fs=8000; delta_f=2^(1/12); A = 220; A_flat = A*delta_f^11; A_sharp = A*delta_f; B = A*delta_f^2; B_flat = A*delta_f; C = A*delta_f^3; C_sharp = A*delta_f^4; D = A*delta_f^5; D_flat = A*delta_f^4; D_sharp = A*delta_f^6; E = A*delta_f^7; E_flat = A*delta_f^6; F = A*delta_f^8; F_sharp = A*delta_f^9; G = A*delta_f^10; G_flat = A*delta_f^9; G_sharp = A*delta_f^11; fourth_note = 1:M; half_note = 1:2*M; whole_note = 1:4*M; N1 = sin(2*pi*A*half_note/fs); N2 = sin(2*pi*A*fourth_note/fs); N3 = sin(2*pi*E*fourth_note/fs); N4 = sin(2*pi*B*fourth_note/fs); N5 = sin(2*pi*C*fourth_note/fs);

DSP2

Lab1

N6 = sin(2*pi*A*whole_note/fs); P = zeros(1,1000); S = [N1 P N2 P N3 P N3 P N3 P N4 P N5 P N4 P N6]; sound(S,fs) 5.2. Sound quality improving (25%) - Improve the quality of the sound with a volume window function (see Section 4.1 above). Try concatenating different function to model ADSR. - Were you able to improve the sound quality? Save the modified music synthesis in a new m-file. Include this .m file in your E-Submit. level_A = linspace(0,1,length(half_note)+length(P)); >> level_D = linspace(1,0.75,length(fourth_note)+length(P)); >> level_S = linspace(0.75,0.75,4*length(fourth_note)+4*length(P)); >> level_R +2*length(P)); = linspace(0.75,0,2*length(fourth_note)+length(whole_note)

>> level_ADSR = [level_A, level_D, level_S, level_R]; >> S_ADSR = level_ADSR.*S; >> sound(S_ADSR,8000) 5.3. Note decaying (25%) - As explained in Section 4.2, allow the decaying notes (i.e. with the windowing function) to overlap slightly in time. Decide the overlap duration yourself. - Were you able to improve the sound quality? Save the modified music synthesis in a new .m file. Include this .m file in your E-Submit. P1 = 0.5*[N1(1,length(N1)-499:end) N2(1,1:500)]; P2 = 0.5*[N2(1,length(N2)-499:end) N3(1,1:500)]; P3 = 0.5*[N3(1,length(N3)-499:end) N3(1,1:500)]; P4 = 0.5*[N3(1,length(N3)-499:end) N4(1,1:500)]; P5 = 0.5*[N4(1,length(N4)-499:end) N5(1,1:500)]; P6 = fliplr(P5); P7 = 0.5*[N4(1,length(N4)-499:end) N6(1,1:500)];

DSP2

Lab1

S_overlap = [N1 P1 N2 P2 N3 P3 N3 P3 N3 P4 N4 P5 N5 P6 N4 P7 N6]; sound(S_overlap,fs)

DSP2

Lab1

You might also like