audio.py 543 B

1234567891011121314151617181920
  1. import IPython.display
  2. import numpy as np
  3. import json
  4. from scipy.io.wavfile import write
  5. def Audio(audio: np.ndarray, rate: int, name: str):
  6. filename = f"output/{name}.wav"
  7. amplitude = np.iinfo(np.int16).max
  8. write(filename, rate, (audio*amplitude).astype(np.int16))
  9. return IPython.display.HTML(f"""
  10. <audio controls>
  11. <source src="{filename}" type="audio/wav">
  12. Your browser does not support the audio element.
  13. </audio>
  14. <br/>
  15. <a href="{filename}">{filename}</a>
  16. """)