A tiny JS ringtone generator – how it works

We are talking about the ZzFXM tool, which is useful for developers of indie games or web applications who are forced to operate with a very limited amount of memory.

Let’s also talk about analogs – rFXGen, wafxr.

Photo chuttersnap / Unsplash

More about the project

ZzFXM introduced by Frank Force, programmer and author of the game development blog Killed By a Pixel. In his materials, he pays special attention to the issues of saving memory. He once wrote piano simulator JavaScript, which is only one kilobyte.

The Melody Generator continues this idea – Frank designed it specifically to generate music for ultra-small programs. Such projects are implemented within the framework of open competitions js13k Gameswhose members write games in HTML5 and JavaScript using tens of KB of memory.

ZzFXM can be used to develop full-fledged indie games. It spreads over MIT licensestherefore suitable for commercial projects.

Sources and instructions for setting up are in the repository on GitHub

What’s under the “hood”

The utility uses the engine ZzFX for writing sound effects reminiscent of eight-bit. It allows you to control nineteen sound parameters, from volume to frequency and waveform. Examples of sounds that ZzFX is capable of generating can be found on Frank Force website… ZzFXM uses some of them as samples.

The format of the finished files resembles MODused to store and play music on a personal computer Amiga, so all instrument sound samples are stored in different files (modularity principle).

How it works

The composer composes a sequence of notes indicating which instrument should play it and when. This sequence is called a track. Several tracks sounding in parallel form a block (pattern) with their own number. Further, the composer specifies in the program code which pattern should sound and when.

This approach allows you to quickly form the desired composition, but the resulting code is difficult to read without comments. In the repository on GitHub, the author of the generator leads next example:

[                                     // Song
  [                                     // Instruments
    [.9, 0, 143, , , .35, 3],             // Instrument 0
    [1, 0, 216, , , .45, 1, 4, , ,50],    // Instrument 1
    [.75, 0, 196, , .08, .18, 3]          // Instrument 2
  [                                     // Patterns
    [                                     // Pattern 0
      [                                     // Channel 0
        0,                                    // Using instrument 0
        -1,                                   // From the left speaker
        1,                                    // play C-1
        0, 0, 0,                              // rest (x3)
        3.5,                                  // play E-1 with 50% attenuation
        0, 0, 0                               // rest (x3)
      [                                     // Channel 1
        1,                                    // Using instrument 1
        1,                                    // From the right speaker
        2,                                    // play D-1
        2.25,                                 // play D-1 with 25% attenuation
        3.5,                                  // Play E-1 with 50% attenuation
        4.75,                                 // Play F-1 with 75% attenuation
        -1,                                   // Release the note
        0, 0, 0                               // rest (x3)
  [                                     // Sequence
    0,                                    // Play pattern 0
    0,                                    // ...and again
  120,                                  // 120 BPM
  {                                     // Metadata
    title: "My Song",                      // Name of the song
    author: "Keith Clark"                  // Name of the author/composer

Listen to how the tracks generated using the utility sound, can be on GitHub… Song size does not exceed 550 bytes.

What are the analogues

One of the analogues of ZzFXM is rFXGen… It is written in C and based on the project sfxr – in 2007 it was presented by one of the participants of the LD48 competition. Within its framework, each developer must submit a small game in 48 hours. RFXGen utility supports oscillators, vibrato, and low and high pass filters.

Photo Ohmydearlife / Pixabay

Another compact sound generator – wafxr… It renders live audio using WebAudio API and library wasgen… Wafxr also supports oscillators, tremolo / vibrato and various filters. Demo work available on GitHub – there you can evaluate the tool yourself.

Reading on the topic from the “Hi-Fi World”:

What is music programming – who does it and why
Take and join music programming – languages ​​that will help you do it
How to reproduce realistic sound in PC games and VR and why it’s difficult
How Sporth works – a language software for music live sessions
Entertaining music: Number 5 and a little about how usability and programmer “see” music

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *