の nokakoi

sfx-song / sfx-pack カード実装ガイドsfx-song / sfx-pack Card Implementation Guide

他の Nostr クライアント作者向け。投稿内の nostr:naddr... が曲や効果音パックを指していたら、生 JSON ではなく再生・試聴できるカードとして表示する方法をまとめた。形式そのものは曲仕様とパック仕様を参照。

For authors of other Nostr clients: how to render a nostr:naddr... quote that points to a song or sound pack as a playable/auditionable card instead of raw JSON. See the song spec and pack spec for the formats themselves.

1. 結論: 分岐は kind と d だけ1. TL;DR: branch on kind and d only

naddr を通常どおり解決してイベントを取得したら、汎用の kind:30078 表示へ落とす前に次を判定する。

After resolving an naddr to an event as usual, check the following before falling back to generic kind:30078 rendering.

kindd表示Render as
30078sfx-song/<name>曲カード(再生ボタン、タイトル、作者、BPM、長さ、トラック数、ループ)Song card (play button, title, author, BPM, duration, track count, loop)
30078sfx-pack/<name>パックカード(タイトル、作者、収録音数、波形、各音の試聴ボタン)Pack card (title, author, sound count, waveform, per-sound audition buttons)
その他anything else—従来どおりExisting behavior

直接表示、イベント詳細、返信プレビューなど、イベントを表示する場所では同じ分岐を使うと一貫する。カードの隅に sfx-song / sfx-pack とそのまま書くと、利用者が仕組みの名前を覚えやすい。

Use the same branch wherever events are shown (direct display, event details, reply previews) for consistency. Labeling the card literally sfx-song / sfx-pack helps users learn the name of the mechanism.

2. naddr の解決2. Resolving the naddr

  1. NIP-19 でデコードし、kind、pubkey、identifier(d の値)、任意の relays を得る。Decode with NIP-19 to get kind, pubkey, identifier (the d value), and optional relays.
  2. kind === 30078 かつ identifier が sfx-song/ または sfx-pack/ で始まる場合だけ、カード候補にする。Only treat it as a card candidate when kind === 30078 and the identifier starts with sfx-song/ or sfx-pack/.
  3. naddr に含まれるリレーと、普段の引用解決で使うリレーへ問い合わせる。Query the relays embedded in the naddr plus the relays you normally use for quote resolution.
  4. 複数返ってきたら created_at が最大のものを最新版として使う。naddr は常に最新版を指す。古い版を固定したい参照は nevent の役割。If several versions return, use the one with the greatest created_at. An naddr always points at the latest version; pinning an old version is nevent's job.
  5. 返ってきたイベントの kind、pubkey、d が naddr と一致することを確認する。Verify that the returned event's kind, pubkey, and d match the naddr.
{ "kinds": [30078], "authors": ["<pubkey>"], "#d": ["sfx-song/test-drive"] }

3. content の検証3. Validating content

content は JSON 文字列。表示前に必ず検証・正規化し、数値は安全な範囲へ丸める。タイトルは制御文字を取り除いて長さを制限し、HTML へ入れる場合はエスケープする。

content is a JSON string. Always validate and normalize before rendering, clamping numbers to safe ranges. Strip control characters from titles, cap their length, and escape them when inserting into HTML.

sfx-songsfx-pack
主な項目Main fieldstitle, bpm, loop, tracks[]title, wave, volume, sounds{}
楽譜Score[周波数 or 音名, 拍][frequency or note name, beats][周波数 or 音名, 秒][frequency or note name, seconds]
取り下げWithdrawntracks: []sounds: {}
読む側の上限例Example reader limits4トラック、1トラック8192 steps、4800拍、BPM 40〜300、20〜8000Hz、音量0.01〜0.34 tracks, 8192 steps/track, 4800 beats, BPM 40–300, 20–8000 Hz, volume 0.01–0.3音名32種、1音32音、合計4秒、20〜8000Hz、音量0.01〜0.332 sound names, 32 notes/sound, 4 seconds total, 20–8000 Hz, volume 0.01–0.3

取り下げ・不正なイベントでも生 JSON は出さず、「取り下げられています」「再生できません」等の無効カードにする。タイトルがなければ d の <name> 部分を表示名にする。作者名はイベントの pubkey から通常のプロフィール解決で得る。

For withdrawn or invalid events, show a disabled card (“Withdrawn”, “Cannot play”, etc.) instead of raw JSON. If there is no title, use the <name> part of d. Resolve the author name from the event pubkey as usual.

4. 再生4. Playback

5. 疑似コード5. Pseudocode

const decoded = nip19.decode(naddr).data;
if (decoded.kind !== 30078) return renderGenericQuote(decoded);

const isSong = decoded.identifier.startsWith("sfx-song/");
const isPack = decoded.identifier.startsWith("sfx-pack/");
if (!isSong && !isPack) return renderGenericQuote(decoded);

const events = await queryRelays(decoded.relays, {
  kinds: [30078],
  authors: [decoded.pubkey],
  "#d": [decoded.identifier]
});
const event = latestByCreatedAt(events);
if (!event || event.pubkey !== decoded.pubkey || dTag(event) !== decoded.identifier) {
  return renderUnavailableCard();
}

const parsed = safeJsonParse(event.content);
if (isSong) return renderSongCard(event, sanitizeSong(parsed));
return renderPackCard(event, sanitizePack(parsed));

6. テスト用 naddr6. Test naddrs

以下は実在する公開作品。投稿本文へ nostr: 付きで貼ると、対応クライアントではカードになる。

These are real published works. Paste them with the nostr: prefix into a post and supporting clients will render cards.

作品Worknaddr
sfx-song/test-drivenostr:naddr1qvzqqqr40cpzqgdv99tpkh0fpnwzrx2lcpc82fwd0ry22tv8wgdtdqwn6cyarcklqyxhwumn8ghj77tpvf6jumt9qqfhxenc94ek7mn89a6x2um594j8y6tkv5fg80ds
sfx-song/kira-kira-daysnostr:naddr1qvzqqqr40cpzqgdv99tpkh0fpnwzrx2lcpc82fwd0ry22tv8wgdtdqwn6cyarcklqyxhwumn8ghj77tpvf6jumt9qqthxenc94ek7mn89a4kjunp944kjunp94jxz7tnls6wql
sfx-pack/pink-suikominostr:naddr1qvzqqqr40cpzqgdv99tpkh0fpnwzrx2lcpc82fwd0ry22tv8wgdtdqwn6cyarcklqyxhwumn8ghj77tpvf6jumt9qq2hxenc94cxzcmt9acxjmnt94eh26ttdakkjd4y6x7

7. 参考実装7. Reference implementation

最小実装は「分岐、検証、タイトルと再生/試聴ボタン」だけでも成立する。まずはそこからどうぞ。

A minimal implementation only needs the branch, validation, a title, and play/audition buttons. Start there.