in admin/AmazonAI-PollyService.php [260:451]
public function convert_to_audio( $post_id, $sample_rate, $voice_id, $sentences, $wp_filesystem, $lang ) {
$logger = new AmazonAI_Logger();
$logger->log(sprintf('%s Converting to Audio', __METHOD__));
// Creating new standard common object for interacting with other methods of the plugin.
$common = $this->common;
// This part will be called only in case of translate opration and converting
// text into other languages (only then the last parameter is not empty).
// The last parameter of method specify language for which we are creating audio
foreach ($common->get_all_translatable_languages() as $language_code) {
if ( $language_code == $lang ) {
$voice_id = get_option( 'amazon_polly_trans_langs_' . $language_code . '_voice' );
}
}
if ( empty($voice_id) ) {
return;
}
// Just in case we check if sample rate is valid, if not we will set default value.
$sample_rate_values = array( '24000', '22050', '16000', '8000' );
if ( ! in_array( $sample_rate, $sample_rate_values, true ) ) {
$sample_rate = '24000';
}
// In case of asynchronous synthesis flow.
//$amazon_ai_asynchronous = apply_filters( '$amazon_ai_asynchronous', '' );
//if ( $amazon_ai_asynchronous ) {
// $this->start_speech_synthesis_task($common, $post_id, $sample_rate, $voice_id, $sentences, $lang);
// return;
//}
// Preparing locations and names of temporary files which will be used.
$random = rand(5, 10);
$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 . $random;
$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 );
}
// We might be stiching multiple smaller audio files. This variable will
// be used to detact the first part.
$first_part = true;
// Iterating through each of text parts.
foreach ( $sentences as $key => $text_content ) {
$logger->log(sprintf('%s Part:', __METHOD__));
$logger->log(sprintf('%s', $text_content));
// Remove all tags
$text_content = strip_tags($text_content);
// Modify Speed
$text_content = $common->modify_sentence_speed($text_content);
// Adding breaths sounds (if enabled).
$text_content = $this->add_breaths($common, $text_content);
// If plugin SSML support option is enabled, plugin will try to decode all SSML tags.
$text_content = $this->ssml_support($common, $text_content);
// Adding special polly mark.
$text_content = $this->add_mark_tag($common, $text_content);
// Adding newscaster style tag
$text_content = $this->add_newscaster_tag($common, $text_content, $voice_id);
// Adding conversational style tag
$text_content = $this->add_conversational_tag($common, $text_content, $voice_id);
// Adding speak polly mark.
$text_content = $this->add_speak_tags($common, $text_content);
//Preparing lexicons which will be used create audio.
$lexicons = $common->get_lexicons();
$lexicons_array = explode( ' ', $lexicons );
//Preparing Amazon Polly client object.
$polly_client = $common->get_polly_client();
//Detect Polly Engine (Standard / Neural).
$engine = $common->get_polly_engine($voice_id);
$news_style = $common->should_news_style_be_used($voice_id);
$conversational_style = $common->should_conversational_style_be_used($voice_id);
$logger->log(sprintf('%s Final Polly text:', __METHOD__));
$logger->log($text_content);
$logger->log(sprintf('%s Engine: %s ', __METHOD__, $engine));
$logger->log(sprintf('%s Voice: %s ', __METHOD__, $voice_id));
$logger->log(sprintf('%s SampleRate: %s ', __METHOD__, $sample_rate));
$logger->log(sprintf('%s News Style Enabled: %s ', __METHOD__, $news_style));
$logger->log(sprintf('%s Conversational Style Enabled: %s ', __METHOD__, $conversational_style));
//Call Amazon Polly service.
if ( ! empty( $lexicons ) and ( count( $lexicons_array ) > 0 ) ) {
$result = $polly_client->synthesizeSpeech(
array(
'Engine' => $engine,
'OutputFormat' => 'mp3',
'SampleRate' => $sample_rate,
'Text' => $text_content,
'TextType' => 'ssml',
'VoiceId' => $voice_id,
'LexiconNames' => $lexicons_array,
)
);
} else {
$result = $polly_client->synthesizeSpeech(
array(
'Engine' => $engine,
'OutputFormat' => 'mp3',
'SampleRate' => $sample_rate,
'Text' => $text_content,
'TextType' => 'ssml',
'VoiceId' => $voice_id,
)
);
}
$logger->log(sprintf('%s Audio returned from Polly', __METHOD__));
// 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 );
$logger->log(sprintf('%s Part created ( %s )', __METHOD__, $file_temp_full_name . '_part_' . $key));
// Merge new temporary file with previous ones.
if ( $first_part ) {
$wp_filesystem->put_contents( $file_temp_full_name, $contents );
$first_part = false;
} else {
$common->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 );
}
// Saving audio file in final destination.
$fileHandler = $common->get_file_handler();
$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 . "s3" );
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', $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 );
$logger->log(sprintf('%s Final audio created!', __METHOD__));
}