in cognitive/src/main/scala/com/microsoft/azure/synapse/ml/cognitive/TextToSpeech.scala [53:140]
def setOutputFormat(v: String): this.type = setScalarParam(outputFormat, v)
def setOutputFormatCol(v: String): this.type = setVectorParam(outputFormat, v)
setDefault(outputFormat -> Left("Audio24Khz96KBitRateMonoMp3"))
val locale = new ServiceParam[String](this,
"locale",
s"The locale of the input text",
isRequired = true)
def setLocale(v: String): this.type = setScalarParam(locale, v)
def setLocaleCol(v: String): this.type = setVectorParam(locale, v)
setDefault(locale -> Left("en-US"))
val voiceName = new ServiceParam[String](this,
"voiceName",
s"The name of the voice used for synthesis",
isRequired = true)
def setVoiceName(v: String): this.type = setScalarParam(voiceName, v)
def setVoiceNameCol(v: String): this.type = setVectorParam(voiceName, v)
setDefault(voiceName -> Left("en-US-SaraNeural"))
val language = new ServiceParam[String](this,
"language",
s"The name of the language used for synthesis",
isRequired = true)
def setLanguage(v: String): this.type = setScalarParam(language, v)
def setLanguageCol(v: String): this.type = setVectorParam(language, v)
val text = new ServiceParam[String](this,
"text",
s"The text to synthesize",
isRequired = true)
def setText(v: String): this.type = setScalarParam(text, v)
def setTextCol(v: String): this.type = setVectorParam(text, v)
val outputFileCol = new Param[String](this,
"outputFileCol",
s"The location of the saved file as an HDFS compliant URI")
def setOutputFileCol(v: String): this.type = set(outputFileCol, v)
def getOutputFileCol: String = $(outputFileCol)
override def transform(dataset: Dataset[_]): DataFrame = {
val hconf = new SerializableConfiguration(dataset.sparkSession.sparkContext.hadoopConfiguration)
val toRow = SpeechSynthesisError.makeToRowConverter
dataset.toDF().map { row =>
using(SpeechConfig.fromEndpoint(new URI(getUrl), getValue(row, subscriptionKey))) { config =>
getValueOpt(row, language).foreach(lang => config.setSpeechSynthesisLanguage(lang))
getValueOpt(row, voiceName).foreach(voice => config.setSpeechSynthesisVoiceName(voice))
getValueOpt(row, outputFormat).foreach(format =>
config.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.valueOf(format)))
val (errorOpt, data) = using(new SpeechSynthesizer(config, null)) { synth =>
val res = synth.SpeakText(getValue(row, text))
val error = if (res.getReason.name() == "SynthesizingAudioCompleted") {
None
} else {
Some(SpeechSynthesisCancellationDetails.fromResult(res))
}
(error, res.getAudioData)
}.get
val errorRow = errorOpt.map(e => toRow(SpeechSynthesisError.fromSDK(e))).orNull
if (errorOpt.isEmpty) {
val path = new Path(row.getString(row.fieldIndex(getOutputFileCol)))
val fs = FileSystem.get(path.toUri, hconf.value)
using(fs.create(path)) { os =>
HUtils.copyBytes(new ByteArrayInputStream(data), os, hconf.value)
}.get
}
Row.fromSeq(row.toSeq ++ Seq(errorRow))
}.get
}(RowEncoder(dataset.schema.add(getErrorCol, SpeechSynthesisError.schema)))
}