Spaces:
Runtime error
Runtime error
Commit
·
0042606
1
Parent(s):
b7ff96e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import librosa
|
4 |
+
import soundfile as sf
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Process the color information to create audio frequencies with extended parameters
|
9 |
+
def process_color_to_audio(array, sample_rate=44100, duration=5, **kwargs):
|
10 |
+
amplitude_modulation = kwargs.get('amplitude_modulation', 1.0)
|
11 |
+
frequency_modulation = kwargs.get('frequency_modulation', 0.0)
|
12 |
+
harmonic_content = kwargs.get('harmonic_content', 1.0)
|
13 |
+
attack_time = kwargs.get('attack_time', 0.005)
|
14 |
+
decay_time = kwargs.get('decay_time', 0.1)
|
15 |
+
sustain_level = kwargs.get('sustain_level', 0.7)
|
16 |
+
release_time = kwargs.get('release_time', 0.3)
|
17 |
+
vibrato_freq = kwargs.get('vibrato_freq', 5.0)
|
18 |
+
vibrato_depth = kwargs.get('vibrato_depth', 0.005)
|
19 |
+
|
20 |
+
normalized_colors = array / 255.0
|
21 |
+
brightness = np.mean(normalized_colors, axis=(0, 1))
|
22 |
+
brightness = brightness ** harmonic_content
|
23 |
+
base_freq = 220.0
|
24 |
+
frequencies = base_freq * (1 + frequency_modulation * brightness)
|
25 |
+
t = np.linspace(0, duration, int(duration * sample_rate), endpoint=False)
|
26 |
+
vibrato = vibrato_depth * np.sin(2 * np.pi * vibrato_freq * t)
|
27 |
+
t_env = np.linspace(0, duration, int(duration * sample_rate), endpoint=False)
|
28 |
+
env = np.ones_like(t_env)
|
29 |
+
env[:int(attack_time * sample_rate)] = np.linspace(0, 1, int(attack_time * sample_rate))
|
30 |
+
env[int(attack_time * sample_rate):int((attack_time + decay_time) * sample_rate)] = np.linspace(1, sustain_level, int(decay_time * sample_rate))
|
31 |
+
env[int((attack_time + decay_time) * sample_rate):int((duration - release_time) * sample_rate)] = sustain_level
|
32 |
+
env[int((duration - release_time) * sample_rate):] = np.linspace(sustain_level, 0, int(release_time * sample_rate))
|
33 |
+
audio_data = env * amplitude_modulation * np.sin(2 * np.pi * (frequencies + vibrato) * t)
|
34 |
+
|
35 |
+
return audio_data, sample_rate
|
36 |
+
|
37 |
+
def main():
|
38 |
+
st.title('PNG to Audio Streamlit App')
|
39 |
+
st.write("Upload a PNG image and get the corresponding audio!")
|
40 |
+
|
41 |
+
uploaded_file = st.file_uploader("Choose a PNG image", type="png")
|
42 |
+
if uploaded_file is not None:
|
43 |
+
image = Image.open(uploaded_file)
|
44 |
+
st.image(image, caption='Uploaded PNG image', use_column_width=True)
|
45 |
+
|
46 |
+
with st.expander("Audio Generation Parameters", expanded=True):
|
47 |
+
amplitude_modulation = st.slider("Amplitude Modulation", 0.0, 2.0, 1.0, 0.1)
|
48 |
+
frequency_modulation = st.slider("Frequency Modulation", -1.0, 1.0, 0.0, 0.1)
|
49 |
+
harmonic_content = st.slider("Harmonic Content", 0.1, 2.0, 1.0, 0.1)
|
50 |
+
attack_time = st.slider("Attack Time", 0.001, 0.1, 0.005, 0.001)
|
51 |
+
decay_time = st.slider("Decay Time", 0.01, 0.5, 0.1, 0.01)
|
52 |
+
sustain_level = st.slider("Sustain Level", 0.1, 1.0, 0.7, 0.1)
|
53 |
+
release_time = st.slider("Release Time", 0.1, 1.0, 0.3, 0.1)
|
54 |
+
vibrato_freq = st.slider("Vibrato Frequency", 0.1, 10.0, 5.0, 0.1)
|
55 |
+
vibrato_depth = st.slider("Vibrato Depth", 0.001, 0.01, 0.005, 0.001)
|
56 |
+
|
57 |
+
if st.button("Generate Audio"):
|
58 |
+
array = np.array(image)
|
59 |
+
audio_data, sample_rate = process_color_to_audio(array, amplitude_modulation=amplitude_modulation, frequency_modulation=frequency_modulation,
|
60 |
+
harmonic_content=harmonic_content, attack_time=attack_time,
|
61 |
+
decay_time=decay_time, sustain_level=sustain_level,
|
62 |
+
release_time=release_time, vibrato_freq=vibrato_freq,
|
63 |
+
vibrato_depth=vibrato_depth)
|
64 |
+
|
65 |
+
with BytesIO() as output:
|
66 |
+
sf.write(output, audio_data, sample_rate, format='wav')
|
67 |
+
st.audio(output.getvalue(), format='audio/wav')
|
68 |
+
|
69 |
+
if __name__ == '__main__':
|
70 |
+
main()
|