Lab 1

Matlab

If you are completely new to Matlab, there are a couple tutorials on the resources page that will will help you get acquainted with this powerful tool. Even if you have used Matlab, if you haven’t used it for audio-related tasks the tutorials can be useful.

Part 1: I Saw the Sine

Create the Matlab function:

output = sineTone(fs, frequency, duration)

This function will generate an audio-rate sampled sine wave (sinusoid). Save the function in your working folder as sineTone.m The parameters passed are:

  • fs - the sampling rate, in Hertz
  • frequency - the frequency of the sine wave, also in Hertz
  • duration - the length of the sine wave, in seconds, when played at the sampling rate

The output will be a one-dimensional array of float values between -1.0 and 1.0 (It’s actually a good idea to keep audio samples slightly less than +/- 1.0, so +/- 0.99 might be better. You could alternatively add an argument to the function for amplitude).

Other things to know:

  • A sinusoid can be expressed by the equation: $y = \sin (2 \pi f t)$, where $f$ is frequency and $t$ is the current time.
  • Since we are using digital signals, we can define time $t$ as $\frac{n}{f_s}$, where $f_s$ is our sampling rate and $n$ is the sample number.
  • How many samples long should your output vector be given the duration and sampling rate fs?

Part 2: Ramping Up (and Down)

Create two functions:

  1. output = rampUp(fs, duration, input)
  2. output = rampDown(fs, duration, input)

These functions should be able to take the output of your sineTone function (or the function itself) as the input argument. rampUp should fade in the beginning duration seconds of the input, and rampDown should fade out the last duration seconds of the input.

Parameters:

  1. input - the input to which to apply the ramp. Experienced Matlab users: can you generalize your function to handle any number of channels (i.e. a number of channels by number of samples matrix input)?
  2. fs - sampling rate, should be the same sampling rate you used to generate the input.
  3. duration - the length in seconds of the ramp.

Notes:

  • These functions are creating an envelope: a series of numbers from 0.0 to 1.0 that will scale our signal by specific amounts at specific times. Note here since we’re dealing with scaling, we do want to go all the way up to and including 1.0 as a value.
  • How long should the ramp be given the duration and sampling rate fs?
  • Lookup the Matlab function linspace(a,b,n) (for those of you new to Matlab, type help linspace in the console) - it creates a n-length array of numbers between values a and b. This function might be particularly useful in creating your rampUp and rampDown functions…
  • Indexing in Matlab uses parentheses, not square brackets, and arrays are 1-indexed not 0-indexed. So indexing the first n samples in a Matlab array looks like: array(1:n). You can use the end keyword to denote the end of an array, so indexing the last n samples in a Matlab array looks like: array(end-n+1:end).
  • Matlab was originally built for matrix math, so the * operator defaults to the matrix multiplication operator. To do element-wise multiplication, use .*.

Save your functions as (you guessed it) rampUp.m and rampDown.m.

Part 3: What’s it Sound Like?

Use your newly created functions to create some smoothly fading in and out sine tones! In a separate script, try making a 5 second sine tone at a frequency of 200 Hz with 1 second fade in and out times. Use 44100 Hz as the sampling rate (48000 Hz is the other common audio sampling rate). You can nest your functions, so you should be able to create your sine tone in a single line with something like:
s = rampDown(44100, 1, rampUp(44100, 1, sineTone(44100, 200, 5)));
(Note the ; at the end suppresses text output in Matlab, so unless you want hundreds of thousands of sample values printing in the console, you’ll want to use it).

To listen to your sine tone, use the sound() function (look it up!). You’ll want to pass in the sampling rate you used to generate your sine tone along with your generated sample vector output.
Try different frequency and duration combos to your hearts content! If you’re feeling adventurous, try experimenting with logspace() instead of linspace() in your ramp functions and see what happens to the fades. Just make sure you change back to linspace() before submitting.

Part 4: What’s it Look Like?

Plot the resulting output from your functions using Matlab’s plot() function. 5 seconds of 44100 Hz sampling rate is way too many samples to see clearly what’s happening, so let’s use 0.1 seconds as the sineTone duration, 0.01 seconds rampUp duration and 0.04 seconds rampDown duration. Stick with 200 Hz as the sineTone frequency. Once you’ve got the signal vector, plot it with plot() - does it look how you’d expect? Save your plot to a .pdf and submit it with your functions.

Matlab Deliverables:

  1. Your sineTone.m file from Part 1
  2. Your rampUp.m file from Part 2
  3. Your rampDown.m file from Part 2
  4. Your plot as .pdf from Part 4

Note you do not need to submit your script from Part 3


Game Development: ‘Analog Gaming’: Sonic Board Game

In a group of 3-4, create a first prototype (low resolution paper prototype) of a board game. You must go through each step of the design process and finally hold a playtest. Pay attention to the mechanics you are employing in your game. What can a player do at each point in the game? Are each action and the overall gameplay intuitive? Most importantly, what is the overall goal the player is trying to achieve?

The Design Process

  • Inspiration
  • Design Loop
    • State Problem
    • Brainstorm
    • Prototype
    • Assess
  • Implement
  • Playtest
  • Revisit Design Loop as Necessary

We would like a main part of the game to use sound in some way. Think about how you can use sound to either affect the game (sound as a controller) or as a part of how the game is played (sound as a mechanic). Other than that, your board game can take any form and shape you come up with!

Deliverables:

In addition to the usual Demo and Playtest video links (submit on Canvas), you will submit the physical board game and a set of rules that clearly state the game’s goal and how to play. Bring these to next Tuesday’s lab (April 10th), where we will play your games during Gameplay.

Lecture

Fridays, 10:30 AM - 12:20 PM
CCRMA Classroom (Knoll 217)

Lab

Tuesdays, 6:00 - 7:50 PM
CCRMA Classroom (Knoll 217)

Office Hours

Monday 6-8 PM
Friday 1-2 PM
CCRMA Ballroom

Questions

Post on Piazza

Instructors

Poppy Crum
Instructor
poppy(at)stanford(dot)edu

Mark Hertensteiner
Teaching Assistant
hert(at)stanford(dot)edu