sound.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Player } from "cli-sound"
  2. import { mkdirSync } from "node:fs"
  3. import { tmpdir } from "node:os"
  4. import { basename, join } from "node:path"
  5. import { Process } from "@/util/process"
  6. import { which } from "@/util/which"
  7. import pulseA from "../asset/pulse-a.wav" with { type: "file" }
  8. import pulseB from "../asset/pulse-b.wav" with { type: "file" }
  9. import pulseC from "../asset/pulse-c.wav" with { type: "file" }
  10. import charge from "../asset/charge.wav" with { type: "file" }
  11. const FILE = [pulseA, pulseB, pulseC]
  12. const HUM = charge
  13. const DIR = join(tmpdir(), "opencode-sfx")
  14. const LIST = [
  15. "ffplay",
  16. "mpv",
  17. "mpg123",
  18. "mpg321",
  19. "mplayer",
  20. "afplay",
  21. "play",
  22. "omxplayer",
  23. "aplay",
  24. "cmdmp3",
  25. "cvlc",
  26. "powershell.exe",
  27. ] as const
  28. type Kind = (typeof LIST)[number]
  29. function args(kind: Kind, file: string, volume: number) {
  30. if (kind === "ffplay") return [kind, "-autoexit", "-nodisp", "-af", `volume=${volume}`, file]
  31. if (kind === "mpv")
  32. return [kind, "--no-video", "--audio-display=no", "--volume", String(Math.round(volume * 100)), file]
  33. if (kind === "mpg123" || kind === "mpg321") return [kind, "-g", String(Math.round(volume * 100)), file]
  34. if (kind === "mplayer") return [kind, "-vo", "null", "-volume", String(Math.round(volume * 100)), file]
  35. if (kind === "afplay" || kind === "omxplayer" || kind === "aplay" || kind === "cmdmp3") return [kind, file]
  36. if (kind === "play") return [kind, "-v", String(volume), file]
  37. if (kind === "cvlc") return [kind, `--gain=${volume}`, "--play-and-exit", file]
  38. return [kind, "-c", `(New-Object Media.SoundPlayer '${file.replace(/'/g, "''")}').PlaySync()`]
  39. }
  40. let item: Player | null | undefined
  41. let kind: Kind | null | undefined
  42. let proc: Process.Child | undefined
  43. let tail: ReturnType<typeof setTimeout> | undefined
  44. let cache: Promise<{ hum: string; pulse: string[] }> | undefined
  45. let seq = 0
  46. let shot = 0
  47. function load() {
  48. if (item !== undefined) return item
  49. try {
  50. item = new Player({ volume: 0.35 })
  51. } catch {
  52. item = null
  53. }
  54. return item
  55. }
  56. async function file(path: string) {
  57. mkdirSync(DIR, { recursive: true })
  58. const next = join(DIR, basename(path))
  59. const out = Bun.file(next)
  60. if (await out.exists()) return next
  61. await Bun.write(out, Bun.file(path))
  62. return next
  63. }
  64. function asset() {
  65. cache ??= Promise.all([file(HUM), Promise.all(FILE.map(file))]).then(([hum, pulse]) => ({ hum, pulse }))
  66. return cache
  67. }
  68. function pick() {
  69. if (kind !== undefined) return kind
  70. kind = LIST.find((item) => which(item)) ?? null
  71. return kind
  72. }
  73. function run(file: string, volume: number) {
  74. const kind = pick()
  75. if (!kind) return
  76. return Process.spawn(args(kind, file, volume), {
  77. stdin: "ignore",
  78. stdout: "ignore",
  79. stderr: "ignore",
  80. })
  81. }
  82. function clear() {
  83. if (!tail) return
  84. clearTimeout(tail)
  85. tail = undefined
  86. }
  87. function play(file: string, volume: number) {
  88. const item = load()
  89. if (!item) return run(file, volume)?.exited
  90. return item.play(file, { volume }).catch(() => run(file, volume)?.exited)
  91. }
  92. export function start() {
  93. stop()
  94. const id = ++seq
  95. void asset().then(({ hum }) => {
  96. if (id !== seq) return
  97. const next = run(hum, 0.24)
  98. if (!next) return
  99. proc = next
  100. void next.exited.then(
  101. () => {
  102. if (id !== seq) return
  103. if (proc === next) proc = undefined
  104. },
  105. () => {
  106. if (id !== seq) return
  107. if (proc === next) proc = undefined
  108. },
  109. )
  110. })
  111. }
  112. export function stop(delay = 0) {
  113. seq++
  114. clear()
  115. if (!proc) return
  116. const next = proc
  117. if (delay <= 0) {
  118. proc = undefined
  119. void Process.stop(next).catch(() => undefined)
  120. return
  121. }
  122. tail = setTimeout(() => {
  123. tail = undefined
  124. if (proc === next) proc = undefined
  125. void Process.stop(next).catch(() => undefined)
  126. }, delay)
  127. }
  128. export function pulse(scale = 1) {
  129. stop(140)
  130. const index = shot++ % FILE.length
  131. void asset()
  132. .then(({ pulse }) => play(pulse[index], 0.26 + 0.14 * scale))
  133. .catch(() => undefined)
  134. }
  135. export function dispose() {
  136. stop()
  137. }
  138. export * as Sound from "./sound"