SEGA SOUND / INTRODUCTION

Sega Genesis / Mega Drive Sega PSG Introduction with JavaScript

Explore square-wave and noise sounds with short JavaScript examples.

How to use this page

Run starts each example; Stop ends it. Change one code value and Run again to compare. Playback never starts automatically. Stop the other example before comparing.

These short performances release their sound chip at the end. They do not reproduce the tone of physical speakers or amplifiers.

Another sound chip alongside FM

Alongside the YM2612 FM chip, the Mega Drive uses Sega PSG for square waves and noise. It has three tone channels and one noise channel, each with a level control.

The tones are 50% duty square waves. Unlike Game Boy, there is no duty selector or automatic envelope setting. Create sounds by combining changes in pitch and attenuation.

1. Play a scale with a square wave

Calculate the tone period from note names such as A4. First Run the code unchanged. Then change attenuation = 6 to 12 and hear the quieter sound. Higher values reduce volume; 15 mutes it.

Edit this code in a separate Playground tab ↗

2. Layer three channels

Use three tone channels together to play a C–E–G chord. Change notes to change the chord. Simultaneous channels add in level, so each uses attenuation 10 here.

Edit this code in a separate Playground tab ↗

3. Choose a noise character

Play white noise at low → medium → high, then the same rates for periodic noise. White has an irregular texture; periodic has an audible repeating pattern. Fix type and rate to compare just one sound repeatedly.

With rate: "tone3", the CH3 tone period affects the noise period. This does not allow completely independent adjustment of the CH3 tone and noise.

Edit this code in a separate Playground tab ↗

Pitch, level, and timing

APIPSG operation
tone(ch, {note})A 10-bit period for physical channels 0–2. At the default clock of 3,579,545 Hz, frequency is approximately clock / (32 × period). Note names are rounded to representable periods.
attenuationValues 0–14 give roughly 2 dB attenuation steps; 15 mutes. This is not a linear volume multiplier.
noise({type, rate})Noise type and rate: white / periodic, low / medium / high / tone3.
off(ch) / noiseOff()Sets the corresponding channel's attenuation to 15.

sleep() is a JavaScript wait. Repeated level writes can create a decay, but that is software control rather than a PSG automatic envelope.

Actual songs change pitch, volume, and noise settings during playback. The settings at note-on alone cannot reproduce all those changes.