File size: 7,024 Bytes
5008b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { useEffect, useState } from 'react';
import { CheckCircle2, Circle, Loader } from 'lucide-react';

interface PipelineStage {
  id: string;
  name: string;
  description: string;
  status: 'pending' | 'active' | 'completed';
  progress?: number;
  duration?: number; // in seconds
  startTime?: number; // timestamp when stage started
}

interface ProcessingPipelineProps {
  isActive: boolean;
  currentStage?: string;
  synthesizerStartTime?: number | null; // timestamp from parent when synthesis started
  className?: string;
}

export default function ProcessingPipeline({
  isActive,
  currentStage,
  synthesizerStartTime,
  className = ""
}: ProcessingPipelineProps) {
  const [stages, setStages] = useState<PipelineStage[]>([
    {
      id: 'encoder',
      name: 'Speaker Encoder',
      description: 'Extracting voice embedding',
      status: 'pending',
      progress: 0,
      duration: 3
    },
    {
      id: 'synthesizer',
      name: 'Tacotron2 Synthesizer',
      description: 'Generating mel-spectrogram',
      status: 'pending',
      progress: 0,
      duration: 45
    },
    {
      id: 'vocoder',
      name: 'WaveRNN Vocoder',
      description: 'Converting to audio',
      status: 'pending',
      progress: 0,
      duration: 10
    }
  ]);

  // Real-time sync with backend using elapsed time
  useEffect(() => {
    if (!synthesizerStartTime) {
      return;
    }

    // When a new synthesis starts, reset all stages to pending
    setStages(prev => prev.map(stage => ({
      ...stage,
      status: 'pending',
      progress: 0
    })));

    // Update progress based on actual elapsed time from backend
    const updateProgress = () => {
      const elapsedMs = Date.now() - synthesizerStartTime;
      const elapsedSeconds = elapsedMs / 1000;

      setStages(prevStages => {
        // If synthesis is no longer active, force all stages to completed
        if (!isActive) {
          return prevStages.map(stage => ({
            ...stage,
            status: 'completed',
            progress: 100
          }));
        }

        return prevStages.map(stage => {
          // Define stage timing
          let stageStart = 0;
          let stageDuration = stage.duration || 0;

          if (stage.id === 'encoder') {
            stageStart = 0;
            stageDuration = 3;
          } else if (stage.id === 'synthesizer') {
            stageStart = 3;
            stageDuration = 45;
          } else if (stage.id === 'vocoder') {
            stageStart = 48;
            stageDuration = 10;
          }

          const stageEnd = stageStart + stageDuration;

          // Calculate status based on actual elapsed time
          if (elapsedSeconds < stageStart) {
            // Stage hasn't started yet
            return { ...stage, status: 'pending', progress: 0 };
          } else if (elapsedSeconds >= stageStart && elapsedSeconds < stageEnd) {
            // Stage is currently active
            const stageElapsed = elapsedSeconds - stageStart;
            const rawProgress = (stageElapsed / stageDuration) * 100;
            // While synthesis is active, never show a full 100% for any stage
            const progress = Math.min(99, rawProgress);
            return { ...stage, status: 'active', progress };
          } else {
            // Stage logically finished, but keep it at 99% until synthesis completes
            return { ...stage, status: 'active', progress: 99 };
          }
        });
      });
    };

    // Update immediately
    updateProgress();

    // Update every 100ms for smooth animation
    const interval = setInterval(updateProgress, 100);

    return () => clearInterval(interval);
  }, [isActive, synthesizerStartTime]);

  const getStageIcon = (stage: PipelineStage) => {
    if (stage.status === 'completed') {
      return <CheckCircle2 className="w-6 h-6 text-green-400" />;
    } else if (stage.status === 'active') {
      return <Loader className="w-6 h-6 text-blue-400 animate-spin" />;
    } else {
      return <Circle className="w-6 h-6 text-gray-500" />;
    }
  };

  const getProgressBarColor = (status: string) => {
    switch (status) {
      case 'completed':
        return 'bg-green-500';
      case 'active':
        return 'bg-blue-500';
      default:
        return 'bg-gray-600';
    }
  };

  return (
    <div className={`space-y-4 ${className}`}>
      <div className="flex items-center justify-between">
        <h3 className="text-sm font-semibold text-foreground">
          Processing Pipeline
        </h3>
        {isActive && (
          <span className="text-xs text-blue-400 animate-pulse">
            Synthesizing...
          </span>
        )}
      </div>

      <div className="space-y-3">
        {stages.map((stage, index) => (
          <div key={stage.id} className="space-y-1.5">
            {/* Stage Header */}
            <div className="flex items-center gap-3">
              {getStageIcon(stage)}
              <div className="flex-1">
                <div className="flex items-center justify-between">
                  <p className="text-sm font-medium text-foreground">
                    {stage.name}
                  </p>
                  {stage.progress !== undefined && stage.status !== 'pending' && (
                    <span className="text-xs text-muted-foreground">
                      {Math.round(stage.progress)}%
                    </span>
                  )}
                </div>
                <p className="text-xs text-muted-foreground">
                  {stage.description}
                </p>
              </div>
            </div>

            {/* Progress Bar */}
            <div className="ml-9 h-1.5 bg-surface rounded-full overflow-hidden">
              <div
                className={`h-full ${getProgressBarColor(stage.status)} transition-all duration-300 ${
                  stage.progress === 100 ? 'w-full' : ''
                }`}
                data-progress={Math.round(stage.progress || 0)}
              />
            </div>

            {/* Connector Line */}
            {index < stages.length - 1 && (
              <div className="ml-3 h-2 border-l-2 border-gray-600" />
            )}
          </div>
        ))}
      </div>

      {/* Timeline Info */}
      <div className="mt-4 p-3 bg-surface rounded-lg border border-border">
        <div className="text-xs text-muted-foreground space-y-1">
          <p className="flex items-center gap-2">
            <CheckCircle2 className="w-3 h-3 text-green-400" />
            <span>Speaker Encoder: Loads your voice</span>
          </p>
          <p className="flex items-center gap-2">
            <CheckCircle2 className="w-3 h-3 text-green-400" />
            <span>Tacotron2: Generates speech pattern</span>
          </p>
          <p className="flex items-center gap-2">
            <CheckCircle2 className="w-3 h-3 text-green-400" />
            <span>Vocoder: Creates audio waveform</span>
          </p>
        </div>
      </div>
    </div>
  );
}