File size: 6,382 Bytes
dcdc07a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "id": "audio-gallery",
  "name": "Audio Gallery",
  "description": "Grid of audio players with waveform visualization and selection",
  "author": "gradio",
  "tags": [
    "display",
    "audio",
    "gallery",
    "media"
  ],
  "category": "display",
  "html_template": "<div class=\"audio-gallery-container\">\n    ${label ? `<label class=\"container-label\">${label}</label>` : ''}\n    <div class=\"audio-gallery-grid\" style=\"grid-template-columns: repeat(${columns}, 1fr);\">\n        ${audio_urls.map((url, i) => `\n            <div class=\"audio-item\" data-index=\"${i}\">\n                <div class=\"audio-label\">${labels && labels[i] ? labels[i] : 'Audio ' + (i + 1)}</div>\n                <canvas class=\"waveform-canvas\" data-url=\"${url}\" width=\"300\" height=\"80\"></canvas>\n                <audio src=\"${url}\" preload=\"metadata\" ${value === url ? 'data-selected=\"true\"' : ''}></audio>\n                <div class=\"audio-controls\">\n                    <button class=\"play-btn\">\u25b6</button>\n                    <div class=\"time-display\">0:00</div>\n                </div>\n            </div>\n        `).join('')}\n    </div>\n</div>",
  "css_template": ".audio-gallery-container { padding: 8px; }\n.container-label { display: block; margin-bottom: 12px; font-weight: 600; }\n.audio-gallery-grid { display: grid; gap: 12px; }\n.audio-item { border: 2px solid #e5e7eb; border-radius: 10px; padding: 12px; cursor: pointer; transition: all 0.2s; }\n.audio-item:hover { border-color: #f97316; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }\n.audio-item[data-selected=\"true\"] { border-color: #f97316; background-color: #fff7ed; }\n.audio-label { margin-bottom: 8px; text-align: center; font-size: 13px; font-weight: 500; }\n.waveform-canvas { width: 100%; height: 80px; background: #f9fafb; border-radius: 6px; margin-bottom: 8px; }\n.audio-controls { display: flex; align-items: center; gap: 8px; }\n.play-btn { width: 32px; height: 32px; border-radius: 50%; border: none; background: #f97316; color: white; cursor: pointer; font-size: 14px; }\n.play-btn:hover { opacity: 0.8; }\n.time-display { font-size: 12px; color: #6b7280; }",
  "js_on_load": "const audioItems = element.querySelectorAll('.audio-item');\naudioItems.forEach((item, index) => {\n    const canvas = item.querySelector('.waveform-canvas');\n    const audio = item.querySelector('audio');\n    const playBtn = item.querySelector('.play-btn');\n    const timeDisplay = item.querySelector('.time-display');\n    const ctx = canvas.getContext('2d');\n    drawWaveform(canvas, ctx);\n    item.addEventListener('click', (e) => {\n        if (e.target === playBtn) return;\n        audioItems.forEach(i => i.removeAttribute('data-selected'));\n        item.setAttribute('data-selected', 'true');\n        props.value = audio.src;\n    });\n    playBtn.addEventListener('click', (e) => {\n        e.stopPropagation();\n        if (audio.paused) {\n            element.querySelectorAll('audio').forEach(a => a.pause());\n            element.querySelectorAll('.play-btn').forEach(b => b.textContent = '\u25b6');\n            audio.play();\n            playBtn.textContent = '\u23f8';\n        } else {\n            audio.pause();\n            playBtn.textContent = '\u25b6';\n        }\n    });\n    audio.addEventListener('timeupdate', () => {\n        const currentTime = Math.floor(audio.currentTime);\n        const minutes = Math.floor(currentTime / 60);\n        const seconds = currentTime % 60;\n        timeDisplay.textContent = minutes + ':' + seconds.toString().padStart(2, '0');\n        const progress = audio.currentTime / audio.duration;\n        drawWaveform(canvas, ctx, progress);\n    });\n    audio.addEventListener('ended', () => {\n        playBtn.textContent = '\u25b6';\n        drawWaveform(canvas, ctx, 0);\n    });\n});\nfunction drawWaveform(canvas, ctx, progress) {\n    progress = progress || 0;\n    const width = canvas.width;\n    const height = canvas.height;\n    const bars = 50;\n    const barWidth = width / bars;\n    ctx.clearRect(0, 0, width, height);\n    for (let i = 0; i < bars; i++) {\n        const barHeight = (Math.sin(i * 0.5) * 0.3 + 0.5) * height * 0.8;\n        const x = i * barWidth;\n        const y = (height - barHeight) / 2;\n        ctx.fillStyle = i / bars < progress ? '#f97316' : '#d1d5db';\n        ctx.fillRect(x, y, barWidth - 2, barHeight);\n    }\n}",
  "default_props": {
    "value": null,
    "audio_urls": [
      "https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav",
      "https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample-1-4.wav",
      "https://github.com/gradio-app/gradio/raw/main/gradio/media_assets/audio/cantina.wav"
    ],
    "labels": [
      "Sample 1",
      "Sample 2",
      "Cantina"
    ],
    "columns": 3,
    "label": "Select an audio file"
  },
  "python_code": "class AudioGallery(gr.HTML):\n    def __init__(self, audio_urls, *, value=None, labels=None,\n                 columns=3, label=None, **kwargs):\n        html_template = \"\"\"\n        <div class=\"audio-gallery-container\">\n            ${label ? `<label>${label}</label>` : ''}\n            <div class=\"audio-gallery-grid\"\n                 style=\"grid-template-columns: repeat(${columns}, 1fr);\">\n                ${audio_urls.map((url, i) => `\n                    <div class=\"audio-item\" data-index=\"${i}\">\n                        <div class=\"audio-label\">\n                            ${labels && labels[i] ? labels[i] : 'Audio ' + (i+1)}\n                        </div>\n                        <canvas class=\"waveform-canvas\" width=\"300\" height=\"80\"></canvas>\n                        <audio src=\"${url}\" preload=\"metadata\"></audio>\n                        <div class=\"audio-controls\">\n                            <button class=\"play-btn\">\u25b6</button>\n                            <div class=\"time-display\">0:00</div>\n                        </div>\n                    </div>\n                `).join('')}\n            </div>\n        </div>\n        \"\"\"\n        super().__init__(\n            value=value, audio_urls=audio_urls,\n            labels=labels, columns=columns, label=label,\n            html_template=html_template,\n            css_template=CSS_TEMPLATE,\n            js_on_load=JS_ON_LOAD, **kwargs\n        )"
}