MUZAK58

From SizeCoding
Revision as of 13:33, 13 February 2024 by WiRe (talk | contribs) (Created page with "[https://www.pouet.net/prod.php?which=96071 MUZAK58] was created by wiRe/Napalm and is 58 bytes in size. It achieved 4th place at the Lovebyte 2024 demoparty and is a pure tec...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

MUZAK58 was created by wiRe/Napalm and is 58 bytes in size. It achieved 4th place at the Lovebyte 2024 demoparty and is a pure tech demo of a size-optimized bytebeat player for MSDOS and COVOX LPT-DAC, as also used in other sizecoding releases by wiRe. This page will describe how this player works and how it can be adopted for other releases. Feel free to reuse those ideas and techniques in your own productions, but please give a credit to wiRe then. Before you continue, make sure to read Output#Producing_sound for the basic knowledge.

COVOX LPT-DAC

The COVOX LPT-DAC, also called Disney Sound, is an 8-bit digital-to-analog converter (DAC) connected to the 8 data-out-lines of a parallel printer port (LPT). Typically it was assembled using a simple R-2R resistor ladder to perform the conversion into an analog signal, why it was very cheap to build such hardware device on your own those days. Playing back an 8-bit sample, like it is the output of a byte-beat algorithm, through COVOX LPT DAC is a very easy task. Assuming the next sample value is inside register AL, then this is all you must do:

          mov     dx, 0378h                 ;BA7803     ;load LPT1 port address into DX
          out     dx, al                    ;EE         ;send 8-bit sample data to COVOX device

HINT: It is also possible to have 2 COVOX adapters, e.g. connected to LPT1 and LPT2, and send out two samples in parallel for stereo output, one sample for the left and one for the right channel.

But for a good audio playback quality, the time between two LPT1 writes should match the sampling rate pretty well. Also the bytebeat algorithm will require a time counter as input, which reflects the current sample number. This is why we need a good time source.

Time Source

For playing data through the COVOX LPTDAC, we need a pretty accurate timer. Typical sampling rate would be 8 kHz, but also higher values could be used. Lower values may also work in some special cases. There are multiple possible options to get such a time counter:

  • Timer Interrupt
  • Poll INT8 Counter
  • Poll PIT Counter
  • Alternative Options

Timer Interrupt

As described here: Output#Advanced_PC_Speaker_and_COVOX_sound_via_interrupt. While this is the most accurate way to drive the COVOX, it is very likely also the most expensive one. Setting up the new interrupt handler (let's even ignore the backup and restore of the old handler), the overhead of the handler itself and the problem of exchanging any counters between handler and firmware. All this will cost bytes. In most cases it will require less size if the timer is polled instead, like in all other variants described next. But it must be also clear that the polling approach will make it necessary to perform this task at a higher frequency than the actual samplingrate, i.e. 8kHz. This requires the polling to happen inside an inner loop, e.g. after each pixel update.

Poll PIT Counter

The DWORD at [0:0x046C] holds the number of timer ticks. Typically INT8 will run at a frequency of 18.2 Hz. On each call the default interrupt handler will increase this value by 1. Reusing this default handler will prevent the costs for setting up an own handler just to implement the counter increment logic. So, a simple solution to get a timer counter is to reconfigure the PIT for an 8kHz rate. This will trigger the default INT8 handler 8000 times per second. Then this counter can be polled periodically inside the inner loop. Once its LSB changes, another sample must be generated, also using this counter value as sample counter, and send to LPT1. This coudl look like this:

          mov     al, 149                   ;B095       ;program PIT #0 to ~8kHz (1.19318181818 MHz / 149 = 8007.93 Hz)
          out     40h, al                   ;E640       ;
          salc                              ;D6         ;  AL := 0 (if CF=0)
          out     40h, al                   ;E640       ;

          ; ...

suplp:    mov     al, [046Ch]               ;A0xxxx     ;load LSB from BIOS timer, assuming DS=0
_tcmp:    cmp     al, 0x55                  ;2C??       ;did timer value changed?
          jz      ntick                     ;74xx       ;  no -> skip audio
          mov     [_tcmp+1], al             ;A2xxxx     ;remember last BIOS timer value (selfmodifying code)

          inc     ebp                       ;6645       ;increment 32-bit timer counter

          ; ... set AL to next audio sample based on EBP ...

          mov     dx, 0378h                 ;BA7803     ;load LPT1 port address into DX
          out     dx, al                    ;EE         ;send 8-bit sample data to COVOX device

ntick:
          ; ...

          jmp     short suplp

Poll PIT Counter

(...more soon...)

Alternative Options

Another option to get an accurate time is to read the processor's time-stamp counter using the RDTSC instruction.

Music Sequence Table

MUZAK58 is to some degree a generic background music player. Of course it is also possible to modify the player itself to change the music style (more on that later), but the source of the played music comes from a sequence table in memory. Changing those words results in an entirely new music tune being played. Also spending more words for this table will vary the tune, that it will not repeat as fast.

(...more soon...)