├── CONTRIBUTING.md ├── LICENCE.md ├── README.md ├── cover.png ├── hardware.yml ├── ks3.md ├── learn.md ├── lesson-1 ├── files │ ├── Lesson-1-Statement-Worksheet.pdf │ └── Lesson-1-computer-program-cards.pdf └── lesson.md ├── lesson-2 ├── files │ ├── Lesson-2-Debug-Cards.pdf │ └── Lesson-2-Iteration-Cards.pdf └── lesson.md ├── lesson-3 ├── files │ └── Lesson-3-randomisation-conditional-cards.pdf └── lesson.md ├── lesson-4 ├── files │ └── Lesson-4-numeric-cards.pdf └── lesson.md ├── lesson-5 ├── files │ ├── Lesson-5-thread-cards.pdf │ └── threads.pdf ├── images │ └── threads.png └── lesson.md ├── meta.yml └── overview.md /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | All contributions are assumed to be licensed under the same license as the source, i.e. CC BY-SA. This license must remain in all derivatives of this work. 4 | 5 | ## Issues 6 | 7 | If you find a mistake, bug or other problem, please [open an issue](https://github.com/raspberrypilearning/sonic-pi-lessons/issues) in this repository. 8 | 9 | ## Pull Requests 10 | 11 | If you fix a mistake, bug or problem or have something to contribute, please create a pull request for each modification. Please consider grouping modifications sensibly, i.e. don't bundle typo fixes in the same pull request as code changes, instead file them separately. 12 | 13 | Please note that sometimes things are done for pedagogical reasons so changes which make sense from a software engineering perspective (reducing duplication or making use of more advanced programming language features) may not be suitable to maintain the intended educational value. 14 | 15 | ## Derivatives 16 | 17 | The licence must remain in all derivatives of this work. 18 | 19 | ## Licence 20 | 21 | Unless otherwise specified, everything in this repository is covered by the following licence: 22 | 23 | ![Creative Commons License](http://i.creativecommons.org/l/by-sa/4.0/88x31.png) 24 | 25 | ***Sonic Pi Lessons*** by the [Raspberry Pi Foundation](http://raspberrypi.org) is licenced under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/). 26 | 27 | Based on a work at https://github.com/raspberrypilearning/sonic-pi-lessons 28 | -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | # Licence 2 | 3 | Unless otherwise specified, everything in this repository is covered by the following licence: 4 | 5 | ![Creative Commons License](http://i.creativecommons.org/l/by-sa/4.0/88x31.png) 6 | 7 | ***Sonic Pi Lessons*** by the [Raspberry Pi Foundation](http://raspberrypi.org) is licenced under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/). 8 | 9 | Based on a work at https://github.com/raspberrypilearning/sonic-pi-lessons 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **This is an archived resource.** The repo will remain available but the resource will no longer be maintained or updated. Some or all parts of the resource may no longer work. To see our latest resources, please visit [raspberrypi.org](http://www.raspberrypi.org). 2 | 3 | # Sonic Pi Scheme of Work 4 | 5 | ![](cover.png) 6 | 7 | [Sonic Pi](http://www.cl.cam.ac.uk/projects/raspberrypi/sonicpi/) is an open source programming environment developed by Dr Sam Aaron, designed to explore and teach programming concepts through the process of creating new sounds. This is a scheme of work which emphasises the importance of creativity in the learning process and gives users the control to turn their sonic ideas into reality. 8 | 9 | This scheme of work is specifically targetted towards introductory KS3 Computer Science. This has been developed in harmony with the new Computing curriculum in the UK. This scheme of work is the result of a close collaboration between Computing & ICT teachers and researchers at the University of Cambridge. It has been successfully trialled at a number of schools. 10 | 11 | ## Students will learn: 12 | 13 | Over the course of six lessons, students will learn: 14 | 15 | - What a Raspberry Pi is, what its main features are, and how to set up and use one 16 | - How to write text-based code to produce a music track 17 | - Computer Science concepts: 18 | - Sequencing 19 | - Debugging 20 | - Iteration 21 | - Conditionals 22 | - Data Structures 23 | - Algorithms 24 | - Functions 25 | - Concurrency 26 | 27 | ## Computing Programme of Study Met: 28 | 29 | **KS3:** 30 | 31 | - Use 2 or more programming languages, at least one of which is textual, to solve a variety of computational problems; make appropriate use of data structures [for example, lists, tables or arrays]; design and develop modular programs that use procedures or functions. 32 | 33 | **Partially meets:** 34 | 35 | - Understand the hardware and software components that make up computer systems, and how they communicate with one another and with other systems. 36 | - Understand how instructions are stored and executed within a computer system; understand how data of various types (including text, sounds and pictures) can be represented and manipulated digitally, in the form of binary digits. 37 | 38 | [National Curriculum Computing Programmes of Study](https://www.gov.uk/government/publications/national-curriculum-in-england-computing-programmes-of-study/national-curriculum-in-england-computing-programmes-of-study#key-stage-3) 39 | 40 | ## Resources 41 | 42 | For the majority of the lesson, it is suggested that work is carried out by students in pairs. Each pair should have access to the standard equipment described below. In addition, it is suggested that you have your own teacher setup connected with a speaker for the demonstration sections. 43 | 44 | - A Raspberry Pi per pair; 45 | - Latest NOOBS SD card with the Sonic Pi software installed per pair; 46 | - A keyboard and mouse connected to the RPi per pair; 47 | - A monitor connected to the RPi per pair; 48 | - A headphone splitter connected to the RPi audio jack per pair; 49 | - A pair of headphones connected to the splitter per student. 50 | 51 | ## Lessons 52 | 53 | - [Lesson 1 - Getting started with Sonic Pi on a Raspberry Pi](lesson-1/lesson.md) 54 | - [Lesson 2 - Debugging & Iteration](lesson-2/lesson.md) 55 | - [Lesson 3 - Conditionals & Randomisation](lesson-3/lesson.md) 56 | - [Lesson 4 - Data Structures](lesson-4/lesson.md) 57 | - [Lesson 5 - Concurrency](lesson-5/lesson.md) 58 | 59 | ## Community 60 | 61 | For more information, take a look at the [Sonic Pi website](http://sonic-pi.net). 62 | 63 | If you would like to contribute to this project or have questions about it please use the [Sonic Pi Forum here](https://groups.google.com/forum/#!forum/sonic-pi). 64 | 65 | For the latest news, take a look at the [Sonic Pi Twitter account](https://twitter.com/sonic_pi) and sign up to the [Sonic Pi Weekly](http://eepurl.com/9SLYX) newsletter. 66 | 67 | The full source code can be viewed and obtained from the [Sonic Pi GitHub repository](https://github.com/samaaron/sonic-pi). 68 | 69 | ## Licence 70 | 71 | Unless otherwise specified, everything in this repository is covered by the following licence: 72 | 73 | [![Creative Commons License](http://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/) 74 | 75 | ***Sonic Pi Lessons*** by the [Raspberry Pi Foundation](http://www.raspberrypi.org) is licenced under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/). 76 | 77 | Based on a work at https://github.com/raspberrypilearning/sonic-pi-lessons 78 | -------------------------------------------------------------------------------- /cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/cover.png -------------------------------------------------------------------------------- /hardware.yml: -------------------------------------------------------------------------------- 1 | - name: Headphones or a speaker 2 | img: headphones-speaker 3 | -------------------------------------------------------------------------------- /ks3.md: -------------------------------------------------------------------------------- 1 | ### Meets 2 | 3 | - Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems; make appropriate use of data structures [for example, lists, tables, or arrays]; design and develop modular programs that use procedures or functions. 4 | 5 | ### Partially meets 6 | 7 | - Understand the hardware and software components that make up computer systems, and how they communicate with one another and with other systems. 8 | - Understand how instructions are stored and executed within a computer system; understand how data of various types (including text, sounds, and pictures) can be represented and manipulated digitally, in the form of binary digits. 9 | -------------------------------------------------------------------------------- /learn.md: -------------------------------------------------------------------------------- 1 | Through this scheme of work, students will learn: 2 | 3 | - What a Raspberry Pi is, what its main features are, and how to set one up and use it 4 | - How to write text-based code to produce a music track 5 | - Computer Science concepts: 6 | - Sequencing, iteration, and conditionals 7 | - Data structures, functions, and algorithms 8 | - Debugging and concurrency 9 | 10 | This resource covers elements from the following strands of the [Raspberry Pi Digital Making Curriculum](https://www.raspberrypi.org/curriculum/): 11 | 12 | - [Combine programming constructs to solve a problem](https://www.raspberrypi.org/curriculum/programming/builder) 13 | -------------------------------------------------------------------------------- /lesson-1/files/Lesson-1-Statement-Worksheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-1/files/Lesson-1-Statement-Worksheet.pdf -------------------------------------------------------------------------------- /lesson-1/files/Lesson-1-computer-program-cards.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-1/files/Lesson-1-computer-program-cards.pdf -------------------------------------------------------------------------------- /lesson-1/lesson.md: -------------------------------------------------------------------------------- 1 | # Lesson 1: Getting started with Sonic Pi on a Raspberry Pi 2 | 3 | The Raspberry Pi is a tiny computer, smaller than a pack of cards, which can transform the way we perceive and approach computation. In this lesson, we will introduce the basic components of the Raspberry Pi and how they relate to a traditional computer. We will discuss the generic nature of computation and how the same computer can be programmed to do many different things simultaneously, from word processing to music synthesis. Finally, we will introduce the most basic principle of programming: a program as a sequence of instructions. 4 | 5 | ## Learning Objectives 6 | 7 | - Know that there are many different types of computing devices. 8 | - Understand how a computer uses a sequence of statements to do something, and that this sequence is called a program. 9 | - Be able to give the Raspberry Pi some instructions to make some music. 10 | 11 | ## Learning Outcomes 12 | 13 | ### All students are able to: 14 | 15 | - Know how to connect the components of a Raspberry Pi together. 16 | - Act out some basic statements, and understand that a sequence of statements is a program. 17 | - Write a simple program. 18 | 19 | ### Most students are able to: 20 | 21 | - Create their own programs for others to act out, and have an idea of what might happen when it is acted out. 22 | 23 | ### Some students are able to: 24 | 25 | - Invent their own statements. 26 | - Start to explore the limitation of a program containing only statements and control flow. 27 | - Understand the consequences of their program before they run it, and therefore design a musical program that's interesting to them. 28 | 29 | ## Lesson Summary 30 | 31 | - An introduction to the basic physical parts of a Raspberry Pi 32 | - A demonstration that the Raspberry Pi can behave like a traditional computer 33 | - A group exercise to act out a simple program 34 | - Starting the Sonic Pi application 35 | - The first music program 36 | 37 | ## Starter 38 | 39 | Have a demonstration Raspberry Pi already connected and the Sonic Pi software running. Hold up a Raspberry Pi board and ask the students what they think it is. Explain that it's actually a computer and that in the coming lessons we're going to do something special with it. Instead of running apps and games other people have created for us, we're going to learn to write our own software to make music. 40 | 41 | Start the demo code below, play it for a moment or two and explain that in a few weeks the students will be able to make computers do this for themselves. Emphasise that they'll be free to do what they want with it and have a lot of fun in the process; programming is about getting the computer to do exactly what you want it to do. It's not important for the students to see the application or any code at this stage, just for them to hear the sounds coming from the computer. 42 | 43 | Note: this is one of the examples available in the help system in v2.3. If you're using an older version, feel free to pick one of the other examples. 44 | 45 | ```ruby 46 | use_debug false 47 | 48 | live_loop :low do |idx| 49 | # idx = 0 50 | synth :zawa, wave: 1, invert_wave: 1, phase: 0.25, release: 5, note: :e1, cutoff: (range 60, 120, 10)[idx] 51 | sleep 4 52 | idx += 1 53 | end 54 | 55 | live_loop :lands, auto_cue: false do |idx| 56 | use_synth :dsaw 57 | use_random_seed 66679 58 | with_fx :reverb, room: 1 do 59 | 16.times do 60 | ns = (scale :e2, :minor_pentatonic, num_octaves: 3) 61 | play ns.choose, detune: 12, release: 0.1, amp: 2, amp: rand + 0.5, cutoff: rrand(70, 120) 62 | sleep 0.125 63 | end 64 | end 65 | end 66 | 67 | live_loop :bikes do |idx| 68 | sleep 0.25 69 | sample :guit_em9, rate: -1 70 | sleep 7.75 71 | end 72 | 73 | live_loop :time, auto_cue: false do |idx| 74 | sample :bd_haus, amp: 2.5 75 | sleep 0.5 76 | end 77 | 78 | ``` 79 | 80 | ## Main Development 81 | 82 | 1. Start with all the parts of the Raspberry Pi on a table: keyboard, mouse, speaker, memory card, power supply, monitor, monitor cable, and the Raspberry Pi itself. Ask the class to name and describe each component as you connect it to the Raspberry Pi in front of the class. Finally, plug in the power and watch it boot up. An alternative demonstration would be to leave out the memory card and attempt to boot the Pi, which will fail. You can then describe the memory card as something that contains instructions to tell the Raspberry Pi how to start. The Raspberry Pis should all be booted and sitting on the login prompt waiting for authentication. 83 | 84 | 1. Split the class into groups again and give each group a deck of the [computer program cards](files/Lesson-1-computer-program-cards.pdf). Ask each group to take out the statement cards and the control card from the deck. Then ask each group to form a line and to give each member of the group a statement card after shuffling them. The person at the start of the line should be given the control card. Explain that the person holding the control card should carry out the instructions on the statement card, and then pass the control card to the next person in the line like a relay baton. When the control card has reached the end of the line, they should stop. This should be repeated for a number of random orderings, after which the groups could be invited to create their own orderings. A helpful analogy might be cooking, where collections of statements are recipes and the control flow is which stage of the recipe you're at. 85 | 86 | 1. Start the Sonic Pi software. First, invite the students to log into their Raspberry Pi and start the graphical environment. It might help to display instructions on how to achieve this on a projector for all to see. 87 | 88 | 1. Explain to them that they can use the same statements on the cards in the computer program: `play` and `sleep`. Invite them to spend the remaining time writing their own programs and listening to the results. 89 | 90 | ## Plenary 91 | 92 | Groups should be invited to choose card orderings for other groups to act out. Following this, a discussion should be held about how this relates to a computer. A computer works by executing statements one after another in a specific order. A given order of statements is called a **program**. Each program executes with a given **control flow**; this describes which statement we are executing and what the next statement will be. 93 | 94 | ## Homework 95 | 96 | Students should be asked to invent statements of their own which they could act out with their family using the [Programming Statement Worksheet](files/Lesson-1-Statement-Worksheet.pdf). 97 | -------------------------------------------------------------------------------- /lesson-2/files/Lesson-2-Debug-Cards.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-2/files/Lesson-2-Debug-Cards.pdf -------------------------------------------------------------------------------- /lesson-2/files/Lesson-2-Iteration-Cards.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-2/files/Lesson-2-Iteration-Cards.pdf -------------------------------------------------------------------------------- /lesson-2/lesson.md: -------------------------------------------------------------------------------- 1 | # Lesson 2 - Debugging and Iteration 2 | 3 | In this lesson we will be given an opportunity to experience the art of programming from debugging to careful syntax placement, to the joy of a working program. Also, in order to construct some meaningful and interesting musical structures, we need to learn some meaningful and interesting programming structures. Today we are going to learn how to create a bass line. 4 | 5 | ## Learning Objectives 6 | 7 | - Understand that computers don’t know what to do with an error. 8 | - Write a simple program using text and identify any errors. 9 | - Iterate/repeat some code a number of times to create a 'bass line'. 10 | 11 | ## Learning Outcomes 12 | 13 | ### All students will be able to: 14 | 15 | - Understand that computers cannot complete a sequence of instructions if they contain an error. 16 | - Write a simple program. 17 | - Iterate some code a number of times. 18 | 19 | ### Most students will be able to: 20 | 21 | - Understand that subtle errors in language are just as unintelligible to the computer as a foreign language is to someone who doesn’t speak it. 22 | - Understand the consequences of their program before they run it, and therefore design a musical program that’s interesting to them. 23 | - Understand that the `do` and `end` keywords are structural syntax, rather than actions such as `play`. 24 | 25 | ### Some students will be able to: 26 | 27 | - Read an error message and identify the cause of the error. 28 | - Understand that iterations can be nested within each other. 29 | - Use the advanced commands in the pupil notes. 30 | 31 | ## Lesson Summary 32 | 33 | - A short exercise to recap the notion of statements and control flow within a program. 34 | - A debugging exercise. 35 | - Starting the Sonic Pi application. 36 | - Iteration as a repeating structure. 37 | - Syntax punctuating structure. 38 | 39 | ## Starter 40 | 41 | If any of the pupils have interesting statements that they created as part of their homework, they are invited to demonstrate them by getting a number of other pupils to act them out. This could be done in groups or with the class as a whole. As a backup the computer program cards could be used, similar to the previous lesson. 42 | 43 | ## Main Development 44 | 45 | 1. With just one group acting and with the rest of the class watching, introduce [the two error cards](files/Lesson-2-Debug-Cards.pdf) into the deck of computer program cards; shuffle them up and repeat the exercise described in the previous lesson. Observe what the pupil with the foreign language error card does, and then have a short discussion with the class about what perhaps should happen. Then point out that there’s another error card, but with a subtle error in the spelling. Explain that to the computer they are both as unintelligible as each other. The computer isn’t clever enough to read through the subtle error. 46 | 47 | 1. The pupils are then separated into pairs and are given their own set of Sonic Pi equipment. Pupils follow the instructions to connect the various parts of the Raspberry Pi together and once it has booted, they start the Sonic Pi program by clicking on the menu item under **start -> programming -> sonic-pi**. 48 | 49 | 1. Next, the pupils are given their first introduction to **debugging**. They are asked to type in the following one-line program which is incorrect: `pllay 42`. They are then asked to observe what happens when they run the program. At this point it’s important to emphasise that this is a typical situation for real programmers. It means that they have done something that the computer doesn’t understand; because the computer is not very clever, this is very easy to do. The students are invited to correct (debug) the program by removing the extra `l`. They are then asked to observe the output of the program. 50 | 51 | 1. A selection of pupils are asked to form a line with the computational cards, and to act out the program as carried out in the previous lesson. The class is then asked how we can repeat this program twice. One of the answers might be to duplicate the line, forming a line twice as long as the original with the original sequence of statements duplicated twice. Now, ask the class to act out this new longer program. Once this has been completed ask how we might repeat the original program 10 times, or 100 times. What about 1000 times? Clearly, we’d run out of people! 52 | 53 | 1. Introduce [the iteration cards](files/Lesson-2-Iteration-Cards.pdf): `1.times`, `do` and `end`. Wrap the original program in the `do` and `end` cards, and prefix everything with the `1.times` card. Explain that this is just a long-winded way of doing the same thing as the original program. The new cards are extra syntax which should be thought of as the same as punctuation; capital letters, commas, full stops and so on. Like punctuation, this syntax helps the computer structure the program. However, with this extra syntax, we have actually formed a circle out of our code. The syntax defines the start and the end points of the loop: the `do` and `end`. In this case we only loop round the circle once, but if we replace the first card with `3.times` we loop round our program 3 times. If you have enough space, try forming a circle and enact the loop. Explain that this loop structure is called iteration. 54 | 55 | 1. Pupils are asked to enter the following ‘bass line’ code into the Sonic Pi application: 56 | 57 | ```ruby 58 | play 42 59 | sleep 0.5 60 | play 45 61 | sleep 0.85 62 | play 54 63 | sleep 1 64 | play 54 65 | sleep 0.7 66 | play 45 67 | sleep 0.2 68 | play 49 69 | sleep 1 70 | ``` 71 | 72 | 1. They are then asked to repeat this line 5 times. They do this by writing: 73 | 74 | ```ruby 75 | 5.times do 76 | play 42 77 | sleep 0.5 78 | ... rest of code ... 79 | sleep 1 80 | end 81 | ``` 82 | 83 | 1. The pupils are then invited to play around with this, modifying it as they feel fit, for the remainder of the time. For the adventurous pupils, you can suggest that they nest the iterations; in other words, have one iteration inside another. 84 | 85 | ## Plenary 86 | 87 | Each group is invited to demonstrate the program they have written by executing it and playing the resulting music. This can be achieved by passing the speaker round to each group, and playing the music through the speaker rather than the headphones. Groups should also be able to explain verbally through teacher questioning where in their code they have used loops, why they have used them and how they resolved any bugs. 88 | 89 | ## Homework 90 | 91 | Students should write their own musical programs on paper to be discussed in the next lesson. 92 | -------------------------------------------------------------------------------- /lesson-3/files/Lesson-3-randomisation-conditional-cards.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-3/files/Lesson-3-randomisation-conditional-cards.pdf -------------------------------------------------------------------------------- /lesson-3/lesson.md: -------------------------------------------------------------------------------- 1 | # Lesson 3 - Conditionals and Randomisation 2 | 3 | If we want to make some meaningful and interesting musical structures, we need to learn some meaningful and interesting programming structures. 4 | 5 | ## Learning Objectives 6 | 7 | - Know that computer programs can make decisions, and that a simple form of decision is called a conditional. 8 | - Understand that computer programs can contain random acts. 9 | - Be able to use comments to explain interesting parts of a program. 10 | 11 | ## Learning Outcomes 12 | 13 | ### All students will be able to: 14 | 15 | - Play a random note. 16 | - Use an `if` statement. 17 | - Place a comment in their code. 18 | 19 | ### Most students will be able to: 20 | 21 | - Play random notes with varying randomness (by supplying a different argument to `rand`). 22 | - Write useful comments. 23 | - Modify code in the `if` branches to create different execution paths. 24 | 25 | ### Some students will be able to: 26 | 27 | - Use `rand` to sleep for a random amount of time. 28 | - Understand that using `rand` as a test with `<` allows for different probability distributions other than 50/50. 29 | - Nest `if` statements inside other `if` statements. 30 | - Understand that comments are about communicating intent. 31 | 32 | ## Lesson Summary 33 | 34 | - Simple use of randomisation. 35 | - Using conditionals to make decisions. 36 | - Combining randomisation and conditionals. 37 | - Comments as a programming tool. 38 | 39 | ## Starter 40 | 41 | Pupils are first invited to set up and connect their Raspberry Pi hardware. They may then load the Sonic Pi application and find their work from the previous lesson. Through questioning, select students to explain what they did in the previous lesson. 42 | 43 | ## Main Development 44 | 45 | 1. Pupils should be should be shown how to add some randomisation to their code. This can be achieved by using the statement `rand(10)`, which returns a random value between `0` and `10` (from 0 up to but not including the number you specify). You can specify other numbers for larger ranges; for instance, `rand(20)` will return values from `0` to `20`. Let’s use this in our program by adding our random number to a note with the `+` operator: 46 | 47 | ```ruby 48 | 3.times do 49 | play 60 + rand(10) 50 | sleep 0.5 51 | end 52 | ``` 53 | 54 | Invite the pupils to observe the actual number of the note played in the output window. 55 | 56 | 1. Pupils are then asked to form a line as with previous lessons, except this time the line should split into two separate lines (called branches) at one point. Give out the [Conditional Statement Cards](files/Lesson-3-randomisation-conditional-cards.pdf). The pupil directly before the split should be given the special `if` card, and the rest of the pupils should be given cards as with previous exercises. The control card should start at the first pupil, and is passed down as each pupil carries out the action of their card. When it gets to the pupil with the `if` card, they should toss a coin. If it’s heads, the control card should be passed to the first of the two separate lines; otherwise the control card should be passed to the second of the two lines. Once the control card is in one of the separate lines it continues until the end of that line, then the program is terminated. It should be pointed out that the line that didn’t get the control card passed to it was essentially ignored. The `if` statement is called a conditional and allows for decisions to be made in the program. 57 | 58 | 1. Pupils should then be shown how to write an `if` statement in the Sonic Pi application. Ask them to copy the following code into their worksheet: 59 | 60 | ```ruby 61 | if rand < 0.5 62 | play 60 63 | sleep 0.5 64 | play 62 65 | else 66 | play 72 67 | sleep 0.25 68 | play 71 69 | sleep 0.25 70 | play 70 71 | end 72 | ``` 73 | 74 | 1. Once they have copied this code, point out the syntax of the `if` statement, specifically the words `else` and `end`. These are similar to the `do` and `end` found in the iteration block discussed in the previous lesson, in that they’re like punctuation. The `else` separates the two different branches of the `if` statement. 75 | 76 | 1. The first line should also be discussed; this is equivalent to a coin toss in that `rand(1)` returns a random value between `0` and `1`, and we’re testing to see if that random value is less than 0.5. For the advanced pupils, you may wish to point out that changing the 0.5 to different values will affect the probability of which branch is selected. For example, a value of 0.1 would mean that (on average) every 10 runs the first branch be selected only once, and the second branch will be selected 9 times. 77 | 78 | 1. The pupils are then invited to press the play button a number of times so that they can hear the different branches being executed, the decision of which branch to execute being random each time. 79 | 80 | 1. Pupils are invited to play around with the constructs of this lesson, in addition to everything they’ve learned so far, to design a simple musical program. 81 | 82 | ## Plenary 83 | 84 | Finally, teach the class that the hash symbol `#` is used to make a comment. Invite them to place comments in their code to explain what is happening. This is not just for other programmers who might read their code; it is also for themselves in the future, when they look back at old code they may have written a long time ago and have forgotten what it does. For example: 85 | 86 | ```ruby 87 | # Toss a virtual coin 88 | if rand(1) < 0.5 89 | # if heads, play two ascending notes 90 | play 60 91 | sleep 0.5 92 | play 62 93 | else 94 | # if tails, play three descending notes 95 | play 72 96 | sleep 0.25 97 | play 71 98 | sleep 0.25 99 | play 70 100 | end 101 | ``` 102 | 103 | ## Homework 104 | 105 | Students may research electronic-sounding music and think about how they could improve the sound of their own composition. 106 | -------------------------------------------------------------------------------- /lesson-4/files/Lesson-4-numeric-cards.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-4/files/Lesson-4-numeric-cards.pdf -------------------------------------------------------------------------------- /lesson-4/lesson.md: -------------------------------------------------------------------------------- 1 | # Lesson 4 - Data Structures and Algorithms 2 | 3 | Although the world is constructed from many tiny pieces, this isn’t how we perceive it. Instead, we see useful structures and aggregations of these pieces. Rather than individual atoms or molecules, we see trees, people, bicycles, and so on. In our previous lessons, the information we have dealt with has been simple numbers: numbers for notes, numbers for time, even numbers for chance. Data structures allow us to compose and aggregate these simple data into useful structural forms. One of the simplest of these aggregate structures is the list which we will discuss in this lesson. Once we have structures like the list, we can start using a useful library of mini-programs which can perform operations such as sorting and shuffling the list. 4 | 5 | ## Learning Objectives 6 | 7 | - Know that numbers can be aggregated into data structures such as lists. 8 | - Understand that algorithms are a series of steps or instructions for solving a problem such as sorting and shuffling a list of numbers. 9 | - Be able to use data structures and built-in algorithms as part of Sonic Pi composition, using comments to explain what is happening. 10 | 11 | ## Learning Outcomes 12 | 13 | ### All students will be able to: 14 | 15 | - Know that numbers represent data or information. 16 | - Use a list. 17 | - Use sort and shuffle to manipulate a list. 18 | 19 | ### Most students will be able to: 20 | 21 | - Understand that lists are aggregate data structures. 22 | - Understand that algorithms are solutions to problems such as sorting a list. 23 | 24 | ### Some students will be able to: 25 | 26 | - Describe the steps for an implementation of sort (i.e. bubble sort). 27 | - Describe data algorithms other than sort and shuffle which are useful for lists, such as repeat, split in half and reverse. 28 | 29 | ## Lesson Summary 30 | 31 | - Numbers as a kind of data. 32 | - Introduction to lists. 33 | - Playing a list of numbers. 34 | - Introduction to sorting. 35 | - Sorting and shuffling a list of numbers. 36 | 37 | ## Starter 38 | 39 | Pupils are first invited to set up and connect their Raspberry Pi hardware. They may then load the Sonic Pi application and find their work from the previous lesson. Through questioning, select students to explain what they did in the previous lesson. 40 | 41 | ## Main Development 42 | 43 | 1. Pupils should be asked what kind of information they have been using in the previous lessons; in other words, the things they’ve been able to change. They have been able to change the time to sleep and the note to play. Ask them what kind of information this is, the answer being that they are all numbers. Explain that a number is a kind of data, and there are many others that computers may understand. 44 | 45 | 1. Explain to the pupils that although numbers are one of the most useful forms of data, it’s also very handy to represent a list of numbers. Ask them if they can think of any things that could be represented by a list of numbers; for example, the finish times of runners in a race, the numbers drawn in the lottery and the notes of a bass line. 46 | 47 | 1. Invite the pupils to enter the following code into a new worksheet and press play: 48 | 49 | ```ruby 50 | play_pattern [40, 25, 45, 25, 25, 50, 50] 51 | ``` 52 | 53 | 1. Explain that `play_pattern` is similar to `play` except instead of taking a number representing the note to play, it takes a list of numbers to play one after the other. This list of numbers has special syntax to tell the computer that it’s a list. Firstly, it has to start with a `[` and end with a `]`. Secondly, each number in the list has to be separated from the other numbers with a comma. 54 | 55 | 1. Invite the pupils to write their own lists, choosing different numbers and different lengths of numbers, for example: 56 | 57 | ```ruby 58 | [43, 24, 60, 57, 30] 59 | [60] 60 | [48, 48, 48, 60] 61 | ``` 62 | 63 | 1. Once they’ve had a short play with this, invite them to form a line and hand out the number cards (in no particular order), so each pupil in the line holds just one card. Explain that they have formed a list of numbers and that there are useful things that you can do with this. One example is sorting the numbers numerically, so that the smallest numbers are first and the largest last. Introduce the word 'algorithm' as a method for solving such problems. 64 | 65 | 1. Next, explain that we will explore a simple sorting algorithm: bubble sort. Start at the left hand side of the line and ask the first two pupils to compare their numbers. If they are in the right order do nothing, otherwise ask the pupils to swap. Then continue to the second and third pupils, and compare and swap again if necessary. Continue down the line. If at least one pair has swapped, start at the beginning of the line and repeat. If no pairs have swapped, the list is sorted. 66 | 67 | 1. Explain that most programming languages provide many such algorithms to make programming easier, and to reduce the amount of work you have to do as a programmer. Ask the pupils to type the following code: 68 | 69 | ```ruby 70 | play_pattern [40, 25, 45, 25, 25, 50, 50].sort 71 | play_pattern [40, 25, 45, 25, 25, 50, 50].shuffle 72 | ``` 73 | 74 | 1. Pupils are invited to play around with the constructs of this lesson, in addition to everything they’ve learned so far, to design a simple musical program. 75 | 76 | ## Plenary 77 | 78 | Ask the pupils to consider how they might design an algorithm to shuffle a list. Another thing to consider is the existence of algorithms other than sort and shuffle. 79 | 80 | ## Homework 81 | 82 | Students could create a sorting algorithm in Scratch to explain how one would shuffle the notes in a list. 83 | -------------------------------------------------------------------------------- /lesson-5/files/Lesson-5-thread-cards.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-5/files/Lesson-5-thread-cards.pdf -------------------------------------------------------------------------------- /lesson-5/files/threads.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-5/files/threads.pdf -------------------------------------------------------------------------------- /lesson-5/images/threads.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/sonic-pi-lessons/2c3b006ea0daad6b1b021f5716f4d2e6a70caa98/lesson-5/images/threads.png -------------------------------------------------------------------------------- /lesson-5/lesson.md: -------------------------------------------------------------------------------- 1 | # Lesson 5 - Concurrency 2 | 3 | If we want to make some meaningful and interesting musical structures, we need to learn some meaningful and interesting programming structures. 4 | 5 | ## Learning Objectives 6 | 7 | - Know that computers can multitask (do many things at once). 8 | - Understand that a simple program has one flow of control; in other words, it has one thread of execution. Programs can, however, have multiple such threads. 9 | - Be able to program multiple threads that work at the same time (concurrently). 10 | 11 | ## Learning Outcomes 12 | 13 | ### All students will be able to: 14 | 15 | - Know that computers can do multiple things at once. 16 | - Use an `in_thread` block. 17 | 18 | ### Most students will be able to: 19 | 20 | - Use multiple `in_thread` blocks in the same program. 21 | 22 | ### Some students will be able to: 23 | 24 | - Understand that threads have a separate flow of control. 25 | 26 | ## Lesson Summary 27 | 28 | - Overview of the role of the control card and control flow. 29 | - Introduction to threads. 30 | - Using threads to play sounds at the same time. 31 | 32 | ## Starter 33 | 34 | Pupils are first invited to set up and connect their Raspberry Pi hardware. They may then load the Sonic Pi application and find their work from the previous lesson. Through questioning, select students to explain what they did in the previous lesson. 35 | 36 | ## Main Development 37 | 38 | 1. Pupils are asked to form a simple line with the original computation cards and act out the program. Ensure that the control card is correctly handed down the line like a relay baton. 39 | An example program might be: 40 | 41 | ```ruby 42 | beep 43 | sleep 1 44 | whoosh 45 | sleep 0.5 46 | ping 47 | sleep 2 48 | boop 49 | ``` 50 | 51 | 1. Once they have acted this out, invite them to talk about the structure of this code. Lead them into seeing the clear linear structure: they are standing in a line. Also, point out that the control card represents control flow; it represents where the computer is in the program. Ask them to discuss the main limitation of only having one control card: only one thing can happen at once. What if we wanted a bass line to play at the same time as a melody? 52 | 53 | 1. Explain that we’re going to introduce a way of having more than one control card in the same program, which will mean more than one thing can happen at the same time. In the programming world, we call this concurrency. The following figure shows how we can move the line of pupils from a single-threaded program to a multi-threaded program. The plain circles represent the standard program cards we’ve used so far (whoosh, ping, sleep and so on), and the orange circle represents one of the thread cards. Organise the pupils into such a form with one main line as before, but with a threaded branch. Remind them that this is similar to the conditional branch in the previous lesson. However, rather than just one of the branches being taken both will be taken! Start the program as normal with the first person (denoted by A in the diagram) having the control card, performing the action, and then passing the control card on to the next person. However, when the control card reaches the person holding the thread card, pass the holder of the thread card another control card and ask them to pass one each to both branches. Now execution happens **concurrently** and independently. 54 | 55 | ![](images/threads.png "concurrency diagram") 56 | 57 | 1. Now invite the pupils to write the following code: 58 | 59 | ```ruby 60 | in_thread do 61 | 10.times do 62 | play 60 63 | sleep 0.25 64 | end 65 | end 66 | 67 | play 60 68 | sleep 0.5 69 | play 62 70 | sleep 0.5 71 | play 64 72 | sleep 0.5 73 | play 65 74 | sleep 0.5 75 | play 67 76 | sleep 0.5 77 | play 69 78 | sleep 0.25 79 | play 72 80 | ``` 81 | 82 | 1. Explain that this code works exactly like the system they just acted out. The code within the `in_thread` block gets its own control flow, and therefore runs at the same time as the `play` & `sleep` code below. 83 | 84 | ## Plenary 85 | 86 | Pupils are invited to play around with the constructs of this lesson, in addition to everything they’ve learned so far, to design a simple musical program. 87 | -------------------------------------------------------------------------------- /meta.yml: -------------------------------------------------------------------------------- 1 | title: Sonic Pi Lessons 2 | category: teach 3 | lessons: 4 | 1: Getting started with Sonic Pi on a Raspberry Pi 5 | 2: Debugging & Iteration 6 | 3: Conditionals & Randomisation 7 | 4: Data Structures 8 | 5: Concurrency 9 | -------------------------------------------------------------------------------- /overview.md: -------------------------------------------------------------------------------- 1 | [Sonic Pi](http://www.cl.cam.ac.uk/projects/raspberrypi/sonicpi/) is an open-source programming environment developed by Dr Sam Aaron, designed to explore and teach programming concepts through the process of creating new sounds. This is a scheme of work which emphasises the importance of creativity in the learning process and gives users the control to turn their sonic ideas into reality. 2 | 3 | This scheme of work is specifically targeted towards introductory KS3 Computer Science. It has been developed in harmony with the new Computing curriculum in the UK. The scheme of work is the result of a close collaboration between Computing and ICT teachers and researchers at the University of Cambridge. It has been successfully trialled at a number of schools. 4 | --------------------------------------------------------------------------------