-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a115c3b
commit 3dfcf6b
Showing
30 changed files
with
301 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ __test__ | |
# production | ||
dist | ||
es | ||
lib | ||
logs | ||
|
||
# misc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ __snapshots__ | |
# production | ||
dist | ||
es | ||
lib | ||
logs | ||
|
||
# umi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import urlJoin from 'url-join'; | ||
|
||
export const MICROSOFT_SPEECH_PROXY_URL = process.env.MICROSOFT_SPEECH_PROXY_URL || ''; | ||
export const AZURE_SPEECH_KEY = process.env.AZURE_SPEECH_KEY || ''; | ||
export const AZURE_SPEECH_REGION = process.env.AZURE_SPEECH_REGION || ''; | ||
export const OPENAI_API_KEY = process.env.OPENAI_API_KEY || ''; | ||
export const OPENAI_PROXY_URL = process.env.OPENAI_PROXY_URL || 'https://api.openai.com/v1'; | ||
export const OPENAI_TTS_URL = (api?: string) => urlJoin(api || OPENAI_PROXY_URL, 'audio/speech'); | ||
export const OPENAI_STT_URL = (api: string) => | ||
urlJoin(api || OPENAI_PROXY_URL, 'audio/transcriptions'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["alloy", "echo", "fable", "onyx", "nova", "shimmer"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
export { fetchAzureSpeech } from './services/fetchAzureSpeech'; | ||
export { fetchEdgeSpeech } from './services/fetchEdgeSpeech'; | ||
export { fetchMicrosoftSpeech } from './services/fetchMicrosoftSpeech'; | ||
export { fetchOpenaiSTT } from './services/fetchOpenaiSTT'; | ||
export { fetchOpenaiTTS } from './services/fetchOpenaiTTS'; | ||
export { useAzureSpeech } from './useAzureSpeech'; | ||
export { useEdgeSpeech } from './useEdgeSpeech'; | ||
export { useMicrosoftSpeech } from './useMicrosoftSpeech'; | ||
export { useOpenaiTTS } from './useOpenaiTTS'; | ||
export { usePersistedSpeechRecognition } from './useSpeechRecognition/usePersistedSpeechRecognition'; | ||
export { useSpeechRecognition } from './useSpeechRecognition/useSpeechRecognition'; | ||
export { useSpeechSynthes } from './useSpeechSynthes'; | ||
export { | ||
getAzureVoiceList, | ||
getEdgeVoiceList, | ||
getOpenaiVoiceList, | ||
getSpeechSynthesVoiceList, | ||
} from './utils/getVoiceList'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
src/services/postMicrosoftSpeech.ts → src/services/fetchMicrosoftSpeech.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import { OPENAI_API_KEY, OPENAI_STT_URL } from '@/const/api'; | ||
|
||
export interface OpenaiTtsOptions { | ||
api: { | ||
key: string; | ||
proxy: string; | ||
}; | ||
model?: 'whisper-1'; | ||
} | ||
|
||
// 纯文本生成语音 | ||
export const fetchOpenaiSTT = async ( | ||
speech: Blob, | ||
{ api, model = 'whisper-1' }: OpenaiTtsOptions, | ||
): Promise<string> => { | ||
const key = api.key || OPENAI_API_KEY; | ||
const url = OPENAI_STT_URL(api.proxy); | ||
|
||
const headers = new Headers({ | ||
'Authorization': `Bearer ${key}`, | ||
'Content-Type': 'multipart/form-data', | ||
}); | ||
|
||
const body = new FormData(); | ||
body.append('file', speech, `${uuidv4()}.webm`); | ||
body.append('model', model); | ||
|
||
const response: Response = await fetch(url, { body, headers, method: 'POST' }); | ||
|
||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
|
||
const json = await response.json(); | ||
|
||
return json?.text; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { OPENAI_API_KEY, OPENAI_TTS_URL } from '@/const/api'; | ||
|
||
import { type SsmlOptions } from '../utils/genSSML'; | ||
|
||
export type OpenaiVoice = 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer'; | ||
|
||
export interface OpenaiTtsOptions extends SsmlOptions { | ||
api: { | ||
key: string; | ||
proxy: string; | ||
}; | ||
model?: 'tts-1' | 'tts-1-hd'; | ||
name: OpenaiVoice; | ||
} | ||
|
||
// 纯文本生成语音 | ||
export const fetchOpenaiTTS = async ( | ||
text: string, | ||
{ api, model = 'tts-1', ...options }: OpenaiTtsOptions, | ||
): Promise<AudioBufferSourceNode> => { | ||
const key = api.key || OPENAI_API_KEY; | ||
const url = OPENAI_TTS_URL(api.proxy); | ||
|
||
const headers = new Headers({ | ||
'Authorization': `Bearer ${key}`, | ||
'Content-Type': 'application/json', | ||
}); | ||
|
||
const body = JSON.stringify({ | ||
input: text, | ||
model, | ||
voice: options.name, | ||
}); | ||
|
||
const response: Response = await fetch(url, { body, headers, method: 'POST' }); | ||
|
||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
|
||
const audioData = await response.arrayBuffer(); | ||
const audioContext = new AudioContext(); | ||
const audioBufferSource = audioContext.createBufferSource(); | ||
audioBufferSource.buffer = await audioContext.decodeAudioData(audioData); | ||
audioBufferSource.connect(audioContext.destination); | ||
return audioBufferSource; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.