@extends('template')
@section('title')
Pictures and Sounds
@stop
@section('head')
@stop
@section('content')
# Lab 1: Pictures and Sounds
Most programs that we use every day display images and play sounds, and
Python can do those things too.
## Drawing Pictures
In your `lab01` folder, **create a new file** named `drawing.py`, and
paste in the following code:
```py
from turtle import *
fd(50)
lt(90)
fd(100)
rt(45)
bk(80)
```
What happens when you run the file? Discuss with your partner: what do
you think `fd`, `lt`, `rt`, and `bk` functions do, and what do those
names stand for? (try changing the values to see what changes)
Click here to show the answers
`fd` stands for "forward," `lt` and `rt` stand for "left" and "right,"
and `bk` stands for "backwards." Each of these functions acts like a
command that moves or turns the cursor, and wherever the cursor moves, a
line is drawn. Here is a picture of what gets drawn, with a second
version showing the motions of the cursor during the drawing process in
pink:
{{-- WHY does the first image in each details have to have an absolute URL? Our PHP URL rewriting rules must be broken somehow? --}}
Now add these lines to the end of your file:
```py
pensize(20)
pencolor('DodgerBlue4')
bk(20)
```
What do you expect to be drawn if you run the file again?
Click here to show the answer
These commands create a large dark blue pen, which will draw a thick line
with rounded ends, going backwards from the end of the original drawing.
It will look like this (second image shows direction of movement):
What happens if you leave out the `import` line? What do you think
`import` does?
Click here to show the answer
Without `import`, Python will complain that `fd` is not defined. `import`
makes new functions available in the current file. The specific code
`from turtle import *` is a way of asking to import "everything" from the
`turtle` module, which is where the drawing functions we'll use are
defined.
Using functions from the `turtle` library, we can draw lines, and we've
provided an additional module called `turtleBeads` which allows you to
more easily draw complex shapes like circles or ellipses. At the top of
your file, add the following line:
```py
from turtleBeads import *
```
(Make sure that your file is saved in the same directory as the
`turtleBeads.py` file we provided.)
Then, at the bottom of your file, add these lines of code:
```py
leap(170)
drawCircle(30)
```
Can you guess what they will do?
Click here to show the answer
`drawCircle` draws a circle, which is pretty easy to guess, but `leap` is
a bit more obscure. As it turns out, `leap` moves the cursor forward just
like `fd` does, but without drawing a line. These images show the result
and indicate where the leap happened:
Note that in the base `turtle` library, the functions `penup` and
`pendown` can be used to stop or restart drawing, which is a more complex
way to move the cursor without drawing. `turtleBeads` also includes a
`hop` function to move sideways without drawing.
## Making Noise
We use the `turtle` functions to draw, and we can use functions from a
library called `wavesynth` to make sounds. Like `turtleBeads.py`,
`wavesynth.py` is a file that we've supplied, so it has to be in the same
directory as your file.
In addition, the library cannot play sounds directly unless you install
the `simpleaudio` package. To do that in Thonny, go to the "Tools" menu,
click on "Manage packages," search for "simpleaudio," select the first
result, and then click the "Install" button at the bottom (you can then
close the package manager).
Click here for video overview of installing simpleaudio
This short video gives an overview of how to install simpleaudio (you only need to do this once).
Once `simpleaudio` is installed, **create a new file** named `music.py`
and paste the following code into it:
```py
from wavesynth import *
addNote(0.5)
climbUp(1)
addNote(0.5)
climbUp(1)
addNote(0.5)
climbDown(1)
addNote(0.5)
climbDown(1)
addNote(0.5)
saveTrack("music.wav")
playTrack()
```
Run this code and listen to the result. What do you think the `addNote`,
`climbUp`, and `climbDown` functions do? (Hint: try removing them or
changing the numbers and seeing what happens.) What about `saveTrack` and
`playTrack`?
Click here to show the answer
The code should play a sequence of five notes one after the other, the
first three going up and the last two coming back down. Each
note lasts 1/2 second. It should sound like this:
The `addNote` function adds a note to the audio being produced, and the
argument (the number in parentheses) controls how long the note will be.
Meanwhile, the `climbUp` and `climbDown` functions will change the
current pitch, meaning that notes added afterwards will sound higher or
lower (their arguments determine how much higher or lower). `saveTrack`
produces an audio file containing all of the notes added so far, while
`playTrack` actually plays the track. The argument to `saveTrack`
determines the name of the file that we save.
Now, add the following code to your file, **before** `saveTrack` and
`playTrack` but after the three original notes have been added:
```py
rewind(2.5)
climbDown(8)
addNote(2.5)
```
Discuss with your partner what you think will happen now. What would you
guess `rewind` does?
Click here to show the answer
The code plays the same 5-note sequence, but this time there's a much
lower 2.5-second note playing at the same time. It should sound like this:
The `rewind` function backs up so that the next note begins at an earlier
time, instead of beginning at the end of the previous note. You can use
this to create multiple notes that play simultaneously.
Next, add these lines before your `saveTrack` line:
```py
rewind(2.5)
addBeat(0.5)
addRest(1)
quieter(2)
addBeat(0.5)
addBeat(0.5)
```
Run the file to hear what changes. What do you think `quieter` does? How
about `addBeat` and `addRest`?
Click here to show the answers
The code plays the same 3-note sequence with a fourth lower note, but now
adds some drum beats as well: a half-second beat starting halfway through
the first note, and then two half-second beats during the last note. It
should sound like this:
`addBeat` creates a drum sound, while `addRest` just skips ahead without
any sound playing (you can think of it as "adding silence"). The
`quieter` function makes any notes or beats added afterwards sound
quieter than those before, with the argument controlling how much (each
step is a factor of 2/3). `louder` can be used as the opposite of
`quieter`, although there is a maximum volume level.
Finally, try adding this line of code **to the start** of your file,
before the first `addNote` call:
```py
setPitch(C5)
```
Run the file again, then "comment out" the `setPitch` line by adding a
'#' at the start, and run it to hear the old sound. How does `setPitch`
affect the outcome? You don't need to know what `C5` means at this point,
but you can try any name starting with a letter `A` through `G`
and a number 1 through 8 (warning: pitches with a number higher than
about 6 might be uncomfortably high).
Click here to show the answers
By adding `setPitch`, you change how high or low the starting note is
(that's what "pitch" means in music), and the other notes are based on
that pitch too. Here's what it should sound like using `C5` as the
starting pitch:
It's not necessary for you to understand the pitch system completely, but
in general, a higher number makes a higher-pitched sound, and the letter
`A` through `G` controls exactly how high. So `C5` is a lot higher than
`C4`, while `D4` is just slightly higher than `C4`. For the
curious, we're using [scientific pitch
notation](https://en.wikipedia.org/wiki/Scientific_pitch_notation).
## Function Info
We've been asking you to guess what different functions do, but in fact,
there are two ways that you can get more information about the functions
you will use in this class. First, within Python, there's a function
called `help` which can tell you some details about any other function.
For example, **in the shell**, try running:
```py
help(rewind)
```
This should show you some text describing what the function does. In
addition to the `help` function, our website has [a quick-reference
page](/reference/quickref) that has brief descriptions of each operator
and function you'll need in this class, along with examples of how to use
them. It includes specific sections [for
`turtle`](/reference/quickref#turtle) and [for
`wavesynth`](/reference/quickref#wavesynth) that each have some extra
advice about using those modules.
For `turtle` specifically, you can also find [official
documentation](https://docs.python.org/3.7/library/turtle.html), and we
have [a page that lists the available color names](/reference/colors).
## Next Up
When you're finished with this part of the lab, decide with your partner
whether you're more interested in exploring drawing or music further, and
then move on to either the [Turtle graphics](introTurtles) part or the
[Wavesynth sounds](introWavesynth) part as appropriate. You probably
won't have time to complete either part in lab, but we'll post solutions
to both parts which you can use as references when working on the problem
set for this week.
@include('/labs/lab01/_toc')
@stop