feat(voice): implement real-time voice-to-voice streaming engine
Introduce a complete low-level audio processing and streaming pipeline for live voice conversations. - Implement `VoiceAudioEngine` using `AudioWorklet` for low-latency PCM16 capture and playback. - Add `pcm-capture` and `pcm-playback` worklets for high-performance audio processing outside the main thread. - Implement `VoiceStreamService` to manage the voice-to-voice state machine (LISTENING, PROCESSING, SPEAKING, INTERRUPTED). - Add `VoiceApiService` for managing conversation and session lifecycle via REST and WebSocket. - Create `VoiceStreamPanelComponent` to display real-time transcripts, status indicators, and the animated voice orb. - Integrate voice streaming into the `AiChatInputComponent` with support for seamless UI transitions between text and voice modes.
This commit is contained in:
@@ -2,14 +2,6 @@
|
||||
display: block;
|
||||
}
|
||||
|
||||
:host-context(.has-messages) {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.ai-chat-input-wrapper {
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
@@ -27,9 +19,7 @@
|
||||
/* Layers cross-fade */
|
||||
.composer-layer,
|
||||
.voice-layer {
|
||||
transition:
|
||||
opacity 0.35s ease,
|
||||
transform 0.35s ease;
|
||||
transition: opacity 0.35s ease, transform 0.35s ease;
|
||||
}
|
||||
|
||||
.composer-layer {
|
||||
@@ -57,6 +47,13 @@
|
||||
transform: translateY(0) scale(1);
|
||||
pointer-events: auto;
|
||||
}
|
||||
:host-context(.has-messages) {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
/* ---------- Chat composer ---------- */
|
||||
.chat-composer {
|
||||
@@ -69,30 +66,26 @@
|
||||
padding: 0.625rem 0.75rem;
|
||||
background: var(--surface-bg);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 2rem;
|
||||
box-shadow:
|
||||
0 2px 12px rgba(0, 0, 0, 0.05),
|
||||
0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
transition:
|
||||
box-shadow 0.2s ease,
|
||||
border-color 0.2s ease;
|
||||
border-radius: 1.5rem;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
transition: box-shadow 0.2s ease, border-color 0.2s ease;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.chat-composer:focus-within {
|
||||
border-color: var(--color-primary);
|
||||
box-shadow:
|
||||
0 4px 20px rgba(59, 130, 246, 0.12),
|
||||
0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
box-shadow: 0 4px 20px rgba(59, 130, 246, 0.12), 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
}
|
||||
|
||||
.composer-textarea-wrapper {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
align-self: stretch;
|
||||
}
|
||||
|
||||
.composer-textarea {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
min-height: 24px;
|
||||
max-height: 192px;
|
||||
padding: 0.375rem 0;
|
||||
@@ -140,10 +133,7 @@
|
||||
background: transparent;
|
||||
color: var(--muted);
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background 0.2s ease,
|
||||
color 0.2s ease,
|
||||
transform 0.15s ease;
|
||||
transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
|
||||
flex-shrink: 0;
|
||||
user-select: none;
|
||||
}
|
||||
@@ -208,9 +198,7 @@
|
||||
background: var(--surface-bg);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 0.875rem;
|
||||
box-shadow:
|
||||
0 8px 30px rgba(0, 0, 0, 0.12),
|
||||
0 2px 8px rgba(0, 0, 0, 0.06);
|
||||
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.06);
|
||||
overflow: hidden;
|
||||
z-index: 50;
|
||||
animation: menuFadeIn 0.15s ease;
|
||||
@@ -318,9 +306,7 @@
|
||||
background: var(--surface-bg);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 1.5rem;
|
||||
box-shadow:
|
||||
0 2px 12px rgba(0, 0, 0, 0.05),
|
||||
0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
}
|
||||
|
||||
.orb-container {
|
||||
@@ -447,11 +433,7 @@
|
||||
font-family: inherit;
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background 0.2s ease,
|
||||
color 0.2s ease,
|
||||
border-color 0.2s ease,
|
||||
transform 0.15s ease;
|
||||
transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease, transform 0.15s ease;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
@@ -571,3 +553,35 @@
|
||||
min-width: 160px;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- Live voice call (taller view for the running transcript) ---------- */
|
||||
.ai-chat-input-wrapper.stream-active {
|
||||
min-height: min(580px, 78vh);
|
||||
}
|
||||
|
||||
.send-btn svg {
|
||||
animation: sendIconIn 0.18s ease;
|
||||
}
|
||||
|
||||
@keyframes sendIconIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: scale(0.7);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: scale(1);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.send-btn svg {
|
||||
animation: none !important;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.ai-chat-input-wrapper.stream-active {
|
||||
min-height: min(540px, 80vh);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
<div
|
||||
class="ai-chat-input-wrapper"
|
||||
[class.voice-active]="isVoiceMode()"
|
||||
[class.voice-active]="anyVoiceActive()"
|
||||
[class.stream-active]="streamOpen()"
|
||||
[attr.data-voice-state]="voiceState()"
|
||||
(keydown.escape)="onEscape()"
|
||||
>
|
||||
<!-- Announces every voice state change to screen readers -->
|
||||
<div class="sr-only" role="status" aria-live="polite">{{ statusText() }}</div>
|
||||
|
||||
<div class="composer-layer" [attr.inert]="isVoiceMode() ? '' : null">
|
||||
<div class="composer-layer" [attr.inert]="anyVoiceActive() ? '' : null">
|
||||
<div class="chat-composer" #composer>
|
||||
<button
|
||||
#plusBtn
|
||||
@@ -61,13 +62,26 @@
|
||||
<button
|
||||
type="button"
|
||||
class="composer-btn send-btn"
|
||||
[disabled]="!message().trim() || disabled()"
|
||||
(click)="sendMessage()"
|
||||
aria-label="ارسال پیام"
|
||||
[class.is-voice-call]="!canSend()"
|
||||
[disabled]="disabled()"
|
||||
[attr.aria-label]="actionLabel()"
|
||||
[attr.title]="actionLabel()"
|
||||
(click)="onPrimaryAction()"
|
||||
>
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
||||
<path d="M12 19V5M5 12l7-7 7 7" />
|
||||
</svg>
|
||||
@if (canSend()) {
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
||||
<path d="M12 19V5M5 12l7-7 7 7" />
|
||||
</svg>
|
||||
} @else {
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
||||
<path d="M2 10v3" />
|
||||
<path d="M6 6v11" />
|
||||
<path d="M10 3v18" />
|
||||
<path d="M14 8v7" />
|
||||
<path d="M18 5v13" />
|
||||
<path d="M22 10v3" />
|
||||
</svg>
|
||||
}
|
||||
</button>
|
||||
|
||||
@if (isAttachmentMenuOpen()) {
|
||||
@@ -136,9 +150,12 @@
|
||||
|
||||
<div
|
||||
class="voice-layer"
|
||||
[attr.inert]="isVoiceMode() ? null : ''"
|
||||
[attr.aria-hidden]="isVoiceMode() ? null : 'true'"
|
||||
[attr.inert]="anyVoiceActive() ? null : ''"
|
||||
[attr.aria-hidden]="anyVoiceActive() ? null : 'true'"
|
||||
>
|
||||
@if (layerMode() === 'stream') {
|
||||
<app-voice-stream-panel />
|
||||
} @else {
|
||||
<div class="voice-mode" role="region" aria-label="حالت صوتی">
|
||||
<div class="orb-container" #orbContainer aria-hidden="true">
|
||||
@if (!orbAvailable()) {
|
||||
@@ -190,5 +207,6 @@
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,3 +1,4 @@
|
||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
@@ -21,6 +22,9 @@ import {
|
||||
SpeechTranscript,
|
||||
} from '../../../services/speech-recognition/speech-recognition.service';
|
||||
import { VoiceOrbService } from '../../../services/voice-orb/voice-orb.service';
|
||||
import { VoiceStreamService } from '../../../services/voice-stream/voice-stream.service';
|
||||
import { VoiceMessage } from '../../../services/voice-stream/voice-stream.types';
|
||||
import { VoiceStreamPanelComponent } from '../voice-stream-panel/voice-stream-panel.component';
|
||||
|
||||
export type VoiceState =
|
||||
| 'idle'
|
||||
@@ -49,6 +53,8 @@ export const DEFAULT_ATTACHMENT_ACTIONS: readonly AttachmentAction[] = [
|
||||
];
|
||||
|
||||
const NOTICE_TIMEOUT_MS = 7000;
|
||||
/** Keeps the previous voice view mounted while the layer fades out (matches the 0.35 s CSS fade). */
|
||||
const LAYER_SWAP_MS = 400;
|
||||
|
||||
const MIC_ERRORS: Record<string, string> = {
|
||||
unsupported: 'مرورگر شما از دسترسی به میکروفون پشتیبانی نمیکند (اتصال امن HTTPS لازم است).',
|
||||
@@ -70,6 +76,7 @@ const SPEECH_ERRORS: Record<SpeechErrorKind, string> = {
|
||||
@Component({
|
||||
selector: 'app-ai-chat-input',
|
||||
standalone: true,
|
||||
imports: [VoiceStreamPanelComponent],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './ai-chat-input.component.html',
|
||||
styleUrl: './ai-chat-input.component.css',
|
||||
@@ -79,6 +86,7 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
private readonly voiceOrb = inject(VoiceOrbService);
|
||||
private readonly mic = inject(MicrophoneService);
|
||||
private readonly speech = inject(SpeechRecognitionService);
|
||||
private readonly voiceStream = inject(VoiceStreamService);
|
||||
|
||||
// ---- API ------------------------------------------------------------------
|
||||
readonly placeholder = input<string>('پیام خود را بنویسید...');
|
||||
@@ -91,6 +99,8 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
readonly messageSent = output<string>();
|
||||
readonly fileSelected = output<File>();
|
||||
readonly voiceToggled = output<boolean>();
|
||||
/** Emitted when a live voice call ends, with the full two-sided transcript. */
|
||||
readonly voiceSessionEnded = output<VoiceMessage[]>();
|
||||
|
||||
// ---- state ----------------------------------------------------------------
|
||||
readonly message = signal('');
|
||||
@@ -102,6 +112,14 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
readonly liveInterim = signal('');
|
||||
readonly orbAvailable = signal(true);
|
||||
|
||||
// Live voice call (voice-to-voice stream)
|
||||
readonly streamOpen = this.voiceStream.isOpen;
|
||||
/** Which view the voice layer shows; swapped after the fade so nothing flashes. */
|
||||
readonly layerMode = signal<'dictation' | 'stream'>('dictation');
|
||||
readonly canSend = computed(() => !!this.message().trim());
|
||||
readonly anyVoiceActive = computed(() => this.isVoiceMode() || this.streamOpen());
|
||||
readonly actionLabel = computed(() => (this.canSend() ? 'ارسال پیام' : 'شروع گفتگوی صوتی'));
|
||||
|
||||
readonly voiceSupported = this.mic.isSupported && this.speech.isSupported;
|
||||
|
||||
/** The orb view replaces the composer while recording, transcribing and stopping. */
|
||||
@@ -155,6 +173,7 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
/** Textarea content when recording started; dictated text is appended to it. */
|
||||
private baseText = '';
|
||||
private noticeTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private layerTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor() {
|
||||
// The outside-click listener only exists while the menu is open.
|
||||
@@ -169,13 +188,23 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
document.addEventListener('mousedown', onMouseDown);
|
||||
onCleanup(() => document.removeEventListener('mousedown', onMouseDown));
|
||||
});
|
||||
|
||||
this.voiceStream.sessionEnded$.pipe(takeUntilDestroyed()).subscribe(({ messages, error }) => {
|
||||
this.voiceToggled.emit(false);
|
||||
this.voiceSessionEnded.emit(messages);
|
||||
if (error) this.showNotice({ kind: 'error', text: error });
|
||||
this.scheduleLayerReset();
|
||||
this.focusTextarea();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.voiceSession++;
|
||||
this.speech.dispose();
|
||||
this.mic.release();
|
||||
this.voiceStream.dispose();
|
||||
this.voiceOrb.dispose();
|
||||
if (this.layerTimer) clearTimeout(this.layerTimer);
|
||||
this.clearNoticeTimer();
|
||||
}
|
||||
|
||||
@@ -297,11 +326,61 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
if (this.isAttachmentMenuOpen()) {
|
||||
this.closeAttachmentMenu();
|
||||
this.plusBtnRef()?.nativeElement.focus();
|
||||
} else if (this.streamOpen()) {
|
||||
this.voiceStream.end();
|
||||
} else if (this.isVoiceMode()) {
|
||||
void this.stopVoice(true);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- live voice call (voice-to-voice stream) --------------------------------
|
||||
|
||||
/** Send arrow when there is text, waveform (start call) when the textarea is empty. */
|
||||
onPrimaryAction(): void {
|
||||
if (this.canSend()) {
|
||||
this.sendMessage();
|
||||
} else {
|
||||
void this.startVoiceStream();
|
||||
}
|
||||
}
|
||||
|
||||
async startVoiceStream(): Promise<void> {
|
||||
if (this.disabled() || this.streamOpen()) return;
|
||||
const dictating = this.voiceState() !== 'idle' && this.voiceState() !== 'error';
|
||||
if (dictating) return;
|
||||
|
||||
this.dismissNotice();
|
||||
this.closeAttachmentMenu();
|
||||
this.cancelLayerReset();
|
||||
this.layerMode.set('stream');
|
||||
this.voiceToggled.emit(true);
|
||||
|
||||
const result = await this.voiceStream.start();
|
||||
if (!result.ok) {
|
||||
// The call never connected, so no sessionEnded event is emitted for it.
|
||||
this.voiceToggled.emit(false);
|
||||
this.scheduleLayerReset();
|
||||
if (result.reason !== 'cancelled') {
|
||||
this.showNotice({ kind: 'error', text: result.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleLayerReset(): void {
|
||||
this.cancelLayerReset();
|
||||
this.layerTimer = setTimeout(() => {
|
||||
this.layerTimer = null;
|
||||
if (!this.streamOpen()) this.layerMode.set('dictation');
|
||||
}, LAYER_SWAP_MS);
|
||||
}
|
||||
|
||||
private cancelLayerReset(): void {
|
||||
if (this.layerTimer) {
|
||||
clearTimeout(this.layerTimer);
|
||||
this.layerTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- voice: start ---------------------------------------------------------
|
||||
|
||||
async toggleVoiceMode(): Promise<void> {
|
||||
@@ -322,8 +401,10 @@ export class AiChatInputComponent implements OnDestroy {
|
||||
}
|
||||
|
||||
private async startVoice(): Promise<void> {
|
||||
if (this.disabled()) return;
|
||||
if (this.disabled() || this.streamOpen()) return;
|
||||
|
||||
this.cancelLayerReset();
|
||||
this.layerMode.set('dictation');
|
||||
this.dismissNotice();
|
||||
this.closeAttachmentMenu();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user