package LinuxIsdn::Call::Audio;
use strict;
use warnings;

use base qw(Exporter);
use fields qw(call on_start debug generation started aborted dtmf_seen
              dtmf_dispatch class);

sub anew {
    my LinuxIsdn::Call::Audio $audio = []; 
    $audio->{dtmf_seen}  = "";
    return $audio;
}

use fields::accessor qw(on_start	&
                        debug		$
                        aborted		?
                        started		?
                        call		$
                        dtmf_seen	$
                        dtmf_dispatch	%
                        generation	.
                        );

our (%audio_mode, @audio_mode, @audio_speed);
# Maybe should query these with AT+VSM=?
for (qw[
        2;ADPCM;2;0;(8000)
        3;ADPCM;3;0;(8000)
        4;ADPCM;4;0;(8000)
        5;ALAW;8;0;(8000)
        6;ULAW;8;0;(8000)
        ]) {
    /^\s*(\d+);(\S+);(\d+);(\d+);\((\d+)\)\s*$/ or
        die "Could not parse $_";
    $audio_mode{"$2-$3"} = $1;	# "ADPCM-3" => 3
    # Special case conventional name of aLAW and uLAW
    if ($2 eq "ALAW") {
        $audio_mode[$1] = "aLAW";
    } elsif ($2 eq "ULAW") {
        $audio_mode[$1] = "uLAW";
    } else {
        $audio_mode[$1] = "$2-$3";	# 3 => "ADPCM-3"
    }
    $audio_speed[$1] = $3*$5/8;	# bytes/sec
}
for (keys %audio_mode) {
    my $nr = $audio_mode{$_};
    # Alternative names
    $_ = lc($_);
    $audio_mode{$_} = $nr;
    $_ =~ s/-//g;
    $audio_mode{$_} = $nr;
    $audio_mode{lc($audio_mode[$nr])} = $nr;
}

our $VERSION   = "0.0";
our @EXPORT    = qw();
our @EXPORT_OK = qw(%audio_mode @audio_mode @audio_speed);

# Every activity of an Audio object should get an unique generation number
# 0 will means: not active
my $generation = 1;

sub listen_on {
}

sub listen_off {
}

sub activate {
    my LinuxIsdn::Call::Audio $audio = shift;
    return if $audio->{generation};
    $audio->{generation} = $generation++;
    $audio->{aborted} = 0;
    print STDERR "Activated audio $audio->{class}\n" if $audio->{debug};
    $audio->listen_on();
}

sub deactivate {
    my LinuxIsdn::Call::Audio $audio = shift;
    return unless $audio->{generation};
    $audio->{generation} = 0;
    print STDERR "DeActivate audio $audio->{class}\n" if $audio->{debug};
    $audio->listen_off();
}

sub start {
    my LinuxIsdn::Call::Recorder $audio = $_[0];
    $audio->{started} = 1;
    print STDERR "Start audio $audio->{class}\n" if $audio->{debug};
    goto $audio->{on_start};
}

sub end {
    my LinuxIsdn::Call::Recorder $audio = shift;
    $audio->{started}  = 0;
    return unless $audio->{debug};
    print STDERR "End   audio $audio->{class}\n";
}

sub dtmf {
    my LinuxIsdn::Call::Audio $audio = shift;
    print STDERR "DTMF $_[0]\n" if $audio->{debug};
    $audio->{dtmf_seen} .= $_[0];
    my $fun = $audio->{dtmf_dispatch}{$audio->{dtmf_seen}} || return;
    @_ = ($audio, $audio->{dtmf_seen});
    $audio->{dtmf_seen} = "";
    goto $fun;
}

sub busy {
    my LinuxIsdn::Call::Audio $audio = shift;
    return unless $audio->{debug};
    print STDERR "Busy tone received\n";
}

sub fax {
    my LinuxIsdn::Call::Audio $audio = shift;
    return unless $audio->{debug};
    print STDERR "FAX calling tone received\n";
}

1;
