Upgrade to Pro — share decks privately, control downloads, hide ads and more …

用程式碼寫音樂 - Sonic Pi 教學 2

Kalan
March 21, 2020

用程式碼寫音樂 - Sonic Pi 教學 2

Video: https://youtu.be/0kccvDX9lqQ

這部影片的靈感是來自好和弦在前幾週的直播,如果沒看過的話可以趕快到資訊卡裡頭看看!好和弦一直是我很喜歡的 YouTube 頻道之一,甚至連我開頭音樂都是用好和弦推薦的 PixiTracker 做出來的。

Sonic Pi 是由 Sam Aaron 與 Raspberry Pi Foundation 協作開發的軟體,可以透過 live coding 的方式來做音樂,最初用在教學音樂課跟程式理論上,不過隨著功能越來越多,你甚至可以用它來做現場演奏。而且為了讓初學者可以快速上手,這個軟體在語法上以及環境上下了非常大的功夫,所以寫起來也相當直覺,馬上就可以看到成果。

第二部我們繼續學習 Sonic Pi 的基本功能,包含好用的 tick, spread, sync、函數、隨機等等,在創作上有更豐富的元素等著我們使用。

Kalan

March 21, 2020
Tweet

More Decks by Kalan

Other Decks in Technology

Transcript

  1. 數拍子 tick live_loop :tick1 do play chord(:c4, :major).tick sleep 0.25

    end 每次呼叫後會自動 +1 live_loop :tick1 do play scale(:c4, :major).tick play scale(:c4, :major).tick sleep 0.25 end chord(:c4, :major).each do |note| play note sleep 0.25 end scale(:c4, :major).each do |note| play note sleep 0.25 end
  2. 取得目前的 beat,但不增加 tick look live_loop :tick1 do play scale(:c4, :major).tick

    play chord(:c4, :major).look sleep 0.25 end current_beat = tick tick += 1 play scale(:c4, :major)[current_beat] play chord(:c4, :major)[current_beat]
  3. 方便建立節奏、音符的函數 ring notes = ring(:g4, :e4, :e4, :f4, :d4, :d4,

    :c4, :d4, :e4, :f4, :g4, :g4, :g4) sleeps = ring(0.25, 0.25, 0.5, 0.25, 0.25, 0.5, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.5) live_loop :name do play notes.tick sleep sleeps.look end
  4. 組合技 (進階)用 ring 與 define 自製節奏! define :create_my_beat do |tempo|

    return ring(*tempo.split("").map { |beat| beat == "*" }) end my_tempo = "*--*--*-" # ring (true, false, false, true, false, false, true, false)
  5. my_tempo = create_my_beat("*--*--*-") melody = ring(:a5, :c6, :a5, :a5, :c6,

    :a5, :a5, :c6, :a5, :b5, :a5, :g5) live_loop :my_beat do if my_tempo.tick(:name) then sample :drum_heavy_kick play melody.tick(:name1), release: 0.25 end sleep 0.125 end 實際應用(Shape Of You)
  6. 創造神奇節拍(輾轉相除法) spread spread(1,8) = (ring true , false, false, false,

    false, false, false, false) spread(2,8) = (ring true , false, false, false, true , false, false, false) spread(3,8) = (ring true , false, false, true , false, false, true , false) spread(4,8) = (ring true , false, true , false, true , false, true , false) spread(5,8) = (ring true , false, true , false, true , true , false, true) spread(6,8) = (ring true , false, true , true , true , false, true , true) spread(7,8) = (ring true , false, true , true , true , true , true , true) spread(8,8) = (ring true , true , true , true , true , true , true , true)
  7. live_loop :melody do sample :drum_heavy_kick, amp: 2 if spread(2, 4).tick

    sample :drum_cymbal_open if spread(5, 8).look play scale(:a4, :major_pentatonic, num_octaves: 2).choose() if spread(6, 13).look sleep 0.25 end 隨便用用看
  8. live_loop :random_dorian do play scale(:c5, :dorian).choose(), release: 0.7 sleep rrand(0.25,

    1) end live_loop :chorus do [6, 4, 1, 5].each do |d| 6.times do use_synth :blade play (chord_degree d, :c, :major, 3).shuffle().take(3).choose() sleep 0.25 end end end live_loop :drum do sample :drum_heavy_kick if one_in(2) sample :drum_cymbal_soft if one_in(6) sample :elec_cymbal, amp: 0.25 sleep 0.25 end 佛系作曲
  9. live_loop :drum do sample :drum_heavy_kick sleep 1 end live_loop :melody

    do sync :drum play :c4, release: 0.25 sleep 0.25 play :d4, release: 0.25 sleep 0.25 play :e4, release: 0.25 sleep 0.25 end sync 和其它聲部同步
  10. sound effect 玩起來! FX with_fx :reverb, room: 1 do notes.each_with_index

    do |n, i| use_synth :beep play n, amp: 0.7, release: 0.4 sleep 0.25 end end with_fx :echo do notes.each_with_index do |n, i| use_synth :beep play n, amp: 0.7, release: 0.4 sleep 0.25 end end