| 1 | package POE::Filter::Audio::Mad::Stdio; | ||
| 2 | require 5.6.0; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use POE; | ||
| 8 | use POE::Filter; | ||
| 9 | |||
| 10 | our $VERSION = '0.2'; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class) = @_; | ||
| 14 | |||
| 15 | return bless (\(my $temp), $class); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub get { | ||
| 19 | my ($self, $stream) = @_; | ||
| 20 | |||
| 21 | my ($pos, $index, @record) = (0, undef); | ||
| 22 | ${$self} .= join('', @{$stream}); | ||
| 23 | |||
| 24 | while (($index = index(${$self}, "\n\n")) > -1) { | ||
| 25 | (my $raw, ${$self}) = split(/\n\n/, ${$self}, 2); | ||
| 26 | my @cmd = split(/\n/, $raw); | ||
| 27 | |||
| 28 | my $msg = { id => shift(@cmd), data => {} }; | ||
| 29 | for (@cmd) { | ||
| 30 | my ($k, $v) = split(/\s+/, $_, 2); | ||
| 31 | $msg->{data}->{$k} = $v; | ||
| 32 | } | ||
| 33 | |||
| 34 | push(@record, $msg); | ||
| 35 | } | ||
| 36 | |||
| 37 | return \@record; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub put { | ||
| 41 | my ($self, $data) = @_; | ||
| 42 | |||
| 43 | my @record; | ||
| 44 | |||
| 45 | for (@{$data}) { | ||
| 46 | my $raw = "$_->{id}\n"; | ||
| 47 | if (defined($_->{data}) && ref($_->{data}) eq 'HASH') { | ||
| 48 | while (my ($k, $v) = each(%{$_->{data}})) { $raw .= "$k $v\n" } | ||
| 49 | } elsif (defined($_->{data}) && $_->{data} ne '') { | ||
| 50 | $raw .= "$_->{data}\n"; | ||
| 51 | } | ||
| 52 | $raw .= "\n"; | ||
| 53 | |||
| 54 | push(@record, $raw); | ||
| 55 | } | ||
| 56 | |||
| 57 | return \@record; | ||
| 58 | } | ||
| 59 | |||
| 60 | ############################################################################## | ||
| 61 | =pod | ||
| 62 | |||
| 63 | =head1 NAME | ||
| 64 | |||
| 65 | POE::Filter::Audio::Mad - Simple filter support POE::Component::Audio::Mad::Handle | ||
| 66 | |||
| 67 | =head1 SYNOPSIS | ||
| 68 | |||
| 69 | ## could be any wheel, really.. | ||
| 70 | my $wheel = POE::Wheel::ReadWrite->new( | ||
| 71 | Filter => POE::Filter::Audio::Mad->new(), | ||
| 72 | ## other options.. | ||
| 73 | ); | ||
| 74 | |||
| 75 | =head1 DESCRIPTION | ||
| 76 | |||
| 77 | POE::Filter::Audio::Mad is a simple filter designed to translate streams | ||
| 78 | of characters into structured command packets. This is a fairly simple | ||
| 79 | filter, as such, it places certain restrictions on the format of | ||
| 80 | the data passed through it. However, the input and output of this | ||
| 81 | filter are very similar and have the same restrictions. | ||
| 82 | |||
| 83 | Input is achived by translating textual input into command packets; | ||
| 84 | command packets are hashrefs with two fields: 'id' and 'data'. The | ||
| 85 | 'id' field is interpreted as a string, and contains the name of the | ||
| 86 | command to execute. The 'data' field is interpreted as a list of | ||
| 87 | named options, similar to a hash, but you are not allowed to use | ||
| 88 | any whitespace characters in your keys. | ||
| 89 | |||
| 90 | To start a command, you send the command name followed by a newline. | ||
| 91 | If you wish to include any named paramaters, include them each on | ||
| 92 | their own line. place the name of the parmater first on the line, | ||
| 93 | followed by whitespace and then the value of that paramater followed | ||
| 94 | by a newline. To finish the command, send a blank line follwed by | ||
| 95 | a newline. | ||
| 96 | |||
| 97 | For example, to create a command packet with an id of 'open' and | ||
| 98 | two named parmaters: 'filename' that contains a path to a file, | ||
| 99 | and 'play' which is a boolean you would send: | ||
| 100 | |||
| 101 | open | ||
| 102 | filename /path/to/some/stream.mp3 | ||
| 103 | play 1 | ||
| 104 | |||
| 105 | |||
| 106 | Output is achived almost through exactly the same rules, except | ||
| 107 | of course, reversed. Status packets are sent to the filter as | ||
| 108 | hashrefs with two fields 'id' and 'data', id is the name of | ||
| 109 | the status message and data is a hashref possibly containing | ||
| 110 | key/value pairs for named values. | ||
| 111 | |||
| 112 | The filter will then translate the status packet into a text | ||
| 113 | string for output to wherever. The string starts with the | ||
| 114 | 'id' field of the status packet followed by a newline. Then | ||
| 115 | each key/value pair in the 'data' field is sent. The key is | ||
| 116 | sent first and is guaranteed not to contain any whitespace, | ||
| 117 | then a single space and the value is sent followed by a | ||
| 118 | newline. The entire packet is followed by a blank line and | ||
| 119 | a newline. | ||
| 120 | |||
| 121 | For example, a status packet packet with an id field containing | ||
| 122 | 'DECODER_STATUS_DATA' and the data field containing the | ||
| 123 | named paramaters: 'played' containing the value 50, and | ||
| 124 | 'progress' containing the value 10 would be translated to: | ||
| 125 | |||
| 126 | DECODER_STATUS_DATA | ||
| 127 | played 50 | ||
| 128 | progress 10 | ||
| 129 | |||
| 130 | This filter is a bit simplistic, but intentionally so. It's | ||
| 131 | meant to provide an incredibly simple interface to the | ||
| 132 | POE::Wheel::Audio::Mad decoder core. You could just as well | ||
| 133 | use the POE::Filter::Reference filter to communicate more | ||
| 134 | easily with another perl process. This is meant to be used | ||
| 135 | with thin clients. | ||
| 136 | |||
| 137 | =head1 SEE ALSO | ||
| 138 | |||
| 139 | perl(1) | ||
| 140 | |||
| 141 | POE::Component::Audio::Mad::Handle(3) | ||
| 142 | |||
| 143 | Audio::Mad(3) | ||
| 144 | Audio::OSS(3) | ||
| 145 | |||
| 146 | =head1 AUTHOR | ||
| 147 | |||
| 148 | Mark McConnell, E<lt>mischke@cpan.orgE<gt> | ||
| 149 | |||
| 150 | =head1 COPYRIGHT AND LICENSE | ||
| 151 | |||
| 152 | Copyright 2003 by Mark McConnell | ||
| 153 | |||
| 154 | This library is free software; you can redistribute it and/or modify | ||
| 155 | it under the same terms as Perl itself with the exception that you | ||
| 156 | must also feel bad if you don't email me with your opinions of | ||
| 157 | this module. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | 1; | ||
| 162 | __END__ |