-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputfieldTextSynthesis.module
More file actions
190 lines (162 loc) · 6.86 KB
/
InputfieldTextSynthesis.module
File metadata and controls
190 lines (162 loc) · 6.86 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace ProcessWire;
/**
* @method FieldsArray findCompatibleFields()
*/
class InputfieldTextSynthesis extends InputfieldFile
{
public static function getModuleInfo(): array {
return [
'title' => __('Text Synthesis', __FILE__), // Module Title
'summary' => __('One or more file uploads (sortable)', __FILE__), // Module Summary
'version' => 102,
'icon' => 'microphone',
'requires' => 'ProcessTextSynthesis'
];
}
public function __construct() {
parent::__construct();
$this->label = 'TextSynthesis';
}
/**
* Configuration settings for InputfieldFile
* @return InputfieldWrapper
* @throws WireException
*/
public function ___getConfigInputfields(): InputfieldWrapper {
$inputfields = parent::___getConfigInputfields();
$fieldset = new InputfieldFieldset();
$fieldset->label = __('TextSynthesis');
$fieldset->add([
'type' => 'Radios',
'name' => 'textSynthesisMode',
'label' => __('Mode'),
'options' => [
'text' => __('**Text** – use plain text for speech synthesis'),
'ssml' => __('**SSML** – use ssml for speech synthesis. [More about ssml](https://cloud.google.com/text-to-speech/docs/ssml).')
],
'value' => $this->hasField->textSynthesisMode ?? ''
]);
$fieldset->add([
'type' => 'AsmSelect',
'name' => 'textSynthesisTextConfig',
'label' => __('Synthesis Fields'),
'description' => __('Add synthesis text fields. All fields are aligned with each other.'),
'options' => $this->findCompatibleFields()->explode('label|name'),
'value' => $this->hasField->textSynthesisTextConfig,
'showIf' => 'textSynthesisMode=text'
]);
$example = '<speak>
<par>
<media xml:id="athmo" soundLevel="+15dB" fadeInDur="8s" fadeOutDur="4s" end="text.end+2.0s">
<audio repeatDur="infinity" src="https://upload.wikimedia.org/wikipedia/commons/0/05/Daniel_Simion_-_Creepy_Background_Sound.ogg">
<desc>Creepy Background Sound</desc>
</audio>
</media>
<media xml:id="text">
<speak>
<p>{title}</p>
<p>It was just one of those things, you know.</p><p>Purely an accident.</p>
{myComposedField}
<p>My husband had, <break time="1000ms"/> been drinking, <emphasis level="reduced">and he came home about three hours late.</emphasis></p>
<p><emphasis level="strong">So he wasn\'t exactly in the greatest mood that night.</emphasis></p>
<p>And, well, Danny had scattered some of his school papers all over the room, and my husband grabbed his arm and pulled him away from them.</p>
</speak>
</media>
</par>
</speak>';
$fieldset->add([
'type' => 'Textarea',
'name' => 'textSynthesisSsmlConfig',
'label' => __('SSML Configuration'),
'placeholder' => __("Please insert ssml configuration", __FILE__),
'notes' => __('Get a look to the example code below. Supported [markup tags](https://cloud.google.com/text-to-speech/docs/ssml) for the Google Cloud API.'),
'value' => $this->hasField->textSynthesisSsmlConfig ?? '',
'showIf' => 'textSynthesisMode=ssml'
]);
if(!empty($this->hasField->textSynthesisSsmlConfig)) {
/** @var Inputfield $ssmlConfigField */
$ssmlConfigField = $fieldset->get('textSynthesisSsmlConfig');
// validate ssml config
$dom = ProcessTextSynthesis::validateSsml($this->hasField->textSynthesisSsmlConfig);
if($dom instanceof \DOMDocument) $ssmlConfigField->value = $dom->saveXML();
elseif(is_array($dom) && count($dom)) {
$ssmlConfigField->error('There are some errors in the xml. Please read the explanation below.');
$errors = array_map(function(\LibXMLError $error) { return sprintf(
'Row %1$s, Column %2$s: %3$s',
$error->line, $error->column, $this->sanitizer->text($error->message)
); }, $dom);
$ssmlConfigField->appendMarkup = implode("<br>", $errors);
}
$ssmlConfigField->rows = max(5, substr_count($ssmlConfigField->value, "\n")+1);
}
$fieldset->add([
'type' => 'Markup',
'name' => 'textSynthesisConfigExampleSSML',
'label' => __('SSML Example'),
'description' => __("Please enter all text fields on the page that are to be used for speech synthesis here."),
'markupText' => '<pre style="margin:0"><code>' . htmlentities($example) . '</code></pre>',
'notes' => __('Wrap field names in curly brackets. You can use `wire::addHookProperty` to create your own composed fields. [More about page property hooks](https://processwire.com/api/ref/wire/add-hook-property/)'),
'showIf' => 'textSynthesisMode=ssml',
'textFormat' => Inputfield::textFormatBasic,
'collapsed' => Inputfield::collapsedYes
]);
$fieldset->add([
'type' => 'Textarea',
'name' => 'textSynthesisRequest',
'label' => __('Request'),
'description' => __('Adjust voice, language and audio parameters such as pitch, encoding and sample rate.'),
'notes' => __("Go to [Request Documentation](https://cloud.google.com/text-to-speech/docs/reference/rest/v1beta1/text/synthesize).\n The `input` property is automatically generated and cannot be configured.\n This field is formatted automatically and must contain validated json."),
'value' => $this->hasField->textSynthesisRequest ?? '',
]);
if(!empty($this->hasField->textSynthesisRequest)) {
/** @var Inputfield $requestField */
$requestField = $fieldset->get('textSynthesisRequest');
$json = json_decode($this->hasField->textSynthesisRequest);
if(empty($json)) $requestField->error(__('Invalid json. Please check the notation.'));
else $requestField->value = json_encode($json, JSON_PRETTY_PRINT);
// set field height
$requestField->rows = max(5, substr_count($requestField->value, "\n")+1);
}
// Add fieldset to inputfields
$inputfields->insertBefore($fieldset, 'visibility');
return $inputfields;
}
/**
* Add css from InputfieldFile
* @param Inputfield|null $parent
* @param bool $renderValueMode
* @return bool
*/
public function renderReady(Inputfield $parent = null, $renderValueMode = false): bool {
$config = config();
// load inputfieldFile assets
$parentClassName = basename(str_replace('\\', '/', get_parent_class($this)));
$parentModulePath = $config->urls->$parentClassName;
if($parentModulePath) {
$config->styles->add($parentModulePath . $parentClassName . '.css');
$config->scripts->add($parentModulePath . $parentClassName . '.js');
}
// Add link to queue
$queuePage = pages()->findOne('template=admin, process=ProcessTextSynthesis');
if($queuePage->id) {
if($this->notes) $this->notes .= " \n";
$this->notes .= sprintf(
__('If this field is empty, the job may still be in [the queue](%s).'),
$queuePage->url
);
}
return parent::renderReady($parent, $renderValueMode);
}
/**
* Find compatible synthesis input fields
* @return ?FieldsArray
*/
public function ___findCompatibleFields(): ?FieldsArray {
$fields = clone fields()->getAll();
foreach($fields as $field) {
if(!($field->type instanceof FieldtypeText)) $fields->removeQuietly($field);
}
return $fields->sort('label|name');
}
}