File Coverage

blib/lib/Audio/Radio/V4L.pm
Criterion Covered Total %
statement 10 55 18.1
branch 0 22 0.0
condition 0 2 0.0
subroutine 4 15 26.6
pod 7 7 100.0
total 21 101 20.7


line stmt bran cond sub pod time code
1             BEGIN {
2 1     1   23067 $VERSION = '0.01';
3             }
4             ########################################### main pod documentation begin ##
5              
6             =head1 NAME
7              
8             Audio::Radio::V4L
9              
10             =head1 SYNOPSIS
11              
12             use Audio::Radio::V4L;
13             my $radio = Audio::Radio::V4L->new;
14             $radio->open("/dev/radio");
15             $radio->set_frequency( 88800 ); # frequency in khz
16            
17             sleep( 10 );
18            
19             $radio->close();
20              
21             =head1 DESCRIPTION
22              
23             Audio::Radio::V4L uses the Video4Linux interface to control radio receivers (eg. internal radio cards or USB receivers).
24              
25             =head1 USAGE
26              
27             Open the device via open().
28             Get the highest and lowest supported frequency of the radio: get_freq_min() and get_freq_max().
29             Set the frequency with set_frequency().
30             Listen or record.
31             Close with close().
32              
33             =head1 PUBLIC METHODS
34              
35             Each public function/method is described here.
36             These are how you should interact with this module.
37              
38             =cut
39              
40             ############################################# main pod documentation end ##
41             package Audio::Radio::V4L;
42 1     1   7 use strict;
  1         3  
  1         28  
43 1     1   4 use Carp;
  1         2  
  1         757  
