File size: 1,299 Bytes
9bac1ce
 
 
 
aeee224
9bac1ce
 
84f504d
9bac1ce
84f504d
 
f90815b
84f504d
 
 
9bac1ce
84f504d
 
aeee224
 
84f504d
aeee224
84f504d
aeee224
 
 
 
b2ea9aa
9bac1ce
 
 
2d504a7
3ec4410
9bac1ce
 
 
 
 
 
84f504d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
import numpy as np
from PIL import Image
import scipy.io.wavfile as wav
import tempfile

def waveform_to_sound(image, speed):
    img = Image.open(image).convert('L')  # Convert to grayscale
    img_data = np.array(img)

    # Thresholding: only keep white pixels
    threshold = 1  # You can adjust this value as needed
    img_data = np.where(img_data > threshold, img_data, 0)

    # Normalize the remaining data
    normalized_data = img_data / 255.0

    # Generate audio signal
    sample_rate = 44100
    duration = len(normalized_data) / (sample_rate / speed)
    waveform = normalized_data.flatten() * 2 - 1  # Scale to [-1, 1]
    audio = (waveform * 32767).astype(np.int16)

    # Save to a temporary WAV file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
        wav.write(tmpfile.name, sample_rate, audio)
        return tmpfile.name

iface = gr.Interface(
    fn=waveform_to_sound,
    inputs=[
        gr.Image(type="filepath", label="Upload Oscillographic Waveform Image"),
        gr.Slider(0.1, 5.0, value=1.0, label="Speed Adjustment")
    ],
    outputs="audio",
    title="Waveform to Sound Converter",
    description="Upload an oscillographic waveform image and adjust the speed to convert it into sound."
)

iface.launch()