audio.py 607 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.Markdown(f"[{filename}]({filename})")
  10. return IPython.display.HTML(f"""
  11. <audio controls>
  12. <source src="{filename}" type="audio/wav">
  13. Your browser does not support the audio element.
  14. </audio>
  15. <br/>
  16. <a href="{filename}">{filename}</a>
  17. """)