2.6 Questions for the reader#
Exercise 5
Loop vs vectorized. Synthesize 5 seconds of a 440 Hz sine in two ways: once with a plain Python for loop calling math.sin, once vectorized with NumPy. Time them both with time.perf_counter() and report the speedup.
Exercise 6
Stereo broadcasting. Using broadcasting with np.newaxis, synthesize a 1-second stereo signal where the left channel is a 220 Hz sine and the right is a 330 Hz sine. Use the samples to create a stero pq.Audio, then downmix to mono by calling as_mono. Listen to both stereo and mono using pq.play; describe what changes in a sentence or two.
Exercise 7
Headphone safety. Write a small synthesis program that produces an intentionally unsafe pq.Audio as output (say, a sine multiplied by 10). Without running it through headphones, inspect the array values (e.g. audio.peak_amplitude) to confirm they exceed full scale. What does audio.write("path.wav") do with samples outside \([-1, 1]\)? (Read the docstring for Audio.write, or try it and inspect the output.)