44              
45             ################################################ subroutine header begin ##
46              
47             =head2 new()
48              
49             Usage : new Audio::Radio::V4L
50             Purpose : creates a new radio object
51             Returns : the new object
52             Argument : none
53              
54             =cut
55              
56             ################################################## subroutine header end ##
57             sub new() {
58 1     1 1 12 my $class = shift;
59 1         4 my $self = bless {}, $class;
60 1         2 $self;
61             }
62              
63             ################################################ subroutine header begin ##
64              
65             =head2 open()
66              
67             Usage : $radio->open("/dev/radio")
68             Purpose : opens the radio-device, initializes values (max-freq, min-freq)
69             Returns : self
70             Argument : the device
71             Throws : croaks on problems
72              
73             =cut
74              
75             ################################################## subroutine header end ##
76             sub open() {
77 0     0 1   my $self = shift;
78 0   0       my $devicename = shift || "/dev/radio";
79 0           my $devicenumber = shift;
80 0 0         (defined $devicenumber) || ($devicenumber = 0);
81 0           $self->{ _devicenumber } = $devicenumber;
82 0 0         $self->{ _fh } && $self->close();
83 0 0         open($self->{ _fh }, $devicename)
84             or croak "Could not open radio $devicename: $!";
85 0           $self->_initialize_values_from_device();
86 0           $self;
87             }
88              
89             sub _initialize_values_from_device() {
90 0     0     my $self = shift;
91             # struct from linux/videodev.h
92 0           my $videotuner = pack("iZ32LLLss",$self->{ _devicenumber },"",0,0,0,0);
93             ioctl(
94             $self->{ _fh },
95 0           $self->_get_VIDIOCGTUNER,
96             $videotuner
97             );
98 0           $self->{ _vt } = $videotuner;
99 0           my @values = unpack("iZ32LLLss", $videotuner);
100 0           $self->{ _devicename } = $values[ 1 ];
101 0           $self->{ _rangelow } = $values[ 2 ];
102 0           $self->{ _rangehigh } = $values[ 3 ];
103 0           $self->{ _deviceflags } = $values[ 4 ];
104 0           $self->{ _devicemode } = $values[ 5 ];
105 0           $self->{ _devicesignal } = $values[ 5 ];
106 0           $self;
107             }
108              
109             ################################################ subroutine header begin ##
110              
111             =head2 get_devicename()
112              
113             Usage : $radio->get_devicename()
114             Returns : returns the devicename of the opened radio
115             Argument : none
116              
117             =cut
118              
119             ################################################## subroutine header end ##
120             sub get_devicename() {
121 0     0 1   my $self = shift;
122 0 0         $self->{ _fh } or croak "No device opened!";
123 0           $self->{ _devicename };
124             }
125              
126             ################################################ subroutine header begin ##
127              
128             =head2 get_freq_min()
129              
130             Usage : $radio->get_freq_min()
131             Returns : the minimal supported frequency of the radio
132             Argument : none
133              
134             =cut
135              
136             ################################################## subroutine header end ##
137             sub get_freq_min() {
138 0     0 1   my $self = shift;
139 0 0         $self->{ _fh } or croak "No device opened!";
140 0           $self->{ _rangelow } / $self->_get_frequency_factor();
141             }
142              
143             ################################################ subroutine header begin ##
144              
145             =head2 get_freq_max()
146              
147             Usage : $radio->get_freq_max()
148             Returns : the maximal supported frequency of the radio
149             Argument : none
150              
151             =cut
152              
153             ################################################## subroutine header end ##
154             sub get_freq_max() {
155 0     0 1   my $self = shift;
156 0 0         $self->{ _fh } or croak "No device opened!";
157 0           $self->{ _rangehigh } / $self->_get_frequency_factor();
158             }
159              
160             ################################################ subroutine header begin ##
161              
162             =head2 close()
163              
164             Usage : $radio->close()
165             Purpose : closes the device
166             Returns : self
167             Argument : none
168             Throws : croaks on problems
169              
170             =cut
171              
172             ################################################## subroutine header end ##
173             sub close() {
174 0     0 1   my $self = shift;
175 0 0         croak "No radio to close" unless $self->{ _fh };
176             close( $self->{ _fh } )
177 0 0         or croak "Could not close radio: $!";
178 0           delete $self->{ _fh };
179 0           $self;
180             }
181              
182             ################################################ subroutine header begin ##
183              
184             =head2 set_frequency()
185              
186             Usage : $radio->set_frequency( 106500 );
187             Purpose : sets the frequency of the device
188             Returns : self
189             Argument : the frequency in khz
190              
191             =cut
192              
193             ################################################## subroutine header end ##
194             sub set_frequency() {
195 0     0 1   my $self = shift;
196 0           my $frequency = shift;
197 0 0         croak "Open the radio first!" unless $self->{ _fh };
198             ioctl(
199             $self->{ _fh },
200 0 0         $self->_get_VIDIOCSFREQ,
201             pack(
202             "L",
203             int( $frequency * $self->_get_frequency_factor() )
204             )
205             )
206             or croak "Could not set frequency: $!";
207 0           $self;
208             }
209              
210             sub _get_VIDIOCSFREQ() {
211 0     0     return 0x4004760f;
212             # return __get_VIDIOCSFREQ();
213             }
214              
215             #use Inline C => <<'END_OF_C';
216             # #include
217             # long __get_VIDIOCSFREQ() {
218             # return VIDIOCSFREQ;
219             # }
220             #
221             #END_OF_C
222              
223             sub _get_VIDIOCGTUNER() {
224 0     0     return 0xC0347604;
225             # return __get_VIDIOCGTUNER();
226             }
227              
228             #use Inline C => <<'END_OF_C';
229             # #include
230             # long __get_VIDIOCGTUNER() {
231             # return VIDIOCGTUNER;
232             # }
233             #
234             #END_OF_C
235              
236             sub _get_VIDEO_TUNER_LOW() {
237 0     0     return 8;
238             # return __get_VIDEO_TUNER_LOW;
239             }
240              
241             #use Inline C => <<'END_OF_C';
242             # #include
243             # long __get_VIDEO_TUNER_LOW() {
244             # return VIDEO_TUNER_LOW;
245             # }
246             #
247             #END_OF_C
248              
249             sub _get_frequency_factor() {
250 0     0     my $self = shift;
251 0 0         $self->{ _deviceflags } & _get_VIDEO_TUNER_LOW() ?
252             16
253             : .016;
254             }
255              
256             1;
257              
258             #
259             =head1 BUGS
260             Many, but none known :)
261              
262             =head1 SUPPORT
263              
264             =head1 AUTHOR
265              
266             Nathanael Obermayer
267             natom-cpan@smi2le.net
268             http://neuronenstern.de
269              
270             =head1 COPYRIGHT
271              
272             Copyright (c) 2003 Nathanael Obermayer. All rights reserved.
273             This program is free software; you can redistribute
274             it and/or modify it under the same terms as Perl itself.
275              
276             The full text of the license can be found in the
277             LICENSE file included with this module.
278              
279             =head1 SEE ALSO
280              
281             Video::Capture::V4l
282            
283             =cut