MCPcopy Create free account
hub / github.com/FunAudioLLM/Fun-Audio-Chat / process

Method process

web_demo/client/src/audio-processor.ts:157–237  ·  view source on GitHub ↗
(inputs, outputs, parameters)

Source from the content-addressed store, hash-verified

155 }
156
157 process(inputs, outputs, parameters) {
158 const output = outputs[0][0];
159 const outputLength = output.length;
160
161 // 更新延迟统计
162 const currentDelay = this.totalQueuedSamples / sampleRate;
163 if (this.isPlaying && this.totalQueuedSamples > 0) {
164 this.maxDelay = Math.max(this.maxDelay, currentDelay);
165 this.minDelay = Math.min(this.minDelay, currentDelay);
166 }
167
168 // 还未开始播放 - 输出静音
169 if (!this.isPlaying) {
170 // output 默认已经是 0
171 return true;
172 }
173
174 // 启动倒计时 - 额外等待一小段时间让缓冲更充足
175 if (this.startupCountdown > 0) {
176 this.startupCountdown -= outputLength;
177 return true;
178 }
179
180 // 从队列获取音频
181 const { buffer, actualSamples } = this.getAudioFromQueue(outputLength);
182
183 // 复制到输出
184 if (actualSamples > 0) {
185 output.set(buffer.subarray(0, actualSamples), 0);
186
187 // 首次输出 - 淡入效果
188 if (this.isFirstOutput) {
189 this.isFirstOutput = false;
190 console.log(
191 this.timestamp(),
192 `[Playback] First output, queue: ${asMs(this.totalQueuedSamples)}ms`
193 );
194 // 淡入
195 for (let i = 0; i < Math.min(actualSamples, 256); i++) {
196 output[i] *= i / 256;
197 }
198 }
199 }
200
201 // Buffer underrun - 队列空了
202 if (actualSamples < outputLength) {
203 this.underrunCount++;
204 console.log(
205 this.timestamp(),
206 `[Underrun #${this.underrunCount}] Missing ${outputLength - actualSamples} samples,`,
207 `queue: ${asMs(this.totalQueuedSamples)}ms`
208 );
209
210 // 淡出已播放的部分,避免爆音
211 if (actualSamples > 0) {
212 const fadeLength = Math.min(actualSamples, 128);
213 for (let i = 0; i < fadeLength; i++) {
214 output[actualSamples - 1 - i] *= i / fadeLength;

Callers

nothing calls this directly

Calls 3

getAudioFromQueueMethod · 0.95
timestampMethod · 0.95
asMsFunction · 0.85

Tested by

no test coverage detected