-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-SpeechToText.ps1
43 lines (39 loc) · 1.54 KB
/
Get-SpeechToText.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function Get-SpeechToText() {
<#
.SYNOPSIS
Takes a WAV file with spoken words and retrieves the text via Azure Cognitive Services
.DESCRIPTION
Takes a WAV file with spoken words and retrieves the text via Azure Cognitive Services
.PARAMETER SubscriptionKey
The Speech Services API key for this Cognitive Speech Service object
.PARAMETER Token
Token received after calling Get-SpeechAccessToken
.PARAMETER Path
The full path to the file
.PARAMETER Region
The Azure region where the Speech Service resides, ex. "eastus"
.PARAMETER Language
The language code to use, ex. "en-US"
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)] [Alias("FilePath")] [String] $path,
[Parameter(Mandatory = $true)] [string] $Token,
[Parameter(Mandatory = $true)] [string] $Region,
[Parameter(Mandatory = $false)] [string] $Format = "simple",
[Parameter(Mandatory = $false)] [Alias("LangCode")] [string] $Language = "en-US"
)
# Read audio into byte array
$audioBytes = [System.IO.File]::ReadAllBytes($path)
# Conversion URI
$conversionUri = "https://$Region.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=$Language&format=$format"
# Conversion Headers
$Headers = @{
'Authorization' = "Bearer " + $token;
'Content-type' = 'audio/pcm; codec=audio/pcm; samplerate=16000'
}
# Convert
$result = Invoke-RestMethod -Method POST -Uri $conversionURI -Headers $Headers -Body $audioBytes -SkipHeaderValidation -TransferEncoding "chunked"
# Result
return $result
}