deutsch     english    français     Print

 

4.3 RECORDING SOUNDS

 

 

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.

PROGRAMMING CONCEPTS: Blocking and non-blocking function

 

 

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()
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

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

 

Of course, we are often interested in the graphical representation of the recorded sound. You should already know how you can do this with the GPanel.

The adjacent graphics shows the recording of the words
"one two three for five six seven eight nine ten".

 

 


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])
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

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")
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

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

 

1.


Record individual words


2.


Put these words together in a sentence..


3*.


Let the computer speak in single digits a phone number given as a text.