ELE 301, Fall 2010 Laboratory No. 2: 1 Background

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

000

001
ELE 301, Fall 2010
002 Laboratory No. 2
003
004
005
006
1 Background
007
008
The objective of this lab is to illustrate the fact that complex signals can be synthesized
from rather simple signals by the use of just a few elementary signal operations: envelope
009
modulation, time shifts, and superposition (addition). We will use the application of music
010
synthesis to explore these ideas in an interesting and fun context.
011
012 You will write a MATLAB program that inputs the score of a short piece of classical mu-
013 sic (in more or less standard musical notation) and synthesizes a time sampled waveform
014
corresponding to the specified music.
015
016 1.1 .wav files
017
In order to convert and save the matlab digital audio signal in .wav format use the following
018
command
019
020 wavwrite(s,fs,N, ’e:\pathname\filename’); % save s in .wav file format
021 % with quantization to N bits.
022
023 This command quantizes values in s to N bits and then together with the sampling fre-
024 quency fs converts the signal into .wav format and saves it to the file ”filename” on drive
025
”e:” and path ”pathname”. You should store files in your home directory or a subdirectory
of your home directory. You can then listen to the signal by starting the audio player and
026
opening this file.
027
028
1.2 Music Synthesis
029
030
031
032
033
034
035
036
037
Figure 1: Morning Mood
038
039
The first few bars of Morning Mood by E. Grieg are shown in Fig. 1. The position and note
value of each note indicates the frequency, and duration of the waveform or signal associated
040
with that note and the order of the notes indicates timing.
041
042 Suppose that a single note of frequency f , duration d and unit amplitude played at time
043 0 yields the waveform wf,d (t). Let the frequency, duration and amplitude of the j th note
044 in the above score be denoted by fj , dj and Aj respectively. Then we can construct the
045
waveform described by the score as the sum:
046 M
X
047 m(t) = Aj wfj ,dj (t − τj ) (1)
048 j=1
049
050
were τj is the time at which the j th note is played. The music is thus the sum, or superpo-
sition, of the all of the notes played at their appropriate times.
051
052 In principle, we can attempt to automate this process on a computer as follows: construct
053 the waveforms wf,d (t) for each note in the score; then form the sum (1) using the appropriate
delays and amplitudes as indicated in the score.

1
054
Of course we have to work with sampled versions of the waveforms. So the actual syn-
055 thesis will be done using discrete-time signals intended to model sampled versions of the
056 continuous-time waveforms.
057
058
059
060 1.3 Notes
061
062 Musical notation indicates the fundamental frequency of each note by its vertical location
063 on the musical staff, and its duration by the note type: whole, half, quarter, eighth, etc. The
064 musical notes are grouped into octaves with each octave containing 12 notes. The octave
065 containing middle C covers the frequency range from 220Hz to 440Hz. Within each octave
066 the notes are logarithmically spaced so that jumping one octave doubles the frequency. Thus
067 the frequency of each note is 21/12 times the frequency of the note below it. The frequencies
068 of the notes in the middle C octave are shown in Figure 2.
069
070
071 A 220Hz
072 As 21/12 *A ≈ 233Hz
073 B 21/12 *As ≈ 247Hz
074 C 21/12 *B ≈ 262Hz
075 Cs 21/12 *C ≈ 277Hz
076
D 21/12 *Cs ≈ 294Hz
077
Ds 21/12 *D ≈ 311Hz
078
E 21/12 *Ds ≈ 330Hz
079
F 21/12 *E ≈ 349Hz
080
Fs 21/12 *F ≈ 370Hz
081
082
G 21/12 *Fs ≈ 392Hz
083
Gs 21/12 *G ≈ 415Hz
084
Figure 2: Notes in the middle C octave.
085
086
087
088
089
090 1.4 Representing the score
091
092
We need to select a numerical representation for the note frequencies and durations. For
093
the durations we will use 1 to represent a whole note, 2 to represent a half note, 4 for a
094
quarter, and 8 for an eighth.
095
096 The frequency information can be represented in MATLAB be defining a variable for each
097 note frequency: s=2^
(1/12); A=220; As=A*s; B=As*s; C=B*s; ... etc. This has already
098
been done for you and is available in the file notes.m. This M-file defines variables cor-
responding to three octaves. The variable names are: A0, As0, B0, C0, Cs0, D0, Ds0,
099
E0, F0, Fs0, G0, Gs0 for the octave below middle C; A, As, B, C, Cs, D, Ds, E, F,
100
Fs, G, Gs for the octave containing middle C; and A2, As2, B2, C2, Cs2, D2, Ds2,
101 E2, F2, Fs2, G2, Gs2 for the octave above middle C.
102
103 Be careful: do not use these variable names in your program for other purposes. In
104
particular Fs has been used for the note F sharp, so you cannot use it for the sampling
frequency.
105
106 We can represent the first few bars of Morning Mood in MATLAB by 3 matrices and a
107 scalar as follows:

