gradio/demo/generate_tone/run.py
Abubakar Abid d1f044145a
Use covariant container types across the codebase and add typing to our demos (#8854)
* more typing

* add changeset

* tweaks

* more changes

* more fixes

* more changes

* more fixes

* more fixes

* delete

* add changeset

* notebooks

* restore

* restore

* format

* add changeset

* more typing fixes

* fixes

* change

* fixes

* fix

* format

* more fixes

* fixes

* fixes for python3.9

* demo

* fix

* fixes

* fix typo

* type

* formatting

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: aliabd <ali.si3luwa@gmail.com>
2024-07-19 18:34:34 -07:00

26 lines
720 B
Python

import numpy as np
import gradio as gr
notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
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)
duration = int(duration)
audio = np.linspace(0, duration, duration * sr)
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
return sr, audio
demo = gr.Interface(
generate_tone,
[
gr.Dropdown(notes, type="index"),
gr.Slider(4, 6, step=1),
gr.Textbox(value="1", label="Duration in seconds"),
],
"audio",
)
if __name__ == "__main__":
demo.launch()