| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package PDL::Demos::Sound; |
|
2
|
1
|
|
|
1
|
|
7
|
use PDL::Demos; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
80
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
644
|
use File::Which qw/which/; |
|
|
1
|
|
|
|
|
1663
|
|
|
|
1
|
|
|
|
|
448
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $player; |
|
7
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my %args = ( |
|
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
|
|
|
|
|
|
|
$args{sox} = [ @{$args{play}}, '--default' ]; |
|
28
|
|
|
|
|
|
|
FIND_EXE: |
|
29
|
|
|
|
|
|
|
for my $candidate (qw(aplay play sox)) { |
|
30
|
|
|
|
|
|
|
which $candidate and do { |
|
31
|
|
|
|
|
|
|
$player = $candidate; |
|
32
|
|
|
|
|
|
|
$player_args = $args{$player}; |
|
33
|
|
|
|
|
|
|
last FIND_EXE; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub init { |
|
38
|
0
|
|
|
0
|
0
|
0
|
return <<'EndOfText'; |
|
39
|
|
|
|
|
|
|
our $player = $PDL::Demos::Sound::player; |
|
40
|
|
|
|
|
|
|
our $player_args = $PDL::Demos::Sound::player_args; |
|
41
|
|
|
|
|
|
|
EndOfText |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub demo { |
|
82
|
0
|
0
|
|
0
|
0
|
0
|
return @demo if $PDL::Demos::Sound::player; |
|
83
|
0
|
|
|
|
|
0
|
([comment => <
|
|
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
|
|
|
|
|
|
|
sub info { |
|
96
|
1
|
|
|
1
|
0
|
6
|
('sound', 'Sound (requires a sound player)') |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |