#------------------------------------------------------------------------------
# File:         cuepointlist.config
#
# Description:  ExifTool config file to generate list of cue points and labels
#               in WAV audio files
#
# Usage:        exiftool -config cuepointlist.config -cuepointlist -b FILE
#
# Requires:     ExifTool version 12.25 or later
#
# Revisions:    2021-04-20 - P. Harvey Created
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        CuePointList => {
            Require => {
                0 => 'CuePoints',
                1 => 'SampleRate',
            },
            Desire => {
                2 => 'CuePointLabel',
                3 => 'CuePointNotes',
                4 => 'LabeledText',
            },
            ValueConv => q{
                SetByteOrder('II');
                my (%lbl, %rgn, %rtyp, %rlbl, %note, $pos, $i);
                # get labels if available
                for ($i=1; defined $val[2]; ++$i) {
                    $lbl{$1} = $2 if $val[2] =~ /^(\d+) (.*)/ and length $2;
                    $val[2] = $self->GetValue("CuePointLabel ($i)");
                }
                # get notes if available
                for ($i=1; defined $val[3]; ++$i) {
                    $note{$1} = $2 if $val[3] =~ /^(\d+) (.*)/ and length $2;
                    $val[3] = $self->GetValue("CuePointNotes ($i)");
                }
                # get regions if available
                for ($i=1; defined $val[4]; ++$i) {
                    if ($val[4] =~ /^(\d+) (\d+) '(.*)' \d+ \d+ \d+ \d+ (.*)/) {
                        $rgn{$1} = $2;
                        $rtyp{$1} = $3;
                        $rlbl{$1} = $4;
                    }
                    $val[4] = $self->GetValue("LabeledText ($i)");
                }
                my $buff = "Cue\tStart\tEnd\tLabel\tPurpose\tText\tNotes\n";
                for ($pos=4; $pos+24<=length(${$val[0]}); $pos+=24) {
                    my $n = Get32u($val[0], $pos);
                    my $start = Get32u($val[0], $pos + 4);
                    my (@lbl, $lbl);
                    push @lbl, $lbl{$n} if defined $lbl{$n};
                    push @lbl, $rlbl{$n} if defined $rlbl{$n};
                    push @lbl, '-', $note{$n} if defined $note{$n};
                    my $lbl = join ' ', @lbl;
                    $buff .= sprintf("%d\t%.3f\t%.3f\t%s\t%s\t%s\t%s\n",
                             $n, $start/$val[1], ($start+($rgn{$n}||0))/$val[1],
                             defined($lbl{$n}) ? $lbl{$n} : '',
                             defined($rtyp{$n}) ? $rtyp{$n} : '',
                             defined($rlbl{$n}) ? $rlbl{$n} : '',
                             defined($note{$n}) ? $note{$n} : '');
                }
                return $buff;
            },
        },
    },
);

1; # end