Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use sdk to complete azure text-to-speech
fix: use sdk to complete azure text-to-speech
- Loading branch information
Showing
9 changed files
with
76 additions
and
49 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
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,41 +1,32 @@ | ||
import axios, { AxiosRequestConfig } from 'axios'; | ||
import * as sdk from 'microsoft-cognitiveservices-speech-sdk'; | ||
import { azureSynthesisErrorNotify } from '../components/Notification'; | ||
|
||
const textToSpeech = async ( | ||
const speechSynthesizeWithAzure = async ( | ||
subscriptionKey: string, | ||
region: string, | ||
text: string, | ||
voiceName: string, | ||
language: string | ||
) => { | ||
const request: AxiosRequestConfig = { | ||
method: 'POST', | ||
url: `https://${region}.tts.speech.microsoft.com/cognitiveservices/v1`, | ||
headers: { | ||
'Content-Type': 'application/ssml+xml', | ||
'X-Microsoft-OutputFormat': 'riff-16khz-16bit-mono-pcm', | ||
Authorization: `Bearer ${await getAccessToken(subscriptionKey, region)}`, | ||
console.time('Azure speech synthesis'); | ||
const speechConfig = sdk.SpeechConfig.fromSubscription(subscriptionKey, region); | ||
speechConfig.speechRecognitionLanguage = language; | ||
speechConfig.speechSynthesisVoiceName = voiceName; | ||
const player = new sdk.SpeakerAudioDestination(); | ||
const audioConfig = sdk.AudioConfig.fromSpeakerOutput(player); | ||
const speechSynthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig); | ||
speechSynthesizer.speakTextAsync( | ||
text, | ||
result => { | ||
console.timeEnd('Azure speech synthesis'); | ||
speechSynthesizer.close(); | ||
}, | ||
data: `<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='${language}'><voice name='${voiceName}'>${text}</voice></speak>`, | ||
responseType: 'arraybuffer', | ||
}; | ||
|
||
const response = await axios(request); | ||
|
||
return new Blob([response.data], { type: 'audio/wav' }); | ||
}; | ||
|
||
const getAccessToken = async (subscriptionKey: string, region: string) => { | ||
const request: AxiosRequestConfig = { | ||
method: 'POST', | ||
url: `https://${region}.api.cognitive.microsoft.com/sts/v1.0/issueToken`, | ||
headers: { | ||
'Ocp-Apim-Subscription-Key': subscriptionKey, | ||
}, | ||
}; | ||
|
||
const response = await axios(request); | ||
|
||
return response.data; | ||
error => { | ||
console.log(error); | ||
azureSynthesisErrorNotify(); | ||
speechSynthesizer.close(); | ||
} | ||
); | ||
return player; | ||
}; | ||
|
||
export default textToSpeech; | ||
export default speechSynthesizeWithAzure; |
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,18 @@ | ||
import axios from 'axios'; | ||
|
||
export async function getAzureToken(subscriptionKey: string, region: string): Promise<string> { | ||
const url = `https://${region}.api.cognitive.microsoft.com/sts/v1.0/issueToken`; | ||
|
||
try { | ||
const response = await axios.post(url, null, { | ||
headers: { | ||
'Ocp-Apim-Subscription-Key': subscriptionKey, | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
|
||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Error getting token: ${error}`); | ||
} | ||
} |
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
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
6fb0ae2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
speechgpt – ./
speechgpt-hahahumble.vercel.app
speechgpt-alpha.vercel.app
speechgpt-git-main-hahahumble.vercel.app