50

ProgrammingfMaDA_MEAP_ch1

Embed Size (px)

Citation preview

Page 1: ProgrammingfMaDA_MEAP_ch1
Page 2: ProgrammingfMaDA_MEAP_ch1

MEAP Edition Manning Early Access Program

Programming for Musicians and Digital Artists Version 1

Copyright 2013 Manning Publications

For more information on this and other Manning titles go to www.manning.com

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

Page 3: ProgrammingfMaDA_MEAP_ch1

Welcome

Thank you for purchasing the MEAP for Programming for Musicians and Digital Artists: Creating music with ChucK. We are excited to see the book reach this stage and look forward to its continued development and eventual release.

This is a beginner level book designed for anyone who is looking to learn how to program, especially in a creative context. We are going to start at the very beginning, so don’t be afraid. We're going to teach programming through musical examples because we believe that being able to “hear” what your code is doing will aid you in learning key computer science concepts, as well as make this process enjoyable.

We have strived to make the content both approachable and meaningful, and to explain not just how to do things with ChucK but how general computer science concepts work in most languages including C++, java, and others. We believe that ChucK and this book can serve as a gateway to these harder-to-get-started languages.

We’re releasing the first five chapters to start. They are really for the beginning programmer, introducing key concepts needed to be able to begin coding. Chapter 0 introduces ChucK to the reader showing examples of how it has been used in creative projects. Chapter 1 begins with the basics of computer science including variables, types, conditional statements and looping structures. Chapter 2 introduces libraries built in ChucK, and shows how random numbers and different mathematical equations can be used to make more expressive compositions. Chapter 3 introduces arrays and shows how to make melody in ChucK. In Chapter 4, we introduce how to use sound files in ChucK and how you can use them to create soundscapes and even build a drum machine.

Looking ahead, we will go over topics including Functions, Unit Generators, Audio Signal Processing, Concurrency and Multithreading, Classes and Objects, HID device input, and MIDI with ChucK. We expect to have updates to the book every two to three weeks, whether that is a new chapter or an update to an existing chapter.

As you’re reading, we hope you’ll take advantage of the Author Online forum. We’ll be reading your comments and responding, and your feedback is helpful in the development process.

— Ajay Kapur, Perry Cook, Spencer Salazar, and Ge Wang

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

Page 4: ProgrammingfMaDA_MEAP_ch1

brief contents

0 Introduction: ChucK Programming for Artists 1 Basics: Sound, Waves, and ChucK Programming 2 Libraries 3 Arrays 4 Samples and Sound File Manipulation 5 Functions 6 Unit Generators(UGs) 7 Synthesis ToolKit (STK) UGs and Instruments 8 Multi-Threading and Concurrency 9 Objects and Classes 10 Polling vs. Events 11 MIDI and Open Sound Control (OSC) 12 Conclusion appendix

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

Page 5: ProgrammingfMaDA_MEAP_ch1

0 Introduction: ChucK Programming

for Artists

This chapter covers

• Computer-mediated art works and systems

• Why do artists need to program?

• What is ChucK?

• Why program in ChucK?

• Why we and others use ChucK

• Some of our own computer-mediated art works

For many years now, the words “musician” and “artist” have been changing meaning, rapidly, almost daily, largely due to the introduction of computer technology. Artists perform live with computer technology all the time. The ones who interact directly with computers as part of their performances might call themselves DJs, laptop artists, controllerists, live coders, and a host of other names. Many of these musicians do not program or write software, but an increasing number want more direct control over their process and the results. Others want to make new instruments or controllers, or configure existing controllers such as drum pads, DJ control “decks,” etc. to use in new ways for their live performances. Still others want to produce “songs” and “albums” (.wav and/or .mp3 files) as the final result, but would like more control in the process than off-the-shelf music software provides. Some others like (or want) to program as part of their creative process and workflow.

Many non-music artists also use computers in their art-making, some for production and manipulation to yield a final result, such as graphic designers, animators, film editors, set

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

1

Page 6: ProgrammingfMaDA_MEAP_ch1

designers, sculptors, and others who use computer graphics and design software. Increasingly, many works of installation art, public art, multi-media works, sound sculptures, soundscapes, etc. involve using various combinations of sound, sensors, graphics, video, and displays to create interactive environments. These pieces can make the audience or spectator part of the performance or experience. In some cases, members of the public witnessing these pieces don’t know exactly what they are doing or controlling. In some cases the process, control, and effects are more obvious, but the discovery, learning, interacting, and performing is much of the point, and reward.

We like to call all of these new types of art and performance opportunities, “computer mediated” art systems. In all cases, the musician or artist either needed to know how to program or collaborate with someone how knew how to program. This increasingly popular movement of computers as a new medium for creativity, motivated our team to write this book to make it easier and fun for everyone to learn to write their own programs. Specifically, we want to teach readers of this book how to program through making code that translates into music and sound. We accomplish this by using a programming language called ChucK that is specially designed for sonic coding.

In this chapter we begin with a discussion on why we think musicians and artists need to learn how to program. We then go on to explain why we believe ChucK is a great first language to learn. We continue by describing how programming has allowed each of us to create new works in the digital art arena.

0.1 Why do Musicians and Artists Need to Program? As we mentioned earlier, many artists are happy with over-the-counter software systems and controllers for real-time performance work. And there are many who just want to use computers to produce static final products in the form of .wav/.mp3 files, CDs or collections of songs, sound tracks for videos, etc. And a large number of those artists are happy to learn and use the packages and tools available from commercial or free sources.

But there are many, and we’re betting you’re one, who want more. Perhaps you already know how to program in a language such as java, but you find it doesn’t do what you want. Or maybe you are coming to this book with a big idea (or many), and just want the tools to help you realize it/them. Maybe you’re looking to shift directions in your art making.

Others think that programming will help them get a job in the future. While we can’t promise immediate outplacement after you finish this book, we can say that we have nice jobs, and our students have nice jobs (those who want them ), due in no small-part to their ability to program and solve problems with computers. There is increasing demand for people who know how to program, or at least understand the jargon, and who grasp the implications/importance of what computer programs and programmers do.

We, in our individual projects, art works, and teaching, have used ChucK and other computer music/art languages for years now. Some of us have programmed, and still do, in multiple languages. Whether you’re already a programmer or not, you’ll think differently after working through the examples and exercises presented here. There are few painters

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

2

Page 7: ProgrammingfMaDA_MEAP_ch1

who ever suffered because they knew more about the chemistry and physics of their paints, canvasses, brushes, and solvents. Even self-taught artists developed and used natural knowledge of the processes underlying their art making. Knowing programming is similar for the digital artist. Artists who know one or more computer languages, even those who don’t actually write computer code daily, still have a better sense of what’s going on when they drag down a menu, select an item, and watch the progress bar move for a while.

We’re certain that by the end of this book, you’ll be able to do lots of what you want, and likely much more than you might have thought possible. The power available in ChucK will suggest new things to do, and you’ll know how to do them, or how to figure out how. And, even if you use commercial software a lot, we’re going to show you new ways to control and interact with it using ChucK. It’s like being able to put a new engine in your existing car.

0.2 What is ChucK? ChucK is a programming language designed specifically for real-time sound synthesis and music creation. “Real-time” means that ChucK synthesizes the sound as you are hearing it (rather than just playing back a sound file, although ChucK can also do that), and often in response to signals and gestures from the “outside world,” such as you typing on the keyboard, moving the computer mouse, manipulating a joystick or other game controller, playing the keys on a musical keyboard connected to your computer, or a host of other things that are available to hook up to our computers. Not just for sound and music, ChucK is also good for controlling and/or interacting with almost any type of real-time computer media and art, such as graphics, robots, or whatever can communicate with your computer.

ChucK was designed specifically to allow and encourage “on-the-fly” programming, which means you can add, remove, modify and edit, and layer segments of code at any and all times, hearing the results instantly, without interrupting other sounds being synthesized and heard. This is one of the primary differences of ChucK from all other languages, which makes it extremely fun to learn and use, because you can try things out and immediately hear the results. Most other languages require you to compile, run, and debug code in a fashion that doesn’t let you hear what you’re doing immediately. In fact, almost all computer languages (such as C, C++, Java, etc.) were not designed specifically from the ground-up for sound, music, and other real-time tasks. But ChucK makes immediate, real-time sound a priority.

Another great feature of ChucK is that it is “open-source” (not secret or protected by licenses, passwords, keys, etc.), and it is freely available on all major computer platforms, including Mac OSX, Windows, and Linux. Open-source means that the community of ChucK users can have direct input into the process of making ChucK better in the future. It also means that ChucK doesn’t cost anything to get and use.

0.3 Why Program in ChucK? (more about ChucK) A tool, which is one way to look at a programming language, cannot help but shape its user’s mindset, and naturally suggests ways of achieving various tasks. And like any tool, a

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

3

Page 8: ProgrammingfMaDA_MEAP_ch1

programming language should change the way you think and go about doing things – and ChucK definitely presents a different way to program sound and music. In keeping with the tool analogy, there are tradeoffs that make certain things straightforward and other things more difficult. The sincere hope is that for the programmer, the language design choices help more than they hinder!

ChucK was initially created by Ge Wang when he was Perry’s graduate student at Princeton University (and now an Assistant Professor at Stanford University, and co-author of this book) to provide a different way to think about programming sound and in a rapid prototyping (trying out lots of ideas quickly) mindset. More than a decade later, ChucK research and development has only intensified. Ge’s detailed chronicle of the history, motivation, and people behind ChucK can be found in this book’s Foreword. As for more detail on why one might learn and use ChucK, below are a few more reasons:

