INTRODUCTION |
In order to process a sound signal in the computer, it must first be digitized. To do this, one samples it at equidistant time steps and converts the value of the signal to a number at every sampling time using an analog-to-digital converter. The sound signal then yields a sequence of numbers that can be stored and processed in the computer. The sampling frequency (or sampling rate) is understood as the number of samples per second. This is standardized for the WAV audio format and can have the following values: 8000, 11025, 16000, 22050 and 44100 Hertz. The higher the sampling frequency, the more precisely the sound can be restored by a digital-to-analog conversion. The value range of the samples is also important for the quality. In the TigerJython sound library, values are always stored as integers in a list in the 16-bit range (-32768 and 32767). We also need to distinguish whether we are dealing with a monaural or a binaural sound. Depending on this, one or two channels are used. In the case of two channels (stereo), the values for the left and the right channel are stored as consecutive numbers. In this chapter you will need a computer with a sound card, the ability to listen to sound through a speaker or headphones, and a microphone. |
LISTENING TO SOUND |
Find a fun sound clip in WAV format that has a short duration (around 2 to 5 seconds long). You can look on the Internet. Copy the sound file under the name mysound.wav in the same directory as your program. First, import all of the functions of the sound library. Next, copy the sound samples into the list samplesand write the information of the sound file into the console window, for you need to know the sampling rate. In the example shown here, its value is 22050 Hz. With openMonoPlayer you have the ability to play back the sound. If you enter the wrong sampling rate, the sound will be played with a different speed, hence with other frequencies. from soundsystem import * samples = getWavMono("mysound.wav") print(getWavInfo("mysound.wav")) openMonoPlayer(samples, 22050) play() |
MEMO |
The function getWavMono() provides the sound samples in a Python list. Each value is an integer in the range between -32768 and 32767. The function openMonoPlayer() provides a sound player so that the sound can be played with play(). Since lists can only have a certain maximum size that depends on the memory capacity of your computer, only relatively short sound clips can be read with getWavMono(). |
SOUND WAVE |
from soundsystem import * samples = getWavMono("mysound.wav") print(getWavInfo("mysound.wav")) openMonoPlayer(samples, 44100) play() from gpanel import * makeGPanel(0, len(samples), -33000, 33000) for i in range(len(samples)): draw(i, samples[i]) |
MEMO |
We have to choose the coordinate system of the GPanel conveniently. The values displayed in the x-direction are between 0 and the number of sampling values, which is equal to the length of the sample list. The values of the y-direction are between -32768 and 32767. This is why we use a range of +-33000. |
THERE IS AN EASIER WAY |
If you just want to play a sound file, you only need three lines of code. You can even play long sounds, for example your favorite songs. from soundsystem import * openSoundPlayer("myfavoritesong.wav") play() |
MEMO |
You can also use some sound clips that come in the distribution of TigerJython. Choose any of the following file names:
(The list is constantly updated. If you have a WAV sound file with the same name in your own subdirectory wav, that one will be used.) The sound player knows many control commands, just as a professional music player does. You can, for example, stop the song with pause() and then continue playing the song at the same spot using play(). The duration of the sound is not limited in these functions because the sound is only read and played back in small packets (streaming player).
|
PLAYING MP3 SOUND FILES |
To play sounds in the MP3 format, you will need additional library files which you can download and unzip separately here. Create the subdirectory Lib (if it does not already exist) in the directory where tigerjython2.jar is located, and then copy the unzipped files into it. Instead of using openSoundPlayer(), openMonoPlayer(), and openStereoPlayer() for MP3 files use openSoundPlayerMP3(), openMonoPlayerMP3() and openStereoPlayerMP3() and indicate the path to the sound file. To play, use the same functions mentioned above. from soundsystem import * openSoundPlayerMP3("song.mp3") play() |
MEMO |
To play MP3 sound files, you will need additional JAR library files that need to be located in the directory Lib of the home directory to tigerjython2.jar. |
EXERCISES |
|