in admin/class-amazonpolly-admin.php [637:760]
private function convert_to_audio( $post_id, $sample_rate, $voice_id, $sentences, $wp_filesystem, $lang ) {
foreach ( $this->translate_langs as $lan ) {
if ( $lan == $lang ) {
$voice_id = get_option( 'amazon_polly_trans_langs_' . $lan . '_voice' );
}
}
$sample_rate_values = array( '24000', '22050', '16000', '8000' );
if ( ! in_array( $sample_rate, $sample_rate_values, true ) ) {
$sample_rate = '24000';
}
$upload_dir = wp_upload_dir()['basedir'];
$file_prefix = 'amazon_polly_';
$file_name = $file_prefix . $post_id . $lang . '.mp3';
$file_temp_full_name = trailingslashit($upload_dir) . 'temp_' . $file_name;
$dir_final_full_name = trailingslashit($upload_dir);
if ( get_option('uploads_use_yearmonth_folders') ) {
$dir_final_full_name .= get_the_date( 'Y', $post_id ) . '/' . get_the_date( 'm', $post_id ) . "/";
}
$file_final_full_name = $dir_final_full_name . $file_name;
// Delete temporary file if already exists.
if ( $wp_filesystem->exists( $file_temp_full_name ) ) {
$wp_filesystem->delete( $file_temp_full_name );
}
// Delete final file if already exists.
if ( $wp_filesystem->exists( $file_final_full_name ) ) {
$wp_filesystem->delete( $file_final_full_name );
}
$first_part = true;
foreach ( $sentences as $key => $text_content ) {
// Depending on the plugin configuration SSML tags for automated breaths sound will be added.
$is_breaths_enabled = $this->amazon_polly_is_auto_breaths_enabled();
if ( $is_breaths_enabled ) {
$text_content = '<amazon:auto-breaths>' . $text_content . '</amazon:auto-breaths>';
}
// If plugin SSML support option is enabled, plugin will try to decode all SSML tags.
$is_ssml_enabled = $this->amazon_polly_is_ssml_enabled();
if ( $is_ssml_enabled ) {
$text_content = $this->amazon_polly_decode_ssml_tags( $text_content );
}
$text_content = str_replace( '**AMAZONPOLLY*SSML*BREAK*time=***500ms***SSML**', '<break time="500ms"/>', $text_content );
$text_content = str_replace( '**AMAZONPOLLY*SSML*BREAK*time=***1s***SSML**', '<break time="500ms"/>', $text_content );
$amazon_polly_mark_value = 'wp-plugin-awslabs';
$amazon_polly_mark_value = apply_filters( 'amazon_polly_mark_value', $amazon_polly_mark_value );
$ssml_text_content = '<speak><mark name="' . esc_attr( $amazon_polly_mark_value ) . '"/>' . $text_content . '</speak>';
$lexicons = $this->get_lexicons();
$lexicons_array = explode( ' ', $lexicons );
if ( ! empty( $lexicons ) and ( count( $lexicons_array ) > 0 ) ) {
$result = $this->polly_client->synthesizeSpeech(
array(
'OutputFormat' => 'mp3',
'SampleRate' => $sample_rate,
'Text' => $ssml_text_content,
'TextType' => 'ssml',
'VoiceId' => $voice_id,
'LexiconNames' => $lexicons_array,
)
);
} else {
$result = $this->polly_client->synthesizeSpeech(
array(
'OutputFormat' => 'mp3',
'SampleRate' => $sample_rate,
'Text' => $ssml_text_content,
'TextType' => 'ssml',
'VoiceId' => $voice_id,
)
);
}//end if
// Grab the stream and output to a file.
$contents = $result['AudioStream']->getContents();
// Save first part of the audio stream in the parial temporary file.
$wp_filesystem->put_contents( $file_temp_full_name . '_part_' . $key, $contents );
// Merge new temporary file with previous ones.
if ( $first_part ) {
$wp_filesystem->put_contents( $file_temp_full_name, $contents );
$first_part = false;
} else {
$this->remove_id3( $file_temp_full_name . '_part_' . $key );
$merged_file = $wp_filesystem->get_contents( $file_temp_full_name ) . $wp_filesystem->get_contents( $file_temp_full_name . '_part_' . $key );
$wp_filesystem->put_contents( $file_temp_full_name, $merged_file );
}
// Deleting partial audio file.
$wp_filesystem->delete( $file_temp_full_name . '_part_' . $key );
}//end foreach
// Saving audio file in final destination.
$fileHandler = $this->getFileHandler();
$audio_location_link = $fileHandler->save($wp_filesystem, $file_temp_full_name, $dir_final_full_name, $file_final_full_name, $post_id, $file_name);
// This will bust the browser cache when a content revision is made.
$audio_location_link = add_query_arg( 'version', time(), $audio_location_link );
// We are using a hash of these values to improve the speed of queries.
$amazon_polly_settings_hash = md5( $voice_id . $sample_rate . $fileHandler->get_type() );
if ( $lang == '' ) {
update_post_meta( $post_id, 'amazon_polly_audio_link_location', $audio_location_link );
update_post_meta( $post_id, 'amazon_polly_audio_location', $fileHandler->get_type() );
} else {
update_post_meta( $post_id, 'amazon_polly_translation_' . $lang, '1' );
}
// Update post meta data.
update_post_meta( $post_id, 'amazon_polly_enable', 1 );
update_post_meta( $post_id, 'amazon_polly_voice_id', get_option( 'amazon_polly_voice_id' ) );
update_post_meta( $post_id, 'amazon_polly_sample_rate', $sample_rate );
update_post_meta( $post_id, 'amazon_polly_settings_hash', $amazon_polly_settings_hash );
}