2
108
%% MUSIC: MORNING MOOD
109 %% ————————————————–
110 % nf=Notes to play
111 % nd=Duration of each note: 8,4,2,1= 1/8,1/4,1/2,1
112 % na=Relative amplitude of each note
113 % TD=Time duration of one whole note in secs
114 nf=[G E D C D E G E D C D E G E G A2 E A2 G E D C ];
115 nd=[4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4/5];
116 na=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ];
117
TD=1.5;
%% ————————————————–
118
119
120 So nf(j) is the frequency of the j th note, TD/nd(j) is its duration in seconds and na(j) is
121 its relative amplitude.
122
123 1.5 Generating a note: the instrument
124
125
A primitive model for a note of frequency f and duration d is a sine wave of that frequency
and duration followed by a short period of silence. This can be written as the continuous
126
time signal
127
wf,d (t) = sin(2πf t)(u(t) − u(t − d)), t ∈ [0, D]
128
129
where D is the total duration and d is the note duration.
130 The above model does not produce anything resembling realistic instrument sound. Real
131 instruments have at least two additional important characteristics. First, when a particular
132 note is played not only is the fundamental frequency is generated but also higher harmonics
133 of the fundamental. The amplitudes of the harmonics are usually significantly less than that
134 of the fundamental.
135 In addition to harmonics, the instrument also produces a characteristic note envelope. Figure
136 3 shows the waveform of actual piano notes. The signal envelope has a significant rise time
137 and a slower exponential-like decay. In the primitive model, the note envelope is just a
138 rectangular pulse u(t) − u(t − d). Producing a more realistic sound requires replace this
139 term by an envelope signal e(t).
140
1
141
0.8
142
0.6
143
0.4
144
145 0.2

146 0

147 −0.2

148 −0.4

149 −0.6

150
−0.8

151
−1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
152
153
Figure 3: Piano notes.
154
155
156
157
158
159
160
161

3
162
ELE 301
163 Laboratory No. 2
164
165
166
167
2 Lab Procedure
168
Several MATLAB .m files will be provided: notes.m, odetojoy.m, mornmood.m, and
169
furelise.m. The file notes.m contains the note definitions. Look in the file to see what
170
variables have been defined. The example program included at the end indicates how this
171 file could be used. The files odetojoy.m (short), mornmood.m (medium), and furelise.m
172 (long) are files defining musical scores. When you are working with scores start with short
173 ones first to test your program.
174
WARNING: Some programs you will write, although not very long, are complex and can
175
be memory hungry. Write the program in simple steps. Test each step before proceding to
176
the next, and use variables efficiently.
177
178
2.1 Task 1: Make a single note
179
180 Write three MATLAB programs that will generate a single note waveform of specified fre-
181 quency and duration. The first program is the master program, mkmusic.m, that will define
182 variables, and for the moment plot and play the result. The other two programs, myinst.m
183 and mknote.m, are called by the master program. The program myinst.m specifies the
184 instrument characteristics and the program mknote.m generates the actual note waveform.
185 Do this in the following sequence of steps:
186
187 1. Write the program mkmusic.m. This program should
188 • clear memory and figures.
189 • define the note frequency variables. This is easy: you can use the M-file
190 notes.m just like a regular matlab built-in command.
191 • call the program myinst.m (which you will write later) to define the instrument
192 variables and characteristcs.
193 • define the variables:
194
195 TD - the duration, in seconds, of one whole note.
196
nf - the frequency of the note to be played
nd - the duration (1, 2, 4, 8) of the note to be played
197
na - the amplitude of the note to be played
198
tt - the time vector for the note
199
200
201 For now just put in one note, so nf, nd and na will each have one entry.
202 • call the program myinst.m (which you will write later) to define the instrument
203 variables and characteristcs.
204 • call a program mknote.m (which you will write later) to generate the note
205 specified by frequency, duration and amplitude.
206 • Plot the note waveform vs time (for the single note)
207 • Play the note using the MATLAB sound command.
208 2. Write the program myinst.m to specify the instrument characteristics. This will
209 include the harmonic amplitudes for a note (relative to a unity amplitude note),
210 and the note envelope. Use the variables:
211
ha - for the harmonic amplitudes
212
env - for the note envelope
213
214
215 At first, assume that the instrument is primitive: it has no harmonics and its
envelope is just the pulse u(t) − u(t − d), where d is the note duration.