• It’s all about time. Time is at the core of how ChucK works, and how one works with ChucK to make sound. As a programmer, you specify how to move through time and assert control at specific points in time – and sound essentially just happens (and, conveniently, for precisely the amount of time you have moved through). Why such emphasis on time? Sound is a time-based phenomenon; without the passage of time, there would be no sound. By controlling how and when we do things through time, it’s a different and powerful way to work with sound at every level – every molecule of it.

• It’s text, plain and simple. While programming with text may initially seem more abstract/complex than say, with graphical representations – it is arguably much easier once you start adding a lot of expressive nuance and logic into your code (which you’ll invariably need to do). There is little hidden or inferred – the important parts are in plain sight (for example, how time flows in a program). At the same time, much of the mundane aspects are taken care of under the hood (scheduling, real-time sound input/output, book keeping for all the sound generators, etc.). Readability is a central design goal of the language, and that makes it a good learning tool as well.

• It’s fun and immediate. ChucK was designed to be a fun language and environment to work in, to experiment in. You can synthesize sounds, make fantastical automations, map physical gestures (e.g., with controllers) to sound, network different computers together, and even analysis to computational make sense of sound.

• It’s free. As in beer – but also as in freedom, to experiment, explore, to tinker. ChucK has been in development and non-stop use for more than 10 years, and it continues to evolve. It’s open source, which really means it belongs to everyone who care to use it, modify it, fix it, improve it, or extend it.

• It’s familial. ChucK’s development team and community consist of generally happy, helpful, down-to-earth people who love music and art, and making music and art with other people and computers. This is very little feeling of “elitism” or snobbery, partly due to the nature/personality of the language, and also the culture that it creates. As

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

4

Page 9: ProgrammingfMaDA_MEAP_ch1

mentioned in the Foreword, ChucK is designed to be your friend (it’s named ChucK, after all). ChucK is a bit wacky, whimsical, and has a “personality” that influences how one works with the language.

• It’s a Gateway Language. Once you learn how to write ChucK code, it will be easier for you to learn JAVA, C, C++, and any other language you desire to learn.

As devoted users of ChucK, we each have own reasons for using it. Perry volunteers, “I need to make music, program, and solder fairly often, or I start to go

mad. ChucK is the only language that lets me drop in for a few seconds, or as long as I like, and get something gratifying done. If I have an idea, I can try it out immediately in ChucK.”

While Spencer says, “ChucK allows me to clearly and concisely express complex and nuanced aesthetic ideas in code. More so than any other tool, ChucK gives me both the ability to control computational processes and the means to synthesize them satisfyingly.”

And Ajay, “As a robotic artist, I work with MIDI from my custom built controllers and for my custom built robotic instruments. The power of ChucK has been the multithreading and how I can have multiple processes running sensor data, actuator control and composition processes all at different rates! This is why I use the language in teaching computer science to artists... showing them a language paradigm of the future...”

We asked a few other people why they use ChucK, and here is what they had to say:

“[ChucK] suggests a mindset in approaching music, and the design of music, the experimentation with music, and the learning of coding. […] I like the way that ChucK makes me think, and I was really depressed about music programming before I found ChucK.”

– Rebecca Fiebrink, Assistant Professor, Princeton University

“[…] however, when everything worked the way it was supposed to, when my spontaneous arrangement of computer lingo transformed into a musical composition, it was a truly amazing experience. The ability to control duration and pitch with loops, integers, and frequency notation sent me on a serious power trip.” (On learning to program with ChucK)

– Anna Wittstruck, Princeton University, class of 2008

0.4 Some Examples of our own Computer Mediated Art The goal of this section is to present some examples of computer-mediated art works that we’ve created using computer programming. As artists with programming skills, we used those skills to create new musical instruments, controllers, ensembles, and systems. One time-honored way of making a new instrument is to look at existing instruments, and look at ways to augment and/or improve. Many of our experiences lie in this area. From the Cook-Morrill MIDI Trumpet in 1988 (this didn’t use ChucK, because it didn’t exist yet, but it used the predecessor of ChucK, STK, which is now included in ChucK), and Kapur’s e-Sitar of 2004, through the Laptop Orchestras of 2005 to present, to mobile musical instruments, and beyond, we, along with our crazy friends, have created a variety of computer-mediated

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

5

Page 10: ProgrammingfMaDA_MEAP_ch1

systems based on, and inspired by traditional musical instruments and groups. In some cases we put sensors on the instrument, leaving the inherent sound-producing capabilities in place. In other cases, we gutted the acoustical parts of the instrument and filled it with processors, sensors, and speakers, leaving only the shell and form to suggest the interaction (experience). These include the DigitalDoo, SqueezeVox (accordions, Perry Cook and Colby Leider), the ETabla and EDholak (Ajay Kapur) , BOSSA (The Bowed Sensor, Speaker Array, Dan Trueman and Perry Cook), the SBass (Sensor-Speaker Bass, Curtis Bahn), and others.

The proliferation of powerful mobile devices such as the iPhone and iPad gave rise to a re-envisioning of traditional musical instruments, such as the Ocarina for iPhone (Ge Wang / Smule), as well as entirely new mobile musical interactions. Figure 0.1 shows a variety of these computer-augmented instruments. In all of these cases, we needed to write custom code to have a microchip or a cell phone transform gesture into musical experience.

Figure 0.1 top row: Cook-Morrill Trumpet, DigitalDoo (Cook), SqueezeVox Maggie (Cook), SBass (Bahn). bottom row: BOSSA (Trueman), ESitar (Kapur), Smule’s Ocarina (Wang)

Another way to make entirely new musical performance systems is to put sensors on dancers or other artists who are not traditionally musical performers. Figure 0.2 shows PikaPika, an Anime-inspired dancer character created by Tomie Hahn. PikaPika is fully loaded with sensors, wireless transceivers, amplifiers and speakers. Pika’s SSPeaPer (Sensor-Speaker Person) dancer hardware was created and programmed by Curtis Bahn. Figure 0.2 also shows Raakhi Kapur, an Indian dancer, wearing sensors on her wrists that control musical robots in a KarmetiK Machine Orchestra production featuring musical robots and human musicians (more on that soon). A lot of computer programming was needed to successfully map these dancers’ gesture to sonic responses.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

6

Page 11: ProgrammingfMaDA_MEAP_ch1

Figure 0.2 Cyber-Anime PikaPika (dancer Tomie Hahn). Raakhi Kapur controls musical robots.

Another means to make amusing art and music works is to put sensors on other objects that are not traditionally associated with musical performance. Figure 0.3 shows some musical kitchenware, Perry Cook’s jazz “Pico Mug”, the “Fillup Glass”, atop the musical table of P-Ray’s Café. Also shown is an early prototype of a computer-mediated musical tap-shoe. Turning these everyday objects into musical instruments obviously needs programming, and once again, mapping, to make them sonically interesting. These devices all put out signals that the computer must use to make sound or control other objects/processes, and ChucK is the easiest place to do that type of mapping. In one of our favorite ChucK programs, Perry turned the Pico Mug (computerized coffee mug shown in Figure 0.3) into a digital trumpet.

Figure 0.3 P-Ray’s Café musical table with “Pico” coffee mug, and “Fillup Glass,” (right) musical tapshoe.

The possibilities of entire ensembles of laptops themselves, and/or other large-scale computer-mediated musical groups, brought about the Princeton Laptop Orchestra (PLOrk), followed soon by the Stanford Laptop Orchestra (SLOrk), and the human+robot populated KarmetiK Machine Orchestra. All of these ensembles rely heavily on augmenting human

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

7

Page 12: ProgrammingfMaDA_MEAP_ch1

performers with sensors, networking, and ChucK. Further, each orchestra “instrument” features player-local sound via hemispherical speakers, meaning that there is no routing of all laptops to a mixer and “house sound”, but rather each laptop connects to an individual multi-channel (6 speakers arranged evenly on the face of a hemisphere) speaker located next to each player. Thus the sound of the ensemble is the acoustic mixture, in the room or concert hall, of the individual sounds from each player, just like it would be with a traditional orchestra. Figure 0.4 shows the original (freshman class of) PLOrk, the first mobile phone orchestra at Stanford, SLOrk, and the KarmetiK Machine Orchestra.

Figure 0.4 top row: Princeton Laptop Orchestra (PLOrk) and KarmetiK Machine Orchestra. Mobile music performance. Bottom row: Stanford Mobile Phone Orchestra (MoPhO), Stanford Laptop Orchestra (SLOrk)

These examples only scratch the surface of possible artistic things when we know how to “hack.” Here we use “hack” in the best sense of the word, combining the inventor’s dream, with the ability to modify, build, experiment, and, possibly most important, program.

0.5 Summary In this chapter, we hope we’ve convinced you that learning to program is important for digital artists, and that ChucK is a really good place to learn programming. We spent quite a bit of time talking about what ChucK is, a little on how it was designed, and why we and many others think it is the best programming language for doing digital art. If you already know a programming language, we think you’ll find that ChucK allows you to do many things that you might find hard or impossible in other languages, and do them very quickly. We also showed you some of the types of projects we’ve created using computers and programming. Hopefully you’re convinced, inspired, and ready to begin. So let’s learn to program in ChucK!

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

8

Page 13: ProgrammingfMaDA_MEAP_ch1

0.6 Road Map of the Book (move to About the Book) This book is written to teach a novice programmer how to code. We are going to start at the very beginning, so don’t be afraid. We are going to teach programming through musical examples. We believe that being able to “hear” what your code is doing, will aid you in learning key computer science concepts, as well as make this process enjoyable. If you are an experienced programmer, you may be able to skip a couple of the beginning chapters depending on your level. We promise that there is something to learn for everyone.

