Lab 4
No design recipe steps are required unless the item says "develop", as in "develop a program" or "develop a function".
Also, the rsound library provides several functions like sine-wave and sawtooth-wave that ... are probably not helpful to you. Using these functions will lead you into the world of signals, and we haven’t covered that in lecture at all, at this point. Don’t use them.
For your test cases in this lab, you’re going to be comparing inexact numbers. It’s not a good idea to compare inexact numbers for equality (they’re inexact, after all), so check-expect won’t let you do it. Instead, use the check-within form.
Next, here’s a tiny example of how you might use indexed-signal and signal->rsound to construct a second of noise:
(define (noisefun i) (/ (random 100) 100)) (define noisysound (signal->rsound (* 1 FRAME-RATE) (indexed-signal noisefun)))
Add (require rsound/draw) to the beginning of your file.
Using rs-read/clip, extract a sample from a song of about ten seconds.
Call rs-draw on your sound. Use the + key to zoom in enough so that you can see the details of the sound.
Using functions you know, add that sound to itself, delayed by 10000 frames. Use rs-scale to lower the volume to 0.5 before adding them together, so that it doesn’t distort.
Once again, add that sound to itself, delayed by only 10 frames. How does it sound? Try it with 10, 5, and 1 frames. Write a comment describing how they sound. You might want to listen with headphones to the last few.
Change your language level to "Intermediate". Yay!
Next, we want to reverse this ten-second sound. In order to do this, we need a function that describes what samples are in the new sound. More specifically, we need a function that maps frame number to sample. (Note: we’re going to ignore the right channel, and use only the left channel, for now.) As a warmup, use a comment to write down what the first four samples of the new (reversed) sound should be. In order to figure it out, use rs-ith/left on the old sound.
Next, develop a function called revsnd that accepts a frame index (natural number) and returns the correct sample for the reversed sound. Because of the interface of indexed-signal, below, the revsnd function must have the signature Number -> Number. Be sure to follow the steps of the design recipe.
Using indexed-signal and signal->rsound and the function you just wrote to reverse the sound. Play it, and make sure it’s backward.
Following a similar set of steps, develop a stretchsnd function to stretch the sound out by a factor of 2, duplicating each sample twice. How does it sound? Write your answer in the form of a comment.
Following a similar set of steps, develop a sinesnd function that produces a sine wave of 440 Hz. Once again: don’t use the sine-wave function, please. Just the simple sin function. Play it.
Following a similar set of steps, develop a multsnd function that multiplies your original sampled sound by a sine wave at 20 Hz. It might help to develop two separate functions for this.
Develop a function that constructs a square wave. This should be 1.0 for a period of time, and then 0.0 for the same period of time, then continue to alternate between 1.0 and 0.0. It should complete a single cycle of high/low 250 times per second, making a wave of 250 Hz. Hint: look up the documentation on the modulo function.
The following steps are awesome, but optional.
OPTIONAL: Construct a sawtooth wave. This is a wave that goes from zero up to 1.0 in a smooth sloped line and then jumps back to zero again. Your wave should have frequency 300 Hz. That is, it should jump back to zero 300 times per second.
OPTIONAL: multiply your sampled sound by a sawtooth wave.
OPTIONAL: multiply the sampled sound by the square wave.