feat: added lookAhead option
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { ConfigManager } from '../../../core/config/ConfigManager';
|
||||
import { KGAudioInterface } from '../../../core/audio-interface/KGAudioInterface';
|
||||
|
||||
const BehaviorSettings: React.FC = () => {
|
||||
const [chatboxDefaultOpen, setChatboxDefaultOpen] = useState<boolean>(true);
|
||||
const [audioLookaheadTime, setAudioLookaheadTime] = useState<string>('50');
|
||||
const [enableAudioCapture, setEnableAudioCapture] = useState<boolean>(false);
|
||||
const [validationErrors, setValidationErrors] = useState<string[]>([]);
|
||||
|
||||
const configManager = ConfigManager.instance();
|
||||
|
||||
@@ -15,6 +18,8 @@ const BehaviorSettings: React.FC = () => {
|
||||
}
|
||||
|
||||
setChatboxDefaultOpen((configManager.get('chatbox.default_open') as boolean) ?? true);
|
||||
const lookaheadTimeSeconds = (configManager.get('audio.lookahead_time') as number) ?? 0.05;
|
||||
setAudioLookaheadTime(((lookaheadTimeSeconds * 1000).toFixed(0)));
|
||||
setEnableAudioCapture((configManager.get('audio.enable_audio_capture_for_screen_sharing') as boolean) ?? false);
|
||||
};
|
||||
|
||||
@@ -28,6 +33,36 @@ const BehaviorSettings: React.FC = () => {
|
||||
await configManager.set('chatbox.default_open', boolValue);
|
||||
};
|
||||
|
||||
const handleAudioLookaheadTimeChange = async (value: string) => {
|
||||
// Allow empty string, treat as 0 ms
|
||||
const numValueMs = value === '' ? 0 : parseFloat(value);
|
||||
const numValueSeconds = numValueMs / 1000;
|
||||
const errors: string[] = [];
|
||||
|
||||
// Validate the input
|
||||
if (isNaN(numValueMs)) {
|
||||
errors.push('Lookahead time must be a valid number');
|
||||
} else if (numValueSeconds < 0) {
|
||||
errors.push('Lookahead time must be between 0 and 0.5 seconds (0-500ms)');
|
||||
} else if (numValueSeconds > 0.5) {
|
||||
errors.push('Lookahead time must be between 0 and 0.5 seconds (0-500ms)');
|
||||
}
|
||||
|
||||
setValidationErrors(errors);
|
||||
|
||||
// Only apply if valid
|
||||
if (errors.length === 0) {
|
||||
setAudioLookaheadTime(value);
|
||||
await configManager.set('audio.lookahead_time', numValueSeconds);
|
||||
|
||||
// Apply the change immediately without restart
|
||||
const audioInterface = KGAudioInterface.instance();
|
||||
audioInterface.setLookaheadTime(numValueSeconds);
|
||||
|
||||
console.log(`Audio lookahead time changed to: ${numValueSeconds}s (${numValueMs}ms)`);
|
||||
}
|
||||
};
|
||||
|
||||
const handleEnableAudioCaptureChange = async (value: string) => {
|
||||
const boolValue = value === 'yes';
|
||||
setEnableAudioCapture(boolValue);
|
||||
@@ -61,7 +96,34 @@ const BehaviorSettings: React.FC = () => {
|
||||
|
||||
<div className="settings-group">
|
||||
<h4>Audio</h4>
|
||||
|
||||
|
||||
<div className="settings-item">
|
||||
<label className="settings-label">
|
||||
Lookahead Time (ms)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
className="settings-select"
|
||||
value={audioLookaheadTime}
|
||||
onChange={(e) => handleAudioLookaheadTimeChange(e.target.value)}
|
||||
min="0"
|
||||
max="500"
|
||||
step="1"
|
||||
/>
|
||||
{validationErrors.length > 0 && (
|
||||
<div className="settings-validation-errors">
|
||||
{validationErrors.map((error, index) => (
|
||||
<div key={index} className="settings-validation-error">
|
||||
{error}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="settings-help" style={{ fontSize: '12px', color: '#888', marginTop: '4px' }}>
|
||||
Audio scheduling lookahead time (0-500ms). Lower values (10-20ms) reduce MIDI input latency but may cause audio glitches on slower systems. Higher values (100ms+) are better for playback stability. Changes apply immediately without restart.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="settings-item">
|
||||
<label className="settings-label">
|
||||
Capture Audio for Screen Sharing
|
||||
|
||||
Reference in New Issue
Block a user