2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 18:59:33 +00:00

98_MediaList.pm: definition of round changed

98_Text2Speech.pm: optimized module loading, some bugfixes


git-svn-id: https://svn.fhem.de/fhem/trunk@19378 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
Tobias.Faust 2019-05-12 13:41:34 +00:00
parent cb6754f4ef
commit ae57944d8c
2 changed files with 147 additions and 69 deletions

View File

@ -43,7 +43,7 @@ use IO::File;
use Fcntl;
use File::Basename;
use File::Copy;
use Math::Round qw/round/;
use Math::Round ();
require 'Blocking.pm';
require 'HttpUtils.pm';
use vars qw($readingFnAttributes);
@ -582,7 +582,7 @@ sub MediaList_GetMP3Tags($$) {
utf8::encode($album);
utf8::encode($comment);
$res = {"Artist" => $artist, "Title" => $title, "Album" => $album, "Time" => round($mp3info->{SECS}), "File" => $file, "Cover" => ""};
$res = {"Artist" => $artist, "Title" => $title, "Album" => $album, "Time" => math::round($mp3info->{SECS}), "File" => $file, "Cover" => ""};
Log3 $hash, 5, "GetMP3Tags: ".Dumper($res);
return $res;

View File

@ -21,17 +21,10 @@ package main;
use strict;
use warnings;
use Blocking;
use IO::File;
use HttpUtils;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
use Text::Iconv;
#use Encode::Detect::Detector;
use Data::Dumper;
use lib ('./FHEM/lib', './lib');
# use Data::Dumper;
# loading in attr function of TTS_RESSOURCE if Paws is really needed
# require Paws::Polly
use lib ('./FHEM/lib', './lib');
sub Text2Speech_OpenDev($);
sub Text2Speech_CloseDev($);
@ -231,6 +224,69 @@ sub Text2Speech_Define($$)
$hash->{STATE} = "Initialized";
my $ret = Text2Speech_loadmodules($hash, "");
if ($ret) {
Log3 $hash->{NAME}, 3, $ret;
}
return undef;
}
##########################
# Überprüfung und Einladen der notwendigen Module
##########################
sub Text2Speech_loadmodules($$) {
my ($hash, $TTS_Ressource) = @_;
eval {
require IO::File;
IO::File->import;
1;
} or return "IO::File Module not installed, please install";
eval {
require Digest::MD5;
Digest::MD5->import;
1;
} or return "Digest::MD5 Module not installed, please install";
eval {
require URI::Escape;
URI::Escape->import;
1;
} or return "URI::Escape Module not installed, please install";
eval {
require Text::Iconv;
Text::Iconv->import;
1;
} or return "Text::Iconv Module not installed, please install";
eval {
require Encode::Guess;
Encode::Guess->import;
1;
} or return "Encode::Guess Module not installed, please install";
eval {
require MP3::Info;
MP3::Info->import;
1;
} or return "MP3::Info Module not installed, please install";
if ($TTS_Ressource eq "Amazon-Polly") {
# Module werden nur benötigt mit der Polly Engine
eval {
require Paws::Polly;
Paws::Polly->import;
1;
} or return "Paws Module not installed. Please install, goto https://metacpan.org/source/JLMARTIN/Paws-0.39";
eval {
require File::HomeDir;
File::HomeDir->import;
1;
} or return "File::HomeDir Module not installed. Please install";
}
return undef;
}
@ -263,18 +319,9 @@ sub Text2Speech_Attr(@) {
return "This Attribute is only available in direct or server mode" if($hash->{MODE} !~ m/(DIRECT|SERVER)/ );
} elsif ($a[2] eq "TTS_Ressource" && $value eq "Amazon-Polly") {
Log3 $hash->{NAME}, 4, "Wechsele auf Amazon Polly, Lade Librarys nach.";
eval {
require Paws::Polly;
Paws::Polly->import;
1;
} or return "Paws Module not installed. Please install, goto https://metacpan.org/source/JLMARTIN/Paws-0.39";
eval {
require File::HomeDir;
File::HomeDir->import;
1;
} or return "File::HomeDir Module not installed. Please install";
Log3 $hash->{NAME}, 4, $hash->{NAME}. ": Wechsele auf Amazon Polly, Lade Librarys nach.";
my $ret = Text2Speech_loadmodules($hash, $a[2]);
if ($ret) {return $ret;} # breche ab wenn Module fehlen
if (! -e File::HomeDir->my_home."/.aws/credentials"){
return "No AWS credentials in FHEM Homedir found, please check ".File::HomeDir->my_home."/.aws/credentials <br> please refer https://metacpan.org/pod/Paws#AUTHENTICATION";
@ -458,6 +505,13 @@ sub Text2Speech_Set($@)
return "No APIKey specified" if (!defined($TTS_APIKey) && ($ttsAPIKey{$TTS_Ressource} || length($ttsAPIKey{$TTS_Ressource})>0));
return "No Username for TTS Access specified" if (!defined($TTS_User) && ($ttsUser{$TTS_Ressource} || length($ttsUser{$TTS_Ressource})>0));
my $ret = Text2Speech_loadmodules($hash, $TTS_Ressource);
if ($ret) {
# breche ab wenn Module fehlen
Log3 $me, 3, $ret;
return $ret;
}
my $cmd = shift(@a); # Dummy
$cmd = shift(@a); # DevName
@ -548,7 +602,18 @@ sub Text2Speech_PrepareSpeech($$) {
#-- we may have problems with umlaut characters
# ersetze Sonderzeichen die Google nicht auflösen kann
my $converter;
if($TTS_Ressource eq "Google") {
# wandle per standard alles nach UTF8
# check only ascii, utf8 and UTF-(16|32) with BOM, if not enough use function set_suspects
# Encode::Guess->set_suspects(qw/euc-jp shiftjis 7bit-jis/); # for japanese codepages
my $enc = guess_encoding($t);
if ($enc->name ne "utf8") {
Log3 $hash, 4, "$me: ermittelte CodePage: " .$enc->name. " , konvertiere nach UTF-8";
$converter = Text::Iconv->new($enc->name, "utf-8");
$t = $converter->convert($t);
}
#if($TTS_Ressource eq "Google") {
# Google benötigt UTF-8
# $t =~ s/ä/ae/g;
# $t =~ s/ö/oe/g;
@ -557,12 +622,9 @@ sub Text2Speech_PrepareSpeech($$) {
# $t =~ s/Ö/Oe/g;
# $t =~ s/Ü/Ue/g;
# $t =~ s/ß/ss/g;
#}
# -> use Encode::Detect::Detector;
#Log3 $hash, 4, "$me: ermittelte CodePage: " .detect($t). " , konvertiere nach UTF-8";
#$converter = Text::Iconv->new(detect($t), "utf-8");
#$t = $converter->convert($t);
} elsif ($TTS_Ressource eq "Amazon-Polly") {
if ($TTS_Ressource eq "Amazon-Polly") {
# Amazon benötigt ISO-8859-1 bei Nutzung Region eu-central-1
$converter = Text::Iconv->new("utf-8", "iso-8859-1");
$t = $converter->convert($t);
@ -750,7 +812,6 @@ sub Text2Speech_CalcMP3Duration($$) {
my $time;
my ($hash, $file) = @_;
eval {
use MP3::Info;
my $tag = get_mp3info($file);
if ($tag && defined($tag->{SECS})) {
$time = int($tag->{SECS}+0.5);
@ -784,6 +845,8 @@ sub Text2Speech_Download($$$) {
my $TTS_Speed = AttrVal($hash->{NAME}, "TTS_Speed", "");
my $cmd;
Log3 $hash->{NAME}, 4, $hash->{NAME}.": Verwende ".$TTS_Ressource." Resource zur TTS-Generierung";
if($TTS_Ressource =~ m/(Google|VoiceRSS)/) {
my $HttpResponse;
my $HttpResponseErr;
@ -798,7 +861,6 @@ sub Text2Speech_Download($$$) {
$url .= "&" . $ttsSpeed{$TTS_Ressource} . $TTS_Speed if(length($ttsSpeed{$TTS_Ressource})>0);
$url .= "&" . $ttsQuery{$TTS_Ressource} . uri_escape($text);
Log3 $hash->{NAME}, 4, $hash->{NAME}.": Verwende ".$TTS_Ressource." OnlineResource zum Download";
Log3 $hash->{NAME}, 4, $hash->{NAME}.": Hole URL: ". $url;
#$HttpResponse = GetHttpFile($ttsHost, $ttsPath . $ttsLang . $TTS_Language . "&" . $ttsQuery . uri_escape($text));
my $param = {
@ -858,10 +920,16 @@ sub Text2Speech_Download($$$) {
#Log3 $hash, 4, $hash->{NAME}.":" .$cmd;
#system($cmd);
my $fh;
my $texttype = "text";
$texttype = "ssml" if($text =~ m/^<speak>.*<\/speak>$/);
Log3 $hash->{NAME}, 4, $hash->{NAME}.": Folgender TextTyp wurde für ".$TTS_Ressource." erkannt: ".$texttype;
my $polly = Paws->service('Polly', region => 'eu-central-1');
my $res = $polly->SynthesizeSpeech(
VoiceId => $TTS_Language,
Text => $text,
TextType => $texttype,
OutputFormat => 'mp3',
);
@ -985,8 +1053,16 @@ sub Text2Speech_DoIt($) {
system($cmd);
my $t = substr($Mp3WrapFile, 0, length($Mp3WrapFile)-4)."_MP3WRAP.mp3";
if(-e $t){
Log3 $hash->{NAME}, 4, $hash->{NAME}.": Benenne Datei um von <".$t."> nach <".$Mp3WrapFile.">";
rename($t, $Mp3WrapFile);
#falls die Datei existiert den ID3V1 und ID3V2 Tag entfernen
eval{
remove_mp3tag($Mp3WrapFile, 2);
remove_mp3tag($Mp3WrapFile, 1);
Log3 $hash, 4, $hash->{NAME}.": Die ID3 Tags von $Mp3WrapFile wurden geloescht";
} or Log3 $hash->{NAME}, 3, "MP3::Info Modul fehlt, konnte MP3 Tags nicht entfernen";
} else {Log3 $hash->{NAME}, 3, $hash->{NAME}.": MP3WRAP Fehler!, Datei wurde nicht generiert.";}
}
if ($TTS_OutputFile && $TTS_OutputFile ne $Mp3WrapFile) {
@ -1454,7 +1530,8 @@ sub Text2Speech_WriteStats($$$$){
<li><b>tts</b>:<br>
Setzen eines Textes zur Sprachausgabe. Um mp3-Dateien direkt auszugeben, müssen diese mit f&uuml;hrenden
und schließenden Doppelpunkten angegebenen sein. Die MP3-Dateien müssen unterhalb des Verzeichnisses <i>TTS_FileTemplateDir</i> gespeichert sein.<br>
Der Text selbst darf deshalb selbst keine Doppelpunte beinhalten. Siehe Beispiele.
Der Text selbst darf deshalb selbst keine Doppelpunte beinhalten. <br>
Für die SpracheEngine Amazon Polly kann auch SSML verwendet werden, Siehe Beispiele.
</li>
<li><b>volume</b>:<br>
Setzen der Ausgabe Lautst&auml;rke.<br>
@ -1652,7 +1729,8 @@ sub Text2Speech_WriteStats($$$$){
<code>attr TTS_EG_WZ TTS_Language Deutsch</code><br>
<code>attr TTS_EG_WZ TTS_MplayerCall /usr/bin/mplayer</code><br>
<code>attr TTS_EG_WZ TTS_Ressource Amazon-Polly</code><br>
<code>attr TTS_EG_WZ TTS_UseMP3Wrap 1</code><br>
<code>attr TTS_EG_WZ TTS_UseMP3Wrap 1</code><br><br>
<code>set MyTTS tts &lt;speak&gt;Mary had a little lamb.&lt;/speak&gt;</code>
<br>
<code>define MyTTS Text2Speech hw=0.0</code><br>
<code>set MyTTS tts Die Alarmanlage ist bereit.</code><br>