File size: 2,367 Bytes
11ac8d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import folium
from folium.plugins import MousePosition
import tempfile
import scipy.io.wavfile
import numpy as np
from geopy.geocoders import Nominatim

# Terrain prompt mapping
terrain_prompts = {
    "forest": "ambient electronic music with forest and bird sounds",
    "urban": "lo-fi chillhop with city ambience and soft beats",
    "highway": "energetic synthwave for night highway driving",
    "mountain": "cinematic orchestral music with epic strings",
    "rainy": "melancholic piano with background rain and thunder",
    "water": "relaxing ocean waves with ambient synths"
}

def classify_location(lat, lon):
    geolocator = Nominatim(user_agent="geo-audio-app")
    try:
        loc = geolocator.reverse((lat, lon), language='en', timeout=10)
        addr = loc.raw.get("address", {})
        if "city" in addr or "town" in addr:
            return "urban"
        if "forest" in addr or "protected_area" in addr:
            return "forest"
        if "mountain" in addr:
            return "mountain"
        if "highway" in addr or "road" in addr:
            return "highway"
        if "water" in addr or "lake" in addr or "coast" in addr:
            return "water"
    except:
        pass
    return "urban"

def synthesize_audio(prompt: str):
    # Simple tone simulator; replace with real model
    sr = 16000
    t = np.linspace(0, 5, sr * 5, endpoint=False)
    tone = 0.5 * np.sin(2 * np.pi * 440 * t).astype(np.float32)

    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
    scipy.io.wavfile.write(tmp.name, rate=sr, data=(tone * 32767).astype(np.int16))
    return tmp.name

def generate_audio(lat, lon):
    terrain = classify_location(lat, lon)
    prompt = terrain_prompts.get(terrain, "ambient atmospheric music")
    audio_file = synthesize_audio(prompt)
    return terrain, prompt, audio_file

with gr.Blocks() as demo:
    gr.Markdown("# 🌍 Geo-Audio Synth with Gradio")
    coords = gr.Point(label="Click on map to select location", interactive=True)
    terrain_out = gr.Text(label="Detected terrain")
    prompt_out = gr.Text(label="Generated prompt")
    audio_out = gr.Audio(label="Simulated Audio")

    coords.change(fn=lambda pt: pt, inputs=coords, outputs=[])
    coords.submit(generate_audio, inputs=[coords.x, coords.y], outputs=[terrain_out, prompt_out, audio_out])

demo.launch()