| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/AudioTrack.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/29 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::DOM::AudioTrack; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::MediaTrack ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use HTML::Object::Exception; | ||
| 23 | our $VERSION = 'v0.2.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | return( $self->error({ | ||
| 33 | message => sprintf( "At least 1 argument required, but only %d passed", scalar( @_ ) ), | ||
| 34 | class => 'HML::Object::TypeError', | ||
| 35 | }) ) if( scalar( @_ ) < 3 ); | ||
| 36 | my $kind = shift( @_ ); | ||
| 37 | my $label = shift( @_ ); | ||
| 38 | my $lang = shift( @_ ); | ||
| 39 | return( $self->error({ | ||
| 40 | message => "kind argument provided is not a string", | ||
| 41 | class => 'HML::Object::TypeError', | ||
| 42 | }) ) if( $kind !~ /^\w+$/ ); | ||
| 43 | # If set, this should be the same as the id in HTML::Object::DOM::Element::Track | ||
| 44 | $self->{id} = undef; | ||
| 45 | $self->{kind} = $kind; | ||
| 46 | $self->{label} = $label; | ||
| 47 | $self->{language} = $lang; | ||
| 48 | $self->{_init_strict_use_sub} = 1; | ||
| 49 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 50 | return( $self ); | ||
| 51 | } | ||
| 52 | |||
| 53 | # Note: property | ||
| 54 | sub enabled : lvalue { return( shift->_set_get_boolean( 'enabled', @_ ) ); } | ||
| 55 | |||
| 56 | # Note: property id inherited | ||
| 57 | |||
| 58 | # Note: property kind inherited | ||
| 59 | |||
| 60 | # Note: property label inherited | ||
| 61 | |||
| 62 | # Note: property language inherited | ||
| 63 | |||
| 64 | # Note: property | ||
| 65 | sub sourceBuffer { return; } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | # NOTE: POD | ||
| 69 | __END__ | ||
| 70 | |||
| 71 | =encoding utf-8 | ||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | HTML::Object::DOM::AudioTrack - HTML Object DOM AudioTrack Class | ||
| 76 | |||
| 77 | =head1 SYNOPSIS | ||
| 78 | |||
| 79 | use HTML::Object::DOM::AudioTrack; | ||
| 80 | my $audio = HTML::Object::DOM::AudioTrack->new || | ||
| 81 | die( HTML::Object::DOM::AudioTrack->error, "\n" ); | ||
| 82 | |||
| 83 | =head1 VERSION | ||
| 84 | |||
| 85 | v0.2.0 | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | The C<AudioTrack> interface represents a single audio track from one of the HTML media elements, <audio> or <video>. | ||
| 90 | |||
| 91 | =head1 INHERITANCE | ||
| 92 | |||
| 93 | +-----------------------+ +---------------------------+ +-------------------------------+ +-------------------------------+ | ||
| 94 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::MediaTrack | --> | HTML::Object::DOM::AudioTrack | | ||
| 95 | +-----------------------+ +---------------------------+ +-------------------------------+ +-------------------------------+ | ||
| 96 | |||
| 97 | =head1 PROPERTIES | ||
| 98 | |||
| 99 | Inherits properties from its parent L<HTML::Object::DOM::MediaTrack> | ||
| 100 | |||
| 101 | =head2 enabled | ||
| 102 | |||
| 103 | A boolean value which controls whether or not the audio track's sound is enabled. Setting this value to false mutes the track's audio. | ||
| 104 | |||
| 105 | Example: | ||
| 106 | |||
| 107 | sub swapCommentaryMain | ||
| 108 | { | ||
| 109 | my $videoElem = $doc->getElementById( 'main-video' ); | ||
| 110 | my $audioTrackMain; | ||
| 111 | my $audioTrackCommentary; | ||
| 112 | |||
| 113 | $videoElem->audioTracks->forEach(sub | ||
| 114 | { | ||
| 115 | my $track = shift( @_ ); | ||
| 116 | if( $track->kind == 'main' ) | ||
| 117 | { | ||
| 118 | $audioTrackMain = $track; | ||
| 119 | } | ||
| 120 | elsif( $track->kind == 'commentary' ) | ||
| 121 | { | ||
| 122 | $audioTrackCommentary = $track; | ||
| 123 | } | ||
| 124 | }); | ||
| 125 | |||
| 126 | if( $audioTrackMain && $audioTrackCommentary ) | ||
| 127 | { | ||
| 128 | my $commentaryEnabled = $audioTrackCommentary->enabled; | ||
| 129 | $audioTrackCommentary->enabled = $audioTrackMain->enabled; | ||
| 130 | $audioTrackMain->enabled = $commentaryEnabled; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack/enabled> | ||
| 135 | |||
| 136 | =head2 id | ||
| 137 | |||
| 138 | A string which uniquely identifies the track within the media. This ID can be used to locate a specific track within an audio track list by calling C<AudioTrackList>.getTrackById(). The ID can also be used as the fragment part of the URL if the media supports seeking by media fragment per the Media Fragments URI specification. | ||
| 139 | |||
| 140 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack/id> | ||
| 141 | |||
| 142 | =head2 kind | ||
| 143 | |||
| 144 | A string specifying the category into which the track falls. For example, the main audio track would have a kind of "main". | ||
| 145 | |||
| 146 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack/kind> | ||
| 147 | |||
| 148 | =head2 label | ||
| 149 | |||
| 150 | A string providing a human-readable label for the track. For example, an audio commentary track for a movie might have a label of "Commentary with director John Q. Public and actors John Doe and Jane Eod." This string is empty if no label is provided. | ||
| 151 | |||
| 152 | Example: | ||
| 153 | |||
| 154 | use Module::Generic::Array; | ||
| 155 | sub getTrackList | ||
| 156 | { | ||
| 157 | my $el = shift( @_ ); | ||
| 158 | my $trackList = Module::Generic::Array->new; | ||
| 159 | my $wantedKinds = [ | ||
| 160 | "main", "alternative", "main-desc", "translation", "commentary" | ||
| 161 | ]; | ||
| 162 | |||
| 163 | $el->audioTracks->forEach(sub | ||
| 164 | { | ||
| 165 | my $track = shift( @_ ); | ||
| 166 | if( $wantedKinds->includes( $track->kind ) ) | ||
| 167 | { | ||
| 168 | $trackList->push({ | ||
| 169 | id => $track->id, | ||
| 170 | kind => $track->kind, | ||
| 171 | label => $track->label | ||
| 172 | }); | ||
| 173 | } | ||
| 174 | }); | ||
| 175 | return( $trackList ); | ||
| 176 | } | ||
| 177 | |||
| 178 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack/label> | ||
| 179 | |||
| 180 | =head2 language | ||
| 181 | |||
| 182 | A string specifying the audio track's primary language, or an empty string if unknown. The language is specified as a BCP 47 (RFC 5646) language code, such as "en-US" or "pt-BR". | ||
| 183 | |||
| 184 | Example: | ||
| 185 | |||
| 186 | use Module::Generic::Array; | ||
| 187 | sub getAvailableLanguages | ||
| 188 | { | ||
| 189 | my $el = shift( @_ ); | ||
| 190 | my $trackList = Module::Generic::Array->new; | ||
| 191 | my $wantedKinds = [ | ||
| 192 | "main", "translation" | ||
| 193 | ]; | ||
| 194 | |||
| 195 | $el->audioTracks->forEach(sub | ||
| 196 | { | ||
| 197 | my $track = shift( @_ ); | ||
| 198 | if( $wantedKinds->includes( $track->kind ) ) | ||
| 199 | { | ||
| 200 | $trackList->push({ | ||
| 201 | id => $track->id, | ||
| 202 | kind => $track->kind, | ||
| 203 | language => $track->language | ||
| 204 | }); | ||
| 205 | } | ||
| 206 | }); | ||
| 207 | return( $trackList ); | ||
| 208 | } | ||
| 209 | |||
| 210 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack/language> | ||
| 211 | |||
| 212 | =head2 sourceBuffer | ||
| 213 | |||
| 214 | The C<SourceBuffer> that created the track. Returns C<undef> if the track was not created by a C<SourceBuffer> or the C<SourceBuffer> has been removed from the C<MediaSource>.sourceBuffers attribute of its parent media source. | ||
| 215 | |||
| 216 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack/sourceBuffer> | ||
| 217 | |||
| 218 | =head1 METHODS | ||
| 219 | |||
| 220 | Inherits methods from its parent L<HTML::Object::DOM::MediaTrack> | ||
| 221 | |||
| 222 | =head1 EXAMPLE | ||
| 223 | |||
| 224 | use feature 'signatures'; | ||
| 225 | my $sfx = HTML::Object::DOM::Element::Audio->new( 'sfx.wav' ); | ||
| 226 | my $sounds = $sfx->addTextTrack( 'metadata' ); | ||
| 227 | |||
| 228 | # add sounds we care about | ||
| 229 | sub addFX( $start, $end, $name ) | ||
| 230 | { | ||
| 231 | my $cue = HTML::Object::DOM::VTTCue->new( $start, $end, '' ); | ||
| 232 | $cue->id = $name; | ||
| 233 | $cue->pauseOnExit = 1; # true | ||
| 234 | $sounds->addCue( $cue ); | ||
| 235 | } | ||
| 236 | addFX( 12.783, 13.612, 'dog bark' ); | ||
| 237 | addFX( 13.612, 15.091, 'kitten mew' ); | ||
| 238 | |||
| 239 | sub playSound( $id ) | ||
| 240 | { | ||
| 241 | $sfx->currentTime = $sounds->getCueById( $id )->startTime; | ||
| 242 | $sfx->play(); | ||
| 243 | } | ||
| 244 | |||
| 245 | # play a bark as soon as we can | ||
| 246 | $sfx->oncanplaythrough = sub | ||
| 247 | { | ||
| 248 | playSound( 'dog bark' ); | ||
| 249 | } | ||
| 250 | # meow when the user tries to leave, | ||
| 251 | # and have the browser ask them to stay | ||
| 252 | $doc->onbeforeunload = sub | ||
| 253 | { | ||
| 254 | my $e = shift( @_ ); | ||
| 255 | playSound( 'kitten mew' ); | ||
| 256 | $e->preventDefault(); | ||
| 257 | } | ||
| 258 | |||
| 259 | =head1 AUTHOR | ||
| 260 | |||
| 261 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 262 | |||
| 263 | =head1 SEE ALSO | ||
| 264 | |||
| 265 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/AudioTrack> | ||
| 266 | |||
| 267 | =head1 COPYRIGHT & LICENSE | ||
| 268 | |||
| 269 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 270 | |||
| 271 | All rights reserved | ||
| 272 | |||
| 273 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 274 | |||
| 275 | =cut |