Music#

WebTigerPython plays sounds with Tone.js. Tone.js runs in the main browser thread, because sounds can only be played from there. The Python libraries call it from Pyodide with a callback function (see src/sound/musicStore.ts).

There are three libraries for sound and music.

playTone (gmusic)#

The function playTone from the gmusic library plays single tones or melodies. It is also available in the gturtle library.

from gturtle import *

playTone("C4", 500)                                  # note name, duration in ms
playTone(440, 500)                                   # frequency in Hz
playTone(["C4", "E4", "G4"], 300)                    # a melody
playTone([("C4", 200), ("E4", 200), ("G4", 600)])    # notes with their own durations

Further parameters are block (wait until the tone has been played, default True), instrument and pan (0.0 left, 0.5 center, 1.0 right). setVolume(volume), setInstrument(instrument) and reset() change the defaults. German note names (e.g. "H") are supported as well.

Examples can be found here.

JythonMusic (beta)#

Since version 1.3.0, WebTigerPython contains a port of JythonMusic, a library for music making and creative programming. It is imported with from music import * (or from JythonMusic import *) and provides the known classes such as Note, Phrase, Part, Score and Play, as well as the constants for pitches, durations, instruments and dynamics.

from music import *

phrase = Phrase()
phrase.addNoteList([C4, E4, G4, C5], [QN, QN, QN, HN])
Play.midi(phrase)

Since version 1.8.0, drums can be used as well (e.g. BASS_DRUM, SNARE, CLOSED_HI_HAT on MIDI channel 9).

More information and examples can be found in the JythonMusic documentation.

soundsystem#

The soundsystem library known from TigerJython plays and manipulates sound files and generated waveforms, e.g. with openSoundPlayer(...), play(), stop() and the waveforms SineWave, SquareWave, SawtoothWave and ChirpWave. Recording sound is not supported in WebTigerPython.

soundsystem keeps the API of TigerJython, so that TigerJython programs keep working. In WebTigerPython it is implemented on top of Pygame (pygame-ce) and soundfile, which are installed automatically when a program imports soundsystem.

Sound in games#

GameGrid and Pygame can play sounds as well (since version 1.4.0).