After each chapter there is a suggested composition exercise to engage what you have learned. Our goal is to teach you key computer science concepts, and then have you write a 30 second composition to make sure you understand them. In each composition exercise, we really want you to focus on the concepts learned in the chapter, and often what to limit you to only those concepts. In a way, you can view each chapter as a way we are “unlocking” parts of the language you can use in your composition, which we believe will isolate specific skills we want you focus on, to build your knowledge base.

The first two chapters are really for the beginning programmer, introducing key concepts needed to be able to begin coding. Chapter 1 begins with the basics of computer science including variables, types, conditional statements and looping structures. Chapter 2 introduces libraries built in ChucK, and shows how random numbers and different mathematical equations can be used to make more expressive compositions. We also introduce arrays in the and show how to make melody in ChucK.

In Chapter 3 we introduce how to use sound files in ChucK and how you can use them to create soundscapes and even a techno song. Chapter 4 introduces the concept of functions and how they can be used in modularizing and organizing your code, which again can result in more expressive composition.

Chapter 5 introduces readers how to make audio effects, use built in synthesis techniques and other concepts that will allow your to strongly enhance the sounds your use to make your compositions.

Chapter 6 introduces multi-treading, and how you can make your program “juggle” multiple things at the same time. Chapter 7 introduces object oriented programming and how you can create your own “objects” to use in your code.

Chapter 8 starts looking at how to use ChucK as a live performance tool, by introducing how you can use your keyboard and mouse for real-time control into your compositions. Chapter 9 continues this showing how you can use MIDI devices to talk to ChucK. Chapter 10 looks at a modern way of communication, introducing Open Sound Control (OSC) to the reader.

This book is intended for all artists who want to learn to program. We believe that the way we have laid this out will introduce key concepts of computer science in a fun way. Even if you are not a musician, we believe you will respond favorably to the sonification of these concepts. We also believe that ChucK is truly a “gateway language”, and once you learn how

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

9

Page 14: ProgrammingfMaDA_MEAP_ch1

to program ChucK, it will really be easy for you to jump into any other language you desire to learn.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

10

Page 15: ProgrammingfMaDA_MEAP_ch1

1 Basics: Sound, Waves, and ChucK

Programming

This chapter covers

• Intro to acoustics, sound, and waveforms

• Sine waves and other oscillators

• Variables

• Control structures and logic

So far we have talked about why we think that ChucK is the best and most powerful language to make all kinds of art and artistic systems. Now it’s time to actually begin learning how to program sound and music in ChucK. In this Chapter, we will first talk about sound in general, looking at a graphed sound waveform or two. We’ll talk about properties of sounds in terms of loudness, pitch, and noise. We’ll learn that things that oscillate are fundamental to physics, sound, and music, and we’ll make music with ChucK’s built-in oscillators. We’ll learn about how data is managed and manipulated in ChucK by the use of “variables.” We will learn about the timing mechanisms built into ChucK, which are one of the things that make it different from all other programming languages, and which make it so great for programming music, sound, and time-based art. We’ll also learn about controlling the flow of our programs by using logical variables and tests, and looping. By the end of the chapter, you will have written your first composition in ChucK!

1.1 Soundwaves and waveforms We will start out by talking a bit about the physics and nature of sound. Sound consists of high and low pressure fluctuations (waves) of air caused by one or more vibrating objects. Sound waves then propagate through the air, maybe bouncing off of walls and other

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

11

Page 16: ProgrammingfMaDA_MEAP_ch1

surfaces, finally reaching our ears or a microphone. People who work with sound often graph waveforms (air pressure, or voltage coming from a microphone, as a function of time).Figure 1.1 shows a plot of wave values as a function of time of a man saying the word “see.”

#1 #2

Figure 1.1 Waveform of the spoken word “see.”

Some things are obvious from this plot. For example, the noisy consonant “sss” #1 in the first half of the sound, changes quickly to a different structure for the “eee” vowel in the second half #2. If we zoom into the transition area a little, some more things become obvious, as shown in Figure 1.2.

#3 #4

Figure 1.2 Waveform zoomed into the transition between “sss” and “eee.”

The “sss” #3 still looks about the same, still noisy with lots of wiggles in the waveform, but the “eee” sound quickly enters into what is called “periodic” oscillation, with a basic shape repeating over and over #4. There might be small deviations due to noise, quavers in the voice itself, or other factors, but the oscillation generally repeats over and again (periodic). This is characteristic of sounds that have “pitch.”Pitch is our perception of frequency from low to high. It’s what we sing or whistle of a song melody, and it’s what musicians notate and talk about in terms of note names and numbers. The keys of the piano keyboard are oriented from low (left) to high (right) pitches.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

12

Page 17: ProgrammingfMaDA_MEAP_ch1

Most anything that gives us a sense of pitch exhibits a periodic oscillation, and anything that oscillates periodically will sound pitchy(within certain frequency ranges). The period marked as T in Figure 1.2 is the period of oscillation (usually given in seconds), and 1/T is the frequency of the oscillation in cycles per second. So in the case of our “eee” sound, if the period T is measured as 6.6 milliseconds (a millisecond is one 1000th of a second), then the frequency of oscillation will be 1/0.0066 = 150 cycles per second, or 150 Hz (the unit of frequency named after physicist Heinrich Hertz, pronounced like “hurts.”).

If we repeated that single period of “eee” over and over again, looping at a convenient point like at the main peaks, we could synthesize an “eee” sound of any length we like. If we played that loop slightly faster, the pitch would go up. Slower, lower in pitch. That synthetic sound would sound pretty dull and boring, because by selecting one period and looping that exactly over and over, we would have removed all the noise, slight deviations of pitch, and other things that made the original sound natural. This is one reason that synthetic speech sounds so, uh, synthetic.

Much of electronic music history, and of music history, and actually much of physics, centers on the notion of oscillators. We will use ChucK’s many flavors of built-in oscillators in coming sections, but for now we’ll look at one of the most fundamental oscillators in nature, and in sound; the sine wave. We will use ChucK’s sine wave oscillator very soon to write our very first program that plays a musical note.

So what is a sine wave? A sine wave rises and falls periodically and smoothly, as shown (a few periods worth) in Figure 1.3.

Figure 1.3 A sine wave (right) can be generated by rotating the circle (left) counter-clockwise, and tracing the height of the dot as it changes in time.

The circle off to the left side shows one way that sine waves can be pictured, explained, and generated. If we rotate the circle counter-clockwise at a constant rate, say N rotations per second, then the height of the little dot that begins on the right side of the circle traces out the sine curve shown to the right, as time progresses to the right. So if N were five cycles per second, then the circle and sine wave would complete five cycles in one second.

You might have heard of sine functions if you have studied trigonometry, but as we noted before, sine waves can be found all over in nature and mathematics. Things that rotate can be described in terms of sine waves. The electricity coming out of our AC wall sockets alternates in a sine wave pattern, because the generators in electrical power plants rotate in circles. But not just rotating things generate sine waves. A pendulum sweeps out a sine

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

13

Page 18: ProgrammingfMaDA_MEAP_ch1

wave pattern in displacement as it oscillates back and forth. A simple mechanical system of a mass and spring oscillates in a sine wave pattern. An electrical circuit containing an inductor and a capacitor oscillates in a sine wave pattern. Acoustically, the single resonance of a pop-bottle oscillates in a sine wave pattern. So, as you can see, sine waves are really deep and pervasive dudes.

There are other simple waveforms that are common in nature and in electronic music. These include triangle, saw-tooth, and square waves. Figure 1.4 shows two cycles each of sine, triangle, saw-tooth, and square waves.

Figure 1.4 Two cycles each of sine wave, triangle wave, saw-tooth wave, and square wave.

Sine waves are at the heart of the analysis of sound, as well as synthesis. More complex periodic waves can be built of sine waves. In fact, we can make any sound possible by adding together sine waves of different frequencies, phases (delays), and amplitudes (loudness). Sine waves are the fundamental building blocks of what is called the “spectral” view of sound, where instead of looking at the individual wiggles of the waveform, we look at the sine components that make up that waveform. For example, Figure 1.5 shows that a saw tooth wave can be built by adding sine waves at integer multiples (1, 2, 3, etc.) of some fundamental frequency. Such components are called “harmonic.”Triangle and square waves can be made up of odd sineharmonic (1, 3, 5, etc.) frequencies at differing amplitudes.

Figure 1.5 Sine waves at integer multiple frequencies (1, 2, 3) can be used to build a sawtooth wave..

The sawtooth also has a nice physical analogy in the musical instrument world, because when a bowed string oscillates, the bow alternately sticks to the string and drags it along, then slips, allowing the string to snap back. This creates a sawtooth motion, as observed long ago by Heinrich Helmholtz (1821-1894), who also gave us some our earliest acoustic

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

14

Page 19: ProgrammingfMaDA_MEAP_ch1

theory and experiments with the spectral descriptions of instruments and sounds. So, a sawtooth wave can be a good start on a violin synthesis, and we’ll do that very shortly.

Physically, a clarinet playing its lowest note oscillates very much like a square wave, so we could build a very simple clarinet-sounding synthesis by using a square wave and an envelope generator (more on those soon). But first, let’s program!

1.2 “Hello World” and “Hello Sine!”our first ChucK programs It’s time to get started learning how to program. At this point, you should have read the instructions in Appendix A, on how to install miniAudicle and the basic functionality of the programming environment. Once installed, open up miniAudicle and we will guide you through your first program. Figure 1.6 shows the miniAudicle ready to run some ChucK.

