Lab 6
1 Exercises
HTDP/2e Exercises 20, 21.
2 Help Desk
In a DrRacket window, type string-length. Click to put the cursor on the word, then use F1 or right-click to open Help Desk, a browser-based system of documentation. There’s a lot of documentation there.
3 Stepper
Here’s a program:
;; returns true for strings shorter than ;; five chars (define (short? str) (< (string-length str) 5)) ;; truncates a string to the first four chars ;; unless it's already short (define (maybe-truncate str) (cond [(short? str) str] [else (substring str 0 4)])) (maybe-truncate "This is a long sentence.") (maybe-truncate "Food")
Run this program in the stepper. Make sure that every step makes sense to you, and that you could reproduce these steps on a quiz.
4 Sound
Five functions to develop.
For these exercises, you’ll have to use the "Language defined in source" language level (in order for rsound-frames to work)
Develop the stutter function, that takes a sound, and returns a sounsd that repeats the first 8th of the sound 8 times. Use the rsound-frames function to determine how long a sound is. Return the sound, rather than playing it.
Develop the chord function, that takes three pitches and produces a sound containing those tones, using make-tone and overlay.
Here’s the contract for make-tone:
(make-tone pitch volume duration) → rsound? pitch : number? volume : number? duration : exact-integer? given a pitch in Hz, a volume between 0.0 and 1.0, and a duration in frames, return the rsound consisting of a pure sine wave tone using the specified parameters.classify : takes two pitches, returns "fifth" if they’re in a ratio of 3:2, "third" if they’re in a ratio of 5:4 or 6:5, or "unknown" otherwise.
Develop the function noisy, that takes an integer n and returns a random number between 0 and 100/200. Use the random function with the argument 100, and divide the result by 200. To look up documentation on this, type ’random’, put the cursor on it, and hit the F1 key.
call (mono-signal->rsound 44100 noisy) to turn this squarewave function into a sound. How does it sound?
Change the noisy function so that it returns a number between -50/200 and 50/200. How does it sound now?
Develop the function squarewave, that takes an integer n and returns 10,000 if the number is in the interval [0,100), zero if the number is in the interval [100,200), 10,000 if the number is in the interval [200,300), and so forth. You will need the modulo function for this; you can look it up using the F1 key again.
Use mono-signal->rsound as before to play this one. How does it sound? Does it sound different from the result of make-tone ?