#!/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; }