關於matlab編寫鋼琴曲的學習

關於matlab編寫鋼琴曲的學習

鑒於最近很多童靴詢問,加之剛好老師最近有提及Music and Math的初略講解。現奉上老師給予的貝多芬的《致愛麗絲》,希望大家互相學習。也希望各位童靴在求學或者生活的路上一直自信前行,畢竟我們都還年輕!Fighting、、、

  • 鋼琴共88個鍵,其中A鍵(49號鍵)為國際標準高音即440Hz,如下圖:

  • 其他各按鍵與標準鍵之間的關係,以及各按鍵頻率如下圖:

貝多芬-《致愛麗絲》代碼:

play.m

% Usage : play(treble, treble_duration, bass, bass_duration)%% treble -> 高音音符% treble_duration -> 高音音符持續時間% bass -> 低音音符% bass_duration -> 低音音符持續時間function music = play(treble, treble_duration, bass, bass_duration)fs = 11025; % sampling frequency, 11025 Hz on PC/Macspeed_factor = 2; % cpu speed compensation factortreble_vector = zeros(1,sum(treble_duration)*fs+1); % treble vector generatorn1 = 1; % starting indexfor kk = 1:length(treble) keynum = treble(kk); % if (keynum == 0) % rest period definition A = 0.0; % amplitude at 0.0 freq = 440; else A = 0.5; % note amplitude at 0.5 freq = 440 * (2^( (keynum-49)/12 )); % frequency definition end tt = 0 : (1/fs) : (treble_duration(kk)/speed_factor); % duration generator tone = A * cos( 2* pi* freq* tt); % tone generator % n2 = n1 + length(tone) - 1; % ending index & concatenate vector treble_vector(n1:n2) = treble_vector(n1:n2) + tone; % vector generator n1 = n2; % reset indexendbass_vector = zeros(1,sum(bass_duration)*fs+1); % bass vector generatorn1 = 1;for kk = 1:length(bass) keynum = bass(kk); % if (keynum == 0) A = 0; freq = 440; else A = 0.5; freq = 440 * (2^( (keynum-49)/12 )); end tt = 0 : (1/fs) : (bass_duration(kk)/speed_factor); tone = A * cos( 2* pi* freq* tt); % n2 = n1 + length(tone) - 1; bass_vector(n1:n2) = bass_vector(n1:n2) + tone; n1 = n2;endmusic_vector = treble_vector + bass_vector; % treble and bass vector combinationsound( music_vector, fs ) % generate sound

play_love.m

% These are the piano key numbers and the corresponding durations for% the song Fur Elise.% t and tdur are for the treble clef or upper part and b and bdur are for % the bass clef or lower part. A duration of 1 corresponds to an eighth note% and a note value of 0 is a rest.clear, clct = [ 56 55 56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44 56 55 ... 56 55 56 51 54 52 49 0 40 44 49 51 0 44 52 51 49 0 51 52 54 56 47 57 56 ... 54 45 56 54 52 44 54 52 51 0 44 56 0 0 56 68 0 0 55 56 0 0 55 56 55 ... 56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44 56 55 56 55 56 51 54 52 ... 49 0 40 44 49 51 0 44 52 51 49];tdur = [ .5 .5 .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 ... .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1.5 .5 .5 .5 ... 1.5 .5 .5 .5 1.5 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 ... .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 ... 1 .5 .5 .5 .5 1 .5 .5 .5 .5 2];b = [ 0 0 25 32 37 0 0 20 32 36 0 0 25 32 39 0 0 ... 0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 28 35 40 0 0 ... 23 35 39 0 0 25 32 37 0 0 20 32 44 0 44 56 0 0 55 56 0 0 55 56 0 0 ... 0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 0 0 ... 25 32 37 0 0 20 32 36 0 0 25 32 37 0];bdur = [ 1 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 ... 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 1.5 .5 .5 .5 .5 1 ... .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 1 ... 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 3 ... .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5]; Play(t,tdur,b,bdur)

歡迎留言,互相學習!

推薦閱讀:

TAG:MATLAB | 娛樂 | eLearning |