Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import gradio as gr | |
| # Define notes and their corresponding MIDI number | |
| notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] | |
| def generate_tone(note, octave, duration): | |
| sr = 48000 # Sample rate | |
| a4_freq = 440 # Frequency of A4 | |
| # Calculate the frequency of the note | |
| tones_from_a4 = 12 * (octave - 4) + (note - 9) # Adjusted note to MIDI | |
| frequency = a4_freq * 2 ** (tones_from_a4 / 12) | |
| # Generate the time array | |
| t = np.linspace(0, duration, int(duration * sr), endpoint=False) | |
| # Create the sine wave | |
| audio = (0.5 * np.sin(2 * np.pi * frequency * t)).astype(np.float32) | |
| # Convert to 16-bit PCM format (scale to 16-bit range) | |
| audio = (audio * 32767).astype(np.int16) | |
| return (sr, audio) | |
| # Gradio interface | |
| gr.Interface( | |
| generate_tone, | |
| [ | |
| gr.Dropdown(choices=notes, label="Note", type="index"), | |
| gr.Slider(minimum=4, maximum=6, step=1, label="Octave"), | |
| gr.Number(value=1, label="Duration in seconds"), | |
| ], | |
| "audio", | |
| ).launch() | |