summaryrefslogtreecommitdiff
path: root/numbers-station.raku
blob: b0d776521c30208bad3574569b0219a2d7772293 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/raku

# This is a numbers station script
# It converts letters into numbers and says those numbers
#
# Based on the Major System 
# 0 : s z
# 1 : t d th
# 2 : n
# 3 : m
# 4 : r
# 5 : l
# 6 : ch g sh c
# 7 : k g ch (like, bach the musician) ck
# 8 : f v
# 9 : p b
#   : h j w
#
# I should probably change this as you are supposed to recall the numbers then make a story out of it,
# not the other way around.
#
# This script will need to:
# 1. somehow know the proper puncication of each word ( probably impossible to accurately impliment :(
# 2. convert each word into numbers
# 3. play the sound for each numbers
# 3.1. add a space between each number set

use Audio::PortAudio;
use Audio::Sndfile;

my $audio_location = './audio/';
my $audio_extension = '.wav';
my %numberlist = (1 => <1a 1b 1c>, 2 => <2a 2b 2c>, 3 => <3a 3b 3c>, 4 => <4a 4b 4c>,
	5 => <5a 5b 5c>, 6 => <6a 6b 6c>, 7 => <7a 7b 7c>, 8 => <8a 8b 8c>, 9 => <9a 9b 9c>, 0 => <0a 0b 0c>);

# create audio thing
my $pa;
quietly { $pa = Audio::PortAudio.new };
# create audio stream
my Audio::PortAudio::Stream $st;

sub MAIN($text) {

	if $text.IO.e {
		my $t = w2n(open($text));
		print "$t\n";
		speak($t);
	}
	else {
		my $t = w2n($text);
		print "$t\n";
		speak(w2n($text));
	}

	$pa.terminate;
}

# words to number
sub w2n($file) {
	my @n = $file.words;
	for @n <-> $word {
		$word ~~ s :g:i /(ch)|g|(sh)|c/6/;
		$word ~~ s :g:i /s|z/0/;
		$word ~~ s :g:i /t|d|(th)/1/;
		$word ~~ s :g:i /n/2/;
		$word ~~ s :g:i /m/3/;
		$word ~~ s :g:i /r/4/;
		$word ~~ s :g:i /l/5/;
		$word ~~ s :g:i /k|g|ck/7/;
		$word ~~ s :g:i /f|v/8/;
		$word ~~ s :g:i /p|b/9/;
		$word ~~ tr/A..Za..z',//;
		# no small numbers ?
		if $word.chars == 1 {
			$word = '';
		}
	}

	# removing empty elements (yes, this looks dumb)
	#dd @n;
	loop (my $i = 0; $i < 5; $i++){
		my $k = 0;
		for @n <-> $e {
			if ! $e {
				splice(@n, $k, 1);
			}
			$k++;
		}
	}
	#dd @n;

	return @n;
}

sub speak(@a) {
	# get ready to stream the audio file
	for @a -> $numbers {
		#print $elem.raku;
		sleep 1;
		print ' ';
		for breakup($numbers) -> $n {
			#print $n;

			my $spoken = %numberlist{$n}[].roll;
			
			# load the actual file
			my $audio = Audio::Sndfile.new(filename => "$audio_location/$spoken$audio_extension", :r);
			
			#$spoken.say;

			$st = $pa.open-default-stream(0, $audio.channels,
				Audio::PortAudio::StreamFormat::Float32, $audio.samplerate, 512);

			# start ...
			$st.start;

			loop {
				my ($buf, $frames) = $audio.read-float(512, :raw);
				try {
					$st.write($buf, $frames);
				}
				last if $frames < 512;
			}

			$st.close;

			# Free up memory?
			$audio = Nil;
		}
	}
}

# returns each char as array
sub breakup($s) {
	my @a;
	loop (my $i = 0; $i != $s.chars; $i++) {
		@a.push( substr($s, $i..$i) );
	}
	return @a;
}