Top window: #1 Main Window where you type and run your code. Lower right window: #B Virtual Machine Monitor Window. Start this once before you begin running ChucK programs. Lower left window: #C Console Monitor Window. ChucK prints messages to you here, and your programs can also print to this window, causing messages to appear.

Figure 1.6 Main windows of the miniAudicle ChucK Integrated Development Environment (IDE).

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

15

Page 20: ProgrammingfMaDA_MEAP_ch1

There are three main windows in the miniAudicle. The main window that you see (top of Figure 1.6) is initially called “Untitled,” and is the window in which you write your programs. You can change the name of this window, which we will do shortly when we write and save our first program. The lower right window in Figure 1.6 is the Virtual Machine Monitor, which acts as the server while running your programs. When you click “Start Virtual Machine”, it creates a new window called the “Console Monitor” (lower left of Figure 1.6). This Console Monitor is where you will receive messages from the computer to help you with your program. For example, if there are errors in your code, this is where the computer will tell you there are problems and specifically what lines they occur on. You can also print messages to yourself (from your running code) in this Console monitor to help you debug (find and fix errors in) your program, or to just keep you informed of what is going on.

In almost all languages, the first program you learn to write is “Hello World”, so we will begin by showing you how to do this in ChucK. Go to the “Untitled” window, Save it as “

.ck” using the File Menu and then type in:

Next make sure that your Virtual Machine is running (you will see running time: value increasing constantly) and then click “Add Shred” on the Main Window #1, which is now labeled “helloworld.ck.” You will instantly see that “Hello World” appears in the Console Monitor. Congratulations, you successfully executed your first ChucK program! The “Add Shred” button told ChucK to run the “Hello World” code that you typed into window #1. You will click this button each time you want to run a ChucK program.

Even though you will always be typing and running code from Window #1 as shown in Figure 1.6, from now on in the book we will just show code as text, with labels and comments. We won’t show you the whole main window with control buttons, but we will show parts of Figure 1.6 in the future to point out some of those controls. Also, all of the windows can be moved around on your desktop as you like, as with many of the programs you are used to working with on your computer.

As discussed above, it is useful as a programmer to print to your console monitor for debugging purposes, and as a composer to tell you what section of your song you’re currently running. From this first example you now know that putting items in between <<<>>>, allows you to print. Putting anything in “quotes” between the triple <<<>>> print characters prints that (in this case quotes) out directly. We will give you more advanced techniques on printing other things as we continue through this chapter.

Because ChucK is a language focused on sound, and our goal is to teach you to program through writing compositions, we need to go a lot further in our programs than printing text to our console monitor. We need to learn how to make sound. Thus our second program we will name “Hello Sine!” Since a sine wave is periodic, it produces a sound that we hear as a

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

16

Page 21: ProgrammingfMaDA_MEAP_ch1

pitch. So let’s think of this first sine wave sound that we make as the first note of our very first song, “Twinkle, twinkle, little star.”

Listing 1.1 Hello Sine!

#1 Connect a sine oscillator (named s) to the Digital to Analog Converter #2 Set the gain of the sine wave to 0.6 #3 Set the frequency of the sine wave to 220.0 Hz. #4 Advance time by 1.0 second, letting sound happen

Let’s dissect this program line by line. In Line #1, we make our first sound signal chain

(or sound network, or also called a “patch,” which we’ll talk about shortly). SinOsc is called a “unit generator,” an object that will generate the sound of a sine wave. The name we give to our oscillator is “s” which is called a “variable name” in programming, because its value can change. It’s also a variable in that we could have chosen any name such as “t”, “mywave”, etc. You as the programmer get to pick how you want to name the things you make. But for simplicity, we have chosen “s”.

The next important thing is the=>” symbol. Notice that it looks like an arrow. This is the “ChucK operator” and the name for the entire language. It is designed to look like signal flow from one side of the arrow to the next. When we use it, we say we are “ChucKing” whatever is on the left side of the arrow to whatever is on the right side. So right now, we have a SinOsc that we have named “s” and we want to send that sound out to our speakers or headphones. For that we use a special object called dac (stands for “digital to analog converter”) which is connected inside your computer to the output of your sound card (speakers, headphones, etc.).

So, by “ChucKing” a SinOsc named s to the dac, we have set up a sound network with a sine wave that we will be able to hear from our speakers. Another name for connecting our sine oscillator Unit Generator to the dac is “making a patch,” which comes from the old days of analog synthesizers where sound making and processing components were connected together using “patch chords,” or literal pieces of wire that made the connections.

Now that we’ve created a sound making signal chain, there are 3 main aspects of this program in creating a sound: how loud, which frequency (or pitch), and how long. In Line #2, we work with how loud. All sound generating and processing objects in ChucK have a .gain, that we can use to control the volume level of sound. Gain can be set to any number, but it usually ranges between 0.0 and 1.0. Note in this code we set the volume to .6 by “ChucKing it” to s.gain.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

17

Page 22: ProgrammingfMaDA_MEAP_ch1

In #3, we work with frequency. This is how you will be able to compose and determine which pitch (specific frequency) you want your SinOsc to make. The .freq of our SinOsc accepts a number in Hertz from 0 – 20,000 (which is the range of frequencies of human hearing). Note in this example we set the frequency by simply ChucKing 220 to s.freq. This is the pitch of the first note of our “Twinkle” song. We’ll talk more in the next chapter about how to determine frequencies corresponding to musical pitches.

In Line #4, we determine how long our synthesis will run, or the duration of our first note. For now we want our sound to play for 1 second, which we accomplish by ChucKing a duration of 1 second to an object called now. The double colon tells ChucK that you want to talk about units of time, so 1::second tells ChucK that you want to wait 1 second while sound is synthesized. If we didn’t have this line, we would hear no sound, because the program would end right there. One of the most powerful aspects of ChucK, is that as programmer you have absolute control of time. But you are also obligated to control time to make sounds. We will discuss time and the now object in more detail in Section 1.4.

The next step in building our song is creating a score of pitches and durations that we would like our SinOsc to make. Let’s say we continue our program to play 4 notes as follows:

Listing 1.2 Sine Wave Music

/* Sine Music by ChucK Programmer January 2025 */ #1 SinOsc s=>dac; // Play one note #2 220 => s.freq; 1.0 => s.gain; 0.3 :: second=>now; 0.0 => s.gain; #3 0.3 :: second=>now; // Play another note, same pitch #4 1.0 => s.gain; 0.3 :: second=>now; 0.0 => s.gain; 0.3 :: second=>now; // Play two more notes, higher, less loud #5 330 => s.freq; 0.3 => s.gain; 0.3 :: second=>now; 0.0 => s.gain; 0.3 :: second=>now;

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

18

Page 23: ProgrammingfMaDA_MEAP_ch1

0.3 => s.gain; 0.3 :: second=>now; 0.0 => s.gain; 0.3 :: second=>now;

Line #1 A comment block, ChucK ignores this. Block #2 Set the gain to 1.0 and frequency to 220 Hz. Let this run for 0.3 second by ChucKing it to now (advance time). Block #3 Make our sine oscillator silent for 0.3 seconds. This is to separate it from the next note. Block #4 Repeat the process of Blocks #2 and #3

#5 Repeat the same two-note pattern of Blocks #2, #3, and #4, but with a different frequency (pitch) and gain (loudness).

