Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import numpy as np | |
| import soundfile as sf | |
| import io | |
| notes_map = { | |
| 'a': 220, 'b': 246.94, 'c': 261.3, 'd': 293.66, 'e': 329.63, 'f': 349.23, 'g': 392, 'h': 440, | |
| 'i': 493.88, 'j': 523.25, 'k': 587.33, 'l': 659.25, 'm': 698.46, 'n': 783.99, 'o': 880, | |
| 'p': 987.77, 'q': 1046.5, 'r': 1147.66, 's': 1318, 't': 1396, 'u': 1567.98, 'v': 1760.66, | |
| 'w': 1975.7, 'x': 2095, 'y': 2349.32, 'z': 2607, '!': 5078, ' ': 0 | |
| } | |
| def generate_music(prompt, duration=1.0): | |
| audio_data = np.array([]) | |
| for char in prompt: | |
| if char in notes_map: | |
| freq = notes_map[char] | |
| t = np.linspace(0, duration, int(44100 * duration), False) | |
| note = np.sin(freq * t * 2 * np.pi) | |
| audio_data = np.append(audio_data, note) | |
| return audio_data | |
| st.sidebar.title("Music Generator") | |
| st.sidebar.write("Music generator based on your input") | |
| prompt = st.sidebar.text_input("Enter prompt...") | |
| if st.sidebar.button("Generate"): | |
| audio = generate_music(prompt) | |
| bytes_io = io.BytesIO() | |
| sf.write(bytes_io, audio, 44100, format ='WAV') | |
| bytes_io.seek(0) | |
| st.audio(bytes_io, format="audio/wav") | |
| st.write("Audio generation finished successfully") | |