File Coverage

blib/lib/Audio/AMaMP.pm
Criterion Covered Total %
statement 9 53 16.9
branch 0 22 0.0
condition 0 9 0.0
subroutine 3 10 30.0
pod 7 7 100.0
total 19 101 18.8


line stmt bran cond sub pod time code
1             package Audio::AMaMP;
2            
3 1     1   20489 use 5.6.0;
  1         4  
  1         35  
4 1     1   5 use strict;
  1         1  
  1         27  
5 1     1   4 use warnings;
  1         5  
  1         1501  
6            
7             require Exporter;
8            
9             our @ISA = qw(Exporter);
10            
11             our @EXPORT = qw();
12            
13             our $VERSION = '0.3';
14            
15             require XSLoader;
16             XSLoader::load('Audio::AMaMP', $VERSION);
17            
18            
19             # Constructor.
20             sub new {
21 0     0 1   return bless {}, shift;
22             }
23            
24            
25             # This starts the core. Essentially OO-wraps the C function.
26             sub startCore {
27             # Get input parameters and check.
28 0     0 1   my ($self, $corePath, $inputFile) = @_;
29 0 0 0       return 0 unless $self && $corePath && $inputFile;
      0        
30            
31             # Now attempt to invoke the core.
32 0           my $core = amampStartCore($corePath, $inputFile);
33 0 0         if ($core) {
34             # Success. Stash core object and return true.
35 0           $self->{'core'} = $core;
36 0           return 1;
37             } else {
38             # Failure. Return false.
39 0           return 0;
40             }
41             }
42            
43            
44             # Gets a message in plain text. Essentially wraps the C function.
45             sub getRawMessage {
46             # Get input.
47 0     0 1   my ($self, $block) = @_;
48 0 0         return undef unless $self;
49 0           $block = 0+$block;
50            
51             # Attempt to call amampGetRawMessage and return what it gives.
52 0           return amampGetRawMessage($self->{'core'}, $block);
53             }
54            
55            
56             # Sends a plain text message. Essentially wraps the C function.
57             sub sendRawMessage {
58             # Get input.
59 0     0 1   my ($self, $message) = @_;
60 0 0         return 0 unless $message;
61            
62             # Attempt to call amampSendRawMessage and return what it gives.
63 0           return amampSendRawMessage($self->{'core'}, $message);
64             }
65            
66            
67             # Sends a message arranged in a hash structure.
68             sub sendMessage {
69             # Get input.
70 0     0 1   my ($self, %input) = @_;
71 0 0 0       return unless $input{'type'} && $input{'parameters'};
72            
73             # Construct message.
74 0           my $message = "$input{'type'}:\n";
75 0           for (keys %{$input{'parameters'}}) {
  0            
76 0           $message .= "\t$_: $input{'parameters'}->{$_}\n";
77             }
78 0           $message .= "\n";
79            
80             # Attempt to send message.
81 0           return $self->sendRawMessage($message);
82             }
83            
84            
85             # Gets a message and parses it into a hash based data structure.
86             sub getMessage {
87             # Get input.
88 0     0 1   my ($self, $block) = @_;
89 0 0         return unless $self;
90            
91             # Get message.
92 0           my $rawMessage = $self->getRawMessage($block);
93 0 0         return () unless $rawMessage;
94            
95             # Parse.
96 0           my $foundType = 0;
97 0           my $line;
98 0           my %message = ();
99 0           my %params = ();
100 0           foreach $line (split(/\n/, $rawMessage)) {
101 0 0         if ($foundType) {
102             # Should be a parameter.
103 0 0         if ($line =~ /^\t([\w_\-]{1,30}): (.+)$/) {
    0          
104 0           $params{$1} = $2;
105             } elsif ($line ne '') {
106             # Parse error.
107 0           return ();
108             }
109             } else {
110             # Should be type line.
111 0 0         if ($line =~ /^([\w_\-]{1,30}):$/) {
112 0           $message{'type'} = $1;
113 0           $foundType = 1;
114             } else {
115             # Parse error.
116 0           return ();
117             }
118             }
119             }
120            
121             # Return message structure.
122 0           $message{'parameters'} = \%params;
123 0           return %message;
124             }
125            
126            
127             # This sub checks if the core is still alive/available.
128             sub isCoreAlive {
129             # Just call C function to check.
130 0     0 1   my ($self) = @_;
131 0           return amampIsCoreAlive($self->{'core'});
132             }
133            
134             1;
135             __END__