Skip to content

Add apu_play_tone helper for simple sound effects without register math #426

@jonathanpeppers

Description

@jonathanpeppers

Playing a tone on the APU currently requires manually poking 3-4 registers with magic values:

// Today in aputest: cryptic register writes
poke(APU_PULSE1_CTRL, 0x4A);    // duty=01, volume=10
poke(APU_PULSE1_SWEEP, 0x00);
poke(APU_PULSE1_TIMER_LO, 0x80);
poke(APU_PULSE1_TIMER_HI, 0xF9);

This requires understanding the APU register bit layout, timer period calculation, and duty cycle encoding.

Proposed API:

// Play a tone on a pulse channel: channel (0-1), period, duty (0-3), volume (0-15)
apu_play_tone(0, 0x0180, 1, 10);

// Stop a channel
apu_stop(0);

The subroutine packs the duty/volume into the control register byte and splits the period into timer lo/hi writes. This makes simple sound effects accessible without register-level NES knowledge.

For more advanced use cases (sweep, length counter, envelope), developers can still use poke() directly.

Once implemented, the aputest sample should be updated to demonstrate both the helper and direct register access.

Related to #30

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions