Skip to content

Commit

Permalink
🐛 fix: Fix hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 12, 2023
1 parent 95aafec commit 48f25c0
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
19 changes: 12 additions & 7 deletions src/services/fetchAzureSpeech.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { AZURE_SPEECH_PROXY_URL } from '@/const/api';
import { AZURE_SPEECH_KEY, AZURE_SPEECH_PROXY_URL, AZURE_SPEECH_REGION } from '@/const/api';
import { arrayBufferConvert } from '@/utils/arrayBufferConvert';
import { type SsmlOptions, genSSML } from '@/utils/genSSML';

export interface AzureSpeechOptions extends SsmlOptions {
api: {
key: string;
region: string;
api?: {
key?: string;
proxy?: string;
region?: string;
};
}

export const fetchAzureSpeech = async (
text: string,
{ api, ...options }: AzureSpeechOptions,
{ api = {}, ...options }: AzureSpeechOptions,
): Promise<AudioBuffer> => {
const data = JSON.stringify({
api,
api: {
key: api?.key || AZURE_SPEECH_KEY,
region: api?.region || AZURE_SPEECH_REGION,
},
ssml: genSSML(text, options),
});
const url = api?.proxy || AZURE_SPEECH_PROXY_URL;

const response: Response = await fetch(AZURE_SPEECH_PROXY_URL, {
const response: Response = await fetch(url, {
body: data,
method: 'POST',
// @ts-ignore
Expand Down
12 changes: 6 additions & 6 deletions src/services/fetchEdgeSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ const genHeader = (connectId: string) => {
};

export interface EdgeSpeechOptions extends Pick<SsmlOptions, 'name'> {
api: {
key: string;
proxy: string;
api?: {
key?: string;
proxy?: string;
};
}
export const fetchEdgeSpeech = async (
text: string,
{ api, ...options }: EdgeSpeechOptions,
{ api = {}, ...options }: EdgeSpeechOptions,
): Promise<AudioBuffer> => {
const connectId = uuidv4().replaceAll('-', '');
const url = qs.stringifyUrl({
query: {
ConnectionId: connectId,
TrustedClientToken: api.key || EDDGE_API_TOKEN,
TrustedClientToken: api?.key || EDDGE_API_TOKEN,
},
url: api.proxy || EDDGE_PROXY_URL,
url: api?.proxy || EDDGE_PROXY_URL,
});

const { configHeader, contentHeader } = genHeader(connectId);
Expand Down
9 changes: 6 additions & 3 deletions src/services/fetchMicrosoftSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { type SsmlOptions } from '@/utils/genSSML';
import { genSSML } from '@/utils/genSSML';

export interface MicrosoftSpeechOptions extends SsmlOptions {
api?: string;
api?: {
proxy?: string;
};
}

export const fetchMicrosoftSpeech = async (
text: string,
{ api, ...options }: MicrosoftSpeechOptions,
{ api = {}, ...options }: MicrosoftSpeechOptions,
): Promise<AudioBuffer> => {
const data = JSON.stringify({
offsetInPlainText: 0,
Expand All @@ -19,8 +21,9 @@ export const fetchMicrosoftSpeech = async (
ssml: genSSML(text, options),
ttsAudioFormat: 'audio-24khz-160kbitrate-mono-mp3',
});
const url = api?.proxy || MICROSOFT_SPEECH_PROXY_URL;

const response: Response = await fetch(api || MICROSOFT_SPEECH_PROXY_URL, {
const response: Response = await fetch(url, {
body: data,
method: 'POST',
// @ts-ignore
Expand Down
6 changes: 2 additions & 4 deletions src/services/fetchOpenaiSTT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { OPENAI_API_KEY, OPENAI_STT_URL } from '@/const/api';

export interface OpenaiSttOptions {
api?: {
key: string;
key?: string;
proxy?: string;
};
model?: 'whisper-1';
Expand All @@ -13,7 +13,7 @@ export interface OpenaiSttOptions {
// 纯文本生成语音
export const fetchOpenaiSTT = async (
speech: Blob,
{ api, model = 'whisper-1' }: OpenaiSttOptions,
{ api = {}, model = 'whisper-1' }: OpenaiSttOptions,
): Promise<string> => {
const key = api?.key || OPENAI_API_KEY;
const url = OPENAI_STT_URL(api?.proxy);
Expand All @@ -37,7 +37,5 @@ export const fetchOpenaiSTT = async (

const json = await response.json();

console.log(json);

return json?.text;
};
10 changes: 5 additions & 5 deletions src/services/fetchOpenaiTTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export type OpenaiVoice = 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimme

export interface OpenaiTtsOptions extends Pick<SsmlOptions, 'name'> {
api: {
key: string;
proxy: string;
key?: string;
proxy?: string;
};
model?: 'tts-1' | 'tts-1-hd';
name: OpenaiVoice | string;
}
export const fetchOpenaiTTS = async (
text: string,
{ api, model = 'tts-1', ...options }: OpenaiTtsOptions,
{ api = {}, model = 'tts-1', ...options }: OpenaiTtsOptions,
): Promise<AudioBuffer> => {
const key = api.key || OPENAI_API_KEY;
const url = OPENAI_TTS_URL(api.proxy);
const key = api?.key || OPENAI_API_KEY;
const url = OPENAI_TTS_URL(api?.proxy);

const headers = new Headers({
'Authorization': `Bearer ${key}`,
Expand Down
4 changes: 2 additions & 2 deletions src/useAudioRecorder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export const useAudioRecorder = (onBolbAvailable?: (blob: Blob) => void) => {
setMediaRecorder();
});
})
.catch((error: DOMException) => {
console.log(error.name, error.message, error.cause);
.catch((error) => {
console.error(error);
});
}, [timerInterval, _startTimer, url]);

Expand Down
9 changes: 3 additions & 6 deletions src/useAzureSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { useState } from 'react';
import { AzureSpeechOptions, fetchAzureSpeech } from '@/services/fetchAzureSpeech';
import { useTTS } from '@/useTTS';

export const useAzureSpeech = (
defaultText: string,
{ api, name, style, pitch, rate }: AzureSpeechOptions,
) => {
export const useAzureSpeech = (defaultText: string, options: AzureSpeechOptions) => {
const [text, setText] = useState<string>(defaultText);
const rest = useTTS(name, text, (segmentText: string) =>
fetchAzureSpeech(segmentText, { api, name, pitch, rate, style }),
const rest = useTTS(options.name, text, (segmentText: string) =>
fetchAzureSpeech(segmentText, options),
);
return {
setText,
Expand Down
6 changes: 3 additions & 3 deletions src/useEdgeSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useState } from 'react';
import { EdgeSpeechOptions, fetchEdgeSpeech } from '@/services/fetchEdgeSpeech';
import { useTTS } from '@/useTTS';

export const useEdgeSpeech = (defaultText: string, { api, name }: EdgeSpeechOptions) => {
export const useEdgeSpeech = (defaultText: string, options: EdgeSpeechOptions) => {
const [text, setText] = useState<string>(defaultText);
const rest = useTTS(name, text, (segmentText: string) =>
fetchEdgeSpeech(segmentText, { api, name }),
const rest = useTTS(options.name, text, (segmentText: string) =>
fetchEdgeSpeech(segmentText, options),
);
return {
setText,
Expand Down
9 changes: 3 additions & 6 deletions src/useMicrosoftSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { useState } from 'react';
import { type MicrosoftSpeechOptions, fetchMicrosoftSpeech } from '@/services/fetchMicrosoftSpeech';
import { useTTS } from '@/useTTS';

export const useMicrosoftSpeech = (
defaultText: string,
{ api, name, pitch, rate, style }: MicrosoftSpeechOptions,
) => {
export const useMicrosoftSpeech = (defaultText: string, options: MicrosoftSpeechOptions) => {
const [text, setText] = useState<string>(defaultText);
const rest = useTTS(name, text, (segmentText: string) =>
fetchMicrosoftSpeech(segmentText, { api, name, pitch, rate, style }),
const rest = useTTS(options.name, text, (segmentText: string) =>
fetchMicrosoftSpeech(segmentText, options),
);
return {
setText,
Expand Down
6 changes: 3 additions & 3 deletions src/useOpenaiTTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useState } from 'react';
import { type OpenaiTtsOptions, fetchOpenaiTTS } from '@/services/fetchOpenaiTTS';
import { useTTS } from '@/useTTS';

export const useOpenaiTTS = (defaultText: string, { api, name }: OpenaiTtsOptions) => {
export const useOpenaiTTS = (defaultText: string, options: OpenaiTtsOptions) => {
const [text, setText] = useState<string>(defaultText);
const rest = useTTS(name, text, (segmentText: string) =>
fetchOpenaiTTS(segmentText, { api, name }),
const rest = useTTS(options.name, text, (segmentText: string) =>
fetchOpenaiTTS(segmentText, options),
);
return {
setText,
Expand Down

0 comments on commit 48f25c0

Please sign in to comment.