From 333dfebbea38bc80f51f02e9f035d8a270b33869 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 3 Mar 2022 06:24:58 -0500 Subject: Play audio back to the user --- numbers-station.raku | 95 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/numbers-station.raku b/numbers-station.raku index 82a993d..b0d7765 100755 --- a/numbers-station.raku +++ b/numbers-station.raku @@ -16,19 +16,44 @@ # 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>); +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; -my $file = w2n(open('country_roads.txt')); +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)); + } -#say "$audio_location, $audio_extension, " ~ %numberlist{1}[].roll; + $pa.terminate; +} # words to number sub w2n($file) { @@ -45,11 +70,71 @@ sub w2n($file) { $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 = ''; + $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; } -print $file ~ "\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; +} -- cgit v1.2.3