generate-learning-sounds.py 964 B

1234567891011121314151617181920
  1. """Original short, softly enveloped cues; reproducible, no external audio license."""
  2. import math, struct, wave
  3. from pathlib import Path
  4. root = Path(__file__).resolve().parent.parent / 'public/assets/audio'
  5. root.mkdir(parents=True, exist_ok=True)
  6. for kind, notes in [('success', [(660, 0, .14), (880, .075, .16)]), ('error', [(330, 0, .17)])]:
  7. rate, duration = 22050, .26
  8. frames = []
  9. for i in range(int(rate * duration)):
  10. t = i / rate
  11. value = 0
  12. for freq, start, length in notes:
  13. x = t - start
  14. if 0 <= x < length:
  15. envelope = min(1, x / .012) * (1 - x / length) ** 2
  16. value += .32 * envelope * math.sin(2 * math.pi * freq * x)
  17. frames.append(struct.pack('<h', round(value * 32767)))
  18. with wave.open(str(root / f'learning-{kind}.wav'), 'wb') as output:
  19. output.setparams((1, 2, rate, 0, 'NONE', 'not compressed'))
  20. output.writeframes(b''.join(frames))