Note that we are creating the first individual note by setting the gain of the sine oscillator to 1.0 (block #2), then zero (block #3), letting our sound network run for a little while each time by chucking a duration of .3 seconds to now. We repeat the same note in block #4. We have to make that silence between the notes (by setting gain to 0.0), or else those first two repeated notes would just sound like one long note. In block 5, we repeat the whole two-note sequence, but with a higher pitch (the second “twinkle” in our song), and at a quieter volume (the gain of an oscillator correlates to volume, larger gain, higher volume). You can probably imagine creating an entire composition, stringing commands like this together. However, by the end of this chapter, we will show you many techniques to create scores of commands like this in a much more concise and powerful manner.

Meanwhile, as you are now ready to start composing using these simple techniques, we want to enable you to use more than just the SinOsc object to make sound. As discussed in Section 1.1, it is possible to make sound using many different types of waveforms. Take a minute and try out the 3 waveforms shown in Figure 1.7 in the program above by changing the SinOsc in Line #1 to one of the following: a Square Wave, (SqrOsc), a Triangle Wave (TriOsc), and a Sawtooth Wave (SawOsc).

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

19

Page 24: ProgrammingfMaDA_MEAP_ch1

Figure 1.7 ChucK has built in oscillator types for Square, Triangle, and Sawtooth.

TESTING AND DEBUGGING (FINDING ERRORS IN YOUR CODE) Each time you make a change in your program, you will hit the “Add Shred” button to tell ChucK to run that new code. If there is an error in your typing, ChucK will print it into the Console Window, usually with the line number where it found the error. That line number will also change to red in the miniAudicle main window where you typed your code. Go back and fix the error, and re run by pressing “Add Shred” until things work. If things still don’t work or make sense, re-type the code from scratch. That’s how all programmers do it!!

Also, at any time you can use the “Save As” menu item to save a new ChucK code file under a new filename. This will be important later as we write more and more programs.

Some of the most important elements in programming are the comments the author makes while creating their code. As seen in the first few lines of Listing 1.2 (reprinted here), comments are made in 2 ways: #1 a set of slashes and asterisk: “/* “ and “*/”, or #2 Two forward slashes : “//”. In method 2, anything on the line past the // is not registered by ChucK as computer code. Method 1 is for multiple lines where all lines in between the 2 sets of characters are ignored by the compiler.

/* Sine Music by ChucK Programmer January 2025 */ #1 SinOsc s=>dac; // Play one note #2 220 => s.freq;

Good Coding Practices: Commenting You should get into the habit of commenting every line (or most lines, or at least every block) of code to make notes to yourself and future collaborators. Also, you should always put your name and date at the top of each program you write.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

20

Page 25: ProgrammingfMaDA_MEAP_ch1

The example of Listing 1.3 brings together all the concepts discussed in Section 1.2. The code is well documented (commented), and we print something to the Console output #1 at the beginning, then play two notes of music.

Listing 1.3 Using Comments to Document Code, and Control Execution

#A Initial comment, documents who wrote the program and when #B Sound signal chain #1 Print a greeting message #C Set up parameters to play a note #D Play another note #2 Don’t play this last note right now, because we’ve “commented it out”

In the last block #2, we show how you can use the /* FORM */ of comments to cause some code to not be executed, since ChucK assumes that this text is a comment and not code. This can be super helpful for “debugging” your code, or skipping across some of your composition so you can work with later parts.

We now know how to make sound (notes) with oscillators, how to change the pitch with .freq and the volume with .gain, and how to control the durations of our notes using now. But there are other things we need to know to make better music.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

21

Page 26: ProgrammingfMaDA_MEAP_ch1

1.3 Data Types and Variables The programs we’ve seen so far make sound, but it would be really hard to think about making a whole song using this type of literal coding of each gain, frequency, and time. The next concept to learn is how to make your program easy to change, given decisions you might make as a composer, or even decisions the program itself might make.

One key to doing this is to use variables. In ChucK and many other programming languages, each variable has to have a particular “data type,” meaning that it can only hold one specific type of value or collection of values. Just like our examples in 1.2, where we named our SinOsc the variable named “s”, we can set up variables to hold a number, a time, a letter, or even words. But each one can only hold one type of data.

A very important ChucK data type holds integer (int) numbers. An integer is a special type of number that does not have a fractional part, thus it needs no decimal point. In ChucK, an integer variable can store any number in the range of −2,147,483,648 to +2,147,483,647. You can declare and initialize(give a value to) an integer variable as shown in Listing 1.4. Declaring a new variable creates space in the computer to store that variable, and registers the newly declared name and type for that variable, so we can refer to it by name later. Initializing a variable gives it a value. In Listing 1.4, right after we declare the new myPitch variable, we store the value of 220 into it (by ChucKing).

Good Coding Practices: Variable Names Since you are the programmer, you can pick any name you like for your variables, but it is almost always better to pick names that are meaningful. Names like myPitch, myVolume, and fastTempo are much more meaningful than x, j, and z. People reading your code can see those names and have an idea of their purpose. Later on we will see that some variables are temporary, used quickly in code just after they are created, and never used again. For these types of variables, i, temp, x, etc. are fine. But for most variables, a good name (not too long, but definitely not too short) is really important.

Listing 1.4 Definiting and using an integer variable

// integer variable // declare an integer int myPitch; // store a value in it 220 => myPitch; // print it out <<< myPitch >>>;

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

22

Page 27: ProgrammingfMaDA_MEAP_ch1

We can also initialize integers (give them a value) at the time they are declared, as in this case where we simultaneously declare an int named myPitch, and set its value to 220:

Listing 1.5 Initializing an Integer as it is declared

// another way to initialize integers // store 220 in newly declared myPitch, all at once 220 => int myPitch;

Many times we want to change the value of a variable (that’s one reason they’re called variables). And there are times when we want to use the value of a variable to initialize a new variable, or set the value of one variable as related to another. Also, deriving new variables from others can make your programs more readable, and easier to modify. Since integers are numbers, we can do all sorts of arithmetic with them, as shown in Listing 1.6. This will become more important later on as we play more and more notes.

Listing 1.6 Doing math with integers

// arithmetic with integers 220 => int myPitch; 1 => int gain; // add or subtract myPitch + myPitch - 110 => int anotherPitch; // multiply 2 * myPitch => int higherPitch; // divide d by b myPitch/ 2 => int lowerPitch; // print them all out <<<myPitch, anotherPitch, higherPitch, lowerPitch>>>;

which prints out in the console window:

220 330 440 110

[[[ EXERCISE: Walk through the variable math operations of Listing 1.6 using pencil and paper. See if you come up with the same numbers. Type this code into ChucK to check your answers. Which was easier? You can use ChucK as a sort-of calculator!! ]]]

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

23

Page 28: ProgrammingfMaDA_MEAP_ch1

Listing 1.7 shows some shorthand methods to do arithmetic on variables to change their values “in-place.” By adding a math operator to the beginning of the ChucK => operator, that math is performed on the variable, and stored back into the variable.

Listing 1.7 Shorthand math, multiply, subtract, etc. and ChucK in one step!

// longhand math with integers // shorthand math with integers int myPitch; 220 => int myPitch; 220 => myPitch; // multiply by 2 // multiply by 2, in-place 2 * myPitch=>myPitch; 2 * => myPitch; // “times ChucK” // subtraction // in-place subtraction myPitch – 110 => myPitch; 110 -=> myPitch; // “minus ChucK” <<< myPitch>>>; <<< myPitch>>>;

both of which print out in the console window:

330 :(int)

Let’s use our newfound knowledge about integer variables to play a couple of notes as shown in Listing 1.8. Here we store our pitch in an integer variable, and we create two integers called “onGain” and “offGain” to store the values of 1 and 0, so we can turn the sound of our oscillator on and off. This allows us to separate notes, and play the same note twice (like “Twinkle”).

Listing 1.8 Our Twinkle Song with Integer Variables

/* Sine Music with integer variables by ChucK Programmer January 2025 */ SinOsc s => dac; 220 => int myPitch; #1 1 => int onGain; #2 0 => int offGain; // Play one note #3 myPitch => s.freq; onGain => s.gain; 0.3 :: second=>now; offGain => s.gain; #4 0.3 :: second=>now;

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

24

Page 29: ProgrammingfMaDA_MEAP_ch1

2 *=> myPitch; #5

// Play another note, with a higher pitch myPitch => s.freq; #6 onGain => s.gain; 0.3 :: second=>now; offGain => s.gain; #7 0.3 :: second=>now;

#1 Declare and initialize an integer called variable “myPitch” #2 Declare and initialize two integers for controlling gain #3 Play a note using our new integer variables #4 Turn the sound of our oscillator off, to separate the notes #5 Multiply our pitch by 2, in place #6 Set frequency and turn on oscillator, to start our 2nd note #4 Turn the sound of our oscillator off again, to end the 2nd note

By creating some integer variables in #1 and #2, we can now use those to set our sine wave parameters #3#6 instead of numbers. Since they are variables, we can change their value at any time, so the beauty lies in #5 where we multiply our pitch by 2. Setting the sine wave frequency to this new value makes the pitch higher.

This amount of pitch change (doubling the pitch) is called a “musical interval” of one “octave” up (for somewhat obscure reasons). An octave is a doubling (up) or halving (downward) of frequency. For those of you familiar with keyboard instruments, an octave is the space between any two “C” notes (middle C to the octave above for instance), “A” notes, “G” notes, etc. For you guitar players, if you play an open string, then put your finger on the 12th fret and play that same string, you will hear a difference of an octave (doubling of frequency, and not-coincidentally, you are cutting the length of the string in half).

So we can play some notes using integer variables, but what if we wanted to play a different interval, as is the case in our “Twinkle” song, where we want to play two notes at 220 Hz, then two notes at 330 Hz? You might note that we could multiply 220 by 1.5 to yield 330.0, but ChucK won’t allow us to do that, because we can’t use fractions or decimal points when dealing with integers. And what about the next two notes in “Twinkle, twinkle,” (“Little” in our song)? Turns out that the frequency of those two notes is 370.4125 Hz. But integer variables can only store numbers with no fractional part (no decimal points allowed).

Fortunately, there is another ChucK data type called “float.” Floating point numbers are numbers that have a decimal point (fractional part), and can thus be more precise. However, as you can imagine, these numbers are stored and operated upon differently inside the computer, thus there is a separate data type for these. You can declare and initialize float variables just like you have done with integers. All of the arithmetic and shorthand operators also apply to floats. Listing 1.9 shows how we can declare and initialize a float to hold our first “twinkle” frequency. We can do math on that, multiplying it by 1.5 to store it

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

25

Page 30: ProgrammingfMaDA_MEAP_ch1

in a newly declared variable called “twinkle2.” Similarly, we can derive our pitch for “little” by multiplying “twinkle” by the correct floating point number.

Listing 1.9 Using and Manipulating float Variables

220 => float twinkle; #1 1.5 * twinkle=>float twinkle2; #2 1.6837 * twinkle => float little; #3 2 * twinkle => float octave; #4 <<< twinkle, twinkle2, little, octave >>>;

#1 Declare and initialize a float to hold our “twinkle” pitch #A Use math to derive our 2nd “twinkle” pitch from the first one #B Use more math to derive “little” pitch from our base “twinkle” #C Make a new pitch an octave above “twinkle”

which produces the console output:

220.000000 330.000000 370.414000 440.000000

The beauty of this code is that just by changing the initialization of our first “twinkle,” all of the other pitches change as well. Since these variables are used to set pitches, this is called “transposition” by musicians and composers. So if we changed #1 above to

110 => float twinkle;

then all of the pitches go down by an octave. We could initialize that initial twinkle variable to any reasonable number, and all other would change proportionally. So the twinkle song would still be intact, but just in a different “musical key.” The power of programming!

Note that we’ve already used floats when we set the gain of our SinOsc unit generator in Listing 1.2 (first to 1.0, then to 0.3 to make it quieter). We’ll look at one more example here just to reinforce the use of floats. The ability to make volume and frequency into variables will become important elements in making expressive compositions, as we will soon see.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

26

Page 31: ProgrammingfMaDA_MEAP_ch1

Listing 1.10Twinkle with floats /* Sine Twinkle Music with float variables by ChucK Programmer January 2025 */ SinOscs=>dac; 220.0 => floattwinkle; 1.6837 * twinkle => float little; #A 1 => int onGain; 0 => int offGain; // Play one note twinkle => s.freq; #B onGain => s.gain; 0.3 :: second => now; offGain=>s.gain; #C 0.3 :: second=>now; 1.5 *=> twinkle; #D // Play other note of 2nd "twinkle" Twinkle => s.freq; onGain => s.gain; 0.3 :: second => now; offGain => s.gain; 0.3 :: second=>now; // Play one note of "little" little => s.freq; onGain => s.gain; 0.3 :: second => now; offGain => s.gain; 0.3 :: second=>now;

#A We can make a float variable for our “twinkle” pitch, and use math to compute the value of another variable to use later for “little” #B Turn “twinkle” note on (set gain using onGain variable, and advance time) #C Turn off note (set gain to offGain, advance time) #D Modify “twinkle” pitch using math, so we can do the 2nd, higher “twinkle”

OK, so we’ve used variables (both int and float) to make our code more flexible, and

readable. But can we do something about all of those lines with numbers controlling time? Well, we could make a float variable and initialize it to 0.3, then use it something like:

0.3 => float myDur; myDur :: second => now;

But there is a better way! We will now learn about two more data types that are built into ChucK, specifically to control time(s) and durations.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

27

Page 32: ProgrammingfMaDA_MEAP_ch1

1.4 Time and Sound So far we’ve been making sound by hooking a sine or other oscillator to the dac, setting its frequency and gain, and ChucKing some duration, such as 0.3::second, to a magical keyword called now. Now we are going to dig into the timing mechanisms of ChucK, introducing two new data types (like float and int) to deal with time and durations.

WHAT IS A KEYWORD? Keywords are special reserved words in computer languages and systems. We’ve used lots of keywords already, such as dac, adc, second, ms, etc. You can tell a keyword when you type or see it in the miniAudicle, because it changes color automatically. The one thing you must remember as a programmer is that you cannot choose those keywords for your variable names. ChucK will not allow that, because those words already have been defined (by the authors of ChucK) for special uses.

You have already used ChucKing durations of time to the ChucK “keyword” now to control time in all of our previous examples, but we can improve our “Twinkle” song further by using variables of a new data type called dur (for duration).

Listing 1.11 Twinkle With dur Variables /* Sine Music using dur variables by ChucK Programmer January 2025 */ SinOsc s => dac; 220.0 => float twinkle; 0.55 :: second => dur onDur; #1 0.05 :: second => dur offDur; 1 => int onGain; 0 => int offGain; // Play one note twinkle => s.freq; onGain => s.gain; onDur => now; #2 offGain => s.gain; offDur => now; #3 1.5 *=> twinkle; #4

// Play other note of 2nd "twinkle" twinkle => s.freq; #5 onGain => s.gain; onDur => now; offGain => s.gain; offDur => now;

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

28

Page 33: ProgrammingfMaDA_MEAP_ch1

In #1, we make two variables of type dur, which we initialize to different values. One

controls the amount of time the sine wave is making sound #2, and the other controls the sound off time #3 (this silent time is often called a “rest” by musicians). In #4, we use in-place variable arithmetic to change the frequency of “twinkle” so we can use it for the 2nd pair of “twinkle2” notes (for musicians and physicists, this musical interval of 3/2 is called a “Perfect Fifth.”). Then in #5 we turn the oscillator on and off again, advancing time using our dur variables, to play the next note.

Understanding time and duration (and floats and ints) is essential for the ChucK programmer. By default, ChucK provides the following special duration keywords (Table 1.2). Of course, you likely know second, minute, hour, day, and week, but the values in the table that you might not be familiar with are ms and sample. One 1000th of a second is called a millisecond, abbreviated ms. So 1000 :: ms is equal to 1 :: second.

The sample duration depends on the sample rate that your sound hardware is running at, but it’s always the fundamental unit of time in any digital audio system. A sample is usually 1/44100 of a second (because there are 44100 audio samples used to represent 1 second of a waveform). This is the sample rate most commonly used in CDs, MP3s, etc.

KEYWORD DURATION

samp 1 digital sample in ChucK time (usually 1/44100th of a second)

ms 1 millisecond

second 1 second

minute 1 minute

hour 1 hour

day 1 day

week 1 week

Table 1.2 Special ChucK duration keywords and their corresponding durations.

[[ THOUGHT PROBLEM HERE? Why didn’t the ChucK designers define a “year” or “month” keyword? (answer, month and year don’t have a specific duration. Months can range from 28-31 days, and years can be either 365 or 366 days long (leap years))]]

Now that we’ve used time and dur a little, let’s talk a bit more deeply about why time is so important in ChucK, and why it is so important for you as a programmer to understand how time is dealt with in ChucK. Sound is a time-based medium. Sound, and by extension, music, happens over time. Without the passage of time, there’d be no sound. This

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

29

Page 34: ProgrammingfMaDA_MEAP_ch1

fundamental relationship between time and sound is at the core of the ChucK language, and allows us to govern how time flows in order to make sound “happen” – and to control sound precisely over time.

There are a few things in ChucK that work together to make this possible:

• time and duration are native data types, just like int and float

• the now keyword holds the current ChucK time, which starts when you press the button labeled “Start Virtual Machine” in the miniAudicle

• by manipulating now (and only by manipulating now), you can cause the passage of time in a ChucK program to generate sound.

In ChucK, time (time) and duration (dur) are basic data types that you can work with, much like integers and floating point numbers – they represent values of a certain type, we can declare variables of those types to store values, and also perform arithmetic on them. The time type represents a point in time (referenced from when ChucK is started). The dur type represents duration, which one can also think of as a length of time, or the space between two times.

For example, 2::second in ChucK is a value of type dur. One can add durations together to form new durations (for example, 2::second + 1::minute), and also store durations into newly-declared variables, for example,

0.8::second => dur quarter; // quarter note duration #A

#A The duration of a quarter note in music depends on the tempo, often expressed in how many quarter note durations happen per second, also called BPM (Beats Per Minute). Here we’ve defined a quarter note to be 0.8 seconds, meaning that our tempo is 75 BPM.

Variables can then be re-used to create new variables, for example, 4::quarter => dur whole; #B

#B Once we’ve set our quarter note duration (thus tempo), we can define a whole note (four quarter notes) from that. We could similarly define eighth notes (quarter/2), half notes (2*quarter) , etc. The special keyword now is at the heart of working with time in ChucK, and it is of type

time. The word now has two special functions. Firstly, when read, now holds the current logical ChucK time (essentially, it is ChucK’s master clock). Secondly, even though now is a variable, you cannot change its value directly, at least not in the normal way. When you try to change the value of now – for example, by ChucKing a particular duration to a now– it has the important side effect of letting time pass (and letting sound generate) for precisely that duration. In effect, ChucK is advancing time until now becomes the value you want it to be! Another way to look at it is that your code stops executing for any duration you ChucK to now, but all the synthesis stuff you’ve connected (SinOsc => dac for example) keeps running and making sound. When that particular duration that you ChucKed to now has passed, the next line of code executes.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

30

Page 35: ProgrammingfMaDA_MEAP_ch1

Figure 1.18 shows this for our two-note “Twinkle” example of Listing 1.11, illustrating how time advances between blocks of code being executed.

Figure 1.8 Chuck code executes until a duration is ChucKed to now. At that point, time keeps advancing and sound is synthesized, until the duration has passed. The cycle is continued, with code being executed until now is advanced by some duration, and sound being synthesized for that duration. When all code has run, and time has advanced as far as the last point, the program stops, and sound stops as well.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

31

Page 36: ProgrammingfMaDA_MEAP_ch1

Working with now is simple (and fun), and yet is absolutely essential to programming in ChucK – you must use it to make sound, period. Here are some important things to keep in mind when working with now.

• chucking a duration to now advances ChucK time precisely by that amount – while time is advancing, your code is automatically and temporarily suspended, while sound is generated by the system (how it does this of course depends on how you have set up the sound synthesis).

• now will never move forward unless / until you manipulate it – so until you explicitly advance time, you are actually working within a single instant in time.

• Another way to think about all this is that ChucK code execution waits until now becomes the time you want to reach. For this reason, you should never chuck a negative duration to now (for example, -1::second => now) – ChucK cannot (yet) go backwards in time, so trying to do this will halt your program.

• There are no restrictions on how much time is advanced (as long as it by a non-negative amount!). So it is possible to advance time, say, by a microsecond, a samp, 2 hours, or 8 years with the same mechanism – it’s all up to you; the system will behave accordingly and predictably.

There are other ways to advance time, for example, by chucking an Event to now (such

as clicking the computer mouse, pressing the fire button on a joystick, or playing a note on a musical MIDI keyboard connected to your computer) for when you don’t know ahead of time how much time to advance. But we will leave that for a later discussion. For now, you have just learned one of the most important aspects of ChucK – controlling time using durations.

Now that we know about int, float, time, and dur data types, we should mention that ChucK has a couple of other built in (called primitive) data types, including string (a string of text characters like “hello world”), and void (a type with no type, in case we need a variable but don’t need a type, which is confusing I know, but we’ll need this later on in the book). Table 1.1 summarizes all of ChucK’s built in data types.

Table 1.1 ChucK data types

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

32

Page 37: ProgrammingfMaDA_MEAP_ch1

We’ve learned a lot about variables and data, and using those to control our sound parameters, and advancing through time. But we’re still typing many lines of ChucK code to make each and every note. There must be a better way… And there is. Read on.

1.5 Logic and Control Structures To motivate the ideas of this next section, let’s look at a different, non “Twinkle” example that plays around with modifying pitch and duration (by advancing time) in a new and flexible way. The very few lines of code in Listing 1.12 can make really interesting sounds, forever, by repeatedly moving through time with now. Type this program in and run it. The magic that you are hearing is ChucK playing a constant stream of notes, randomly changing the frequency and duration of each note.

Now play with the numbers (30, 1000). Each time you make a change, press the “Replace Shred” button on the miniAudicle, and the code that was running will be magically replaced with the new code. If you made significant changes, you will instantly hear the difference! This is another extremely powerful aspect of ChucK - the ability to modify your code “on the fly” while sound is still being synthesized.

Listing 1.12 Random Sine Wave Music

/* Random Sine Music by ChucK Programmer */ TriOsc s => dac; #A // infinite loop runs forever while (true) #1 { // randomly choose frequency from 30 to 1000 Math.random2(30,1000) => s.freq; #2 // random choose duration from 30 to 1000 ms Math.random2f(30,1000) :: ms => now; #3 }

#A Let’s use a triangle wave for variety #1 “Infinite loop” runs forever (more on this shortly) #2 Generate a random number between 30 and 1000 and use that for our pitch #3 Advance time by chucking a random number between 30ms and 1 second to now

Once you’re tired of hearing this and ready to move on, press the “Remove All Shreds” button (marked with a big grey “X” in the upper right corner of your main window), and that will stop the sound. [[ Another figure (sub of 1.6) here with just the control buttons? ]]]

