CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyglet

Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback

Overview
Eval results
Files

audio-video.mddocs/

Audio and Video

Audio/video playback, 3D audio positioning, and media control.

Quick Reference

# Load
sound = pyglet.media.load('file.wav', streaming=False)  # Small files
music = pyglet.media.load('file.mp3', streaming=True)   # Large files

# Play
player = pyglet.media.Player()
player.queue(sound)
player.play()

# Control
player.pause()
player.seek(10.0)  # Seek to 10 seconds
player.volume = 0.5  # 0.0-1.0
player.loop = True

Media Loading

def pyglet.media.load(filename: str, file=None, streaming=True) -> Source:
    """
    Args:
        filename: File path
        file: File-like object (optional)
        streaming: True = stream from disk, False = load into memory

    Returns:
        Source: Media source (audio or video)
    """

Player Control

class pyglet.media.Player:
    """Media player"""
    __init__()

    # Properties
    playing: bool  # Read-only
    time: float  # Current playback position (seconds)
    volume: float  # 0.0-1.0
    min_distance, max_distance: float  # 3D audio
    position: tuple  # (x, y, z) for 3D audio
    pitch: float  # Playback speed (1.0 = normal)
    cone_orientation, cone_inner_angle, cone_outer_angle: float  # 3D audio cone
    source: Source  # Current source (read-only)

    # Methods
    def queue(source: Source)  # Add to queue
    def play()
    def pause()
    def stop()  # Stop and clear queue
    def next_source()  # Skip to next in queue
    def seek(time: float)  # Seek to position

    # Events
    def on_player_eos()  # End of stream
    def on_player_next_source()  # Moving to next source

Media Sources

class pyglet.media.Source:
    """Abstract media source"""
    duration: float  # Length in seconds
    video_format: VideoFormat | None
    audio_format: AudioFormat | None

    def play() -> Player  # Convenience: create player and play
    def get_queue_source() -> Source  # For queueing

# Static source (fully loaded)
class pyglet.media.StaticSource(Source):
    """Source loaded into memory"""

# Streaming source
class pyglet.media.StreamingSource(Source):
    """Source streamed from disk"""

Audio Synthesis

# Generate tones
source = pyglet.media.synthesis.Sine(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.Square(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.Sawtooth(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.Triangle(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.WhiteNoise(duration, sample_rate=44100)

# Silence
source = pyglet.media.synthesis.Silence(duration, sample_rate=44100)

# Play
source.play()

3D Audio

# Set listener position (camera)
listener = pyglet.media.get_audio_driver().get_listener()
listener.position = (0, 0, 0)
listener.forward_orientation = (0, 0, -1)
listener.up_orientation = (0, 1, 0)

# Set source position
player.position = (10, 0, 0)  # 10 units to the right
player.min_distance = 1.0  # Full volume within this distance
player.max_distance = 100.0  # Silent beyond this distance

Examples

Background Music

music = pyglet.media.load('background.mp3', streaming=True)
player = pyglet.media.Player()
player.queue(music)
player.loop = True
player.play()

Sound Effects

jump_sound = pyglet.media.load('jump.wav', streaming=False)

@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.SPACE:
        jump_sound.play()  # Convenience method

Music Playlist

player = pyglet.media.Player()
songs = [
    pyglet.media.load('song1.mp3', streaming=True),
    pyglet.media.load('song2.mp3', streaming=True),
    pyglet.media.load('song3.mp3', streaming=True),
]
for song in songs:
    player.queue(song)
player.play()

@player.event
def on_player_eos():
    print("Playlist finished")

Volume Control

master_volume = 0.7
music_player.volume = master_volume * 0.5  # Background music quieter
sfx_player.volume = master_volume * 1.0  # SFX at full

Pause/Resume

paused = False

@window.event
def on_key_press(symbol, modifiers):
    global paused
    if symbol == pyglet.window.key.P:
        if paused:
            player.play()
        else:
            player.pause()
        paused = not paused

Video Playback

video = pyglet.media.load('movie.mp4')
player = pyglet.media.Player()
player.queue(video)
player.play()

# Get video texture
@window.event
def on_draw():
    window.clear()
    if player.source and player.source.video_format:
        texture = player.get_texture()
        if texture:
            texture.blit(0, 0, width=window.width, height=window.height)

Audio Drivers

# Get available drivers
pyglet.media.get_audio_driver(name=None)  # None = default

# Set driver order (in pyglet.options before importing)
pyglet.options['audio'] = ('openal', 'pulse', 'xaudio2', 'directsound', 'silent')

Performance Tips

  1. Streaming for large files: Use streaming=True for music
  2. Static for small files: Use streaming=False for SFX
  3. Preload SFX: Load sound effects during init
  4. Limit concurrent players: Too many causes audio issues
  5. Queue sources: More efficient than recreating players

Common Issues

  1. No audio: Check pyglet.options['audio'] driver order
  2. Choppy streaming: File I/O bottleneck, use SSD or reduce count
  3. Video sync: Video playback is experimental, may have issues
  4. Seek limitations: Some formats don't support seeking
  5. Format support: Depends on available codecs (FFmpeg recommended)

Install with Tessl CLI

npx tessl i tessl/pypi-pyglet

docs

3d-models.md

app-clock.md

audio-video.md

graphics-rendering.md

gui.md

images-textures.md

index.md

input-devices.md

math.md

opengl.md

resource-management.md

sprites-shapes.md

text-rendering.md

windowing.md

tile.json