| 1 | # -*- cperl -*- | ||||||||||||
| 2 | |||||||||||||
| 3 | # Audio::OSS - Less DWIM, more useful than Audio::DSP | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # Copyright (c) 2000 Cepstral LLC. All rights Reserved. | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # This module is free software; you can redistribute it and/or modify | ||||||||||||
| 8 | # it under the same terms as Perl itself. | ||||||||||||
| 9 | # | ||||||||||||
| 10 | # Written by David Huggins-Daines <dhd@cepstral.com> | ||||||||||||
| 11 | |||||||||||||
| 12 | package Audio::OSS; | ||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | # We should maybe generate these in Makefile.PL, but they don't use | ||||||||||||
| 16 | # any types that are likely to change alignment or size between | ||||||||||||
| 17 | # platforms. | ||||||||||||
| 18 | 1 | ▶ | use constant CINFO_TMPL => 'lll'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | use constant BINFO_TMPL => 'llll'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use constant MINFO_TMPL => 'a16 a32 l'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | |||||||||||||
| 22 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS @DevNames); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | require Exporter; | |||||||||||
| 24 | 1 | @ISA = qw(Exporter); | |||||||||||
| 25 | |||||||||||||
| 26 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=15.1
| |||||||||||||
| 27 | # Defines constants, which is why it needs to be done in BEGIN{} | ||||||||||||
| 28 | # Also creates @EXPORT_OK, hence the push below | ||||||||||||
| 29 | 1 | require Audio::OSS::Constants; | |||||||||||
| 30 | |||||||||||||
| 31 | # Special-case a few useful constants | ||||||||||||
| 32 | 1 | ▶ | unless (defined(&AFMT_S16_NE)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 0 | ▶ | if (unpack("L", pack("C*", 1, 2, 3, 4)) == 0x01020304) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 0 | ▶ | *AFMT_S16_NE = sub () { AFMT_S16_BE() }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 35 | } else { | ||||||||||||
| 36 | 0 | ▶ | *AFMT_S16_NE = sub () { AFMT_S16_LE() }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | } | ||||||||||||
| 39 | # Pseudo-ioctls (we assume that various OSes will form these the | ||||||||||||
| 40 | # same way...) | ||||||||||||
| 41 | 0 | ▶ | *SOUND_MIXER_READ = sub () { SOUND_MIXER_READ_VOLUME() - SOUND_MIXER_VOLUME() }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 42 | 0 | ▶ | *SOUND_MIXER_WRITE = sub () { SOUND_MIXER_WRITE_VOLUME() - SOUND_MIXER_VOLUME() }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 43 | 1 | push @EXPORT_OK, qw(SOUND_MIXER_READ SOUND_MIXER_WRITE); | |||||||||||
| 44 | 1 | push @{$EXPORT_TAGS{mixer}}, qw(SOUND_MIXER_READ SOUND_MIXER_WRITE); | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | # Don't bother getting this from the header file | ||||||||||||
| 48 | 1 | @DevNames = qw( | |||||||||||
| 49 | vol bass treble synth pcm speaker line | ||||||||||||
| 50 | mic cd mix pcm2 rec igain ogain line1 line2 | ||||||||||||
| 51 | line3 dig1 dig2 dig3 phin phout video radio monitor | ||||||||||||
| 52 | ); | ||||||||||||
| 53 | |||||||||||||
| 54 | # Use push, because BEGIN blocks may frob these | ||||||||||||
| 55 | 1 | push @EXPORT_OK, qw(dsp_sync dsp_reset set_fragment get_fmt | |||||||||||
| 56 | get_outbuf_ptr get_inbuf_ptr | ||||||||||||
| 57 | get_outbuf_info get_inbuf_info | ||||||||||||
| 58 | mixer_read mixer_write @DevNames); | ||||||||||||
| 59 | 1 | push @{$EXPORT_TAGS{funcs}}, | |||||||||||
| 60 | qw[ | ||||||||||||
| 61 | dsp_sync | ||||||||||||
| 62 | dsp_reset | ||||||||||||
| 63 | set_fragment | ||||||||||||
| 64 | get_fmt | ||||||||||||
| 65 | get_outbuf_ptr | ||||||||||||
| 66 | get_inbuf_ptr | ||||||||||||
| 67 | get_outbuf_info | ||||||||||||
| 68 | get_inbuf_info | ||||||||||||
| 69 | mixer_read | ||||||||||||
| 70 | mixer_write | ||||||||||||
| 71 | ]; | ||||||||||||
| 72 | |||||||||||||
| 73 | 1 | $VERSION=0.05_01; | |||||||||||
| 74 | |||||||||||||
| 75 | 0 | ▶ | sub dsp_reset { | ||||||||||
|
Subroutine
dsp_reset: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 76 | 0 | my $dsp = shift; | |||||||||||
| 77 | 0 | ▶ | ioctl $dsp, SNDCTL_DSP_SYNC, 0 or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 0 | ioctl $dsp, SNDCTL_DSP_RESET, 0; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | sub dsp_sync { | ||||||||||
|
Subroutine
dsp_sync: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 82 | 0 | my $dsp = shift; | |||||||||||
| 83 | 0 | ioctl $dsp, SNDCTL_DSP_SYNC, 0; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | ▶ | sub get_fmt { | ||||||||||
|
Subroutine
get_fmt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 87 | 0 | my $dsp = shift; | |||||||||||
| 88 | 0 | my $sfmt = pack "L", AFMT_QUERY; | |||||||||||
| 89 | 0 | ▶ | ioctl $dsp, SNDCTL_DSP_SETFMT, $sfmt or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | return unpack "L", $sfmt; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | sub set_fragment { | ||||||||||
|
Subroutine
set_fragment: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 94 | 0 | my ($dsp, $shift, $max) = @_; | |||||||||||
| 95 | |||||||||||||
| 96 | # This is not really documented, but the code of the sound drivers | ||||||||||||
| 97 | # says that this is two halfwords packed together in host byte | ||||||||||||
| 98 | # order, the MSW being the shift (assuming this means size log 2), | ||||||||||||
| 99 | # the lower being the maximum number. In general it seems that | ||||||||||||
| 100 | # shift must be 4 <= shift < 16, maxfrags must be >= 4. | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | my $sfrag = pack "L", (($max << 16) | $shift); | |||||||||||
| 103 | 0 | ioctl $dsp, SNDCTL_DSP_SETFRAGMENT, $sfrag; | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | sub get_outbuf_ptr { | ||||||||||
|
Subroutine
get_outbuf_ptr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 107 | 0 | my $dsp = shift; | |||||||||||
| 108 | 0 | my $cinfo = pack CINFO_TMPL; | |||||||||||
| 109 | 0 | ▶ | ioctl($dsp, SNDCTL_DSP_GETOPTR, $cinfo) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | return unpack CINFO_TMPL, $cinfo; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | sub get_inbuf_ptr { | ||||||||||
|
Subroutine
get_inbuf_ptr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 114 | 0 | my $dsp = shift; | |||||||||||
| 115 | 0 | my $cinfo = pack CINFO_TMPL; | |||||||||||
| 116 | 0 | ▶ | ioctl($dsp, SNDCTL_DSP_GETIPTR, $cinfo) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 0 | return unpack CINFO_TMPL, $cinfo; | |||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | sub get_outbuf_info { | ||||||||||
|
Subroutine
get_outbuf_info: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 121 | 0 | my $dsp = shift; | |||||||||||
| 122 | 0 | my $binfo = pack BINFO_TMPL; | |||||||||||
| 123 | 0 | ▶ | ioctl($dsp, SNDCTL_DSP_GETOSPACE, $binfo) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | 0 | return unpack BINFO_TMPL, $binfo; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 0 | ▶ | sub get_inbuf_info { | ||||||||||
|
Subroutine
get_inbuf_info: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 128 | 0 | my $dsp = shift; | |||||||||||
| 129 | 0 | my $binfo = pack BINFO_TMPL; | |||||||||||
| 130 | 0 | ▶ | ioctl($dsp, SNDCTL_DSP_GETISPACE, $binfo) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 0 | return unpack BINFO_TMPL, $binfo; | |||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | # Some constants may not be defined, don't define their subs either | ||||||||||||
| 135 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.6
| |||||||||||||
| 136 | 1 | ▶ | if (defined &SOUND_MIXER_INFO) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 0 | ▶ | *get_mixer_info = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 138 | 0 | my $mixer = shift; | |||||||||||
| 139 | 0 | my $minfo = pack MINFO_TMPL; | |||||||||||
| 140 | 0 | ▶ | ioctl($mixer, SOUND_MIXER_INFO(), $minfo) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | 0 | return unpack MINFO_TMPL, $minfo; | |||||||||||
| 142 | 1 | }; | |||||||||||
| 143 | 1 | push @EXPORT_OK, 'get_mixer_info'; | |||||||||||
| 144 | 1 | push @{$EXPORT_TAGS{funcs}}, 'get_mixer_info'; | |||||||||||
| 145 | } | ||||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | # Templated ioctls that just read or write a single integer value | ||||||||||||
| 149 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.6
| |||||||||||||
| 150 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 151 | 1 | my @rw_ioctls = ( | |||||||||||
| 152 | dsp_get_caps => SNDCTL_DSP_GETCAPS, | ||||||||||||
| 153 | get_supported_fmts => SNDCTL_DSP_GETFMTS, | ||||||||||||
| 154 | set_sps => SNDCTL_DSP_SPEED, | ||||||||||||
| 155 | set_fmt => SNDCTL_DSP_SETFMT, | ||||||||||||
| 156 | set_stereo => SNDCTL_DSP_STEREO, | ||||||||||||
| 157 | mixer_read_devmask => SOUND_MIXER_READ_DEVMASK, | ||||||||||||
| 158 | mixer_read_recmask => SOUND_MIXER_READ_RECMASK, | ||||||||||||
| 159 | mixer_read_stereodevs => SOUND_MIXER_READ_STEREODEVS, | ||||||||||||
| 160 | mixer_read_caps => SOUND_MIXER_READ_CAPS, | ||||||||||||
| 161 | ); | ||||||||||||
| 162 | 1 | ▶ | while (my ($sub, $ioctl) = splice @rw_ioctls, 0, 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 0 | ▶ | *$sub = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 164 | 0 | my $fh = shift; | |||||||||||
| 165 | 0 | ▶ | my $in = shift || 0; | ||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 166 | 0 | my $out = pack "L", $in; | |||||||||||
| 167 | 0 | ▶ | ioctl($fh, $ioctl, $out) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | 0 | return unpack "L", $out; | |||||||||||
| 169 | 9 | }; | |||||||||||
| 170 | 9 | push @EXPORT_OK, $sub; | |||||||||||
| 171 | 9 | push @{$EXPORT_TAGS{funcs}}, $sub; | |||||||||||
| 172 | } | ||||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | 0 | ▶ | sub mixer_read { | ||||||||||
|
Subroutine
mixer_read: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 176 | 0 | my ($mixer, $channel) = @_; | |||||||||||
| 177 | 0 | my $vol = pack "L"; | |||||||||||
| 178 | 0 | ▶ | ioctl($mixer, SOUND_MIXER_READ + $channel, $vol) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | 0 | return unpack "L", $vol; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 0 | ▶ | sub mixer_write { | ||||||||||
|
Subroutine
mixer_write: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 183 | 0 | my ($mixer, $channel, $left, $right) = @_; | |||||||||||
| 184 | 0 | my $vol = pack("L", $left | ($right << 8)); | |||||||||||
| 185 | 0 | ▶ | ioctl($mixer, SOUND_MIXER_WRITE + $channel, $vol) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 186 | 0 | return unpack "L", $vol; | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | 1 | 1; | |||||||||||
| 190 | __END__ | ||||||||||||