Subject: [公告] 練習題 (這是以前的大一習題)
Date announced: 2002/10/02
Due: 2002/12/09  in class

HWK#6: Choose any one from the following two problems:
    6A: 寫一個程式可以用畫圖方式秀出 Hanoi tower 的搬動過程
        且搬動中要讓 user 可以按下 ESC 鍵就提前結束
        參考 /net/ftp/pub/CSIE/course/cs2/hanoiu.txt
            (就是 ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs2/hanoiu.txt)
        把 hanoiu.exe 抓回 PC 上執行看看(記住用 bin mode)
       參考程式:
             ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs2/testg.cpp
             ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs1/p/testga3.pas
 
6B: 寫一個程式可以從 PC 喇叭撥放一些音樂 至少要有三首歌, 讓 user 以 menu driven 方式選歌 任何時候, 螢幕上要有一個正確的時間(文字就可), 時間要有在走 撥放歌曲時螢幕上除了有時間之外, 也要用跑馬燈方式把該歌曲的簡譜秀出 參考程式: ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs2/menu.pas ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs2/pkbhit.cpp ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs1/p/testga3.pas
musicline.gif>
Purpose: 練習電腦畫圖與電腦音樂, 和 克難式的 Menu driven 方式, 以及如何讓 user 感覺同時做多件事 (多工 multi-tasking)
Detail: (a) 畫圖用 Turbo C++ 的簡單繪圖即可 (b) 聲音用 Turbo C++ 的 sound 函數即可 關於頻率請參考之前 post 過的文章 (或是本篇後面文章) (c)Report to turn in: * 此習題要先繳交一張(最多兩面)的心得報告 (週一) * 程式碼必須在週三的天亮之前E-mail寄給助教 chiaming@csie.nctu.edu.tw * 然後在交心得報告的該週三(2002/12/11) GH 做 Demo -- 跟不上的人真的要好好利用週末! 好好地寫習題了:-) 寫好了再去玩:-)


                            Music and Sound

    Music would not exist without sound. Everything musical is made from
sound. And yet, the reverse is not true. There are many sounds which are
not musical.

    Sound is the vibration of air particles, which travels to your ears
from the vibration of the object making the sound.  These vibrations of
sound in the air are called sound waves.

    Musical sounds are vibrations which are strongly regular. When you hear
a regular vibration, your ear detects the frequency of the vibration, and you
perceive this as the pitch of a musical tone. We know that larger waves make a
louder sound. The size of the waves is called the amplitude of the waveform.
Amplitude can be measured, and this is important for balancing and controlling
the loudness, or volume, of sound.

    The other aspect of a waveform which can be measured is the frequency.
This measurement is only useful or meaningful for musical sounds, where
there is a strongly regular waveform. Frequency is measured as the number of
wave cycles that occur in one second. The unit of frequency measurement is
Hertz (Hz for short). A frequency of 1 Hz means one wave cycle per second,
while a frequency of 10 Hz means ten wave cycles per second, where the cycles
are much smaller and closer together.

    The note(音符) 'A' which is above Middle 'C' has a frequency of 440 Hz.
And, the 'A' note below middle 'C' is at 220 Hz. (中央 A 為 440 Hz)
It is often used as a reference frequency for tuning musical instruments.


    Something very interesting happens when you double the frequency of a
note. The pitch of the doubled frequency sounds higher, but somehow the same
as the original note, while the pitches of all frequencies in between sound
completely different. It seems strange, but there is a logical reason for this
similarity. The sound waves below show us that two cycles of the 880 Hz
frequency fit exactly in the space of a single cycle of the 440 Hz frequency.

    An octave is the difference in pitch between two notes where one has twice
the frequency of the other.  Two notes which are an octave apart always
sound similar and have the same note name, while all of the notes in between
sound distinctly different, and have different names from the outer two.

    This is a very important concept in music.  Although notes are
arranged, like a piano keyboard, in a long series from low to high,
there is a cyclical pattern.

    Notes naturally fall into groups of twelve, which are all one octave
apart from each other.  These groups repeat going up and down the piano
keyboard (and indeed, the musical spectrum for any instrument).

    There is a magic number in music, known as the twelfth root of two,
and it has a value of approximately 1.059463.  This is the number that,
when multiplied by itself twelve times, gives a result of two.

    Why is this important to music? Remember that with notes one octave
apart, the higher note has double the frequency of the lower note. The range
of frequencies in between is divided up into the twelve steps that give us
all of our notes.  (註: 就是所謂的十二平均律)
The difference in pitch between two adjacent notes is called a semitone.(半音)

    The twelfth root of two, when multiplied by the frequency of a note,
gives the frequency of the next note up.(註: 就是升高半音)
After doing this for twelve notes, you end up with twice the frequency,
which is the note one octave up from the starting note. 
We could continue in both directions, until we have calculated the
frequency of every musical note.




    The set of all musical notes is called the Chromatic scale, a name
which comes from the Greek word chroma, meaning color.  In this sense,
chromatic scale means "notes of all colors".  Remember that colors also,
are made up from different frequencies, those of light waves.

    You might be wondering how to refer to a particular C note, since
there are several possible C notes.

    The easy answer is that it often doesn't matter.  Since notes an
octave apart sound similar, a tune played one octave up or down will
still sound the same as the original, just higher or lower.

    If the exact octave is important, you can relate its to Middle C.
This is the C note in the centre of a piano keyboard, which has a
frequency of 261.63 Hz.  For example, you could refer to
"A above Middle C" (which has frequency of 440 Hz) as we did earlier.

    Writing notes on staff lines (in standard musical notation) is
another way to fix them in a specific octave.  Each of the lines and
spaces in between represent a note relative to Middle C.

    You can also follow the note with a number, to indicate the octave.
For example, a common convention is to use C4 to represent Middle C,
while C5 would represent the C note one octave up. 
This is not a standard notation, so you need to make it clear which
octave holds Middle C.

    Also note with this naming scheme that within an octave number,
the notes should be arranged from C to B and not, as you might expect,
from A to G.  In other words, the following notes are in order of
increasing pitch:

      ... B3, C4, D4, E4, F4, G4, A4, B4, C5, D5 ...


   The table below shows the frequencies of the twelve notes between
note A at 440 Hz, and note A one octave up from it. 

       A     440.00 Hz    =  6       A#/Bb 466.16 Hz    =  6#
       B     493.88 Hz    =  7
       C     523.25 Hz    =  1       C#/Db 554.37 Hz    =  1#
       D     587.33 Hz    =  2       D#/Eb 622.25 Hz    =  2#
       E     659.25 Hz    =  3
       F     698.46 Hz    =  4       F#/Gb 739.99 Hz    =  4#
       G     783.99 Hz    =  5       G#/Ab 830.61 Hz    =  5#
       A     880.00 Hz    =  6

 Middle C     261.63 Hz   (C4)
        D     293.66 Hz   (D4)
        E     329.63 Hz   (E4)
        F     349.23 Hz   (F4)
        G     391.99 Hz   (G4)
        A     440.00 Hz   (A4)
        B     493.88 Hz   (B4)
         C     523.25 Hz   (C5)


  Human ears can only hear sounds within a certain range of frequencies.
As people grow older, their hearing range reduces.  A young person can
usually hear sounds in the range of 20 Hz to 20 000 Hz.