Funk-effekt und Mikrifon-Einstellungen hinzugefügt

This commit is contained in:
PxlLoewe
2025-07-16 23:24:55 -07:00
parent 66c32530a7
commit 7be0c701a4
3 changed files with 49 additions and 18 deletions

View File

@@ -5,12 +5,21 @@ import {
handleTrackSubscribed,
handleTrackUnsubscribed,
} from "_helpers/liveKitEventHandler";
import { ConnectionQuality, Participant, Room, RoomEvent, RpcInvocationData } from "livekit-client";
import {
ConnectionQuality,
LocalTrackPublication,
Participant,
Room,
RoomEvent,
RpcInvocationData,
Track,
} from "livekit-client";
import { pilotSocket } from "(app)/pilot/socket";
import { create } from "zustand";
import axios from "axios";
import { useDispatchConnectionStore } from "_store/dispatch/connectionStore";
import { changeDispatcherAPI } from "_querys/dispatcher";
import { getRadioStream } from "_helpers/radioEffect";
let interval: NodeJS.Timeout;
@@ -32,6 +41,7 @@ type TalkState = {
addSpeakingParticipant: (participant: Participant) => void;
removeSpeakingParticipant: (speakingParticipants: Participant) => void;
room: Room | null;
localRadioTrack: LocalTrackPublication | undefined;
};
const getToken = async (roomName: string) => {
const response = await axios.get(`/api/livekit-token?roomName=${roomName}`);
@@ -41,6 +51,7 @@ const getToken = async (roomName: string) => {
export const useAudioStore = create<TalkState>((set, get) => ({
isTalking: false,
localRadioTrack: undefined,
transmitBlocked: false,
message: null,
micDeviceId: null,
@@ -75,9 +86,18 @@ export const useAudioStore = create<TalkState>((set, get) => ({
},
setMic: (micDeviceId, micVolume) => {
set({ micDeviceId, micVolume });
if (get().state === "connected") {
const { room, disconnect, connect } = get();
const role = room?.localParticipant.attributes.role;
console.log(role);
if (room?.name || role) {
disconnect();
connect(room?.name || "", role || "user");
}
}
},
toggleTalking: () => {
const { room, isTalking, micDeviceId, speakingParticipants, transmitBlocked } = get();
const { room, isTalking, speakingParticipants, transmitBlocked } = get();
if (!room) return;
if (speakingParticipants.length > 0 && !isTalking && !transmitBlocked) {
@@ -94,10 +114,7 @@ export const useAudioStore = create<TalkState>((set, get) => ({
});
return;
}
// Todo: use micVolume
room.localParticipant.setMicrophoneEnabled(!isTalking, {
deviceId: micDeviceId ?? undefined,
});
room.localParticipant.setMicrophoneEnabled(!isTalking);
set((state) => ({ isTalking: !state.isTalking, transmitBlocked: false }));
},
@@ -131,6 +148,27 @@ export const useAudioStore = create<TalkState>((set, get) => ({
});
}
const inputStream = await navigator.mediaDevices.getUserMedia({
audio: {
deviceId: get().micDeviceId ?? undefined,
noiseSuppression: true,
},
});
// Funk-Effekt anwenden
const radioStream = getRadioStream(inputStream, get().micVolume);
if (!radioStream) throw new Error("Konnte Funkstream nicht erzeugen");
const [track] = radioStream.getAudioTracks();
if (!track) throw new Error("Konnte Audio-Track nicht erzeugen");
const publishedTrack = await room.localParticipant.publishTrack(track, {
name: "radio-audio",
source: Track.Source.Microphone,
});
await publishedTrack.mute();
set({ localRadioTrack: publishedTrack });
set({ state: "connected", room, message: null });
})
.on(RoomEvent.Disconnected, () => {