
    Pi                      J   d dl Z G d dej                  Zedk    r ej                    5 Z ej        d            eg dg ddd	
          Z ej	        d          Z
 ej        d ee
           ddd           n# 1 swxY w Y   e                                 dS dS )    Nc                   p     e Zd Zddddddee         dedz  dee         dz  dededz  f
 fd	Zd
 Z xZS )AudioGalleryN   )valuelabelscolumnslabel
audio_urlsr   r   r   r	   c                    || _         d}d}d}	 t                      j        d|p|r|d         nd |||	||||dd	| d S )Nu  
        <div class="audio-gallery-container">
            ${label ? `<label class="container-label">${label}</label>` : ''}
            <div class="audio-gallery-grid" style="grid-template-columns: repeat(${columns}, 1fr);">
                ${audio_urls.map((url, i) => `
                    <div class="audio-item" data-index="${i}">
                        <div class="audio-label">${labels && labels[i] ? labels[i] : 'Audio ' + (i + 1)}</div>
                        <canvas class="waveform-canvas" data-url="${url}" width="300" height="80"></canvas>
                        <audio src="${url}" preload="metadata" ${value === url ? 'data-selected="true"' : ''}></audio>
                        <div class="audio-controls">
                            <button class="play-btn">▶</button>
                            <div class="time-display">0:00</div>
                        </div>
                    </div>
                `).join('')}
            </div>
        </div>
        aw  
        .audio-gallery-container { padding: var(--spacing-lg); }
        .container-label { display: block; margin-bottom: var(--spacing-md); font-weight: 600; }
        .audio-gallery-grid { display: grid; gap: var(--spacing-lg); }
        .audio-item { border: 2px solid var(--border-color-primary); border-radius: var(--radius-md); padding: var(--spacing-md); cursor: pointer; transition: all 0.2s; }
        .audio-item:hover { border-color: var(--color-accent); box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
        .audio-item[data-selected="true"] { border-color: var(--color-accent); background-color: var(--background-fill-secondary); }
        .audio-label { margin-bottom: 8px; text-align: center; }
        .waveform-canvas { width: 100%; height: 80px; background: var(--background-fill-secondary); margin-bottom: 8px; }
        .audio-controls { display: flex; align-items: center; gap: 8px; }
        .play-btn { width: 32px; height: 32px; border-radius: 50%; border: none; background: var(--color-accent); color: white; cursor: pointer; }
        .play-btn:hover { opacity: 0.8; }
        .time-display { font-size: 12px; }
        u/
  
        const audioItems = element.querySelectorAll('.audio-item');

        audioItems.forEach((item, index) => {
            const canvas = item.querySelector('.waveform-canvas');
            const audio = item.querySelector('audio');
            const playBtn = item.querySelector('.play-btn');
            const timeDisplay = item.querySelector('.time-display');
            const ctx = canvas.getContext('2d');

            drawWaveform(canvas, ctx);

            item.addEventListener('click', (e) => {
                if (e.target === playBtn) return;
                audioItems.forEach(i => i.removeAttribute('data-selected'));
                item.setAttribute('data-selected', 'true');
                props.value = audio.src;
            });

            playBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                if (audio.paused) {
                    document.querySelectorAll('.audio-item audio').forEach(a => a.pause());
                    document.querySelectorAll('.play-btn').forEach(b => b.textContent = '▶');
                    audio.play();
                    playBtn.textContent = '⏸';
                } else {
                    audio.pause();
                    playBtn.textContent = '▶';
                }
            });

            audio.addEventListener('timeupdate', () => {
                const currentTime = Math.floor(audio.currentTime);
                const minutes = Math.floor(currentTime / 60);
                const seconds = currentTime % 60;
                timeDisplay.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;

                const progress = audio.currentTime / audio.duration;
                drawWaveform(canvas, ctx, progress);
            });

            audio.addEventListener('ended', () => {
                playBtn.textContent = '▶';
                drawWaveform(canvas, ctx, 0);
            });
        });

        function drawWaveform(canvas, ctx, progress = 0) {
            const width = canvas.width;
            const height = canvas.height;
            const bars = 50;
            const barWidth = width / bars;

            ctx.clearRect(0, 0, width, height);

            for (let i = 0; i < bars; i++) {
                const barHeight = (Math.sin(i * 0.5) * 0.3 + Math.random() * 0.7) * height * 0.8;
                const x = i * barWidth;
                const y = (height - barHeight) / 2;

                ctx.fillStyle = i / bars < progress ? '#FF7C00' : '#ccc';
                ctx.fillRect(x, y, barWidth - 2, barHeight);
            }
        }
        r   F)	r   html_templatecss_template
js_on_loadr
   r   r   r	   apply_default_css )r
   super__init__)selfr
   r   r   r   r	   kwargsr   r   r   	__class__s             /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/gradio/components/custom_html_components/audio_gallery.pyr   zAudioGallery.__init__   s     %&A
F 	 	
BZAJqMMT'%!!#	
 	
 	
 	
 	
 	
 	
    c                     dddS )Nstringz	Audio URL)typetitler   )r   s    r   api_infozAudioGallery.api_info   s     
 
 	
r   )	__name__
__module____qualname__liststrintr   r   __classcell__)r   s   @r   r   r      s        
 !#' |
 |
 |
I|
 Tz	|

 S	D |
 |
 Tz|
 |
 |
 |
 |
 |
|
 
 
 
 
 
 
r   r   __main__z# Audio Gallery Demo)zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wavzRhttps://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample-1-4.wavzShttps://github.com/gradio-app/gradio/raw/main/gradio/media_assets/audio/cantina.wavzVhttps://github.com/gradio-app/gradio/raw/main/gradio/media_assets/audio/recording1.wavzXhttps://github.com/gradio-app/gradio/raw/main/gradio/media_assets/audio/heath_ledger.mp3zWhttps://github.com/gradio-app/gradio/raw/main/gradio/media_assets/audio/cate_blanch.mp3)zSample 1zSample 2Cantina	RecordingzHeath LedgerzCate Blanchettr   zSelect an audio file)r
   r   r   r	   zSelected Audio URL)r	   c                     | S )Nr   )xs    r   <lambda>r)      s     r   )fninputsoutputs)gradiogrHTMLr   r   BlocksdemoMarkdowngalleryTextboxoutput	Interfacelaunchr   r   r   <module>r8      s\      C
 C
 C
 C
 C
27 C
 C
 C
L z	 
*+++,     (%
 
 
* "6777{	
 	
 	
 	
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@ 	KKMMMMMC s   A	BB	B