⚠ ⚠ 你正在以 1998 模式浏览本页,一些现代功能被特意关掉了。 回到现在 ➜
A chiptune theme without a single MP3 onyx.xn--fiqs8s/posts/a-chiptune-theme-without-a-single-mp3
首页 RSS 搜索… 1998 模式 访问量 2298

A chiptune theme without a single MP3

📑 本页目录
  1. How it works
  2. Why bother?

Press the ♪ button in the corner. That is the whole soundtrack: oscillators, a gain node, and about forty lines of JavaScript. Nothing is downloaded.

How it works#

const ctx = new AudioContext();
const osc = ctx.createOscillator();   // square wave = 8-bit heart
osc.type = 'square';
osc.frequency.value = 523.25;         // C5
osc.connect(ctx.destination);
osc.start();

That plays one note forever. To get a tune you need a sequencer, a short note table, and a lookahead scheduler so the timing does not drift when the tab is busy.

const MELODY = [
  [0, 523.25], [1, 659.25], [2, 783.99], [3, 659.25],
  [4, 587.33], [5, 493.88], [6, 440.00], [7, 493.88],
];

function schedule(step = 0) {
  const [beat, freq] = MELODY[step % MELODY.length];
  const at = ctx.currentTime + beat * 0.25;
  playNote(freq, at, 0.24);
  setTimeout(() => schedule(step + 1), 260);
}

Why bother?#

Because an autoplaying

💬 评论 (0)

还没有评论——可以在留言簿或这里留一条。

✍️ 写评论

仅支持纯文本,请互相友善。
📶 📶 正在以 56,000 bps 连接… /posts/a-chiptune-theme-without-a-single-mp3 访问量: 2298 在线: 3