DEV Community

Cover image for Generating retro arcade game-style music with Magenta.js
ABA Games
ABA Games

Posted on

Generating retro arcade game-style music with Magenta.js

In this article, games from the early 1980s or so are considered retro arcade games. Examples are Xevious and DigDug. The BGM of retro arcade games of these days often consisted of repeating short phrases of about 4 to 8 bars.

I thought I would like to have such BGM for my mini-games and, if possible, generate it automatically. Therefore, I was looking for a technology to create such BGM.

WolframTones is one such technology. WolframTones cuts out cellular automaton and turns them into melodies. It can easily create a variety of songs. However, it doesn't seem easy to reproduce specific phrases often seen in old games, such as a bass that reciprocates in perfect fifths with sixteenth notes, due to the characteristics of a cellular automaton.

An example of the latest technology is OpenAI's Jukebox. However, this is not intended for retro games, as it generates musical waveforms, not musical scores.

The following technology I looked into was Magenta, a research project by Google that applies machine learning to music and pictures. Magenta.js is available as an implementation that can run in the Magenta browser.

Magenta.js has MusicRNN, which automatically generates the following phrases when entering a musical score. I thought that if I gave it a short score of a retro game, it would create another song like the background music of the retro game. So, I tried to prototype the following.

Pressing the "Generate" button in the center of the screen generates a new phrase based on the phrase on the left of the screen. The generated phrase is displayed on the right side of the screen and played back. Although it is a simple phrase generation consisting of only two tracks (melody and bass), it can create music like retro game background music.

One problem is that I could not figure out how to input multiple tracks as original phrases in Magenta.js. Therefore, I created separate phrases for the melody and bass. I also adjusted the sound of the bass when the bass was dissonant with the melody. The bass sound is adjusted so that the frequency of the melody sound is in the ratio of 1 to 4. Unchecking the checkbox at the bottom of the screen eliminates this adjustment, thus generating a freer phrase.

Another problem was that the Magenta.js built-in player was not capable of simultaneous speech. Therefore, I use Tone.js for playback. I also find it easier to input phrases using MML, so I use mml-iterator to parse MML and convert it into a form that Magenta.js can handle.

Please refer to the following repository if you wish to use the generated BGM in your game.

GitHub logo abagames / good-old-game-sound-generator

Generate sound effects and background music for good old-fashioned mini-games

good-old-game-sound-generator (DEMO)

Generate sound effects and background music for good old-fashioned mini-games. Powered by magenta.js and jsfx.

How to generate background music

  1. Open the DEMO page.

  2. (Optional) Input the base music for the generation in MML (Music Macro Language) at the bottom of the screen.

  3. Click the 'Generate' button.

How to use the generated music in your game

  1. Import sounds-some-sounds or crisp-game-lib library.

  2. On the Demo page, click the 'Copy to clipboard' button to copy the MML strings to your clipboard.

  3. Call the sss.playMml function with the MML strings in the clipboard as the first argument.

Sample game

Sample code

BALL TOUR source code uses crisp-game-lib.

title = "BALL TOUR";
// ... snip ....
function update() {
  if (!ticks) {
    if (!isReplaying) {
      sss.setTempo(80);
      // Play BGM
Enter fullscreen mode Exit fullscreen mode

It would be nice to generate sound effects similarly, but since MusicRNN is only intended to create melodies, it does not seem possible to take the same approach.

The above article has a score of Super Mario Bros. sound effects. The game's sound effects are too specific to be seen in a musical score. Therefore, finding relevant research on sound effect generation methods based on musical scores is probably tricky. Most of the existing sound effect generation tools are based on sfxr-based waveform generation methods, and I have not seen any that generate them as musical scores. Further research is needed on automatic sound effect generation for retro games.

Top comments (1)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Cool it brought back some nostalgia.