|
1 | | -import { AudioChannel } from '@/enums' |
2 | | -import { SonarException } from '@/exceptions' |
| 1 | +import { AudioChannel, StreamerPath } from '@/enums' |
3 | 2 | import { convertVolumeToUser } from '@/functions/converters/convert-volume-to-user' |
4 | | -import type { ApiChannelDataStreamer, ApiVolumeDataStreamer } from '@/models/api-volume-data-streamer.ok' |
5 | | -import type { AudioDataStreamer, ChannelAudioDataStreamer } from '@/types/audio-data-streamer' |
6 | | - |
7 | | -const DEFAULT_ERROR_TEXT = 'Failed to get audio data.' |
| 3 | +import type { VolumeInfoStreamer } from '@/sonar/models/audio-settings/volume-info-streamer' |
| 4 | +import { requestVolumeSettingsStreamer } from '@/sonar/requests/volume-settings/request-volume-settings-streamer' |
| 5 | +import type { ChannelVolumeStreamer } from '@/types/channel-volume-streamer' |
| 6 | +import type { ChannelVolumesStreamer } from '@/types/channel-volumes-streamer' |
8 | 7 |
|
9 | 8 | /** |
10 | 9 | * Gets audio data for all channels. |
11 | 10 | * @param sonarEndpoint Sonar endpoint URL |
12 | 11 | * @returns volume in the range of 0 to 100, |
13 | 12 | */ |
14 | | -export async function getAudioDataStream(sonarEndpoint: string): Promise<AudioDataStreamer> { |
15 | | - let response: Response |
16 | | - |
17 | | - try { |
18 | | - response = await fetch(`${sonarEndpoint}/volumeSettings/streamer`) |
19 | | - } catch (error) { |
20 | | - throw new SonarException(DEFAULT_ERROR_TEXT, error as Error) |
21 | | - } |
22 | | - |
23 | | - if (response.ok) { |
24 | | - const data = (await response.json()) as ApiVolumeDataStreamer |
25 | | - if (data?.masters?.stream == null) { |
26 | | - throw new SonarException(`${DEFAULT_ERROR_TEXT} Missing required data in response.`) |
27 | | - } |
28 | | - const volumeData: AudioDataStreamer = { |
29 | | - [AudioChannel.Master]: createResponseVolumeData(data.masters), |
30 | | - [AudioChannel.Game]: data.devices.game && createResponseVolumeData(data.devices.game), |
31 | | - [AudioChannel.Chat]: data.devices.chatRender && createResponseVolumeData(data.devices.chatRender), |
32 | | - [AudioChannel.Media]: data.devices.media && createResponseVolumeData(data.devices.media), |
33 | | - [AudioChannel.Aux]: data.devices.aux && createResponseVolumeData(data.devices.aux), |
34 | | - [AudioChannel.Mic]: data.devices.chatCapture && createResponseVolumeData(data.devices.chatCapture) |
35 | | - } |
36 | | - return volumeData |
37 | | - } else { |
38 | | - const data = await response.text() |
39 | | - throw new SonarException(DEFAULT_ERROR_TEXT, new Error(data)) |
| 13 | +export async function getAudioDataStream(sonarEndpoint: string): Promise<ChannelVolumesStreamer> { |
| 14 | + const data = await requestVolumeSettingsStreamer(sonarEndpoint) |
| 15 | + const volumeData: ChannelVolumesStreamer = { |
| 16 | + [AudioChannel.Master]: createResponseVolumeData(data.masters.stream), |
| 17 | + [AudioChannel.Game]: data.devices.game && createResponseVolumeData(data.devices.game.stream), |
| 18 | + [AudioChannel.Chat]: data.devices.chatRender && createResponseVolumeData(data.devices.chatRender.stream), |
| 19 | + [AudioChannel.Media]: data.devices.media && createResponseVolumeData(data.devices.media.stream), |
| 20 | + [AudioChannel.Aux]: data.devices.aux && createResponseVolumeData(data.devices.aux.stream), |
| 21 | + [AudioChannel.Mic]: data.devices.chatCapture && createResponseVolumeData(data.devices.chatCapture.stream) |
40 | 22 | } |
| 23 | + return volumeData |
41 | 24 | } |
42 | 25 |
|
43 | | -function createResponseVolumeData(volumeData: ApiChannelDataStreamer): ChannelAudioDataStreamer { |
| 26 | +function createResponseVolumeData(volumeData: VolumeInfoStreamer): ChannelVolumeStreamer { |
44 | 27 | return { |
45 | | - volumeStreamer: convertVolumeToUser(volumeData.stream.streaming.volume), |
46 | | - volumeMonitoring: convertVolumeToUser(volumeData.stream.monitoring.volume), |
47 | | - isMutedStreamer: volumeData.stream.streaming.isMuted, |
48 | | - isMutedMonitoring: volumeData.stream.monitoring.isMuted |
| 28 | + [StreamerPath.Streaming]: { |
| 29 | + volume: convertVolumeToUser(volumeData.streaming.volume), |
| 30 | + isMuted: volumeData.streaming.muted |
| 31 | + }, |
| 32 | + [StreamerPath.Monitoring]: { |
| 33 | + volume: convertVolumeToUser(volumeData.monitoring.volume), |
| 34 | + isMuted: volumeData.monitoring.muted |
| 35 | + } |
49 | 36 | } |
50 | 37 | } |
0 commit comments