4
216
3. Write the program mknote.m that generates a note of frequency nf, duration nd
217 and amplitude na (with the instrument characteristics in ha and env, which for
218 now don’t matter) and returns it in the vector n. This will be very similar to the
219 program harmon.m that you wrote in Lab 1. You can make this program a function
220 or a script.
221
222 4. Test your programs by generating notes at middle C of durations 1, 1/2, 1/4 and
223 1/8, and unity amplitude. The sound output should be shorter for a 1/8 note (nd=8)
224 than for a 1/4 note etc.. Demonstrate your program to the TA before proceeding
225
further.
226
5. Once you have the above programs working you can experiment with instrument
227 sounds by varying the harmonic content and envelope shapes, i.e., by refining the
228 constants in your myinst.m program. Generally: more harmonics with larger ampli-
229 tudes give an organ-like sound, moderate harmonic content and an exponential-like
230 envelope will give a crude piano-like sound. Note that you could make the envelope
231 depend on the note duration. How?
232
233
234
235
236
237
238
239
240
241
242 When you are happy with your instrument sound, specifiy your harmonic amplitudes
243 and your selected envelope function(s) here:
244
245
246
247
248
249
250
251
252
253
254
255
256 Demonstrate your program to the TA. Then print out your programs and attach
257 them to this handout.
258
259
2.2 Task 2: Music
260
261
Now that you can make one note the next task is put the notes together to play an entire
262
score. While this is conceptually simple, obtaining and efficient implementation requires
263 thinking things through carefully.
264
265 The first step is to add a line to the mkmusic program to load the score variables. The
266
command odetojoy, for example, loads the score variables for the first few bars of Ode to
Joy. This will take the place of your definitions for the single note played in the previous
267
part of the lab.
268
269 Now one way to play the score is to add a loop that simply calls mknote for each note in
the score. Try this and play the result.

5
270
You should detect two dawbacks to this approach: it sounds roughly like a one finger piano
271 player because there is too much delay between notes; and we are still using the low quality
272 MATLAB sound command.
273
274
To avoid using the sound command we want to generate the waveform for the entire score
first, save it as a .wav file, and then play it later with the windows audio player. This should
275
also solve the problem of excessive delay between notes.
276
277 We want to generate the waveform for the whole score using a discrete time version of
278 equation (1). In doing this we will allow the note waveforms to overlap so that we model
279 playing the next note while the previous one is still sounding. In principal this is easy:
280
generate each note one at a time in the correct order and then add it as a subvector in the
correct place to a big vector that represents the waveform for the entire score. The correct
281
place corresponds roughly to the value of τj in equation (1). The only tricky parts are:
282
283
284 1. Computing how long the big vector should be. This allows you to define it (and
285
hence reserve the required memory space) before the construction begins.
286
2. Keeping track of where the next note should be added into the big vector. (Use a
287 pointer?)
288
3. Avoiding running out of memory!
289
290
291 You need to think about the above items before coming to the lab.
292
293
1. Modify your program mkmusic.m to generate the waveform for the whole score and
then store your waveform as a .wav file to your directory. Use the windows audio
294
player to play it. (Note you can’t have the file opened by two programs at the same
295
time. So if your MATLAB program is to write to the file, then you must first close
296 it in any other program.)
297
2. Test your program on a short score first (e.g. odetojoy.m). Debug and then try
298
mornmood.m and furelise.m. Play your version of Für Elise for the TA.
299
300 3. Add reverberation by shifting the signal by various delays, scaling them with de-
301
creasing weights for longer delays and adding them to the original signal. Try to
find values that improve sound quality. Record them below:
302
303
304
305
306
307
308
309
310 4. Want to do more? You should be able to easily modify your music program to
311 synthesize a score with several voices. The files odeto2.m contains the notes for a
312
second voice for Ode to Joy. See if you can synthesize the music and play it.
313
314
315
316
317
318
319
320
321
322
323

You might also like