mwg API
    Preparing search index...

    Function composeMusic

    Generated game music: a scene described as data (mood, tension, key, seed, length, voices) becomes a playable MIDI file, the same one every time. Renderer-free; it builds on core (the seeded generator) and audio (the MIDI file shape and the seed mixing of data sound effects), and what it makes plays through renderMidiToBuffer, a registered engine, or AdaptiveMusic for bar-aligned switches between game states.

    • Composes a piece from a scene: the same scene always gives the same notes. A melody walks the mood's scale over a four-bar chord progression (its first two bars come back in the third and fourth with a changed ending), a bass holds the chord roots, a pad or arpeggio fills the chord, and drums join once the scene has the voices and the tension for them. The voices limit is kept by writing fewer layers, never by cutting notes afterwards: 1 voice is the melody alone, 2 adds the bass, 3 adds a one-note arpeggio, 4 adds drums, more adds pad notes.

      Parameters

      Returns GeneratedMusic

      import { composeMusic } from '@datamoc/mwg/music';
      import { renderMidiToBuffer } from '@datamoc/mwg/audio';

      const music = composeMusic({ id: 'harbour', seed: 7, mood: 'calm', tension: 0.2, key: 2, bars: 8, voices: 3 });
      console.log(music.barSeconds, music.polyphony); // 3 or fewer notes at once
      const audio = renderMidiToBuffer(music.file);
      console.log(audio.duration > 0);