Skip to content

Commit cbbb2d4

Browse files
committed
add some options
1 parent 0501515 commit cbbb2d4

File tree

5 files changed

+56
-22
lines changed

5 files changed

+56
-22
lines changed

Diff for: env.yaml.template

+21
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,26 @@ voice_name: ja-JP-Standard-A,ja-JP-Standard-B,ja-JP-Standard-C,ja-JP-Standard-D
2828
# を参照してください。
2929
language_code: ja-JP
3030

31+
# Text-to-Speech API で指定する再生速度。
32+
# 0.25 から 4.00 までの数値。
33+
# 詳細は
34+
# https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize#audioconfig
35+
# を参照してください。
36+
speaking_rate: "1.00"
37+
38+
# Text-to-Speech API で指定する音程。
39+
# -20.0 から 20.0 までの数値。
40+
# 詳細は
41+
# https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize#audioconfig
42+
# を参照してください。
43+
pitch: "0.0"
44+
45+
# Text-to-Speech API で指定するボリューム。
46+
# -96.0 から 16.0 までの数値。
47+
# 詳細は
48+
# https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize#audioconfig
49+
# を参照してください。
50+
volume_gain_db: "0.0"
51+
3152
# language_code において指定した言語における区切り文字。
3253
delimiter: 。

Diff for: ts/Config.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type Config = {
2+
bucket_name: string
3+
input_pdf_path_regexp: string
4+
output_path: string
5+
temp_path: string
6+
voice_name: string
7+
language_code: string
8+
speaking_rate?: number
9+
pitch?: number
10+
volume_gain_db?: number
11+
delimiter: string
12+
};

Diff for: ts/TextToSpeechTask.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Config } from './Config';
12
import { GcsLib } from './GcsLib';
23
const promiseRetry = require('promise-retry');
34
const TextToSpeech = require('@google-cloud/text-to-speech');
@@ -6,17 +7,14 @@ export class TextToSpeechTask {
67
private readonly gcs: GcsLib;
78
private readonly client: typeof TextToSpeech.TextToSpeechClient;
89
private readonly voiceName: string;
9-
private readonly languageCode: string;
10-
private readonly delimiter: string;
11-
private readonly maxConcurrency = 8;
10+
private readonly config: Config;
1211
private readonly maxLength = 5000;
1312

14-
constructor(gcs: GcsLib, voiceName: string, languageCode: string, delimiter: string) {
13+
constructor(gcs: GcsLib, voiceName: string, config: Config) {
1514
this.gcs = gcs;
1615
this.client = new TextToSpeech.TextToSpeechClient();
1716
this.voiceName = voiceName;
18-
this.languageCode = languageCode;
19-
this.delimiter = delimiter;
17+
this.config = config;
2018
}
2119

2220
run(texts: Array<string>, outputPrefix: string): Promise<Array<string>> {
@@ -68,9 +66,20 @@ export class TextToSpeechTask {
6866
ttsRequest(text: string, outputPath: string): Promise<string> {
6967
console.log(`ttsRequest(..., ${outputPath})`);
7068
const request = {
71-
input: {text: text},
72-
voice: {name: this.voiceName, languageCode: this.languageCode, ssmlGender: 'NEUTRAL'},
73-
audioConfig: {audioEncoding: 'MP3'},
69+
input: {
70+
text: text,
71+
},
72+
voice: {
73+
name: this.voiceName,
74+
languageCode: this.config.language_code,
75+
ssmlGender: 'NEUTRAL',
76+
},
77+
audioConfig: {
78+
audioEncoding: 'MP3',
79+
speakingRate: Number(this.config.speaking_rate) || null,
80+
pitch: Number(this.config.pitch) || null,
81+
volumeGainDb: Number(this.config.volume_gain_db) || null,
82+
},
7483
};
7584
return this.client.synthesizeSpeech(request).then(([response]) => {
7685
return new Promise((resolve, reject) => {

Diff for: ts/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { main, Config } from './main';
1+
import { Config } from './Config';
2+
import { main } from './main';
23

34
exports.PdfAudify = async (file, context) => {
4-
await main(file.name, process.env as Config);
5+
await main(file.name, process.env as unknown as Config);
56
};

Diff for: ts/main.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Config } from './Config';
12
import { GcsLib } from './GcsLib';
23
import { OcrTask } from './OcrTask';
34
import { ExtractTextTask } from './ExtractTextTask';
@@ -6,16 +7,6 @@ import { ConcatMp3Task } from './ConcatMp3Task';
67
const promiseRetry = require('promise-retry');
78
const path = require('path');
89

9-
export type Config = {
10-
bucket_name: string
11-
input_pdf_path_regexp: string
12-
output_path: string
13-
temp_path: string
14-
voice_name: string
15-
language_code: string
16-
delimiter: string
17-
};
18-
1910
export function main(gcsPath: string, config: Config): Promise<void> {
2011
console.log(`gcsPath: ${gcsPath}, config: ${JSON.stringify(config)}`);
2112
if (!gcsPath.match(new RegExp(config.input_pdf_path_regexp))) {
@@ -30,7 +21,7 @@ export function main(gcsPath: string, config: Config): Promise<void> {
3021
const gcs = new GcsLib(config.bucket_name);
3122
const ocr = new OcrTask(gcs);
3223
const ext = new ExtractTextTask(gcs, config.delimiter);
33-
const tts = new TextToSpeechTask(gcs, voice_name, config.language_code, config.delimiter);
24+
const tts = new TextToSpeechTask(gcs, voice_name, config);
3425
const concat = new ConcatMp3Task(gcs);
3526

3627
const basename = path.parse(gcsPath.normalize()).name;

0 commit comments

Comments
 (0)