oscilloreader / app.py
issacneedsbread's picture
Update app.py
f90815b verified
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()