ym2149-wasm
WebAssembly bindings for the YM2149 PSG emulator - play YM chiptunes and Arkos Tracker projects directly in your browser!
Features
- 🎵 Play YM2-YM6 and Arkos Tracker
.aksfiles in the browser - 🎮 Full playback control (play, pause, stop, seek)
- 🔊 Volume control and channel muting
- 📊 Real-time waveform data for visualizations
- 📝 Metadata extraction (title, author, comments)
- ⚡ High-performance cycle-accurate emulation
- 🎨 Web Audio API integration
Installation
NPM Package (Coming Soon)
Build from Source
# Install wasm-pack
|
# Build the WASM module
# Or for bundler (webpack, rollup, etc.)
# Shortcut: rebuild + copy into examples/pkg
Quick Start
Basic Usage
import init from './ym2149_wasm.js';
Web Audio API Integration
import init from './ym2149_wasm.js';
// Usage
const player = ;
await player.;
// Load file from user input
const input = document.;
input.;
Playback Control
// Play/Pause
if else
// Volume control (0.0 to 1.0)
player.;
// Seek to position
player.; // Seek to 50%
player.; // Seek to frame 1000
// Channel muting (for karaoke-style playback)
player.; // Mute channel A
player.; // Unmute channel B
player.; // Unmute channel C
// Get playback position
console.log;
console.log;
Metadata Access
const metadata = player.;
console.log;
console.log;
console.log;
console.log;
console.log;
console.log;
console.log;
Visualization
// Get current register values for visualization
const registers = player.; // Returns Uint8Array[16]
// Register layout:
// R0-R1: Channel A period
// R2-R3: Channel B period
// R4-R5: Channel C period
// R6: Noise period
// R7: Mixer control
// R8-R10: Channel volumes
// R11-R12: Envelope period
// R13: Envelope shape
// R14-R15: I/O ports
// Calculate frequencies
const channelAPeriod = registers | ;
const frequencyA = 2000000 / ; // Master clock / (16 * period)
// Draw waveform visualization
API Reference
Ym2149Player
Constructor
constructor(data: Uint8Array): Ym2149Player
Creates a new player from YM file data.
Properties
metadata: YmMetadata- Song metadata (read-only)
Methods
play(): void- Start playbackpause(): void- Pause playbackstop(): void- Stop and reset to beginningrestart(): void- Restart from beginningis_playing(): boolean- Check if currently playingstate(): string- Get playback state as string
Volume Control:
set_volume(volume: number): void- Set volume (0.0-1.0)volume(): number- Get current volume
Seeking:
seek_to_frame(frame: number): void- Seek to specific frameseek_to_percentage(percentage: number): void- Seek to percentage (0.0-1.0)frame_position(): number- Get current frameframe_count(): number- Get total framesposition_percentage(): number- Get position as percentage
Channel Control:
set_channel_mute(channel: number, mute: boolean): void- Mute/unmute channel (0-2)is_channel_muted(channel: number): boolean- Check if channel is muted
Audio Generation:
generateSamples(count: number): Float32Array- Generate audio samplesgenerateSamplesInto(buffer: Float32Array): void- Generate into buffer (zero-alloc)
Visualization:
get_registers(): Uint8Array- Get current PSG register values (16 bytes)
Effects:
set_color_filter(enabled: boolean): void- Enable/disable ST color filter
YmMetadata
interface YmMetadata {
title: string; // Song title
author: string; // Composer/author
comments: string; // Song comments
format: string; // YM format version (e.g., "YM6")
frame_count: number; // Total frames
frame_rate: number; // Frame rate in Hz (typically 50)
duration_seconds: number; // Duration in seconds
}
Examples
See the examples/ directory for complete working examples:
simple-player.html- Minimal web playeradvanced-player.html- Full-featured player with UIvisualizer.html- Player with oscilloscope and spectrum analyzerbundler-example/- Example using webpack/rollup
Performance
The WASM module is highly optimized:
- ⚡ ~6ns per emulator clock cycle
- 🎵 Real-time generation of 44.1kHz audio
- 📦 Small bundle size (~100KB gzipped)
- 🔋 Minimal CPU usage (<1% on modern hardware)
Browser Support
Works in all modern browsers that support:
- WebAssembly
- Web Audio API
- ES6 Modules (or use a bundler)
Tested on:
- ✅ Chrome/Edge 90+
- ✅ Firefox 88+
- ✅ Safari 15+
- ✅ Mobile browsers (iOS Safari, Chrome Mobile)
Building
Development Build
Production Build
Build Options
# Target web (ES modules)
# Target bundler (webpack, rollup, parcel)
# Target Node.js
# With features
License
MIT - See main repository for details.
Links
- Main Repository
- Documentation
- NPM Package (coming soon)
- Examples