September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,26 @@
*TEMPO 8
DIM morse$(63)
FOR char% = 0 TO 63 : READ morse$(char%) : NEXT char%
PROCmorse("The five boxing wizards jump quickly.")
END
DEF PROCmorse(text$)
LOCAL element%, index%, char&, morse$
FOR index% = 1 TO LEN(text$)
char& = ASC(MID$(text$,index%)) AND &7F
IF char& < 32 char& = 32
IF char& > 95 char& -= 32
morse$ = morse$(char&-32)
FOR element% = 1 TO LEN(morse$)
SOUND 1, -15, 148, VAL(MID$(morse$,element%,1))
SOUND 1, -15, 0, 1
NEXT element%
SOUND 1, -15, 0, 2
NEXT index%
ENDPROC
DATA 00,313133,131131,6,1113113,6,13111,133331,31331,313313,6,13131,331133,311113,131313,31131
DATA 33333,13333,11333,11133,11113,11111,31111,33111,33311,33331,333111,313131,6,31113,6,113311
DATA 133131,13,3111,3131,311,1,1131,331,1111,11,1333,313,1311,33,31,333
DATA 1331,3313,131,111,3,113,1113,133,3113,3133,3311,6,6,6,6,113313

View file

@ -0,0 +1,28 @@
100 PROGRAM "Morse.bas"
110 STRING TONE$(48 TO 90)*5,ST$*254
120 SET CHARACTER 46,0,0,0,0,24,24,0,0,0:SET CHARACTER 47,0,0,0,0,126,126,0,0,0
130 FOR I=48 TO 90
140 READ TONE$(I)
150 NEXT
160 DO
170 PRINT :PRINT "String to convert to Morse code: ":INPUT PROMPT ">":ST$
180 LET ST$=LTRIM$(RTRIM$(UCASE$(ST$)))
190 FOR I=1 TO LEN(ST$)
200 LET C=ORD(ST$(I:I))
210 IF C>47 AND C<91 THEN
220 PRINT TONE$(C);" ";
230 FOR J=1 TO LEN(TONE$(C))
240 SOUND PITCH 48,DURATION(ORD(TONE$(C)(J))-45)^3+4
250 SOUND PITCH 126,DURATION 8
260 NEXT
270 ELSE
280 PRINT
290 SOUND PITCH 126,DURATION 16
300 END IF
310 SOUND PITCH 126,DURATION 16
320 NEXT
330 PRINT
340 LOOP UNTIL ST$=""
350 CLEAR FONT
360 DATA .////,..///,...//,..../,.....,/....,//...,///..,////.,/////,"","","","","","",""
370 DATA ./,/...,/./.,/..,.,../.,//.,....,..,.///,/./,./..,//,/.,///,.//.,//./,./.,...,/,../,.../,.//,/../,/.//,//..

View file

@ -0,0 +1,51 @@
class Morse
constructor : (@unit=0.05, @freq=700) ->
@cont = new AudioContext()
@time = @cont.currentTime
@alfabet = "..etianmsurwdkgohvf.l.pjbxcyzq..54.3...2.......16.......7...8.90"
getCode : (letter) ->
i = @alfabet.indexOf letter
result = ""
while i > 1
result = ".-"[i%2] + result
i //= 2
result
makecode : (data) ->
for letter in data
code = @getCode letter
if code != undefined then @maketime code else @time += @unit * 7
maketime : (data) ->
for timedata in data
timedata = @unit * ' . _'.indexOf timedata
if timedata > 0
@maketone timedata
@time += timedata
@time += @unit * 1
@time += @unit * 2
maketone : (data) ->
start = @time
stop = @time + data
@gain.gain.linearRampToValueAtTime 0, start
@gain.gain.linearRampToValueAtTime 1, start + @unit / 8
@gain.gain.linearRampToValueAtTime 1, stop - @unit / 16
@gain.gain.linearRampToValueAtTime 0, stop
send : (text) ->
osci = @cont.createOscillator()
osci.frequency.value = @freq
@gain = @cont.createGain()
@gain.gain.value = 0
osci.connect @gain
@gain.connect @cont.destination
osci.start @time
@makecode text
@cont
morse = new Morse()
morse.send 'hello world 0123456789'

View file

@ -0,0 +1,2 @@
USE: morse
"Hello world!" play-as-morse

View file

@ -0,0 +1,31 @@
using PortAudio
const pstream = PortAudioStream(0, 2)
sendmorsesound(t, f) = write(pstream, SinSource(eltype(stream), samplerate(stream)*0.8, [f]), (t/1000)s)
char2morse = Dict[
"!" => "---.", "\"" => ".-..-.", "$" => "...-..-", "'" => ".----.",
"(" => "-.--.", ")" => "-.--.-", "+" => ".-.-.", "," => "--..--",
"-" => "-....-", "." => ".-.-.-", "/" => "-..-.", "0" => "-----",
"1" => ".----", "2" => "..---", "3" => "...--", "4" => "....-", "5" => ".....",
"6" => "-....", "7" => "--...", "8" => "---..", "9" => "----.", ":" => "---...",
";" => "-.-.-.", "=" => "-...-", "?" => "..--..", "@" => ".--.-.", "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" => "--..",
"[" => "-.--.", "]" => "-.--.-", "_" => "..--.-"]
function sendmorsesound(freq, duration)
cpause() = sleep(0.080)
wpause = sleep(0.400)
dit() = sendmorsesound(0.070, 700)
dash() = sensmorsesound(0.210, 700)
sendmorsechar(c) = for d in char2morse(c) d == '.' ? dit(): dash() end end
sendmorseword(w) = for c in w sendmorsechar(c) cpause() end wpause() end
sendmorse(msg) = for word in uppercase(msg) sendmorseword(word) end
sendmorse("sos sos sos")
sendmorse("The case of letters in Morse coding is ignored."

View file

@ -0,0 +1,29 @@
//!
//! morse_code/src/main.rs
//!
//! Michael G. Cummings
//! 2019-08-26
//!
//! Since Rust doesn't have build-in audio support text output is used.
//!
use std::process;
use structopt::StructOpt;
use morse_code::{Config, Opt, run};
/// Core of the command-line binary.
///
/// By default expects input from stdin and outputs resulting morse code to stdout, but can also
/// read and/or write to files.
/// Use `morse_code --help` for more information about options.
fn main() {
let opts = Opt::from_args();
let mut config = Config::new(opts).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {}", err);
process::exit(1);
});
if let Err(err) = run(&mut config) {
eprintln!("Application error: {}", err);
process::exit(2);
}
}

View file

@ -0,0 +1,116 @@
//!
//! morse_code/src/lib.rs
//!
//! Michael G. Cummings
//! 2019-08-26
//!
#[macro_use]
extern crate structopt;
use std::{fs, io};
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
/// Main library function that does the actual work.
///
/// Each character has one space between them and there are two spaces between words.
/// Unknown characters in the input are replaced with a '#' in the output.
///
pub fn run(config: &mut Config) -> Result<(), Box<dyn Error>> {
let mut contents = String::new();
config.read.read_to_string(&mut contents)?;
let morse_map = init_code_map();
let mut result = String::new();
for char in contents.trim().to_uppercase().chars() {
match morse_map.get(&char) {
Some(hash) => {
result = result + *hash;
}
None => { result = result + "#" }
}
result = result + " ";
}
config.write.write(result.as_ref())?;
Ok(())
}
/// Configuration structure for the input and output streams.
#[derive(Debug)]
pub struct Config {
read: Box<dyn io::Read>,
write: Box<dyn io::Write>,
}
impl Config {
pub fn new(opts: Opt) -> Result<Config, &'static str> {
let input: Box<dyn io::Read> = match opts.input {
Some(p) => Box::new(fs::File::open(p).unwrap()),
None => Box::new(io::stdin()),
};
let output: Box<dyn io::Write> = match opts.output {
Some(p) => Box::new(fs::File::create(p).unwrap()),
None => Box::new(io::stdout()),
};
Ok(Config { read: input, write: output })
}
}
/// Structure used to hold command line opts(parameters) of binary.
///
/// Using StructOpt crate to parse command-line parameters/options.
///
#[derive(Debug, StructOpt)]
#[structopt(rename_all = "kebab-case", raw(setting = "structopt::clap::AppSettings::ColoredHelp"))]
pub struct Opt {
/// Input file, stdin if not present
#[structopt(short, long, parse(from_os_str))]
input: Option<PathBuf>,
/// Output file, stdout if not present
#[structopt(short, long, parse(from_os_str))]
output: Option<PathBuf>,
}
/// Initialize hash map of characters to morse code as string.
pub fn init_code_map() -> HashMap<char, &'static str> {
let mut morse_map: HashMap<char, &str> = HashMap::with_capacity(37);
morse_map.insert(' ', " ");
morse_map.insert('A', "._");
morse_map.insert('B', "_...");
morse_map.insert('C', "_._.");
morse_map.insert('D', "_..");
morse_map.insert('E', ".");
morse_map.insert('F', ".._.");
morse_map.insert('G', "__.");
morse_map.insert('H', "....");
morse_map.insert('I', "..");
morse_map.insert('J', ".___");
morse_map.insert('K', "_._");
morse_map.insert('L', "._..");
morse_map.insert('M', "__");
morse_map.insert('N', "_.");
morse_map.insert('O', "___");
morse_map.insert('P', ".__.");
morse_map.insert('Q', "__._");
morse_map.insert('R', "._.");
morse_map.insert('S', "...");
morse_map.insert('T', "_");
morse_map.insert('U', ".._");
morse_map.insert('V', "..._");
morse_map.insert('W', ".__");
morse_map.insert('X', "_.._");
morse_map.insert('Y', "_.__");
morse_map.insert('Z', "__..");
morse_map.insert('1', ".____");
morse_map.insert('2', "..___");
morse_map.insert('3', "...__");
morse_map.insert('4', "...._");
morse_map.insert('5', ".....");
morse_map.insert('6', "_....");
morse_map.insert('7', "__...");
morse_map.insert('8', "___..");
morse_map.insert('9', "____.");
morse_map.insert('0', "_____");
morse_map
}

View file

@ -0,0 +1,28 @@
object MorseCode extends App {
private val code = Map(
('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', "----. "),
('\'', ".----."), (':', "---... "), (',', "--..-- "), ('-', "-....- "),
('(', "-.--.- "), ('.', ".-.-.- "), ('?', "..--.. "), (';', "-.-.-. "),
('/', "-..-. "), ('-', "..--.- "), (')', "---.. "), ('=', "-...- "),
('@', ".--.-. "), ('"', ".-..-. "), ('+', ".-.-. "), (' ', "/")) // cheat a little
private def printMorse(input: String): Unit = {
println(input)
println(input.trim.replaceAll("[ ]+", " ").toUpperCase
.map(code.getOrElse(_, "").trim).mkString(" "))
}
printMorse("sos")
printMorse(" Hello World!")
printMorse("Rosetta Code")
}