INTRODUCTION |
You can also record and save sounds with the sound system. To get started, you have to connect the sound card input with an external source, e.g. a microphone or a playback device. Laptops usually have a built-in microphone. |
SOUND RECORDER |
Before recording, call openMonoRecorder() to prepare the recording system. You have to specify the sampling rate as a parameter. You can start recording with capture(). This function is non-blocking and returns immediately. You will have to call stopCapture() later to end the recording. The recorded sound samples are copied into a list that you can get with getCapturedSound(). Here you make a recording of 5 seconds duration and then play the sound. from soundsystem import * openMonoRecorder(22050) print("Recording...") capture() delay(5000) stopCapture() print("Stopped") sound = getCapturedSound() openMonoPlayer(sound, 22050) play() |
MEMO |
A command like capture(),which triggers an action and immediately returns, is also called a non-blocking function. With such functions you are able to control tasks from your ongoing program, while these tasks are executed in the background. For instance, you can abort them. |
ILLUSTRATING RECORDED SOUND |
from soundsystem import * openMonoRecorder(22050) print("Recording...") capture() delay(5000) stopCapture() print("Stopped") sound = getCapturedSound() from gpanel import * makeGPanel(0, len(sound), -33000, 33000) for i in range(len(sound)): draw(i, sound[i])
|
MEMO |
You can obtain the number of samples from the length of the sound list. For the graphical representation, simply use a for structure. Play around for a while with different recorded sounds and think about whether you understand the sound curve. |
SAVING WAV FILES |
You can also save the recorded sound as a WAV file with writeWavFile(). from soundsystem import * openMonoRecorder(22050) print("Recording...") capture() delay(5000) stopCapture() print("Stopped") sound = getCapturedSound() writeWavFile(sound, "mysound.wav") |
MEMO |
After saving, you can listen to the sound file with either Python or with any sound player that is installed on your computer. |
EXERCISES |
|