Skip to content

Commit

Permalink
updated the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
muschellij2 committed Dec 25, 2018
1 parent 29a8a22 commit 8ba44da
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 134 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
.RData
.Ruserdata
inst/doc
example.mp3
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,5 +1,5 @@
Package: mscstts
Version: 0.4.0
Version: 0.5.0
Title: R Client for the Microsoft Cognitive Services 'Text-to-Speech' REST API
Description: R Client for the Microsoft Cognitive Services
'Text-to-Speech' REST API, including voice synthesis. A valid account
Expand Down
21 changes: 13 additions & 8 deletions NAMESPACE
@@ -1,21 +1,26 @@
# Generated by roxygen2: do not edit by hand

S3method(print,token)
export(create_ssml)
export(get_ms_tts_key)
export(get_ms_tts_token)
export(have_ms_tts_key)
export(language_to_ms_name)
export(ms_auth_url)
export(ms_create_ssml)
export(ms_expired_token)
export(ms_get_tts_key)
export(ms_get_tts_token)
export(ms_have_tts_key)
export(ms_language_codes)
export(ms_language_to_ms_name)
export(ms_languages)
export(ms_locale_df)
export(ms_locales)
export(ms_region)
export(ms_regions)
export(ms_set_region)
export(ms_set_tts_key)
export(ms_synthesize)
export(ms_synthesize_api_url)
export(ms_valid_tts_key)
export(ms_validate_language_gender)
export(reveal)
export(set_ms_tts_key)
export(valid_ms_tts_key)
export(validate_language_gender)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
Expand Down
12 changes: 6 additions & 6 deletions R/create_ssml.R
Expand Up @@ -11,13 +11,13 @@
#' @export
#'
#' @examples
#' create_ssml("hey I really like things & dogs", escape = TRUE)
#' create_ssml("hey I really like things")
#' create_ssml('hey I <emphasis level="strong">really like</emphasis> things')
#' create_ssml('hey I <emphasis level="strong">really like</emphasis> things',
#' ms_create_ssml("hey I really like things & dogs", escape = TRUE)
#' ms_create_ssml("hey I really like things")
#' ms_create_ssml('hey I <emphasis level="strong">really like</emphasis> things')
#' ms_create_ssml('hey I <emphasis level="strong">really like</emphasis> things',
#' escape = TRUE)
#'
create_ssml = function(
ms_create_ssml = function(
script,
gender = c("Female", "Male"),
language = "en-US",
Expand All @@ -28,7 +28,7 @@ create_ssml = function(
script = gsub("[<>/]", "", script)
script = gsub("&", "and", script)
}
xname = language_to_ms_name(language = language, gender = gender)
xname = ms_language_to_ms_name(language = language, gender = gender)
stopifnot(length(language) == 1)
gender = match.arg(gender)
stopifnot(length(gender) == 1)
Expand Down
12 changes: 6 additions & 6 deletions R/language_to_ms_name.R
Expand Up @@ -7,22 +7,22 @@
#' @export
#'
#' @examples
#' language_to_ms_name()
#' validate_language_gender()
language_to_ms_name = function(
#' ms_language_to_ms_name()
#' ms_validate_language_gender()
ms_language_to_ms_name = function(
language = "en-US",
gender = c("Female", "Male")) {

L = validate_language_gender(
L = ms_validate_language_gender(
language = language,
gender = gender)
xname = L$full_name
return(xname)
}

#' @rdname language_to_ms_name
#' @rdname ms_language_to_ms_name
#' @export
validate_language_gender = function(
ms_validate_language_gender = function(
language = "en-US",
gender = c("Female", "Male")) {
locales = ms_locales()
Expand Down
88 changes: 68 additions & 20 deletions R/ms_synthesize.R
Expand Up @@ -6,16 +6,16 @@
#' provided.
#' @param script A character vector of lines to be spoken
#' @param token An authentication token, base-64 encoded usually from
#' \code{\link{get_ms_tts_token}}. If not provided, will be created from
#' \code{\link{get_ms_tts_token}}
#' \code{\link{ms_get_tts_token}}. If not provided, will be created from
#' \code{\link{ms_get_tts_token}}
#' @param gender Sex of the Speaker
#' @param language Language to be spoken,
#' must be from \code{\link{ms_language_codes}}
#' @param output_format Format of the output, see
#' \url{https://docs.microsoft.com/en-us/azure/cognitive-services/speech/api-reference-rest/bingvoiceoutput}
#' for more information
#' @param escape Should non-standard characters be substituted? Should not
#' be used if \code{script} has SSML tags. See \code{\link{create_ssml}}
#' be used if \code{script} has SSML tags. See \code{\link{ms_create_ssml}}
#' @param ... Additional arguments to send to \code{\link{POST}}
#'
#' @return A list of the request, content, token, and `SSML`.
Expand All @@ -25,11 +25,11 @@
#' then see below example
#'
#' @examples \dontrun{
#' if (have_ms_tts_key()) {
#' if (ms_have_tts_key()) {
#' res = ms_synthesize(
#' script = "hey, how are you doing? I'm doing pretty good",
#' output_format = "audio-16khz-128kbitrate-mono-mp3")
#' tmp <- tempfile()
#' tmp <- tempfile(fileext = ".mp3")
#' writeBin(res$content, con = tmp)
#' mp3 = tuneR::readMP3(tmp)
#' }
Expand All @@ -53,29 +53,25 @@ ms_synthesize = function(
"audio-16khz-32kbitrate-mono-mp3"),
escape = FALSE,
region = NULL,
api = c("tts", "bing"),
...
){

# language_to_ms_name(langu)

L = validate_language_gender(
L = ms_validate_language_gender(
language = language,
gender = gender)
language = L$language
gender = L$gender
xname = L$full_name


# synth_url = paste0(
# 'https://speech.platform.bing.com/',
# 'synthesize')
region = ms_region(region)
synth_url = paste0("https://", region,
".tts.speech.microsoft.com/",
"cognitiveservices/v1")
synth_url = ms_synthesize_api_url(
api = api,
region = region
)

if (is.null(token)) {
token = get_ms_tts_token(api_key = api_key)$token
token = ms_get_tts_token(api_key = api_key)$token
}

auth_hdr = add_headers(
Expand All @@ -87,8 +83,8 @@ ms_synthesize = function(

ctype = content_type("application/ssml+xml")

ssml = create_ssml(script = script, gender = gender, language = language,
escape = escape)
ssml = ms_create_ssml(script = script, gender = gender, language = language,
escape = escape)

if (nchar(ssml) > 1024) {
cat(ssml)
Expand All @@ -108,14 +104,66 @@ ms_synthesize = function(
content = out,
token = token)
return(L)

}


#' @rdname ms_synthesize
#' @param region Subscription region for your key.
#' See \url{https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-apis#text-to-speech}
#'
#' @export
ms_region = function(region = c("westus", "eastasia", "northeurope")) {
ms_region = function(region = ms_regions()) {
if (missing(region)) {
region = getOption("ms_region")
}
region = match.arg(region)
return(region)
}

#' @rdname ms_synthesize
#' @export
#' @examples
#' ms_regions()
ms_regions = function() {
c("westus", "eastasia", "northeurope",
"westus2",
"eastus",
"eastus2",
"southeastasia",
"westeurope")
}

#' @rdname ms_synthesize
#' @export
ms_set_region = function(
region = ms_regions()) {
region = match.arg(region)
options(ms_region = region)
return(region)
}



#' @rdname ms_synthesize
#' @param api which API to authorize on, either
#' \code{tts} for text to speech or \code{bing} for
#' Bing text to speech API
#' @export
ms_synthesize_api_url = function(
api = c("tts", "bing"),
region = NULL
){
api = match.arg(api)

region = ms_region(region)
synth_url = switch(
api,
bing = paste0(
'https://speech.platform.bing.com/',
'synthesize'),
tts = paste0("https://", region,
".tts.speech.microsoft.com/",
"cognitiveservices/v1")
)
return(synth_url)
}

0 comments on commit 8ba44da

Please sign in to comment.