#4 Allow user to set voice speed and pitch.

This commit is contained in:
2017-06-16 15:22:21 +08:00
parent f9a2c7b227
commit e6f666b82d
4 changed files with 33 additions and 9 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ It just like the `say` command in Mac OSX, but born to be much powerful. We used
* 0.1.0 Add basic function to read texts from command line.
* 0.1.3 Add ability to allow user set different voice.
* 0.1.7 Allow user to set voice speed and pitch.
## License
MIT
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "say-it",
"version": "0.1.6",
"version": "0.1.7",
"description": "Read the text you typed in command line by using Baidu tts.",
"main": "say-it.js",
"bin": {
+29 -6
View File
@@ -13,33 +13,56 @@ const _ = require('lodash')
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
const DEFAULT_PERSON = 0
const DEFAULT_SPEED = 5
const DEFAULT_PITCH = 5
const showError = (error = TEXTS.error) => {
console.log(chalk.red(error))
}
var person = 0
const checkInputSource = (input, items) => {
if (!!input && !_.includes(items, Number.parseInt(input))) {
return false
}
return true
}
var person = DEFAULT_PERSON
var speed = DEFAULT_SPEED
var pitch = DEFAULT_PITCH
charm.pipe(process.stdout);
updateNotifier({pkg}).notify(); //show npm package update if available
updateNotifier({
pkg
}).notify(); //show npm package update if available
program
.version('0.1.6')
.command(' ', 'read the texts you typed in', {
isDefault: true
})
.option('-p, --person [person]', "set different voice. 0: female, 1 and 2: male, 3: male with emotion, 4: female with emotion")
.option('-p, --person [person]', "set different voice. 0: female, 1 and 2: male, 3: male with emotion, 4: female with emotion", DEFAULT_PERSON)
.option('-s, --speed [speed]', "set speed of voice. 0 - 9, and default is 5", DEFAULT_SPEED)
.option('-t --pitch [pitch]', "set the voice pitch. 0 - 9, and default is 5", DEFAULT_PITCH)
program.on('*', () => {
const text = program.args[0]
if (!text || text.trim() === '') {
return showError()
}
var personValid = checkInputSource(program.person, [0, 1, 2, 3, 4])
var speedValid = checkInputSource(program.speed, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
var pitchValid = checkInputSource(program.pitch, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
if (!!program.person && !_.includes([0, 1, 2, 3, 4], Number.parseInt(program.person))) {
return showError(TEXTS.invalidPerson)
if (!personValid || !speedValid || !pitchValid) {
return showError(TEXTS.errorOption)
}
person = Number.parseInt(program.person)
speed = Number.parseInt(program.speed)
pitch = Number.parseInt(program.pitch)
sayIt(text)
})
@@ -52,7 +75,7 @@ if (!process.argv.slice(2).length) {
}
function sayIt(text) {
const urlAddress = `http://tsn.baidu.com/text2audio?tex=${text}&lan=zh&cuid=${new Date().getTime()}&ctp=1&tok=24.9d61601aef23f1d3497c9c40eb30e7a7.2592000.1499416588.282335-9739014&per=${person}`
const urlAddress = `http://tsn.baidu.com/text2audio?tex=${text}&lan=zh&cuid=${new Date().getTime()}&ctp=1&tok=24.9d61601aef23f1d3497c9c40eb30e7a7.2592000.1499416588.282335-9739014&per=${person}&pit=${pitch}&spd=${speed}`
charm.write(chalk.bgCyan(TEXTS.loading))
request
.get(encodeURI(urlAddress))
+2 -1
View File
@@ -1,7 +1,8 @@
const TEXTS = {
error: "请确保输入文本正确。 如: 'hello world' 或 \" hello world \" ",
loading: 'loading...',
invalidPerson: 'Invalid person option'
invalidPerson: 'Invalid person option',
errorOption: '请输入正确的选项值'
}
module.exports = TEXTS