git @ Cat's Eye Technologies The-Dipple / 23477ce
Add older waveform generator; converted visualizer; fix tone.py. catseye 12 years ago
3 changed file(s) with 145 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/perl
1
2 ### GLOBALS ###
3
4 $samples_per_sec = 8000;
5 $pi = 3.14159;
6
7 ### SUBS ###
8
9 sub setup_tables()
10 {
11 my $i;
12
13 $s[0] = 1.0;
14 $s[1] = 135 / 128;
15 $s[2] = 9 / 8;
16 $s[3] = 6 / 5;
17 $s[4] = 5 / 4;
18 $s[5] = 4 / 3;
19 $s[6] = 45 / 32;
20 $s[7] = 3 / 2;
21 $s[8] = 8 / 5;
22 $s[9] = 27 / 16;
23 $s[10] = 9 / 5;
24 $s[11] = 15 / 8;
25
26 for ($i = 12; $i <= 96; $i++)
27 {
28 $s[$i] = $s[$i - 12] * 2;
29 }
30
31 $major[0] = 0;
32 $major[1] = 2;
33 $major[2] = 4;
34 $major[3] = 5;
35 $major[4] = 7;
36 $major[5] = 9;
37 $major[6] = 11;
38
39 for ($i = 7; $i <= 64; $i++)
40 {
41 $major[$i] = $major[$i - 7] + 12;
42 }
43 }
44
45 sub play_sound($$$$$$)
46 {
47 my $b_freq = shift;
48 my $e_freq = shift;
49 my $b_amp = shift;
50 my $e_amp = shift;
51 my $dur = shift;
52 my $func = shift;
53 my $sampno, $wavelength;
54 my $freq, $amp;
55 my $sample;
56
57 $func = sub
58 {
59 my $sampno = shift;
60 my $wavelength = shift;
61 return sin($sampno / ($wavelength * $pi)) * 128 + 128;
62 } if $func eq 'sine';
63
64 $func = sub
65 {
66 my $sampno = shift;
67 my $wavelength = shift;
68 # print STDERR "i $sampno wl $wavelength\n";
69 # real modulo.
70 my $modulo = $sampno - (int($sampno / $wavelength) * $wavelength);
71 return 255 if ($modulo < ($wavelength / 2));
72 return 0;
73 } if $func eq 'square';
74
75 for ($sampno = 0; $sampno < $dur; $sampno++)
76 {
77 $freq = ($b_freq + ($e_freq - $b_freq) * ($sampno / $dur)); # XXX logarithms
78 $amp = ($b_amp + ($e_amp - $b_amp) * ($sampno / $dur)); # XXX logarithms
79
80 $wavelength = $samples_per_sec / $freq;
81 $sample = &$func($sampno, $wavelength) * $amp;
82 $sample = 0 if $sample < 0;
83 $sample = 255 if $sample > 255;
84 print chr(int($sample));
85 }
86 }
87
88 sub play_rest($)
89 {
90 my $dur = shift;
91
92 for ($i = 0; $i < $dur; $i++)
93 {
94 print chr(0);
95 }
96 }
97
98 sub play_tone($$)
99 {
100 my $note = shift;
101 my $dur = shift;
102
103 my $freq1 = $s[$note] * 440;
104 # my $freq2 = $s[$note + 1] * 440;
105 play_sound($freq1, $freq1, 1.0, 1.0, $dur, 'square');
106 }
107
108 ### MAIN ###
109
110 setup_tables();
111 for ($n = 7; $n <= 35; $n++)
112 {
113 play_tone($major[$n], 2000);
114 # play_rest(1000);
115 }
0 #!/usr/bin/env python
1
2 import sys
3
4 v = 'x'
5 while len(v) == 1:
6 v = sys.stdin.read(1)
7 if len(v) == 1:
8 c = int((ord(v) / 256.0) * 79.0)
9 print '(%03d)%s' % (ord(v), '*' * c)
00 # usage: python tone.py | aplay
11
2 for d in (5.0, 10.0, 15.0, 20.0):
3 for x in range(0, int(5.0 * d)):
4 v = 0
5 while v < 255.0:
6 print chr(int(v))
7 v += d
8 v -= d
9 while v > 0.0:
10 print chr(int(v))
2 import sys
3
4 for g in range(1, 10):
5 for d in (5.0, 10.0, 15.0, 20.0, 17.5, 15.0, 12.5, 14.1615):
6 for x in range(0, int(5.0 * d)):
7 v = 0
8 while v < 255.0:
9 sys.stdout.write(chr(int(v)))
10 v += d
1111 v -= d
12 while v > 0.0:
13 sys.stdout.write(chr(int(v)))
14 v -= d
1215
13 for d in (5.0, 10.0, 15.0, 20.0):
14 for x in range(0, int(10.0 * d)):
15 v = 0
16 while v < 255.0:
17 print chr(int(v))
18 v += d
16 if False:
17 for d in (5.0, 10.0, 15.0, 20.0):
18 for x in range(0, int(10.0 * d)):
19 v = 0
20 while v < 255.0:
21 sys.stdout.write(chr(int(v)))
22 v += d