Skip to content

Commit

Permalink
fix: wrong azure key error
Browse files Browse the repository at this point in the history
  • Loading branch information
hahahumble committed Apr 21, 2023
1 parent 2b5d908 commit 6a236c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/apis/azureToken.ts
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}`);
}
}
26 changes: 20 additions & 6 deletions src/utils/speechSynthesis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import generateSpeechUrl from '../apis/amazonPolly';
import textToSpeech from '../apis/azureTTS';
import { SpeakerAudioDestination} from 'microsoft-cognitiveservices-speech-sdk'
import speechSynthesizeWithPolly from '../apis/amazonPolly';
import speechSynthesizeWithAzure from '../apis/azureTTS';
import { SpeakerAudioDestination } from 'microsoft-cognitiveservices-speech-sdk';
import { getAzureToken } from '../apis/azureToken';

interface SpeechSynthesisOptions {
text: string;
service: 'System' | 'Amazon Polly' | 'Azure TTS';
Expand Down Expand Up @@ -36,7 +38,7 @@ async function getPollyVoices({
accessKeyId,
secretAccessKey,
}: getPollyVoicesOptions) {
return await generateSpeechUrl(text, voiceName, engine, region, accessKeyId, secretAccessKey);
return await speechSynthesizeWithPolly(text, voiceName, engine, region, accessKeyId, secretAccessKey);
}

function pollyEngineName(engine: string | undefined) {
Expand Down Expand Up @@ -129,12 +131,24 @@ export function speechSynthesis({
});
break;
case 'Azure TTS':
textToSpeech(secretAccessKey || '', region || 'eastus', text, voiceName, language)
if (secretAccessKey == '') {
reject('Azure access key is empty');
notify.emptyAzureKeyNotify();
return;
}
// Check if secret access key and region is valid
getAzureToken(secretAccessKey || '', region || 'eastus')
.then(token => {})
.catch(error => {
notify.invalidAzureKeyNotify();
reject(error);
});
speechSynthesizeWithAzure(secretAccessKey || '', region || 'eastus', text, voiceName, language)
.then(player => {
azureAudio = player;
player.onAudioEnd = () => {
resolve();
}
};
})
.catch(error => {
console.error(error);
Expand Down

0 comments on commit 6a236c3

Please sign in to comment.