Playing audio
To begin with, we have added 24 sound samples in .wav
format in a folder named sounds
in this chapter's code folder. These audio files correspond to the 24 notes on our keyboard. The audio files are named according to the note name it represents.
In order to keep the audio processing separate from the GUI code, we create a new file called audio.py
(7.03
). The code is defined as follows:
import simpleaudio as sa from _thread import start_new_thread import time def play_note(note_name): wave_obj = sa.WaveObject.from_wave_file('sounds/' + note_name + '.wav') wave_obj.play() def play_scale(scale): for note in scale: play_note(note) time.sleep(0.5) def play_scale_in_new_thread(scale): start_new_thread(play_scale,(scale,)) def play_chord(scale): for note in scale: play_note(note) def play_chord_in_new_thread(chord): start_new_thread(play_chord,(chord,))
The code description is as follows:
- The
play_note
method follows the API provided bysimpleaudio
to play an...