blib/lib/PDL/Demos/Sound.pm

filter: Statementstmt 75.0% 18 / 24 Branchbran 33.3% 2 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 3 / 5 Podpod 0.0% 0 / 3 totaltotal 60.5% 23 / 38 CC 2 SCAR 10.4
CC
2
Cov
67%
demo
6.0
init
2.0
BEGIN
1.0
CRAP 2.8
« blib/lib/PDL/PP/Dims.pm ↑ Summary blib/lib/PDL/Config.pm »
1 package PDL::Demos::Sound;
2 1 ▶use PDL::Demos;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use File::Which qw/which/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $player;
7 1 our $player_args;
8  
9 # Find a suitable sound player and provide command line switches
10 # - aplay comes with ALSA (in Debian: package alsa-utils)
11 # - play comes with SoX (in Debian: package sox)
12 1 ▶my %args = (
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/ ? : 0 1 1
13 'aplay' => ['--quiet'], # we mostly use aplay's defaults
14 'play' => [
15 '--no-show-progress',
16 '--buffer' => 256, # makes it work on Win32
17 '--type' => 'raw',
18 '--rate' => 8000, # the aplay default
19 '--bits' => 8, # the aplay default
20 '--channels' => 1, # the aplay default
21 '--encoding' => 'unsigned-integer',
22 '-', # using STDIN
23 '--channels' => 1,
24 ($^O =~ /MSWin32/ ? ('--type' => 'waveaudio') : ()),
25 ]
26 );
27 1 $args{sox} = [ @{$args{play}}, '--default' ];
28 FIND_EXE:
29 1 for my $candidate (qw(aplay play sox)) {
30 3 ▶ which $candidate and do {
Branch: 1 branch
BranchTrueFalseTotal
which($candidate) and do { $player = $candidate; $player_args = $args{$player}; last FIND_EXE } 0 3 3
31 0 $player = $candidate;
32 0 $player_args = $args{$player};
33 0 last FIND_EXE;
34 }
35 }
36  
37 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
38 0 return <<'EndOfText';
39 our $player = $PDL::Demos::Sound::player;
40 our $player_args = $PDL::Demos::Sound::player_args;
41 EndOfText
42 }
43  
44 1 my $scale = <<'EndOfScale';
45  
46 open (my $pipe, '|-', $player, @$player_args)
47 or die "Can not start a sound player. Demo failed.\n";
48 binmode $pipe;
49 my $n = 4000;
50 my $samples = pdl[0..$n-1];
51 my $raw_sound = byte(zeroes($n));
52 my $amplitude = 80;
53 for (8,9,10,32/3,12,40/3,15,16) {
54 $raw_sound = byte($amplitude*(1+sin($samples*($_/8)*440*3.14/$n)));
55 print $pipe ${$raw_sound->get_dataref};
56 }
57 close $pipe;
58 EndOfScale
59  
60 1 my $chord = <<'EndOfChord';
61  
62 open (my $pipe, '|-', $player, @$player_args)
63 or die "Can not start a sound player. Demo failed.\n";
64 binmode $pipe;
65 my $n = 8000;
66 my $samples = pdl[0..$n-1];
67 my $raw_sound = byte(zeroes($n));
68 my $amplitude = 30;
69 for (8,10,12,16) {
70 $raw_sound += byte($amplitude*(1+sin($samples*($_/8)*440*6.28/$n)));
71 }
72 print $pipe ${$raw_sound->get_dataref};
73 close $pipe;
74 EndOfChord
75  
76 1 my @demo = ([comment => "Listen to Perl and PDL: The A major scale\n"],
77 [actnw => $scale],
78 [comment => "Listen to Perl and PDL: A Chord\n"],
79 [actnw => $chord],
80 );
81 0 ▶sub demo {
Subroutine demo: not called CC=2 SCAR=17.9
Pod undocumented
82 0 ▶ return @demo if $PDL::Demos::Sound::player;
Branch: 1 branch
BranchTrueFalseTotal
if $player 0 0 0
83 0 ([comment => <<EndOfComment]);
84 This demo requires an external command to play the sound,
85 unfortunately no suitable candidate could be found.
86 The following players are supported:
87 - 'aplay' (from the ALSA suite) comes with the package 'alsa-utils'
88 on Linux
89 - 'play' and 'sox' (from SoX) are available as package 'sox' on Linux,
90 and can be obtained from https://sourceforge.net/projects/sox/
91 for Microsoft Windows and MacOS.
92 EndOfComment
93 }
94  
95 1 ▶sub info {
Subroutine info: called CC=1 SCAR=0.0
Pod undocumented
96 1 ('sound', 'Sound (requires a sound player)')
97 }
98  
99 1 1;
100  
101 __END__
« blib/lib/PDL/PP/Dims.pm ↑ Summary blib/lib/PDL/Config.pm »