Created
February 20, 2014 20:46
-
-
Save xy4n/9122882 to your computer and use it in GitHub Desktop.
dev/morse.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
use Audio::Wav; | |
use Getopt::Long; | |
our %morsetable = ( | |
"a" => ".-", | |
"b" => "-...", | |
"c" => "-.-.", | |
"d" => "-...", | |
"e" => ".", | |
"f" => "..-.", | |
"g" => "--.", | |
"h" => "....", | |
"i" => "..", | |
"j" => ".---", | |
"k" => "-.-", | |
"l" => ".-..", | |
"m" => "--", | |
"n" => "-.", | |
"o" => "---", | |
"p" => ".--.", | |
"q" => "--.-", | |
"r" => ".-.", | |
"s" => "...", | |
"t" => "-", | |
"u" => "..-", | |
"v" => "...-", | |
"w" => ".---", | |
"x" => "-..-", | |
"y" => "-.--", | |
"z" => "--..", | |
"0" => "-----", | |
"1" => ".----", | |
"2" => "..---", | |
"3" => "...--", | |
"4" => "....-", | |
"5" => ".....", | |
"6" => "-....", | |
"7" => "--...", | |
"8" => "---..", | |
"9" => "----.", | |
"." => ".-.-.-", | |
"," => "--..--", | |
"?" => "..--..", | |
"=" => "-...-", | |
); | |
my $outfile = "out.wav"; | |
my $freq = 2000; | |
my $dotsps = 10; | |
my $samplerate = 44100; | |
my $bits = 16; | |
my $volume = 1; | |
GetOptions("out|o=s" => \$outfile, "frequency|freq|f=i" => \$freq, "d|dps=f" => \$dotsps, "samplerate|s=i" => \$samplerate, "bits|b=i" => \$bits, "volume|v=f" => \$volume); | |
my $wav = Audio::Wav->new(); | |
my $write = $wav->write($outfile, | |
{ bits_sample => $bits, | |
sample_rate => $samplerate, | |
channels => 1 }); | |
my $pi = 3.141592; #close enough | |
my $dotlen = 1/$dotsps; | |
my $dashlen = $dotlen*3; | |
my $pauselen = $dotlen*7; | |
my $max_no = (2 ** $bits) / 2 * $volume; | |
my $text = ""; | |
print "Enter your text: "; | |
while (<STDIN>) { | |
chomp; | |
$text .= $_ . " "; | |
} | |
my @morse; | |
push @morse, " "; | |
foreach my $character (split //, $text) { | |
$character = lc $character; | |
my $c2m; | |
if (defined($morsetable{$character})) { $c2m = $morsetable{$character}; } else { $c2m = " " }; | |
push @morse, (split //, $c2m); | |
push @morse, "|" | |
} | |
foreach my $character (@morse) { | |
my $len = 0; | |
my $pause = 0; | |
if ($character eq ".") { | |
$len = $dotlen * $samplerate; | |
} elsif ($character eq "-") { | |
$len = $dashlen * $samplerate; | |
} elsif ($character eq "|") { | |
$len = $dashlen * $samplerate; | |
$pause = 1; | |
} else { | |
$len = $pauselen * $samplerate; | |
$pause = 1; | |
} | |
if ($pause != 1) { | |
for my $pos (0..$len) { | |
my $time = ($pos/$samplerate) * $freq; | |
$write->write( sin($pi * $time) * $max_no ); | |
} | |
for my $pos (0..($dotlen*$samplerate)) { | |
$write->write( 0 ); | |
} | |
} else { | |
for my $pos (0..$len) { | |
$write->write( 0 ); | |
} | |
} | |
} | |
$write->finish(); | |
`oggenc -q3 $outfile` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment