Skip to content

Unify amy_global transfer_* state (AUDIO/FILE transfer, SAMPLE capture, disk-sample scratch space) #1021

Description

@dpwe

amy_global's transfer_flag/transfer_storage/transfer_length_bytes/transfer_stored_bytes/transfer_file_handle fields are overloaded for three unrelated purposes, with no guard against them clobbering each other:

  1. AUDIO/FILE wire transfer (transfer.c) -- incoming byte-stream ingestion via parse_transfer_message, gated by amy_message_is_transfer_chunk() in api.c.
  2. SAMPLE capture (amy.c amy_fill_buffer(), guarded by transfer_flag == AMY_TRANSFER_TYPE_SAMPLE) -- render-loop-driven capture of AMY's own output or audio-in into a PCM preset buffer (amy.start_sample()/stop_sample()). Not wire-message-driven at all.
  3. Disk-sample async load scratch space (pcm.c:367-368, parse.c:505-507) -- zF (disk_sample) stashes midinote/preset into transfer_stored_bytes/transfer_file_handle while the file load is scheduled onto the MicroPython task, before pcm_load_file() reads them back out. amy.h's own field comments admit this ("using this for midi note before file load" / "using this for preset number before file load").

Since transfer_flag is a single scalar and all three reuse the same storage/length/handle fields, nothing stops them from stepping on each other. Concretely: calling amy.start_sample(...) (arms SAMPLE) and then amy.load_sample(...) (arms AUDIO) before the first finishes silently clobbers the sample capture's state -- no error, just corrupted output. Same risk between any pair of the three.

Proposed fix: give each concern its own dedicated state instead of sharing transfer_*:

  • amy_global.sample_capture (storage/length/stored/source) for SAMPLE, independent of transfer state.
  • Two dedicated scratch fields for the disk-sample-load handoff, instead of borrowing transfer_stored_bytes/transfer_file_handle.
  • amy_global.transfer (flag/storage/length/stored/handle) left for AUDIO/FILE only.

Touches amy.h (the struct), transfer.c, amy.c, pcm.c, parse.c. Not urgent -- none of these three are normally invoked concurrently in practice -- but worth fixing before it bites someone

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions