Metadata-Version: 2.4
Name: pysidwizard
Version: 0.2.0
Summary: Pure-Python reader, writer, and player for SID-Wizard SWM modules
Author: Josh Bailey
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
Project-URL: Homepage, https://github.com/anarkiwi/pysidwizard
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Sound/Audio
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Requires-Dist: black>=24.0; extra == "dev"
Requires-Dist: sidwizard-driver>=0.4.2; extra == "dev"
Provides-Extra: integration
Requires-Dist: pytest>=7.4; extra == "integration"
Requires-Dist: sidwizard-driver>=0.4.2; extra == "integration"
Dynamic: license-file

# pysidwizard

Pure-Python reader, writer, and **bit-exact player** for SID-Wizard
SWM modules. No native dependencies.

[SID-Wizard](https://csdb.dk/release/?id=258573) is a Commodore 64
music tracker by Hermit (Mihaly Horvath). pysidwizard implements its
SWM file format and player IRQ from first principles.

The player is verified against a live capture of SID-Wizard running
inside `asid-vice`: every frame × every SID register, on every PR.
See [Player correctness](#player-correctness) below.

## Install

```bash
pip install pysidwizard
```

## Read an SWM

```python
from pysidwizard import read_swm

swm = read_swm("tune.swm")
print(swm.author_str(), swm.frame_speed, swm.highlight)

# Pattern rows are typed; no byte poking required.
for row in swm.patterns[0].rows[:4]:
    print(row)

# Sequences (one per SID channel) are lists of typed commands.
for cmd in swm.sequences[0][:6]:
    print(cmd)

# Instruments expose fixed fields by name and tables as bytes.
ins = swm.instruments[0]
print(ins.name_str(), ins.attack, ins.decay, ins.sustain, ins.release)
print(ins.wf_table.hex(), ins.pw_table.hex(), ins.filter_table.hex())
```

`read_swm` auto-detects both bare SWM payloads and PRG-wrapped files
(2-byte little-endian load address, then the `SWM1` magic at offset
2). The writer emits the PRG wrapper by default; set
`swm.load_address = None` for a bare payload.

## Build an SWM from scratch

```python
from pysidwizard import (
    End,
    Instrument,
    Pattern,
    PlayPattern,
    Row,
    SWMFile,
    Waveform,
    build_swm,
    straight_tempo,
    write_swm,
)
from pysidwizard.constants import GATE_OFF_FX, SWM_C5_NOTE

instrument = Instrument(
    name=b"LEAD    ",
    control=0x1A,                 # hard-restart timer on, tied PW/CTF off
    hr_attack=0, hr_decay=0xF,
    hr_sustain=0xF, hr_release=0,
    attack=0, decay=0,
    sustain=0xF, release=0,
    default_chord=1,
    first_waveform=Waveform.PULSE,
)

pattern = Pattern(
    rows=[
        Row(note=SWM_C5_NOTE, instrument=1),
        Row(), Row(), Row(), Row(), Row(), Row(),
        Row(note=GATE_OFF_FX),
    ],
    length=8,
)

swm = SWMFile(
    author=b"PYSIDWIZARD DEMO",
    default_pattern_length=8,
    sequences=[[PlayPattern(1), End()]] * 3,
    patterns=[pattern],
    instruments=[instrument],
    subtune_tempos=[(straight_tempo(6), straight_tempo(3))],
)

write_swm(swm, "demo.swm")               # ready to load in SID-Wizard 1.x
assert build_swm(read_swm("demo.swm")) == build_swm(swm)  # byte-exact roundtrip
```

The writer rejects modules that reference patterns or instruments
that don't exist, or sequences missing an `End()` / `Loop()`
terminator. NOP packing, table terminators, and on-disk pointer
arithmetic are handled automatically — you describe content, not
bytes.

### Sequence command types

| Command                | Meaning                                                     |
| ---------------------- | ----------------------------------------------------------- |
| `PlayPattern(n)`       | Play pattern *n* (1-based; `0` is the reserved empty slot). |
| `Transpose(semitones)` | Per-channel transpose, -16..+15 semitones.                  |
| `TempoOverride(value)` | Switch to row-delay `value` until the next override.        |
| `End()`                | Terminate the sequence without looping.                     |
| `Loop(position)`       | Terminate and jump back to `position` within the sequence.  |
| `RawSequenceByte(b)`   | Opaque single byte preserved for byte-exact round-trip.     |

### Row fields

Every column of a `Row` is `None` by default and contributes nothing.

- `note`: a pitch (`0..0x5F`) or a note-column effect:

  | Code   | Constant         | Meaning                                  |
  |--------|------------------|------------------------------------------|
  | `0x78` | `PORTAMENTO_FX`  | Pre-arm tone portamento for the next row |
  | `0x79` | `SYNC_ON_FX`     | Enable hard sync                         |
  | `0x7A` | `SYNC_OFF_FX`    | Disable hard sync                        |
  | `0x7B` | `RING_ON_FX`     | Enable ring modulation                   |
  | `0x7C` | `RING_OFF_FX`    | Disable ring modulation                  |
  | `0x7D` | `GATE_ON_FX`     | Force gate on                            |
  | `0x7E` | `GATE_OFF_FX`    | Force gate off                           |

- `instrument`: a `1..0x3E` instrument index, or an
  instrument-column effect — value `< 0x40` selects an instrument,
  `>= 0x40` runs a small effect. The same small-effect dispatch is
  available in the `fx` column (any value `>= 0x20`):

  | Range          | Effect                                                      |
  |----------------|-------------------------------------------------------------|
  | `0x20..0x2F`   | Set attack nibble of post-HR ADSR                           |
  | `0x30..0x3F`   | Set decay nibble of post-HR ADSR; `0x3F` is *legato*        |
  | `0x40..0x4F`   | Set waveform nibble (not yet modelled)                      |
  | `0x50..0x5F`   | Set sustain ("note volume")                                 |
  | `0x60..0x6F`   | Set release                                                 |
  | `0x70..0x7F`   | Set chord (`0x7n` → chord `n`, looked up in `chord_table`)  |

- `fx`: a `1..0xFF` effect code. Values `< 0x20` are *big-FX* and
  require an `fx_value`:

  | Code   | Big-FX                                                                |
  |--------|-----------------------------------------------------------------------|
  | `0x01` | Pitch slide up (value = step per frame)                               |
  | `0x02` | Pitch slide down                                                      |
  | `0x03` | Tone portamento to the row's note (value = slide speed)               |
  | `0x08` | Set vibrato amp + freq                                                |
  | `0x0B` | Jump to filter-table row (value = row index)                          |
  | `0x10` | Override row delay (= live tempo change)                              |

  Values `>= 0x20` are small-FX, same as the table above.

## Play an SWM

```python
from pysidwizard import SWMPlayer, read_swm

swm = read_swm("tune.swm")
player = SWMPlayer(swm)

# Each play_frame() returns the SID register writes the real
# SID-Wizard player would emit on the C64 this frame, in the same
# order (channel 3 -> 2 -> 1, then global filter + volume).
for _ in range(100):
    for reg, value in player.play_frame():
        print(f"${reg:04X} = ${value & 0xFF:02X}")
```

### Render to WAV

```python
from pysidwizard import render_wav

render_wav("tune.swm", "tune.wav", seconds=60.0, model_name="MOS8580")
```

Or from the command line:

```bash
python -m pysidwizard.player tune.swm tune.wav --seconds 60 --model MOS8580
```

`render_wav` drives [`pyresidfp`](https://pypi.org/project/pyresidfp/)
under the hood (install with `pip install pyresidfp`).

### Iterate raw writes

For SID-state inspection or feeding a different SID emulator:

```python
from pysidwizard import iter_writes

for frame, reg, value in iter_writes(swm, n_frames=1500):
    ...
```

`iter_writes` deduplicates consecutive identical writes to the same
register by default — matching the schema `sidwizard-driver` produces
for ground-truth captures.

## SWM at a glance

An SWM module is a tracker module split into typed pieces. You don't
need to know the byte layout to use this library, but a high-level
picture of the moving parts is useful when reading or writing
patterns:

**Orderlist (`sequences`).** Three independent sequences, one per
SID channel. Each sequence is a list of typed commands
(`PlayPattern`, `Transpose`, `TempoOverride`, `End`, `Loop`).
Channels advance independently; if v0 is playing a long pattern and
v1 finishes early, v1 just stays silent until its sequence loops.

**Patterns (`patterns`).** A list of rows; each row may set
`note` / `instrument` / `fx` / `fx_value` columns. The player runs
one row every `tempo` frames (or alternating funktempo left/right
values), or whatever the current `TempoOverride` says.

**Instruments (`instruments`).** Each instrument is a fixed
ADSR / vibrato / hard-restart header plus three per-instrument
"tables" that the player walks once per frame while the instrument
is active:

- **Waveform / arp table** (`wf_table`). Three-byte rows
  `(waveform, arp_pitch, detune)`. Drives the SID `CTRL` register
  and the per-note pitch offset. Arp byte semantics:
  - `$00..$7E`: relative pitch *up* — add this many semitones to
    the note.
  - `$7F`: take the next pitch from the active chord table.
  - `$80`: NOP — keep the previous pitch.
  - `$81..$DF`: absolute pitch — the low 7 bits become the
    discrete pitch.
  - `$E0..$FF`: relative pitch *down*.
  - `$FE` (in the waveform column): jump to the row whose offset
    is in the next byte.
  - `$FF`: end of table; the walker freezes here.
- **Pulse-width table** (`pw_table`). Three-byte rows. Either
  *set* mode (high byte `$80..$FD`, plus a low byte -> SID pulse-
  width register), or *sweep* mode (low cycle-count byte, signed
  delta, key-track byte).
- **Filter table** (`filter_table`). Three-byte rows controlling
  the global SID filter while this instrument owns it (only one
  voice at a time controls the filter). *Set* mode encodes the
  band switches (LP/BP/HP) + resonance + cutoff hi; *sweep* mode
  drifts the cutoff with a signed delta over a cycle count.

**Chord table (`chord_table`).** A flat byte array indexed by
*chord number*. When the waveform table's arp byte is `$7F`, the
next chord pitch is looked up here. Chords loop until the
instrument's waveform table moves past the chord-trigger row.

Each table terminates on `$FF`. Jumps inside a table use `$FE`
followed by an offset. pysidwizard handles all of this for you
when serialising — pass the table *contents* only.

## Player correctness

The four reference tunes in `tests/fixtures/` (flashitback,
bronkosaurus, euphoria, rain8580) collectively exercise the full
1-SID feature surface: pitched notes, gate-off note-FX, chord
tables, multispeed (`frame_speed=2`), funktempo, BIGFX portamento,
vibrato, filter walking, instrument inheritance across F1, the
WRPITCH detune-with-carry chain.

For each tune, every frame's full SID-register state agrees
byte-for-byte with the reference captured from real SID-Wizard
running inside [`asid-vice`](https://github.com/anarkiwi/asid-vice)
via [`sidwizard-driver`](https://github.com/anarkiwi/sidwizard-driver).
`tests/test_player_reference.py` runs that comparison on every
push.

The integration suite in `tests/integration/` goes one step
further: every PR re-derives a fresh reference CSV from the real
SID-Wizard binary and asserts pysidwizard still matches. If the
binary changes, the integration suite fails before merge.

Out of scope: multi-SID, SFX, slowdown, and non-440 Hz tuning
tables.

## Tests

Unit tests (fast, no Docker):

```bash
pip install -e ".[dev]"
python -m pytest
```

Integration tests (slow, requires Docker; pulls
`anarkiwi/headlessvice`):

```bash
pip install -e ".[integration]"
python -m pytest -m integration tests/integration/
```

The four SWM test tunes are **not** tracked in this repo — they're
SID-Wizard binary artifacts. `tests/_swm_cache.py` fetches them on
demand from the SID-Wizard 1.94 source tarball via
`sidwizard-driver`'s cache and SHA-256 verifies each one.

## Continuous integration

| Workflow                             | What it runs                                                              |
| ------------------------------------ | ------------------------------------------------------------------------- |
| `.github/workflows/test.yml`         | `ruff` + `black` + `pytest` (matrix: Python 3.10-3.13 on Linux + 3.12 on macOS/Windows) |
| `.github/workflows/integration.yml`  | Fresh capture from real SID-Wizard inside `asid-vice`; asserts pysidwizard matches |
| `.github/workflows/publish.yml`      | Builds + uploads to PyPI via trusted publishing on release                |

The integration workflow runs on every push, every PR, and weekly
on cron — so SID-Wizard binary / `sidwizard-driver` / `headlessvice`
drift is caught even when no PR has landed.

## License

Apache 2.0 — see [`LICENSE`](LICENSE).