Wow! With only the four lines of actual ChucK code of Listing 1.12, we’ve made more notes than we ever could by typing them in one at a time. We’re going to learn some of how that’s possible next (and we’ll learn the rest in Chapter 2).

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

33

Page 38: ProgrammingfMaDA_MEAP_ch1

The program of Listing 1.12 randomly changes the frequency of a triangle wave #2. Each new frequency is random, and the time it lasts until the next one is played is also random. The while (true)line #1 begins what programmers call an “infinite loop,” essentially meaning “do everything within the { curly braces } forever. We will talk about more ways to loop very shortly. In the last line #3 of the while loop’s body, we are advancing now by a random amount, at which point ChucK knows to automatically suspend the code, let time pass for that time (and meanwhile generate sound). Precisely after that time has passed, ChucK resumes executing your code. Pressing the “Remove All Shreds” button ends the infinite while loop and stops sound.

We’ve now written our first “loop” using the while keyword, which is called a “control structure,” so let’s look at exactly how that loop, and other loops, work, by learning about logic and control structures. Logic and control structures are essentialo make expressive and interesting compositions from your ChucK code.

Logic statements are either true or false, like “seven is not equal to two,” or “-3 is a negative number, AND negative numbers are less than zero, therefore -3 is less than zero.” Control structures use logic to determine how our computer code runs, and what the effects of it will be. ChucK employs many standard control structures found in programming languages, including if, else, for, and while.

