ONO::Lib::Audio::Speech

package ONO::Lib::Audio::Speech;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::IO;
use ONO::Lib::Basic;
use ONO::Lib::Lang::ToolBox;
use ONO::Lib::Audio::Object;

###############################################################################
# ...
###############################################################################

sub speech_item {

my (
$self,
$lang,
$word,
$size,
$switches,
$ALTDIR,
$ID,
$linkcode,
) = @_;

#: ONO Audio Speech object, allows to play speech files in MP3 format.
#:
#: -e delete: link to instant delete feature if possible (requires ID and linkcode)
#: -e edit: link to media manager if possible (requires ID)
#: -l float left
#: -L link mode (uses $linkcode as link)
#: -M mouseover non-trans (only -t and -R supported)
#: -O onclock mode (uses $linkcode as code)
#: -t transparent (only -R supported)
#: -r float right
#: -R record if not found
#: -s status (green or red light)
#: -T table mode (doesn't work in -R mode)

my $file = &speech_file("",$lang,$word);
my $led = "G";
my $is_alt;

if ($ALTDIR) {
if (!ONO::IO->exists($file)) {
my $alt_file = &speech_file("",$lang,$word,1);
if (ONO::IO->exists("$ALTDIR/$alt_file.mp3")) {
$file = "$ALTDIR/$alt_file.mp3";
$led = "Y";
$is_alt = 1;
}
}
}

if (ONO::IO->exists($file)) {

my $iswitches;
if ($switches =~ /s/) {
$iswitches = $led;
}

my $ICON = ONO::Lib::Audio::Object->icon("","/$file",$size,$iswitches);

if ($switches =~ /(d|e)/ && $is_alt) {
my $mode = $1;
my $IMG = "edit";
if ($mode eq "d") {
$IMG = "delete";
}
my $FILE = $file;
$FILE =~ s~(.*)/~~;
my $LINK;

if ($ALTDIR =~ m~media/users/(.*?)/(.*?)/(.*?)/(.*?)/public/audio/speech~) {
$LINK = qq~/account/?community_screen=media_file&area=public&dir=audio/speech/$lang&file=$FILE~;
}

if ($ALTDIR =~ m~media/virclass/(.*?)/public/audio/speech~) {
$LINK = qq~/cgi-bin/local/perl/school/index_portal.pl?mode=media&community_screen=media_file&area=public&dir=audio/speech/$lang&file=$FILE~;
}

if ($mode eq "d") {
$LINK = $linkcode;
$LINK =~ s~audio_recorder=~audio_delete=~;
}

$ICON = qq~<div class="rel" onmouseover="onojs_block('speech_edit_$ID');" onmouseout="onojs_hide('speech_edit_$ID');">
$ICON
<div id="speech_edit_$ID" class="abs hide" style="top:4px;left:4px">
<a href="$LINK"><img class="block24" src="/ono/osr/images/icons/ono/32x32/$IMG.png" alt=""></a>
</div>
</div>
~;

}

if ($switches =~ /T/) {

return qq~<td class="p0">$ICON</td>~;

} else {

if ($switches =~ /(l|r)/) {

return qq~<div class="f$1">$ICON</div>~;

} else {

return $ICON;

}

}

} else {

if ($switches =~ /R/) {

my ($ON,$CLASS1,$CLASS2);

if ($switches =~ /O/ && $linkcode) {
$CLASS1 .= " cursorlink";
$ON .= qq~ onclick="$linkcode"~;
}

if ($switches =~ /(l|r)/) {
$CLASS1 .= " f$1";
}

if ($switches =~ /t/) {
$CLASS2 .= " trans30";
}

if ($switches =~ /M/) {
$ON .= qq~ onmouseover="onojs_class('ono_speech_item_$ID','block$size');" onmouseout="onojs_class('ono_speech_item_$ID','block$size $CLASS2');"~;
}

my $ICON = qq~<div class="block$size rel$CLASS1"$ON>
<div id="ono_speech_item_$ID" class="$CLASS2"><img class="block$size" src="/ono/osr/images/icons/crystal/64x64/apps/krec.png" alt=""></div>
<img class="block10 abs" src="/ono/osr/images/leds/16x16/red.png" style="right:4px;bottom:0px" alt="">
</div>
~;

if ($switches =~ /L/ && $linkcode) {
$ICON = qq~<a href="$linkcode">$ICON</a>~;
}

return $ICON;

} else {

if (ONO::IO->devstation) {

return "";

} else {

return "";

}

}

}

}

sub speech_file {

#: Return the path to a speech file.

my $file = ONO::Lib::Lang::ToolBox->remove_article($_[2]);
$file = ONO::Lib::Basic->umlaut_simplify($file,3);
$file = lc $file;
$file =~ s~( |-)~_~g;
$file =~ s~[^a-z0-9\_]~~g;

if ($_[3]) {

return "$_[1]/$file";

} else {

return "media/audio/speech/$_[1]/$file.mp3";

}

}

sub speech_alt_dir {

#: Alternative speech file directory (personal, or virtual classroom)

my (
$self,
$user,
$class,
) = @_;

my $ALT_DIR;
if ($user) {
$ALT_DIR = "media/users/".ONO::IO->deepdir($user)."/$user/public/audio/speech";
}
if ($class) {
$ALT_DIR = "media/virclass/$class/public/audio/speech";
}

return $ALT_DIR;

}

sub speech_alt_delete {

#: Delete speech file alternative.

my (
$self,
$dir,
$lang,
$file,
) = @_;

$file = &speech_file("",$lang,$file,1);

my $FILE = "$dir/$file.mp3";

if ($dir =~ m~public/audio/speech~ && $lang =~ /[a-z]/ && $file =~ /[A-Za-z0-9]/) {
ONO::IO->rm($FILE);
return $FILE;
} else {
return "bad file: $FILE";
}

}

###############################################################################
# end of script
###############################################################################

1;

__END__