Sonic π ︰ 即時編程《二》

TOPLAP Logo

All things live coding

A curated list of live coding languages and tools

This list was created with the intention of giving a quick-glance over the many possibilities to engage in this practice. For further information, head over to TOPLAP.

Contents

 

─── 摘自《/awesome-livecoding

 

想嚐鮮 sonic π 即時編程嗎?想知道他有多迷人嘛!

請跟著 Dr . Sam Aaron 博士之步伐︰

9 – Live Coding

One of the most exciting aspects of Sonic Pi is that it enables you to write and modify code live to make music, just like you might perform live with a guitar. One advantage of this approach is to give you more feedback whilst composing (get a simple loop running and keep tweaking it till it sounds just perfect). However, the main advantage is that you can take Sonic Pi on stage and gig with it.

In this section we’ll cover the fundamentals of turning your static code compositions into dynamic performances.

Hold on to your seats…

9.1 – Live Coding

Now we’ve learned enough to really start having some fun. In this section we’ll draw from all the previous sections and show you how you can start making your music compositions live and turning them into a performance. For that we’ll need 3 main ingredients:

  • An ability to write code that makes sounds – CHECK!
  • An ability to write functions – CHECK!
  • An ability to use (named) threads – CHECK!

Alrighty, let’s get started. Let’s live code our first sounds. We first need a function containing the code we want to play. Let’s start simple. We also want to loop calls to that function in a thread:

define :my_sound do
  play 50
  sleep 1
end

in_thread(name: :looper) do
  loop do
    my_sound
  end
end

If that looks a little too complicated to you, go back and re-read the sections on functions and threads. It’s not too complicated if you’ve already wrapped your head around these things.

What we have here is a function definition which just plays note 50 and sleeps for a beat. We then define a named thread called :looper which just loops around calling my_sound repeatedly.

If you run this code, you’ll hear note 50 repeating again and again…

Changing it up

Now, this is where the fun starts. Whilst the code is still running change 50 to another number, say 55, then press the Run button again. Woah! It changed! Live!

It didn’t add a new layer because we’re using named threads which only allow one thread for each name. Also, the sound changed because we redefined the function. We gave :my_sound a new definition. When the :looper thread looped around it simply called the new definition.

Try changing it again, change the note, change the sleep time. How about adding a use_synth statement? For example, change it to:

define :my_sound do
  use_synth :tb303
  play 50, release: 0.3
  sleep 0.25
end

Now it sounds pretty interesting, but we can spice it up further. Instead of playing the same note again and again, try playing a chord:

define :my_sound do
  use_synth :tb303
  play chord(:e3, :minor), release: 0.3
  sleep 0.5
end

How about playing random notes from the chord:

define :my_sound do
  use_synth :tb303
  play choose(chord(:e3, :minor)), release: 0.3
  sleep 0.25
end

Or using a random cutoff value:

define :my_sound do
  use_synth :tb303
  play choose(chord(:e3, :minor)), release: 0.2, cutoff: rrand(60, 130)
  sleep 0.25
end

Finally, add some drums:

define :my_sound do
  use_synth :tb303
  sample :drum_bass_hard, rate: rrand(0.5, 2)
  play choose(chord(:e3, :minor)), release: 0.2, cutoff: rrand(60, 130)
  sleep 0.25
end

Now things are getting exciting!

However, before you jump up and start live coding with functions and threads, stop what you’re doing and read the next section on live_loop which will change the way you code in Sonic Pi forever…

 

踏進『即時迴圈』的世界︰

9.2 – Live Loops

Ok, so this section of the tutorial is the real gem. If you only read one section, it should be this one. If you read the previous section on Live Coding Fundamentals, live_loop is a simple way of doing exactly that but without having to write so much.

If you didn’t read the previous section, live_loop is the best way to jam with Sonic Pi.

Let’s play. Write the following in a new buffer:

live_loop :foo do
  play 60
  sleep 1
end

Now press the Run button. You hear a basic beep every beat. Nothing fun there. However, don’t press Stop just yet. Change the 60 to 65and press Run again.

Woah! It changed automatically without missing a beat. This is live coding.

 

事實上,早有一本『祕笈』在等著你呢☆

Sonic Pi Essentials

Learn to code musical masterpieces with the creator of Sonic Pi in our best Essentials e-book yet!

sonicpi

 

Sam Aaron, the creator of Sonic Pi, has written this book to complement the software’s built-in tutorial and to help you jump-start your live-coding career. The book is packed with fun, instantly rewarding examples designed to highlight many of the amazing things Sonic Pi enables you to do. By the time you’ve finished you’ll be able to code up some phat bass, sparkling synth leads and start practicing for your first live-coding gig…

Live code and craft amazing sounds across 10 chapters, including:

  • Master live loops
  • Build drum breaks
  • Compose your own melodies
  • Make random riffs and loops
  • Learn to shape and sculpt sounds
  • and much, much more…