audio-playback について
JavaScriptアプリでの音声再生とスケジューリングを扱うTone.js向けスキルです。プレーヤー、Transportによるタイミング制御、エフェクト、同期再生、一般的な再生パターンを扱います。
主な機能
- 01音声ファイルの再生と読み込み
- 02Transport制御とイベント予約
- 03エフェクトチェーンとウェット・ドライ調整
- 04複数プレーヤーの同期再生
- 05音楽・効果音の管理パターン
活用シーン
- 01音楽の拍に合わせた音の予約
- 02ループトラック付きプレーヤーの構築
- 03再生音へのリバーブやディレイの適用
- 04複数の音声レイヤーの同期
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ bbeierle12/ skill-mcp-claude --skill audio-playbackインストール後、Claude Code で/audio-playbackを入力して読み込まれたか確認します。
同梱ファイル_meta.json
詳細
オーディオ再生
Tone.js を使用したオーディオの再生とスケジューリングです。
クイックスタート
npm install tone
import * as Tone from 'tone';
// Simple playback
const player = new Tone.Player('/audio/music.mp3').toDestination();
// Must start audio context after user interaction
document.addEventListener('click', async () => {
await Tone.start();
player.start();
});
基本概念
オーディオコンテキストの初期化
import * as Tone from 'tone';
// Audio context requires user gesture to start
async function initAudio() {
await Tone.start();
console.log('Audio context started');
}
// Common pattern: init on first click
document.addEventListener('click', initAudio, { once: true });
プレーヤーの基本
// Create player
const player = new Tone.Player({
url: '/audio/track.mp3',
loop: true,
autostart: false,
onload: () => console.log('Loaded')
}).toDestination();
// Control
player.start();
player.stop();
player.seek(10); // Seek to 10 seconds
player.volume.value = -6; // Volume in dB
// Properties
player.state; // 'started' | 'stopped'
player.loaded; // boolean
player.duration; // in seconds
オーディオの読み込み
// Single file
const player = new Tone.Player('/audio/music.mp3');
await player.load('/audio/music.mp3');
// Multiple files with Players
const players = new Tone.Players({
kick: '/audio/kick.mp3',
snare: '/audio/snare.mp3',
hihat: '/audio/hihat.mp3'
}).toDestination();
// Access individual player
players.player('kick').start();
// Buffer for programmatic access
const buffer = new Tone.Buffer('/audio/sample.mp3', () => {
console.log('Buffer loaded, duration:', buffer.duration);
});
トランスポート
トランスポートの基本制御
// Global transport (master clock)
Tone.Transport.start();
Tone.Transport.stop();
Tone.Transport.pause();
// Position
Tone.Transport.position = '0:0:0'; // bars:beats:sixteenths
Tone.Transport.seconds = 10; // in seconds
// Tempo
Tone.Transport.bpm.value = 120;
// Time signature
Tone.Transport.timeSignature = [4, 4];
イベントのスケジューリング
// Schedule at specific time
Tone.Transport.schedule((time) => {
player.start(time);
}, '0:0:0');
// Schedule repeating
Tone.Transport.scheduleRepeat((time) => {
synth.triggerAttackRelease('C4', '8n', time);
}, '4n'); // Every quarter note
// Schedule once
Tone.Transport.scheduleOnce((time) => {
console.log('One time event at', time);
}, '4:0:0'); // At bar 4
時間表記
| 形式 | 説明 | 例 |
|---|---|---|
'4n' | 4分音符 | 4/4拍子で1拍 |
'8n' | 8分音符 | 半拍 |
'16n' | 16分音符 | 4分の1拍 |
'1m' | 1小節 | 1小節全体 |
'2:0:0' | 小節:拍:16分音符 | 2小節目の開始 |
'+0.5' | 相対秒数 | 今から0.5秒後 |
0.5 | 絶対秒数 | 0.5秒時点 |
エフェクトチェーン
基本的な信号の流れ
// Source → Effects → Destination
const player = new Tone.Player('/audio/track.mp3');
const reverb = new Tone.Reverb(2);
const volume = new Tone.Volume(-6);
player.chain(reverb, volume, Tone.Destination);
一般的なエフェクト
// Reverb
const reverb = new Tone.Reverb({
decay: 2.5,
wet: 0.4
});
// Delay
const delay = new Tone.FeedbackDelay({
delayTime: '8n',
feedback: 0.3,
wet: 0.25
});
// Filter
const filter = new Tone.Filter({
frequency: 1000,
type: 'lowpass',
Q: 2
});
// Compressor
const compressor = new Tone.Compressor({
threshold: -24,
ratio: 4,
attack: 0.003,
release: 0.25
});
// Volume/Gain
const volume = new Tone.Volume(-12);
const gain = new Tone.Gain(0.5);
エフェクトのウェット/ドライミックス
const reverb = new Tone.Reverb(2);
reverb.wet.value = 0.5; // 50% wet, 50% dry
// Automate wet mix
reverb.wet.rampTo(1, 2); // Ramp to 100% wet over 2 seconds
再生パターン
音楽プレーヤー
class MusicPlayer {
constructor() {
this.player = new Tone.Player().toDestination();
this.isPlaying = false;
}
async load(url) {
await this.player.load(url);
}
async play() {
await Tone.start();
this.player.start();
this.isPlaying = true;
}
pause() {
this.player.stop();
this.isPlaying = false;
}
setVolume(db) {
this.player.volume.value = db;
}
seek(seconds) {
const wasPlaying = this.isPlaying;
this.player.stop();
this.player.seek(seconds);
if (wasPlaying) this.player.start();
}
get duration() {
return this.player.buffer?.duration || 0;
}
get currentTime() {
return this.player.immediate();
}
}
効果音マネージャー
class SFXManager {
constructor() {
this.sounds = {};
}
async load(name, url) {
const player = new Tone.Player(url).toDestination();
await player.load(url);
this.sounds[name] = player;
}
play(name) {
const sound = this.sounds[name];
if (sound) {
sound.stop(); // Stop if already playing
sound.start();
}
}
setVolume(name, db) {
if (this.sounds[name]) {
this.sounds[name].volume.value = db;
}
}
setMasterVolume(db) {
Tone.Destination.volume.value = db;
}
}
// Usage
const sfx = new SFXManager();
await sfx.load('click', '/audio/click.mp3');
await sfx.load('success', '/audio/success.mp3');
sfx.play('click');
アンビエントレイヤーのループ再生
class AmbientLayer {
constructor(url) {
this.player = new Tone.Player({
url,
loop: true,
fadeIn: 2,
fadeOut: 2
});
this.volume = new Tone.Volume(-12);
this.reverb = new Tone.Reverb(4);
this.player.chain(this.reverb, this.volume, Tone.Destination);
}
async start() {
await Tone.start();
this.player.start();
}
stop() {
this.player.stop();
}
setIntensity(value) {
// 0-1 range
this.volume.volume.value = -24 + (value * 18); // -24dB to -6dB
this.reverb.wet.value = 0.3 + (value * 0.4); // 30% to 70% wet
}
}
クロスフェード
class CrossfadePlayer {
constructor() {
this.playerA = new Tone.Player();
this.playerB = new Tone.Player();
this.crossfade = new Tone.CrossFade();
this.playerA.connect(this.crossfade.a);
this.playerB.connect(this.crossfade.b);
this.crossfade.toDestination();
this.current = 'a';
}
async loadAndCrossfade(url, duration = 2) {
const nextPlayer = this.current === 'a' ? this.playerB : this.playerA;
const targetFade = this.current === 'a' ? 1 : 0;
await nextPlayer.load(url);
nextPlayer.start();
this.crossfade.fade.rampTo(targetFade, duration);
// Stop old player after crossfade
setTimeout(() => {
const oldPlayer = this.current === 'a' ? this.playerA : this.playerB;
oldPlayer.stop();
}, duration * 1000);
this.current = this.current === 'a' ? 'b' : 'a';
}
}
同期再生
トランスポートとの同期
// Player synced to transport
const player = new Tone.Player('/audio/track.mp3');
player.sync().start(0).toDestination();
// Now transport controls playback
Tone.Transport.start();
Tone.Transport.pause();
Tone.Transport.stop();
複数プレーヤーの同期
const drums = new Tone.Player('/audio/drums.mp3').toDestination();
const bass = new Tone.Player('/audio/bass.mp3').toDestination();
const melody = new Tone.Player('/audio/melody.mp3').toDestination();
// Sync all to transport
drums.sync().start(0);
bass.sync().start(0);
melody.sync().start(0);
// Set tempo
Tone.Transport.bpm.value = 120;
// Control all with transport
Tone.Transport.start();
時間的崩壊パターン
カウントダウンオーディオマネージャー
class CountdownAudio {
constructor() {
this.ambient = new Tone.Player({ loop: true });
this.tickSound = new Tone.Player();
this.finalTicks = new Tone.Player();
this.celebration = new Tone.Player();
// Effects
this.filter = new Tone.Filter(2000, 'lowpass');
this.reverb = new Tone.Reverb(3);
// Routing
this.ambient.chain(this.filter, this.reverb, Tone.Destination);
this.tickSound.toDestination();
this.finalTicks.toDestination();
this.celebration.toDestination();
}
async loadAll() {
await Promise.all([
this.ambient.load('/audio/cosmic-ambient.mp3'),
this.tickSound.load('/audio/tick.mp3'),
this.finalTicks.load('/audio/final-tick.mp3'),
this.celebration.load('/audio/celebration.mp3')
]);
}
async start() {
await Tone.start();
this.ambient.start();
}
tick(secondsRemaining) {
if (secondsRemaining <= 10) {
// Intense ticks for final 10 seconds
this.finalTicks.start();
} else {
this.tickSound.start();
}
}
setIntensity(value) {
// 0-1, increases as countdown nears zero
this.filter.frequency.value = 500 + (value * 3500);
this.ambient.volume.value = -12 + (value * 6);
}
celebrate() {
this.ambient.stop();
this.celebration.start();
}
}
時間同期オーディオイベント
function scheduleCountdownAudio(targetDate) {
const checkInterval = setInterval(() => {
const now = Date.now();
const remaining = targetDate - now;
const seconds = Math.floor(remaining / 1000);
if (seconds <= 0) {
clearInterval(checkInterval);
audio.celebrate();
return;
}
// Tick every second
audio.tick(seconds);
// Increase intensity as countdown progresses
const intensity = Math.max(0, 1 - (seconds / 3600)); // Over 1 hour
audio.setIntensity(intensity);
}, 1000);
}
パフォーマンスのヒント
// 1. Preload audio before needed
await player.load(url);
// 2. Reuse players instead of creating new ones
player.stop();
player.start(); // Reuse same player
// 3. Dispose when done
player.dispose();
// 4. Use buffer for frequently played sounds
const buffer = new Tone.Buffer(url);
// Create players from buffer
const player = new Tone.Player(buffer);
// 5. Limit concurrent sounds
const limiter = new Tone.Limiter(-3).toDestination();
players.forEach(p => p.connect(limiter));
リファレンス
- FFT と周波数抽出については
audio-analysisを参照してください - ビジュアルとオーディオのバインディングについては
audio-reactiveを参照してください - オーディオドメインのルーティングについては
audio-routerを参照してください
コメント
Markdown 対応