We will start by looking closely at the if statement. We all use if statements all the time in every day life, such as “If I get hungry enough, I’ll stop and get food.” If statements in ChucK can be used to make decisions in code, based on the conditions of variables or other things that hold values. Figure 1.9 shows a diagram on what happens with an if condition, walking procedurally (line by line) through the code.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

34

Page 39: ProgrammingfMaDA_MEAP_ch1

Figure 1.9 Flow chart of an “if” conditional. If the condition is true, then some ChucK code is executed. If the condition is false, then the code is skipped, and the next code in the program executes.

Listing 1.13 If Statement Example // sound chain SinOsc s => dac; #A // set frequency #B 220.0 => s.freq; // set volume 0.6 => s.gain; // chance logical variable 1 => int chance; #1 if(chance==1) #2 { // sound will play only if chance == 1 1 :: second => now; #3 } // set new frequency #C 330.0 => s.freq; 1 :: second => now; #4

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

35

Page 40: ProgrammingfMaDA_MEAP_ch1

#A Our usual sound “patch” #B Set frequency and gain #1 Define an integer named chance we will use as a logical variable (will have a vaiue of either 1 or 0) #2 if statement. If the value in the parentheses is equal to 1, then do what’s in the { curly braces}, otherwise skip that code. #3 Advance time and let sound happen if true #C Set new frequency for a different note #4 Then play this new note

Translating that diagram into code, we see here a simple program that uses a new

integer variable chance #1 that is initialized to 1. In code section #2, if chance is equal to 1, (chance == 1) then something will happen.

With the control structures we are learning in this section, we are using the characters { and }. In a way, we can consider these to be like paragraphs, but programmers call them “blocks.” In this case, if the condition is true, then we enter and execute the code #3 in the block. If the condition is not true, then we move on. Also notice that there are 2 equal signs used to express the conditional test. That’s just the convention in ChucK and many other programming languages, to use the “==” symbol to indicate we are doing a logical test for equality, in this case “is the variable named chance equal to 1?”

There are logical conditions in addition to just equals, such as “less than,” “greater than,” and “not equal.” For that, ChucK provides us other symbols to check logical conditions, such as “!=” (not equal), “<” (less than), and“>” (greater than). In our random sine frequency program from Listing 1.9, we used a ChucK keyword logical variable true which always has the value of 1. In ChucK, the value 1 is used to represent true, and the value 0 represents false. In fact, if you type and run this line of ChucK code:

<<<true, false>>>;

You will see that ChucK prints out:

1 0

Note that we have printed two items by placing a comma between them in our <<<>>>

print statement. Similarly, you can type and run this: <<< -3 < 0, true==1, true==false, 1 > 10 >>>;

yielding:

1 1 0 0

meaning that minus 3 is less than zero, and true does equal 1, but true does not equal false, and one is not greater than ten. Cool, huh!!?

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

36

Page 41: ProgrammingfMaDA_MEAP_ch1

Continuing our example from Listing 1.13, because chance==1, the test is true, the program continues into the block and executes line 15, which is to advance time by 1 second, and plays sound for 1 second.

Now, go and change line #1, setting chance to 0, and rerun the new edited code. This in essence changes line #2 to be false and thus line #3 will never be executed, and thus no time will pass, and thus no sound will be made in that conditional code. The program just goes on to play the 2nd note, which is why we only hear that higher note. So when chance==1 we hear two notes, and when chance != 1 we hear only one note.

We will now introduce the else statement, which goes hand and hand with the if statement. As you can see from the diagram of Figure 1.10, the if/else structure forms a fork in the road; if the condition is true then go right, if the condition is false, then go left. As we can see from code example 1.14, the else statement #5 has its own block of code, following the if statements’ block #4. In this case our test #3 will equate to false, so the code will jump down and execute #5, changing the frequency to 330 Hz, and advancing time and playing the sound for 3 seconds.

If you change the initialization of “chance” to 1 rather than 3 in #2, then the if condition #3 will be true and the first block of code #4 will execute, playing a sine wave of frequency 220 Hz for one second.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

37

Page 42: ProgrammingfMaDA_MEAP_ch1

Figure 1.10 Flow chart of an “if/else” conditional. If the condition is true, then some ChucK code is executed. If the condition is false, then different code is executed.

Listing 1.14 if/else Code Example // sound chain SinOsc s => dac; #A // set frequency 220.0 => s.freq; // set volume 0.5 => s.gain; // chance logical variable 3 => int chance; #2 if(chance==1) { // play first "twinkle" note if chance == 1 1 :: second => now; } else { // otherwise, play other, higher twinkle note

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

38

Page 43: ProgrammingfMaDA_MEAP_ch1

330.0 => s.freq; // and play it for a much longer time 3 :: second => now; } // set and play one note, an octave above “twinkle” #B 440.0 => s.freq; 1 :: second => now;

#A sine to dac, set frequency and gain #2 new integer named chance, initialized to value of 3 #3 if chance is equal to 1 #4 do this block #5 Otherwise (else), set sine frequency to something different and play for a longer time. #B After the if/else, play another much higher note

Run the code in Listing 1.14, changing chance to different values. You’ll hear the low “twinkle” note followed by the much higher note only when chance==1, and for any other value you’ll hear the “twinkle2” note followed by the high one.

Just as we can have logical statements that combine or require multiple conditions (“My gas gauge is below ¼ tank, AND gas is pretty cheap right now, so I will buy gas”), it is also possible to have multiple conditions in an if statement. The && (known as And) condition means that all conditions must be true for the overall condition to be true. The || (know as Or) condition means that only one of the conditions needs to be true to execute that block of code. An example of OR might be “If my tank is really empty OR gas is really cheap, then I’ll fill up.” ChucK code examples of these are shown in Listing 1.15.

if/else/and/or statements will become increasingly useful as we learn more about ChucK and music programming.

Listing 1.15 More Complex Logical Conditions // conditional statements if ( (chance == 1) || (chance == 5) ) #A { // code goes here } If ( (chance < 2) && (chance2 >= 6)) #B { // code goes here }

#A Or condition. If chance is equal to one OR 5 then the condition is true #B If chance is less than two, AND chance2 is greater than or equal to 6, then condition is true.

