--- title: "Envelope Example" id: 63192 type: "computer_media" slug: "envelope-example" url: "http://localhost/computer_media/envelope-example/" markdown_url: "http://localhost/computer_media/envelope-example.md" published_at: "2026-01-30T21:13:39+00:00" modified_at: "2026-04-03T07:57:55+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2026/01/envelope-example.png" excerpt: "A BASIC music demo that manually fades AY chip volume registers to fake an envelope, playing 18 notes with data-driven tone periods and decay rates." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" model: - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" indiv: - name: "Kristian Boisvert" slug: "boisvert-kristian" taxonomy: "indiv" url: "http://localhost/indiv/boisvert-kristian/" genre: - name: "Sound" slug: "sound" taxonomy: "genre" url: "http://localhost/type/sound/" media_contents: - id: 63048 title: "Byte Power February 1987" type: "computer_media" url: "http://localhost/computer_media/byte-power-february-1987/" media_type: "Program" programmers: - name: "Kristian Boisvert" slug: "boisvert-kristian" taxonomy: "indiv" url: "http://localhost/indiv/boisvert-kristian/" mediadate: "1987" producer_company: - id: 25181 title: "Byte Power" type: "company" url: "http://localhost/company/byte-power/" images: - url: "http://localhost/wp-content/uploads/2026/01/envelope-example.png" article_media: - id: 63078 title: "Better Programming!" type: "article" url: "http://localhost/article/better-programming-6/" media_type_tags: "Sound" --- # Envelope Example This program generates a musical sequence using the AY-3-8910 sound chip’s envelope and tone registers to simulate an envelope-shaped musical phrase. It reads triplets of data (two tone period values and a duration/step control) and writes them to the AY registers via the SOUND keyword, then performs a software volume fade-out loop using registers 8 and 9. The fade-out step size is controlled by the third data value (c), which also governs the initial PAUSE duration, allowing notes of different lengths to have proportionally scaled decays. Eighteen notes are played in total, driven entirely by the DATA statement at line 40. *** ## Program Structure The program is compact at four lines. Line 0 is a `REM` used as a title/copyright banner. The main logic is a single `FOR z=1 TO 18` loop spanning lines 10–30, which reads three values per iteration from the `DATA` block at line 40. Each iteration sounds a note, holds it, then performs a linear volume fade. 1. **Line 10:** Outer loop counter `z` (1–18); reads `a` (tone period channel A), `b` (tone period channel B), `c` (duration/decay rate selector). 2. **Line 20:** Sets AY registers — mixer (R7=60), volumes (R8=15, R9=15), tone periods for channels A and B. Channel A period is `a`, channel B is `a+2` (a slight detune for chorus/thickness). 3. **Line 30:** Holds the note for `5*c` frames, then ramps volume from 14 down to 0 in steps of `(5-c)`, updating R8 and R9 simultaneously each step with a 1-frame pause. ## AY Register Usage | Register | Function | Value / Source | | --- | --- | --- | | R0/R1 | Channel A tone period (fine/coarse) | `a` (fine), `b` (coarse) | | R2/R3 | Channel B tone period (fine/coarse) | `a+2` (fine), `b` (coarse) | | R7 | Mixer control | 60 (decimal) = channels A+B tone enabled, noise off | | R8 | Channel A amplitude | 15 initially, faded to 0 | | R9 | Channel B amplitude | 15 initially, faded to 0 | ## Notable Techniques **Software envelope simulation:** The TS2068’s AY chip has a hardware envelope generator, but this program instead manually decrements volume registers in a `FOR` loop. This gives the author precise control over decay shape and timing, at the cost of BASIC execution overhead. **Detune for richness:** Channel B’s fine-tune period is set to `a+2` rather than `a`, creating a small frequency difference between channels A and B. This produces a slight chorusing or beating effect that thickens the sound. **Data-driven tempo and decay:** The third data value `c` serves double duty — it scales both the sustain pause (`5*c` frames) and the fade step size (`5-c`). When `c` is large, the note sustains longer but fades more slowly; when small, the note is brief with a faster fade. Values observed in the DATA are 1, 2, and 4. **Mixer value 60:** Binary 00111100 — this sets channels A and B as tone-enabled and disables noise on all channels, while channel C is entirely silent/disabled. ## Data Structure The `DATA` statement at line 40 contains 54 values (18 triplets of `a,b,c`). The `a` values observed are 47, 92, 158, 209, and 226, representing fine-tune tone period bytes for different pitches. The `b` values (3, 4, 5) are coarse period bytes selecting the octave range. ## Source Code ``` 0 REM SIMULATED ENVELOPE ©1986 BYTE POWER WRITTEN BY K. BOISVERT 10 FOR z=1 TO 18: READ a,b,c 20 SOUND 7,60;8,15;9,15;0,a;1,b;2,a+2;3,b 30 PAUSE 5*c: FOR f=14 TO 0 STEP -(5-c): SOUND 8,f;9,f: PAUSE 1: NEXT f: NEXT z 40 DATA 92,4,2,209,5,2,92,4,2,209,5,2,92,4,1,158,4,1,47,5,1,158,4,1,92,4,4,226,3,2,209,5,2,226,3,2,209,5,2,226,3,1,209,5,1,47,5,1,209,5,1,92,4,4 ```