gradio/demo/generate_tone/run.py

30 lines
762 B
Python
Raw Normal View History

2020-08-20 05:27:22 +08:00
import numpy as np
import gradio as gr
2020-08-20 05:27:22 +08:00
notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
2020-08-20 05:27:22 +08:00
def generate_tone(note, octave, duration):
sr = 48000
a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9)
frequency = a4_freq * 2 ** (tones_from_a4 / 12)
2020-08-22 05:20:05 +08:00
duration = int(duration)
2020-08-20 05:27:22 +08:00
audio = np.linspace(0, duration, duration * sr)
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
2020-08-28 23:56:03 +08:00
return sr, audio
2020-08-20 05:27:22 +08:00
2022-03-29 04:49:55 +08:00
demo = gr.Interface(
generate_tone,
2020-08-20 05:27:22 +08:00
[
gr.Dropdown(notes, type="index"),
gr.Slider(value=4, minimum=4, maximum=6, step=1),
gr.Textbox(value=1, type="number", label="Duration in seconds"),
],
"audio",
)
2020-08-28 23:56:03 +08:00
2020-11-11 22:15:53 +08:00
if __name__ == "__main__":
2022-03-29 04:49:55 +08:00
demo.launch()