We will be able to make even more interesting music using the next control structure, called a “for loop.”As the “loop” name implies, this control structure is used to make loops and cyclical behaviors. The for loop begins #1 by setting an initial state with a variable #2. It then contains a condition #3 that it checks to see if it is true (like an if statement). If it is true, it then executes a block (paragraph) of code #4, and at the end executes an update

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

39

Page 44: ProgrammingfMaDA_MEAP_ch1

command #5. Here the initial variable is updated to a new value, and then the condition is checked again. This process continues until the condition is false and the loop is over.

There are many musical cases where we might want to use a for loop, such as playing up the notes of a scale (next Chapter), or sweeping gradually from one frequency to another by hitting all the frequencies in between. Here we are counting up and printing integers.

Figure 1.11 Flow chart of a “for” loop. Variable i is initialized to 0, then counted up (++) until it reaches the value of 4. The code in the block is executed each time.

Listing 1.16 The “for” Loop

#A #1 #2 #3 #4

#A begin for structure #1 initial setup, usually declare and initialize a counter variable #2 conditional test, end condition #3 update thing(s) to do each time around the loop, usually increment our counter #4 Block of code to do each time, as long as conditional test is true

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

40

Page 45: ProgrammingfMaDA_MEAP_ch1

In the example of 1.16, the integer variable i is initialized to zero #1 The conditional test

#2 is of course true (zero is less than four), so the block of code #4 is executed (print i, wait 1 second), then i is incremented #3 (i++ is the same as 1 +=> I, which is the same as 1 + 1 => i) and the for loop is run again with i equal to one. The conditional test is still true (one is less than four), so the block is executed, i is incremented, and the for loop runs again. This continues until i is equal to 4, and the conditional test is now false, so the loop ends.

Lets add sound to this concept so we can start to hear what a for loop can do for us musically. In music programming, for loops can be used to play scales, repeat patterns, smoothly change gains, sweep frequencies up or down, and many other super-musical things. In the code of Listing 1.17, we use a for loop #1 to set frequencies of our SinOsc #3 from values 20 Hz to 400 Hz every 10 milliseconds #4. Imagine having to write all these lines of code out one by one as we did in Section 1.2! Looping allows us to make much more complicated sounds and compositions right away and save us many lines of code. Already, as a composer you will be able to write music that you would have never been able to write without learning computer science and ChucK programming.

Listing 1.17 Musical Use of a for Loop

#A #1

#2

#3 #4

#A sine wave to dac #1 for loop starts with i at 20, and counts it up one at a time until it hits 400 #2 print i #3 set frequency of sine wave to the value of i #4 advance time by 10 ms (0.01 second)

The last control structure we are going to learn in this chapter is the while loop (which

we used before to make an infinite loop). While is similar to a for loop, but is written differently. We can actually rewrite our same for-loop program, but using a while statement.

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

41

Page 46: ProgrammingfMaDA_MEAP_ch1

Figure 1.12 Flow chart of a “while” loop. Integer variable i is first initialized to 0, then the block loops, incrementing i by 1 each time, until the condition is false (i reaches 400).

Listing 1.18 Musical while Loop

#A #1

#2

#3

#4 #5 #6 #7

#A sine wave to dac to make sound #1 initialize our counter/frequency to 20 #2 while loop only has a conditional test #3 block of code to execute #4 set frequency #5 print it out #6 advance time, and #7 increment i (very important!)

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

42

Page 47: ProgrammingfMaDA_MEAP_ch1

After setting up our usual sine wave, we initialize integer i, to be 20 #1. Then we set up our while condition #2 (while i is less than 400). The name suggests that it is a condition, which can make it a little more intuitive and easy to remember than the for loop. The while loop then continues inside its’ block #3, and then notice that we’ve added an update command #7 similar to the for loop update. Running this code you will see it makes the same sound as the for loop code of Listing 1.15. It is important to learn both for loops and while loops as they will be used in different ways throughout our ChucK learning journey.

Note that if we forgot to add the increment line #7 in the block of code of listing 1.16, the program would run forever, with the value of i, and the sine frequency always equal to 20. In most programming languages, the notion of an infinite loop (conditional loop that never reaches a termination condition) is to be avoided at all costs. But this is less so in ChucK, and in fact we already intentionally used an infinite loop (while (true) in listing 1.12) to make really interesting music. The buttons at the top of the miniAudicle to Add, Replace, Remove, and RemoveAll shreds (running programs) allow us to create and use infinite loops without fear. In fact, one attractive power of ChucK is that it can keep running while we add, modify, replace, and layer code to make really interesting sound and music.

1.6 Using multiple oscillators So far we’ve just played single notes and melodies using one sine wave oscillator. One question some of you might be having is, since music involves more than melody and rhythm, how do we play multiple oscillators at the same time? This is easy in ChucK, as shown in Listing 1.19.

Listing 1.19 Musical while Loop

// Using more than one oscillator // connect two oscillators to the dac SinOsc s => dac; #A SinOsc s2 => dac; #1 // set their frequencies and gains 220 => s.freq; #2 1030 => s2.freq; #3 0.5 => s.gain; #4 0.5 => s2.gain; 1 :: second =>now; #B

#A our usual sine wave #B let time pass so we can hear the sound

One great aspect of ChucK is that when you connect more than one sound source to something (like the dac), the sounds are automatically added together. Sound engineers call this “mixing.” So in this example, when we connect the second SinOsc s2 to the dac #1, it

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

43

Page 48: ProgrammingfMaDA_MEAP_ch1

will automatically be mixed with the other SinOsc, and we’ll hear the sound of both oscillators. We set their frequencies to be different #2 #3, so we can hear that there are two oscillators (If we set their frequencies to be exactly the same, we’d just hear one sound louder, because mixing two identical signals together just yields a louder version of those signals). In #4, we set their gains to be 0.5, because we want to avoid overloading the audio output. A good rule of thumb when mixing together a number of sources is to scale their gains so that they all add up to about 1.0. So if we added yet another SinOsc s3 to this mix, we’d want to set all their gains to 0.3, or maybe one to 0.5 and the other two to 0.25.

1.7 Final Example: “Twinkle” with Oscillators, Variables, Logic, and Control Structures

Now its time to bring everything together to make a composition using all the tools you have learned in this chapter. The example program below shows you how to do just that, enhancing our Twinkle example to make some harmonies, and do some really interesting things with loops.

Listing 1.20 Putting Twinkle All Together, With Two Sine Waves!!

// Twinkle, with two oscillators!

SinOsc s => dac;

TriOsc t => dac;

// our main pitch variable

110.0 => float melody;

// gain control for our triangle wave melody

0.5 => float onGain;

// we'll use this for our on and off times

0.3 :: second => dur myDur;

// only play t at first, sweeping pitch upward

1 => t.gain;

0 => s.gain;

while (melody < 220.0) {

melody => t.freq;

1.0 +=> melody;

0.01 :: second => now;

}

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

44

Page 49: ProgrammingfMaDA_MEAP_ch1

// turn both on, set up the pitches

1 => s.gain;

110 => s.freq;

melody => t.freq;

// play two notes, 1st "Twinkle"

for (0 => int i; i < 2; i++) {

onGain => t.gain;

myDur => now;

0 => t.gain;

myDur => now;

}

// new pitches!

138.6 => s.freq;

1.5*melody => t.freq;

// two more notes, 2nd "twinkle"

For (0 => int i; i< 2; i++){

onGain => t.gain;

myDur => now;

0 => t.gain;

myDur=>now;

}

// pitches for "little"

146.8=>s.freq;

1.6837 * melody => t.freq;

// play 2 notes for "little"

for(0 => int i; i < 2; i++) {

onGain => t.gain;

myDur => now;

0 => t.gain;

myDur => now;

}

// setup and play "star!"

138.6 => s.freq;

1.5*melody => t.freq;

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

45

Page 50: ProgrammingfMaDA_MEAP_ch1

onGain => t.gain;

second => now;

// end by sweeping both oscillators down to zero

for (330 =>int i; i >0; i--) {

i => t.freq;

i*1.333 => s.freq;

0.01 :: second => now;

}

Note that in this one example, we’ve used almost all of what we have learned in this chapter. We used sine and triangle waves, int and float variables, for and while loops, conditional tests, math, and the now keyword with durations to control time. We also introduced one new aspect of the for-loop here. That is that we can also count downward in a for loop from a higher number to a lower number. In this case, we sweep our oscillators downward by initializing our integer variable i, then count downward to zero by using the “decrement” operator (i--, which is the same as 1 -=> i, which is the same as i – 1 => i).

1.8 SUMMARY; Whew! We’ve covered a lot already. We learned about sine waves and oscillators, and a little about waves in general. We learned that sine waves can be used to build up essentially any wave or sound, but also that ChucK has a number of built-in oscillator types (square, saw, triangle) that we can use for our music making.

We also learned about variables and data types, including int(eger), float(ing point), dur(ation), and time. We learned how to manipulate time in ChucK, allowing us to control the timing of our programs and sound/music making.

Finally, we learned about logical variables and conditions such as “if, else, and, true,” and “false,” and we learned about two looping structures (for and while) that allow us to control our programs and sound/music without having to type out every event literally.

You should congratulate yourself, because you are now a programmer, and a digital artist! But ChucK offers us so much more power, and invites us to learn more about programming as well as sound, acoustics, human-computer interaction, and more. For example, how in the world did we come up with those numbers for the pitches? ChucK can help with that!

So let’s continue on to learn more about ChucK’s built-in libraries (useful functions we can use), and arrays (ways to organize and manipulate our musical/artistic data).

©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders.

http://www.manning-sandbox.com/forum.jspa?forumID=880

46