4.1 PLAYING BACK SOUND

 

 

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.

PROGRAMMING CONCEPTS: Sound digitization, audio signal, sample, sampling rate

 

 

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

 

 

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

 

It is interesting to also represent sound samples graphically. To do this, simply use a GPanel window and run through the list in a for loop.

 


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

 

 

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

 

 

MEMO

 

You can also use some sound clips that come in the distribution of TigerJython. Choose any of the following file names:

Sound File Description
wav/bird.wav chirping bird
wav/boing.wav boing
wav/cat.wav meowing cat
wav/click.wav click
wav/dummy.wav empty sound
wav/explode.wav explosion
wav/frog.wav croaking frog
wav/mmm.wav eating sound
wav/notify.wav notification sound
wav/ping.wav ping sound

(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).

play() plays back a sound from the current position and returns immediately
blockingPlay() plays back a sound from the current position and waits until it is finished playing
advanceFrames(n) moves forward from the current position by the given number of samples
advanceTime(t) moves forward from the current position by the specified time (ms)
getCurrentPos() returns the current position
getCurrentTime() returns the current playing time
pause() pauses playback. play() will start it again
rewindFrames(n) moves backwards from the current position by the given number of samples
rewindTime(t) moves backwards from the current position by the specified time
stop() stops playback. The playback position is set to the beginning
setVolume(v) adjusts the volume (value between 0 and 1000)

 

 

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

 

 

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

 

1.


Explain why the tone frequencies are changed if you change the sampling rate during playback


2.


Show a sound wave in the GPanel representing a short range of 0.1 seconds starting at the 1 second mark. Explain the image.


3.


Create a sound player with a GPanel where the following commands can be executed using the keyboard:

Key Action
Cursor up play
Cursor down pause
Cursor left rewind by 10 s
Cursor right advance by 10 s
Buchstabe s stop

Write the command list as text in the window. With each key press, the action should be written out in the title line.