Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -1,192 +1,192 @@
|
|||
cpu 8086
|
||||
bits 16
|
||||
;;; I/O ports
|
||||
KBB: equ 61h ; Keyboard controller port B (also controls speaker)
|
||||
PITC2: equ 42h ; Programmable Interrupt Timer, channel 2 (frequency)
|
||||
PITCTL: equ 43h ; PIT control port.
|
||||
;;; Control bits
|
||||
SPKR: equ 3 ; Lower two bits of KBB determine speaker on/off
|
||||
CTR: equ 6 ; Counter select offset in PIT control byte
|
||||
CBITS: equ 4 ; Size select offset in PIT control byte
|
||||
B16: equ 3 ; 16-bit mode for the PIT counter
|
||||
MODE: equ 1 ; Offset of mode in PIT control byte
|
||||
SQWV: equ 3 ; Square wave mode
|
||||
;;; Software interrupts
|
||||
CLOCK: equ 1Ah ; BIOS clock function interrupt
|
||||
DOS: equ 21h ; MS-DOS syscall interrupt
|
||||
;;; MS-DOS syscalls
|
||||
read: equ 3Fh ; Read from file
|
||||
section .text
|
||||
org 100h
|
||||
;;; Set up the PIT to generate a 'C' note
|
||||
cli
|
||||
mov al,(2<<CTR)|(B16<<CBITS)|(SQWV<<MODE)
|
||||
out PITCTL,al
|
||||
mov ax,2280 ; Divisor of main oscillator frequency
|
||||
out PITC2,al ; Output low byte first,
|
||||
xchg al,ah
|
||||
out PITC2,al ; Then high byte.
|
||||
sti
|
||||
;;; Read from stdin and sound as morse
|
||||
input: mov ah,read ; Read
|
||||
xor bx,bx ; from STDIN
|
||||
mov cx,buf.size ; into the buffer
|
||||
mov dx,buf
|
||||
int DOS
|
||||
jc stop ; Carry set = error, stop
|
||||
test ax,ax ; Did we get any characters?
|
||||
jnz go ; If not, we're done, so stop
|
||||
stop: ret
|
||||
go: mov cx,ax ; Loop counter = how many characters we have
|
||||
mov si,buf ; Start at buffer size
|
||||
dochar: lodsb ; Get current character
|
||||
cmp al,26 ; End of file?
|
||||
je stop ; Then stop
|
||||
push cx ; Save pointer and counter
|
||||
push si
|
||||
call char ; Sound out this character
|
||||
pop si ; Restore pointer and counter
|
||||
pop cx
|
||||
loop dochar ; If more characters, do the next one
|
||||
jmp input ; Afterwards, try to get more input
|
||||
;;; Sound ASCII character in AL
|
||||
char: and al,127 ; 7-bit ASCII
|
||||
sub al,32 ; Word separator? (<=32)
|
||||
ja .snd ; If not, look up in table
|
||||
mov bx,4 ; Otherwise, 'sound' a word space
|
||||
jmp delay ; (4 more ticks to form 7-tick delay)
|
||||
.snd: mov bx,morse ; Find offset in morse pulse table
|
||||
xlatb
|
||||
test al,al ; If it is zero, we want to ignore it
|
||||
jz .out
|
||||
xor ah,ah ; Otherwise, find its address
|
||||
mov bx,ax
|
||||
lea si,[bx+morse.P] ; and store it in SI.
|
||||
xor bh,bh ; BX = BL in the following code
|
||||
.byte: lodsb ; Get pulse byte
|
||||
mov cx,4 ; Four pulses per byte
|
||||
.pulse: mov bl,3 ; Load low pulse in BL
|
||||
and bl,al
|
||||
test bl,bl ; If it is zero,
|
||||
jz .out ; We're done
|
||||
mov bp,ax ; Otherwise, keep AX
|
||||
call pulse ; Sound the pulse
|
||||
mov ax,bp ; Restore AX
|
||||
shr al,1 ; Next pulse
|
||||
shr al,1
|
||||
loop .pulse
|
||||
jmp .byte ; If no zero pulse seen yet, next byte
|
||||
.out: mov bl,2 ; 2 ticks more delay to form inter-char space
|
||||
jmp delay
|
||||
;;; Sound morse code pulse w/delay
|
||||
pulse: cli ; Turn off interrupts
|
||||
in al,KBB ; Read current configuration
|
||||
or al,SPKR ; Turn speaker on
|
||||
out KBB,al ; Write configuration back
|
||||
sti ; Turn interrupts back on
|
||||
call delay ; Delay for BX ticks
|
||||
cli ; Turn off interrupts
|
||||
in al,KBB ; Read current configuration
|
||||
and al,~SPKR ; Turn speaker off
|
||||
out KBB,al ; Write configuration back
|
||||
sti ; Turn interrupts back on
|
||||
mov bx,1 ; Intra-character delay = 1 tick
|
||||
;;; Delay for BX ticks
|
||||
delay: push bx ; Keep the registers we alter
|
||||
push cx
|
||||
push dx
|
||||
xor ah,ah ; Clock function 0 = get ticks
|
||||
int CLOCK ; Get current ticks (in CX:DX)
|
||||
add bx,dx ; BX = time for which to wait
|
||||
.wait: int CLOCK ; Wait for that time to occur
|
||||
cmp dx,bx ; Are we there yet?
|
||||
jbe .wait ; If not, try again
|
||||
pop dx ; Restore the registers
|
||||
pop cx
|
||||
pop bx
|
||||
ret
|
||||
section .data
|
||||
morse: ;;; Printable ASCII to pulse mapping (32-122)
|
||||
db .n_-.P, .excl-.P, .dquot-.P, .n_-.P ; !"#
|
||||
db .dolar-.P, .n_-.P, .amp-.P, .quot-.P;$%&'
|
||||
db .open-.P, .close-.P, .n_-.P, .plus-.P;()*+
|
||||
db .comma-.P, .minus-.P, .dot-.P, .slsh-.P;,-./
|
||||
db .n0-.P, .n1-.P, .n2-.P, .n3-.P ;0123
|
||||
db .n4-.P, .n5-.P, .n6-.P, .n7-.P ;4567
|
||||
db .n8-.P, .n9-.P, .colon-.P, .semi-.P;89:;
|
||||
db .n_-.P, .eq-.P, .n_-.P, .qm-.P ;<=>?
|
||||
db .at-.P, .a-.P, .b-.P, .c-.P ;@ABC
|
||||
db .d-.P, .e-.P, .f-.P, .g-.P ;DEFG
|
||||
db .h-.P, .i-.P, .j-.P, .k-.P ;HIJK
|
||||
db .l-.P, .m-.P, .n-.P, .o-.P ;LMNO
|
||||
db .p-.P, .q-.P, .r-.P, .s-.P ;PQRS
|
||||
db .t-.P, .u-.P, .v-.P, .w-.P ;TUVW
|
||||
db .x-.P, .y-.P, .z-.P, .n_-.P ;XYZ[
|
||||
db .n_-.P, .n_-.P, .n_-.P, .uscr-.P;\]^_
|
||||
db .n_-.P, .a-.P, .b-.P, .c-.P ;`abc
|
||||
db .d-.P, .e-.P, .f-.P, .g-.P ;defg
|
||||
db .h-.P, .i-.P, .j-.P, .k-.P ;hijk
|
||||
db .l-.P, .m-.P, .n-.P, .o-.P ;lmno
|
||||
db .p-.P, .q-.P, .r-.P, .s-.P ;pqrs
|
||||
db .t-.P, .u-.P, .v-.P, .w-.P ;tuvw
|
||||
db .x-.P, .y-.P, .z-.P, .n_-.P ;xyz{
|
||||
db .n_-.P, .n_-.P, .n_-.P, .n_-.P ;|}~
|
||||
.P: ;;; Morse pulses are stored four to a byte, lowest bits first
|
||||
.n_: db 0 ; To ignore undefined characters
|
||||
.a: db 0Dh
|
||||
.b: db 57h,0
|
||||
.c: db 77h,0
|
||||
.d: db 17h
|
||||
.e: db 1h
|
||||
.f: db 75h,0
|
||||
.g: db 1Fh
|
||||
.h: db 55h,0
|
||||
.i: db 5h
|
||||
.j: db 0FDh,0
|
||||
.k: db 37h
|
||||
.l: db 5Dh,0
|
||||
.m: db 0Fh
|
||||
.n: db 7h
|
||||
.o: db 3Fh
|
||||
.p: db 7Dh,0
|
||||
.q: db 0DFh,0
|
||||
.r: db 1Dh
|
||||
.s: db 15h
|
||||
.t: db 3h
|
||||
.u: db 35h
|
||||
.v: db 0D5h,0
|
||||
.w: db 3Dh
|
||||
.x: db 0D7h,0
|
||||
.y: db 0F7h,0
|
||||
.z: db 5Fh,0
|
||||
.n0: db 0FFh,3
|
||||
.n1: db 0FDh,3
|
||||
.n2: db 0F5h,3
|
||||
.n3: db 0D5h,3
|
||||
.n4: db 55h,3
|
||||
.n5: db 55h,1
|
||||
.n6: db 57h,1
|
||||
.n7: db 5Fh,1
|
||||
.n8: db 7Fh,1
|
||||
.n9: db 0FFh,1
|
||||
.dot: db 0DDh,0Dh
|
||||
.comma: db 5Fh,0Fh
|
||||
.qm: db 0F5h,5
|
||||
.quot: db 0FDh,7
|
||||
.excl: db 77h,0Fh
|
||||
.slsh: db 0D7h,1
|
||||
.open: db 0F7h,1
|
||||
.close: db 0F7h,0Dh
|
||||
.amp: db 5Dh,1
|
||||
.colon: db 7Fh,5
|
||||
.semi: db 77h,7
|
||||
.eq: db 57h,3
|
||||
.plus: db 0DDh,1
|
||||
.minus: db 57h,0Dh
|
||||
.uscr: db 0F5h,0Dh
|
||||
.dquot: db 5Dh,7
|
||||
.dolar: db 0D5h,35h
|
||||
.at: db 7Dh,7
|
||||
section .bss
|
||||
buf: resb 1024 ; 1K buffer
|
||||
.size: equ $-buf
|
||||
cpu 8086
|
||||
bits 16
|
||||
;;; I/O ports
|
||||
KBB: equ 61h ; Keyboard controller port B (also controls speaker)
|
||||
PITC2: equ 42h ; Programmable Interrupt Timer, channel 2 (frequency)
|
||||
PITCTL: equ 43h ; PIT control port.
|
||||
;;; Control bits
|
||||
SPKR: equ 3 ; Lower two bits of KBB determine speaker on/off
|
||||
CTR: equ 6 ; Counter select offset in PIT control byte
|
||||
CBITS: equ 4 ; Size select offset in PIT control byte
|
||||
B16: equ 3 ; 16-bit mode for the PIT counter
|
||||
MODE: equ 1 ; Offset of mode in PIT control byte
|
||||
SQWV: equ 3 ; Square wave mode
|
||||
;;; Software interrupts
|
||||
CLOCK: equ 1Ah ; BIOS clock function interrupt
|
||||
DOS: equ 21h ; MS-DOS syscall interrupt
|
||||
;;; MS-DOS syscalls
|
||||
read: equ 3Fh ; Read from file
|
||||
section .text
|
||||
org 100h
|
||||
;;; Set up the PIT to generate a 'C' note
|
||||
cli
|
||||
mov al,(2<<CTR)|(B16<<CBITS)|(SQWV<<MODE)
|
||||
out PITCTL,al
|
||||
mov ax,2280 ; Divisor of main oscillator frequency
|
||||
out PITC2,al ; Output low byte first,
|
||||
xchg al,ah
|
||||
out PITC2,al ; Then high byte.
|
||||
sti
|
||||
;;; Read from stdin and sound as morse
|
||||
input: mov ah,read ; Read
|
||||
xor bx,bx ; from STDIN
|
||||
mov cx,buf.size ; into the buffer
|
||||
mov dx,buf
|
||||
int DOS
|
||||
jc stop ; Carry set = error, stop
|
||||
test ax,ax ; Did we get any characters?
|
||||
jnz go ; If not, we're done, so stop
|
||||
stop: ret
|
||||
go: mov cx,ax ; Loop counter = how many characters we have
|
||||
mov si,buf ; Start at buffer size
|
||||
dochar: lodsb ; Get current character
|
||||
cmp al,26 ; End of file?
|
||||
je stop ; Then stop
|
||||
push cx ; Save pointer and counter
|
||||
push si
|
||||
call char ; Sound out this character
|
||||
pop si ; Restore pointer and counter
|
||||
pop cx
|
||||
loop dochar ; If more characters, do the next one
|
||||
jmp input ; Afterwards, try to get more input
|
||||
;;; Sound ASCII character in AL
|
||||
char: and al,127 ; 7-bit ASCII
|
||||
sub al,32 ; Word separator? (<=32)
|
||||
ja .snd ; If not, look up in table
|
||||
mov bx,4 ; Otherwise, 'sound' a word space
|
||||
jmp delay ; (4 more ticks to form 7-tick delay)
|
||||
.snd: mov bx,morse ; Find offset in morse pulse table
|
||||
xlatb
|
||||
test al,al ; If it is zero, we want to ignore it
|
||||
jz .out
|
||||
xor ah,ah ; Otherwise, find its address
|
||||
mov bx,ax
|
||||
lea si,[bx+morse.P] ; and store it in SI.
|
||||
xor bh,bh ; BX = BL in the following code
|
||||
.byte: lodsb ; Get pulse byte
|
||||
mov cx,4 ; Four pulses per byte
|
||||
.pulse: mov bl,3 ; Load low pulse in BL
|
||||
and bl,al
|
||||
test bl,bl ; If it is zero,
|
||||
jz .out ; We're done
|
||||
mov bp,ax ; Otherwise, keep AX
|
||||
call pulse ; Sound the pulse
|
||||
mov ax,bp ; Restore AX
|
||||
shr al,1 ; Next pulse
|
||||
shr al,1
|
||||
loop .pulse
|
||||
jmp .byte ; If no zero pulse seen yet, next byte
|
||||
.out: mov bl,2 ; 2 ticks more delay to form inter-char space
|
||||
jmp delay
|
||||
;;; Sound morse code pulse w/delay
|
||||
pulse: cli ; Turn off interrupts
|
||||
in al,KBB ; Read current configuration
|
||||
or al,SPKR ; Turn speaker on
|
||||
out KBB,al ; Write configuration back
|
||||
sti ; Turn interrupts back on
|
||||
call delay ; Delay for BX ticks
|
||||
cli ; Turn off interrupts
|
||||
in al,KBB ; Read current configuration
|
||||
and al,~SPKR ; Turn speaker off
|
||||
out KBB,al ; Write configuration back
|
||||
sti ; Turn interrupts back on
|
||||
mov bx,1 ; Intra-character delay = 1 tick
|
||||
;;; Delay for BX ticks
|
||||
delay: push bx ; Keep the registers we alter
|
||||
push cx
|
||||
push dx
|
||||
xor ah,ah ; Clock function 0 = get ticks
|
||||
int CLOCK ; Get current ticks (in CX:DX)
|
||||
add bx,dx ; BX = time for which to wait
|
||||
.wait: int CLOCK ; Wait for that time to occur
|
||||
cmp dx,bx ; Are we there yet?
|
||||
jbe .wait ; If not, try again
|
||||
pop dx ; Restore the registers
|
||||
pop cx
|
||||
pop bx
|
||||
ret
|
||||
section .data
|
||||
morse: ;;; Printable ASCII to pulse mapping (32-122)
|
||||
db .n_-.P, .excl-.P, .dquot-.P, .n_-.P ; !"#
|
||||
db .dolar-.P, .n_-.P, .amp-.P, .quot-.P;$%&'
|
||||
db .open-.P, .close-.P, .n_-.P, .plus-.P;()*+
|
||||
db .comma-.P, .minus-.P, .dot-.P, .slsh-.P;,-./
|
||||
db .n0-.P, .n1-.P, .n2-.P, .n3-.P ;0123
|
||||
db .n4-.P, .n5-.P, .n6-.P, .n7-.P ;4567
|
||||
db .n8-.P, .n9-.P, .colon-.P, .semi-.P;89:;
|
||||
db .n_-.P, .eq-.P, .n_-.P, .qm-.P ;<=>?
|
||||
db .at-.P, .a-.P, .b-.P, .c-.P ;@ABC
|
||||
db .d-.P, .e-.P, .f-.P, .g-.P ;DEFG
|
||||
db .h-.P, .i-.P, .j-.P, .k-.P ;HIJK
|
||||
db .l-.P, .m-.P, .n-.P, .o-.P ;LMNO
|
||||
db .p-.P, .q-.P, .r-.P, .s-.P ;PQRS
|
||||
db .t-.P, .u-.P, .v-.P, .w-.P ;TUVW
|
||||
db .x-.P, .y-.P, .z-.P, .n_-.P ;XYZ[
|
||||
db .n_-.P, .n_-.P, .n_-.P, .uscr-.P;\]^_
|
||||
db .n_-.P, .a-.P, .b-.P, .c-.P ;`abc
|
||||
db .d-.P, .e-.P, .f-.P, .g-.P ;defg
|
||||
db .h-.P, .i-.P, .j-.P, .k-.P ;hijk
|
||||
db .l-.P, .m-.P, .n-.P, .o-.P ;lmno
|
||||
db .p-.P, .q-.P, .r-.P, .s-.P ;pqrs
|
||||
db .t-.P, .u-.P, .v-.P, .w-.P ;tuvw
|
||||
db .x-.P, .y-.P, .z-.P, .n_-.P ;xyz{
|
||||
db .n_-.P, .n_-.P, .n_-.P, .n_-.P ;|}~
|
||||
.P: ;;; Morse pulses are stored four to a byte, lowest bits first
|
||||
.n_: db 0 ; To ignore undefined characters
|
||||
.a: db 0Dh
|
||||
.b: db 57h,0
|
||||
.c: db 77h,0
|
||||
.d: db 17h
|
||||
.e: db 1h
|
||||
.f: db 75h,0
|
||||
.g: db 1Fh
|
||||
.h: db 55h,0
|
||||
.i: db 5h
|
||||
.j: db 0FDh,0
|
||||
.k: db 37h
|
||||
.l: db 5Dh,0
|
||||
.m: db 0Fh
|
||||
.n: db 7h
|
||||
.o: db 3Fh
|
||||
.p: db 7Dh,0
|
||||
.q: db 0DFh,0
|
||||
.r: db 1Dh
|
||||
.s: db 15h
|
||||
.t: db 3h
|
||||
.u: db 35h
|
||||
.v: db 0D5h,0
|
||||
.w: db 3Dh
|
||||
.x: db 0D7h,0
|
||||
.y: db 0F7h,0
|
||||
.z: db 5Fh,0
|
||||
.n0: db 0FFh,3
|
||||
.n1: db 0FDh,3
|
||||
.n2: db 0F5h,3
|
||||
.n3: db 0D5h,3
|
||||
.n4: db 55h,3
|
||||
.n5: db 55h,1
|
||||
.n6: db 57h,1
|
||||
.n7: db 5Fh,1
|
||||
.n8: db 7Fh,1
|
||||
.n9: db 0FFh,1
|
||||
.dot: db 0DDh,0Dh
|
||||
.comma: db 5Fh,0Fh
|
||||
.qm: db 0F5h,5
|
||||
.quot: db 0FDh,7
|
||||
.excl: db 77h,0Fh
|
||||
.slsh: db 0D7h,1
|
||||
.open: db 0F7h,1
|
||||
.close: db 0F7h,0Dh
|
||||
.amp: db 5Dh,1
|
||||
.colon: db 7Fh,5
|
||||
.semi: db 77h,7
|
||||
.eq: db 57h,3
|
||||
.plus: db 0DDh,1
|
||||
.minus: db 57h,0Dh
|
||||
.uscr: db 0F5h,0Dh
|
||||
.dquot: db 5Dh,7
|
||||
.dolar: db 0D5h,35h
|
||||
.at: db 7Dh,7
|
||||
section .bss
|
||||
buf: resb 1024 ; 1K buffer
|
||||
.size: equ $-buf
|
||||
|
|
|
|||
40
Task/Morse-code/Ada/morse-code-1.adb
Normal file
40
Task/Morse-code/Ada/morse-code-1.adb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package Morse is
|
||||
|
||||
type Symbols is (Nul, '-', '.', ' ');
|
||||
-- Nul is the letter separator, space the word separator;
|
||||
Dash : constant Symbols := '-';
|
||||
Dot : constant Symbols := '.';
|
||||
type Morse_Str is array (Positive range <>) of Symbols;
|
||||
pragma Pack (Morse_Str);
|
||||
|
||||
function Convert (Input : String) return Morse_Str;
|
||||
procedure Morsebeep (Input : Morse_Str);
|
||||
|
||||
private
|
||||
subtype Reschars is Character range ' ' .. 'Z';
|
||||
-- restricted set of characters from 16#20# to 16#60#
|
||||
subtype Length is Natural range 1 .. 5;
|
||||
subtype Codes is Morse_Str (Length);
|
||||
-- using the current ITU standard with 5 signs
|
||||
-- only alphanumeric characters are taken into consideration
|
||||
|
||||
type Codings is record
|
||||
L : Length;
|
||||
Code : Codes;
|
||||
end record;
|
||||
Table : constant array (Reschars) of Codings :=
|
||||
('A' => (2, ".- "), 'B' => (4, "-... "), 'C' => (4, "-.-. "),
|
||||
'D' => (3, "-.. "), 'E' => (1, ". "), 'F' => (4, "..-. "),
|
||||
'G' => (3, "--. "), 'H' => (4, ".... "), 'I' => (2, ".. "),
|
||||
'J' => (4, ".--- "), 'K' => (3, "-.- "), 'L' => (4, ".-.. "),
|
||||
'M' => (2, "-- "), 'N' => (2, "-. "), 'O' => (3, "--- "),
|
||||
'P' => (4, ".--. "), 'Q' => (4, "--.- "), 'R' => (3, ".-. "),
|
||||
'S' => (3, "... "), 'T' => (1, "- "), 'U' => (3, "..- "),
|
||||
'V' => (4, "...- "), 'W' => (3, ".-- "), 'X' => (4, "-..- "),
|
||||
'Y' => (4, "-.-- "), 'Z' => (4, "--.. "), '1' => (5, ".----"),
|
||||
'2' => (5, "..---"), '3' => (5, "...--"), '4' => (5, "....-"),
|
||||
'5' => (5, "....."), '6' => (5, "-...."), '7' => (5, "--..."),
|
||||
'8' => (5, "---.."), '9' => (5, "----."), '0' => (5, "-----"),
|
||||
others => (1, " ")); -- Dummy => Other characters do not need code.
|
||||
|
||||
end Morse;
|
||||
72
Task/Morse-code/Ada/morse-code-2.adb
Normal file
72
Task/Morse-code/Ada/morse-code-2.adb
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
with Ada.Strings.Maps, Ada.Characters.Handling, Interfaces.C;
|
||||
use Ada, Ada.Strings, Ada.Strings.Maps, Interfaces;
|
||||
|
||||
package body Morse is
|
||||
|
||||
Dit, Dah, Lettergap, Wordgap : Duration; -- in seconds
|
||||
Dit_ms, Dah_ms : C.unsigned; -- durations expressed in ms
|
||||
Freq : constant C.unsigned := 1280; -- in Herz
|
||||
|
||||
Morse_Sequence : constant Character_Sequence :=
|
||||
" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
Morse_charset : constant Character_Set := To_Set (Morse_Sequence);
|
||||
|
||||
function Convert (Input : String) return Morse_Str is
|
||||
Cap_String : constant String := Characters.Handling.To_Upper (Input);
|
||||
Result : Morse_Str (1 .. 7 * Input'Length); -- Upper Capacity
|
||||
First, Last : Natural := 0;
|
||||
Char_code : Codings;
|
||||
begin
|
||||
for I in Cap_String'Range loop
|
||||
if Is_In (Cap_String (I), Morse_charset) then
|
||||
First := Last + 1;
|
||||
if Cap_String (I) = ' ' then
|
||||
Result (First) := ' ';
|
||||
Last := Last + 1;
|
||||
else
|
||||
Char_code := Table (Reschars (Cap_String (I)));
|
||||
Last := First + Char_code.L - 1;
|
||||
Result (First .. Last) := Char_code.Code (1 .. Char_code.L);
|
||||
Last := Last + 1;
|
||||
Result (Last) := Nul;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
if Result (Last) /= ' ' then
|
||||
Last := Last + 1;
|
||||
Result (Last) := ' ';
|
||||
end if;
|
||||
return Result (1 .. Last);
|
||||
end Convert;
|
||||
|
||||
procedure Morsebeep (Input : Morse_Str) is
|
||||
-- Beep is not portable : adapt to your OS/sound board
|
||||
-- Implementation for Windows XP / interface to fn in stdlib
|
||||
procedure win32xp_beep
|
||||
(dwFrequency : C.unsigned;
|
||||
dwDuration : C.unsigned);
|
||||
pragma Import (C, win32xp_beep, "_beep");
|
||||
begin
|
||||
for I in Input'Range loop
|
||||
case Input (I) is
|
||||
when Nul =>
|
||||
delay Lettergap;
|
||||
when Dot =>
|
||||
win32xp_beep (Freq, Dit_ms);
|
||||
delay Dit;
|
||||
when Dash =>
|
||||
win32xp_beep (Freq, Dah_ms);
|
||||
delay Dit;
|
||||
when ' ' =>
|
||||
delay Wordgap;
|
||||
end case;
|
||||
end loop;
|
||||
end Morsebeep;
|
||||
begin
|
||||
Dit := 0.20;
|
||||
Lettergap := 2 * Dit;
|
||||
Dah := 3 * Dit;
|
||||
Wordgap := 4 * Dit;
|
||||
Dit_ms := C.unsigned (Integer (Dit * 1000));
|
||||
Dah_ms := C.unsigned (Integer (Dah * 1000));
|
||||
end Morse;
|
||||
5
Task/Morse-code/Ada/morse-code-3.adb
Normal file
5
Task/Morse-code/Ada/morse-code-3.adb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
with Morse; use Morse;
|
||||
procedure Morse_Tx is
|
||||
begin
|
||||
Morsebeep (Convert ("Science sans Conscience"));
|
||||
end Morse_Tx;
|
||||
|
|
@ -1,22 +1,22 @@
|
|||
TestString := "Hello World! abcdefg @\;" ;Create a string to be sent with multiple caps and some punctuation
|
||||
MorseBeep(teststring) ;Beeps our string after conversion
|
||||
return ;End Auto-Execute Section
|
||||
TestString := "Hello World! abcdefg @\;" ;Create a string to be sent with multiple caps and some punctuation
|
||||
MorseBeep(teststring) ;Beeps our string after conversion
|
||||
return ;End Auto-Execute Section
|
||||
|
||||
|
||||
MorseBeep(passedString)
|
||||
{
|
||||
StringLower, passedString, passedString ;Convert to lowercase for simpler checking
|
||||
Loop, Parse, passedString ;This loop stores each character in A_loopField one by one using the more compact form of "if else", by var := x>y ? val1 : val2 which stores val1 in var if x > y, otherwise it stores val2, this can be used together to make a single line of if else
|
||||
morse .= A_LoopField = " " ? " " : A_LoopField = "a" ? ".- " : A_LoopField = "b" ? "-... " : A_LoopField = "c" ? "-.-. " : A_LoopField = "d" ? "-.. " : A_LoopField = "e" ? ". " : A_LoopField = "f" ? "..-. " : A_LoopField = "g" ? "--. " : A_LoopField = "h" ? ".... " : A_LoopField = "i" ? ".. " : A_LoopField = "j" ? ".--- " : A_LoopField = "k" ? "-.- " : A_LoopField = "l" ? ".-.. " : A_LoopField = "m" ? "-- " : A_LoopField = "n" ? "-. " : A_LoopField = "o" ? "--- " : A_LoopField = "p" ? ".--. " : A_LoopField = "q" ? "--.- " : A_LoopField = "r" ? ".-. " : A_LoopField = "s" ? "... " : A_LoopField = "t" ? "- " : A_LoopField = "u" ? "..- " : A_LoopField = "v" ? "...- " : A_LoopField = "w" ? ".-- " : A_LoopField = "x" ? "-..- " : A_LoopField = "y" ? "-.-- " : A_LoopField = "z" ? "--.. " : A_LoopField = "!" ? "---. " : A_LoopField = "\" ? ".-..-. " : A_LoopField = "$" ? "...-..- " : A_LoopField = "'" ? ".----. " : A_LoopField = "(" ? "-.--. " : A_LoopField = ")" ? "-.--.- " : A_LoopField = "+" ? ".-.-. " : A_LoopField = "," ? "--..-- " : A_LoopField = "-" ? "-....- " : A_LoopField = "." ? ".-.-.- " : A_LoopField = "/" ? "-..-. " : A_LoopField = "0" ? "----- " : A_LoopField = "1" ? ".---- " : A_LoopField = "2" ? "..--- " : A_LoopField = "3" ? "...-- " : A_LoopField = "4" ? "....- " : A_LoopField = "5" ? "..... " : A_LoopField = "6" ? "-.... " : A_LoopField = "7" ? "--... " : A_LoopField = "8" ? "---.. " : A_LoopField = "9" ? "----. " : A_LoopField = ":" ? "---... " : A_LoopField = ";" ? "-.-.-. " : A_LoopField = "=" ? "-...- " : A_LoopField = "?" ? "..--.. " : A_LoopField = "@" ? ".--.-. " : A_LoopField = "[" ? "-.--. " : A_LoopField = "]" ? "-.--.- " : A_LoopField = "_" ? "..--.- " : "ERROR" ; ---End conversion loop---
|
||||
Loop, Parse, morse
|
||||
{
|
||||
morsebeep := 120
|
||||
if (A_LoopField = ".")
|
||||
SoundBeep, 10*morsebeep, morsebeep ;Format: SoundBeep, frequency, duration
|
||||
If (A_LoopField = "-")
|
||||
SoundBeep, 10*morsebeep, 3*morsebeep ;Duration can be an expression
|
||||
If (A_LoopField = " ")
|
||||
Sleep, morsebeep ;Above, each character is followed by a space, and literal
|
||||
} ;Spaces are extended. Sleep pauses the script
|
||||
return morse ;Returns the text in morse code
|
||||
StringLower, passedString, passedString ;Convert to lowercase for simpler checking
|
||||
Loop, Parse, passedString ;This loop stores each character in A_loopField one by one using the more compact form of "if else", by var := x>y ? val1 : val2 which stores val1 in var if x > y, otherwise it stores val2, this can be used together to make a single line of if else
|
||||
morse .= A_LoopField = " " ? " " : A_LoopField = "a" ? ".- " : A_LoopField = "b" ? "-... " : A_LoopField = "c" ? "-.-. " : A_LoopField = "d" ? "-.. " : A_LoopField = "e" ? ". " : A_LoopField = "f" ? "..-. " : A_LoopField = "g" ? "--. " : A_LoopField = "h" ? ".... " : A_LoopField = "i" ? ".. " : A_LoopField = "j" ? ".--- " : A_LoopField = "k" ? "-.- " : A_LoopField = "l" ? ".-.. " : A_LoopField = "m" ? "-- " : A_LoopField = "n" ? "-. " : A_LoopField = "o" ? "--- " : A_LoopField = "p" ? ".--. " : A_LoopField = "q" ? "--.- " : A_LoopField = "r" ? ".-. " : A_LoopField = "s" ? "... " : A_LoopField = "t" ? "- " : A_LoopField = "u" ? "..- " : A_LoopField = "v" ? "...- " : A_LoopField = "w" ? ".-- " : A_LoopField = "x" ? "-..- " : A_LoopField = "y" ? "-.-- " : A_LoopField = "z" ? "--.. " : A_LoopField = "!" ? "---. " : A_LoopField = "\" ? ".-..-. " : A_LoopField = "$" ? "...-..- " : A_LoopField = "'" ? ".----. " : A_LoopField = "(" ? "-.--. " : A_LoopField = ")" ? "-.--.- " : A_LoopField = "+" ? ".-.-. " : A_LoopField = "," ? "--..-- " : A_LoopField = "-" ? "-....- " : A_LoopField = "." ? ".-.-.- " : A_LoopField = "/" ? "-..-. " : A_LoopField = "0" ? "----- " : A_LoopField = "1" ? ".---- " : A_LoopField = "2" ? "..--- " : A_LoopField = "3" ? "...-- " : A_LoopField = "4" ? "....- " : A_LoopField = "5" ? "..... " : A_LoopField = "6" ? "-.... " : A_LoopField = "7" ? "--... " : A_LoopField = "8" ? "---.. " : A_LoopField = "9" ? "----. " : A_LoopField = ":" ? "---... " : A_LoopField = ";" ? "-.-.-. " : A_LoopField = "=" ? "-...- " : A_LoopField = "?" ? "..--.. " : A_LoopField = "@" ? ".--.-. " : A_LoopField = "[" ? "-.--. " : A_LoopField = "]" ? "-.--.- " : A_LoopField = "_" ? "..--.- " : "ERROR" ; ---End conversion loop---
|
||||
Loop, Parse, morse
|
||||
{
|
||||
morsebeep := 120
|
||||
if (A_LoopField = ".")
|
||||
SoundBeep, 10*morsebeep, morsebeep ;Format: SoundBeep, frequency, duration
|
||||
If (A_LoopField = "-")
|
||||
SoundBeep, 10*morsebeep, 3*morsebeep ;Duration can be an expression
|
||||
If (A_LoopField = " ")
|
||||
Sleep, morsebeep ;Above, each character is followed by a space, and literal
|
||||
} ;Spaces are extended. Sleep pauses the script
|
||||
return morse ;Returns the text in morse code
|
||||
} ; ---End Function Morse---
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@
|
|||
(defn note [hz sample-rate ms]
|
||||
(let [period (/ hz sample-rate)]
|
||||
(->> (range (* sample-rate ms 1/1000))
|
||||
(map #(->> (* 2 Math/PI % period)
|
||||
Math/sin
|
||||
(* 127 ,)
|
||||
byte) ,))))
|
||||
(map #(->> (* 2 Math/PI % period)
|
||||
Math/sin
|
||||
(* 127 ,)
|
||||
byte) ,))))
|
||||
|
||||
(def morse-codes
|
||||
{\A ".-" \J ".---" \S "..." \1 ".----" \. ".-.-.-" \: "---..."
|
||||
|
|
@ -35,11 +35,11 @@
|
|||
ms 50]
|
||||
(def sounds
|
||||
{\. (note hz sample-rate (* 1 ms))
|
||||
\- (note hz sample-rate(* 3 ms))
|
||||
:element-gap (note 0 sample-rate (* 1 ms))
|
||||
:letter-gap (note 0 sample-rate (* 3 ms))
|
||||
\space (note 0 sample-rate (* 1 ms))})) ;includes letter-gap on either side
|
||||
|
||||
\- (note hz sample-rate(* 3 ms))
|
||||
:element-gap (note 0 sample-rate (* 1 ms))
|
||||
:letter-gap (note 0 sample-rate (* 3 ms))
|
||||
\space (note 0 sample-rate (* 1 ms))})) ;includes letter-gap on either side
|
||||
|
||||
(defn convert-letter [letter]
|
||||
(->> (get morse-codes letter "")
|
||||
(map sounds ,)
|
||||
|
|
|
|||
|
|
@ -12,14 +12,14 @@
|
|||
;; use "|" as letters separator
|
||||
(define (string->morse str morse)
|
||||
(apply append (map string->list
|
||||
(for/list [(a (string-diacritics str))]
|
||||
(string-append
|
||||
(or (hash-ref morse (string-upcase a)) "?") "|")))))
|
||||
|
||||
(for/list [(a (string-diacritics str))]
|
||||
(string-append
|
||||
(or (hash-ref morse (string-upcase a)) "?") "|")))))
|
||||
|
||||
(define (play-morse)
|
||||
(when EMIT ;; else return #f which stops (at-every)
|
||||
(case (first EMIT)
|
||||
((".") (play-sound 'digit) (write "dot"))
|
||||
(("-") (play-sound 'tick) (write "dash"))
|
||||
(else (writeln) (blink)))
|
||||
(set! EMIT (rest EMIT))))
|
||||
(when EMIT ;; else return #f which stops (at-every)
|
||||
(case (first EMIT)
|
||||
((".") (play-sound 'digit) (write "dot"))
|
||||
(("-") (play-sound 'tick) (write "dash"))
|
||||
(else (writeln) (blink)))
|
||||
(set! EMIT (rest EMIT))))
|
||||
|
|
|
|||
|
|
@ -4,30 +4,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unicode"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// A key represents an action on the morse key.
|
||||
// It's either on or off, for the given duration.
|
||||
type key struct {
|
||||
duration int
|
||||
on bool
|
||||
sym string // for debug output
|
||||
duration int
|
||||
on bool
|
||||
sym string // for debug output
|
||||
}
|
||||
|
||||
var (
|
||||
runeToKeys = map[rune][]key{}
|
||||
interCharGap = []key{{1, false, ""}}
|
||||
punctGap = []key{{7, false, " / "}}
|
||||
charGap = []key{{3, false, " "}}
|
||||
wordGap = []key{{7, false, " / "}}
|
||||
runeToKeys = map[rune][]key{}
|
||||
interCharGap = []key{{1, false, ""}}
|
||||
punctGap = []key{{7, false, " / "}}
|
||||
charGap = []key{{3, false, " "}}
|
||||
wordGap = []key{{7, false, " / "}}
|
||||
)
|
||||
|
||||
const rawMorse = `
|
||||
|
|
@ -43,83 +43,83 @@ I:.. R:.-. 0:----- 9:----. &:.-... @:.--.-.
|
|||
`
|
||||
|
||||
func init() {
|
||||
// Convert the rawMorse table into a map of morse key actions.
|
||||
r := regexp.MustCompile("([^ ]):([.-]+)")
|
||||
for _, m := range r.FindAllStringSubmatch(rawMorse, -1) {
|
||||
c := m[1][0]
|
||||
keys := []key{}
|
||||
for i, dd := range m[2] {
|
||||
if i > 0 {
|
||||
keys = append(keys, interCharGap...)
|
||||
}
|
||||
if dd == '.' {
|
||||
keys = append(keys, key{1, true, "."})
|
||||
} else if dd == '-' {
|
||||
keys = append(keys, key{3, true, "-"})
|
||||
} else {
|
||||
log.Fatalf("found %c in morse for %c", dd, c)
|
||||
}
|
||||
runeToKeys[rune(c)] = keys
|
||||
runeToKeys[unicode.ToLower(rune(c))] = keys
|
||||
}
|
||||
}
|
||||
// Convert the rawMorse table into a map of morse key actions.
|
||||
r := regexp.MustCompile("([^ ]):([.-]+)")
|
||||
for _, m := range r.FindAllStringSubmatch(rawMorse, -1) {
|
||||
c := m[1][0]
|
||||
keys := []key{}
|
||||
for i, dd := range m[2] {
|
||||
if i > 0 {
|
||||
keys = append(keys, interCharGap...)
|
||||
}
|
||||
if dd == '.' {
|
||||
keys = append(keys, key{1, true, "."})
|
||||
} else if dd == '-' {
|
||||
keys = append(keys, key{3, true, "-"})
|
||||
} else {
|
||||
log.Fatalf("found %c in morse for %c", dd, c)
|
||||
}
|
||||
runeToKeys[rune(c)] = keys
|
||||
runeToKeys[unicode.ToLower(rune(c))] = keys
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MorseKeys translates an input string into a series of keys.
|
||||
func MorseKeys(in string) ([]key, error) {
|
||||
afterWord := false
|
||||
afterChar := false
|
||||
result := []key{}
|
||||
for _, c := range in {
|
||||
if unicode.IsSpace(c) {
|
||||
afterWord = true
|
||||
continue
|
||||
}
|
||||
morse, ok := runeToKeys[c]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can't translate %c to morse", c)
|
||||
}
|
||||
if unicode.IsPunct(c) && afterChar {
|
||||
result = append(result, punctGap...)
|
||||
} else if afterWord {
|
||||
result = append(result, wordGap...)
|
||||
} else if afterChar {
|
||||
result = append(result, charGap...)
|
||||
}
|
||||
result = append(result, morse...)
|
||||
afterChar = true
|
||||
afterWord = false
|
||||
}
|
||||
return result, nil
|
||||
afterWord := false
|
||||
afterChar := false
|
||||
result := []key{}
|
||||
for _, c := range in {
|
||||
if unicode.IsSpace(c) {
|
||||
afterWord = true
|
||||
continue
|
||||
}
|
||||
morse, ok := runeToKeys[c]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can't translate %c to morse", c)
|
||||
}
|
||||
if unicode.IsPunct(c) && afterChar {
|
||||
result = append(result, punctGap...)
|
||||
} else if afterWord {
|
||||
result = append(result, wordGap...)
|
||||
} else if afterChar {
|
||||
result = append(result, charGap...)
|
||||
}
|
||||
result = append(result, morse...)
|
||||
afterChar = true
|
||||
afterWord = false
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var ditDuration time.Duration
|
||||
flag.DurationVar(&ditDuration, "d", 40*time.Millisecond, "length of dit")
|
||||
flag.Parse()
|
||||
in := "hello world."
|
||||
if len(flag.Args()) > 1 {
|
||||
in = strings.Join(flag.Args(), " ")
|
||||
}
|
||||
keys, err := MorseKeys(in)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to translate: %s", err)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if k.on {
|
||||
if err := note(true); err != nil {
|
||||
log.Fatalf("failed to play note: %s", err)
|
||||
}
|
||||
}
|
||||
fmt.Print(k.sym)
|
||||
time.Sleep(ditDuration * time.Duration(k.duration))
|
||||
if k.on {
|
||||
if err := note(false); err != nil {
|
||||
log.Fatalf("failed to stop note: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
var ditDuration time.Duration
|
||||
flag.DurationVar(&ditDuration, "d", 40*time.Millisecond, "length of dit")
|
||||
flag.Parse()
|
||||
in := "hello world."
|
||||
if len(flag.Args()) > 1 {
|
||||
in = strings.Join(flag.Args(), " ")
|
||||
}
|
||||
keys, err := MorseKeys(in)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to translate: %s", err)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if k.on {
|
||||
if err := note(true); err != nil {
|
||||
log.Fatalf("failed to play note: %s", err)
|
||||
}
|
||||
}
|
||||
fmt.Print(k.sym)
|
||||
time.Sleep(ditDuration * time.Duration(k.duration))
|
||||
if k.on {
|
||||
if err := note(false); err != nil {
|
||||
log.Fatalf("failed to stop note: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// Implement sound on ubuntu. Needs permission to access /dev/console.
|
||||
|
|
@ -127,11 +127,11 @@ func main() {
|
|||
var consoleFD uintptr
|
||||
|
||||
func init() {
|
||||
fd, err := syscall.Open("/dev/console", syscall.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to get console device: %s", err)
|
||||
}
|
||||
consoleFD = uintptr(fd)
|
||||
fd, err := syscall.Open("/dev/console", syscall.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to get console device: %s", err)
|
||||
}
|
||||
consoleFD = uintptr(fd)
|
||||
}
|
||||
|
||||
const KIOCSOUND = 0x4B2F
|
||||
|
|
@ -140,14 +140,14 @@ const freqHz = 600
|
|||
|
||||
// note either starts or stops a note.
|
||||
func note(on bool) error {
|
||||
arg := uintptr(0)
|
||||
if on {
|
||||
arg = clockTickRate / freqHz
|
||||
}
|
||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, consoleFD, KIOCSOUND, arg)
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
arg := uintptr(0)
|
||||
if on {
|
||||
arg = clockTickRate / freqHz
|
||||
}
|
||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, consoleFD, KIOCSOUND, arg)
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,92 +8,92 @@ import javax.sound.sampled.SourceDataLine;
|
|||
|
||||
public final class MorseCode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<String, String> morseCode = Map.ofEntries(
|
||||
// International Morse Code, ITU standard
|
||||
Map.entry("A", ".-"), Map.entry("B", "-..."), Map.entry("C", "-.-."), Map.entry("D", "-.."),
|
||||
Map.entry("E", "."), Map.entry("F", "..-."), Map.entry("G", "--."), Map.entry("H", "...."),
|
||||
Map.entry("I", ".."), Map.entry("J", ".---"), Map.entry("K", "-.-"), Map.entry("L", ".-.."),
|
||||
Map.entry("M", "--"), Map.entry("N", "-."), Map.entry("O", "---"), Map.entry("P", ".--."),
|
||||
Map.entry("Q", "--.-"), Map.entry("R", ".-."), Map.entry("S", "..."), Map.entry("T", "-"),
|
||||
Map.entry("U", "..-"), Map.entry("V", "...-"), Map.entry("W", ".--"), Map.entry("X", "-..-"),
|
||||
Map.entry("Y", "-.--"), Map.entry("Z", "--.."),
|
||||
|
||||
Map.entry("1", ".----"), Map.entry("2", "..---"), Map.entry("3", "...--"), Map.entry("4", "....-"),
|
||||
Map.entry("5", "....."), Map.entry("6", "-...."), Map.entry("7", "--..."), Map.entry("8", "---.."),
|
||||
Map.entry("9", "----."), Map.entry("0", "-----"),
|
||||
|
||||
// Generally accepted additions
|
||||
Map.entry("'", ".----."), Map.entry(":", "---..."), Map.entry(",", "--..--"), Map.entry("-", "-....-"),
|
||||
Map.entry("(", "-.--.-"), Map.entry(".", ".-.-.-"), Map.entry("?", "..--.."), Map.entry(";", "-.-.-."),
|
||||
Map.entry("/", "-..-."), Map.entry(")", "---.."), Map.entry("=", "-...-"), Map.entry("@", ".--.-."),
|
||||
Map.entry("\"", ".-..-."), Map.entry("+", ".-.-.")
|
||||
);
|
||||
|
||||
prepareSourceDataLine();
|
||||
|
||||
final int frequency = 1280; // Frequency of the sound tone in Hertz
|
||||
final int time = 250; // Time unit in milliseconds
|
||||
|
||||
// Correct pacing of the sound transmission is essential for practical use
|
||||
final int dot = 1; // Time units taken for one dot
|
||||
final int dash = 3; // Time units taken for one dash
|
||||
final int interval = 1; // Time units interval between dots and dashes
|
||||
final int letterInterval = 1; // Time units interval between letters of a word
|
||||
final int wordInterval = 7; // Time units interval between words
|
||||
|
||||
String message = "Hello World";
|
||||
|
||||
for ( String letter : message.toUpperCase().split("") ) {
|
||||
switch ( letter ) {
|
||||
case " " -> tone(0, time * wordInterval, 0);
|
||||
default -> {
|
||||
String code = morseCode.get(letter);
|
||||
for ( String dotDash : code.split("") ) {
|
||||
switch ( dotDash ) {
|
||||
case "." -> { tone(frequency, time * dot, 1); tone(0, time * interval, 0); }
|
||||
case "-" -> { tone(frequency, time * dash, 1); tone(0, time * interval, 0); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tone(0, time * letterInterval, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private static void tone(double frequency, int duration, int volume) {
|
||||
sourceDataLine.start();
|
||||
|
||||
IntStream.range(0, 8 * duration).forEach( i -> {
|
||||
final double angle = i / ( SAMPLE_RATE / frequency ) * 2 * Math.PI;
|
||||
byte[] buffer = new byte[] { (byte) ( Math.sin(angle) * 127 * volume ) };
|
||||
sourceDataLine.write(buffer, BYTE_OFFSET, buffer.length);
|
||||
} );
|
||||
|
||||
sourceDataLine.drain();
|
||||
sourceDataLine.stop();
|
||||
}
|
||||
|
||||
private static void prepareSourceDataLine() {
|
||||
final int sampleSizeInBits = 8;
|
||||
final int numberChannels = 1;
|
||||
final boolean signedData = true;
|
||||
final boolean isBigEndian = false;
|
||||
|
||||
AudioFormat audioFormat = new AudioFormat(
|
||||
SAMPLE_RATE, sampleSizeInBits, numberChannels, signedData, isBigEndian);
|
||||
|
||||
try {
|
||||
sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
|
||||
sourceDataLine.open(audioFormat);
|
||||
} catch (LineUnavailableException lue) {
|
||||
lue.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static SourceDataLine sourceDataLine;
|
||||
|
||||
private static float SAMPLE_RATE = 8_000.0F;
|
||||
private static final int BYTE_OFFSET = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<String, String> morseCode = Map.ofEntries(
|
||||
// International Morse Code, ITU standard
|
||||
Map.entry("A", ".-"), Map.entry("B", "-..."), Map.entry("C", "-.-."), Map.entry("D", "-.."),
|
||||
Map.entry("E", "."), Map.entry("F", "..-."), Map.entry("G", "--."), Map.entry("H", "...."),
|
||||
Map.entry("I", ".."), Map.entry("J", ".---"), Map.entry("K", "-.-"), Map.entry("L", ".-.."),
|
||||
Map.entry("M", "--"), Map.entry("N", "-."), Map.entry("O", "---"), Map.entry("P", ".--."),
|
||||
Map.entry("Q", "--.-"), Map.entry("R", ".-."), Map.entry("S", "..."), Map.entry("T", "-"),
|
||||
Map.entry("U", "..-"), Map.entry("V", "...-"), Map.entry("W", ".--"), Map.entry("X", "-..-"),
|
||||
Map.entry("Y", "-.--"), Map.entry("Z", "--.."),
|
||||
|
||||
Map.entry("1", ".----"), Map.entry("2", "..---"), Map.entry("3", "...--"), Map.entry("4", "....-"),
|
||||
Map.entry("5", "....."), Map.entry("6", "-...."), Map.entry("7", "--..."), Map.entry("8", "---.."),
|
||||
Map.entry("9", "----."), Map.entry("0", "-----"),
|
||||
|
||||
// Generally accepted additions
|
||||
Map.entry("'", ".----."), Map.entry(":", "---..."), Map.entry(",", "--..--"), Map.entry("-", "-....-"),
|
||||
Map.entry("(", "-.--.-"), Map.entry(".", ".-.-.-"), Map.entry("?", "..--.."), Map.entry(";", "-.-.-."),
|
||||
Map.entry("/", "-..-."), Map.entry(")", "---.."), Map.entry("=", "-...-"), Map.entry("@", ".--.-."),
|
||||
Map.entry("\"", ".-..-."), Map.entry("+", ".-.-.")
|
||||
);
|
||||
|
||||
prepareSourceDataLine();
|
||||
|
||||
final int frequency = 1280; // Frequency of the sound tone in Hertz
|
||||
final int time = 250; // Time unit in milliseconds
|
||||
|
||||
// Correct pacing of the sound transmission is essential for practical use
|
||||
final int dot = 1; // Time units taken for one dot
|
||||
final int dash = 3; // Time units taken for one dash
|
||||
final int interval = 1; // Time units interval between dots and dashes
|
||||
final int letterInterval = 1; // Time units interval between letters of a word
|
||||
final int wordInterval = 7; // Time units interval between words
|
||||
|
||||
String message = "Hello World";
|
||||
|
||||
for ( String letter : message.toUpperCase().split("") ) {
|
||||
switch ( letter ) {
|
||||
case " " -> tone(0, time * wordInterval, 0);
|
||||
default -> {
|
||||
String code = morseCode.get(letter);
|
||||
for ( String dotDash : code.split("") ) {
|
||||
switch ( dotDash ) {
|
||||
case "." -> { tone(frequency, time * dot, 1); tone(0, time * interval, 0); }
|
||||
case "-" -> { tone(frequency, time * dash, 1); tone(0, time * interval, 0); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tone(0, time * letterInterval, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private static void tone(double frequency, int duration, int volume) {
|
||||
sourceDataLine.start();
|
||||
|
||||
IntStream.range(0, 8 * duration).forEach( i -> {
|
||||
final double angle = i / ( SAMPLE_RATE / frequency ) * 2 * Math.PI;
|
||||
byte[] buffer = new byte[] { (byte) ( Math.sin(angle) * 127 * volume ) };
|
||||
sourceDataLine.write(buffer, BYTE_OFFSET, buffer.length);
|
||||
} );
|
||||
|
||||
sourceDataLine.drain();
|
||||
sourceDataLine.stop();
|
||||
}
|
||||
|
||||
private static void prepareSourceDataLine() {
|
||||
final int sampleSizeInBits = 8;
|
||||
final int numberChannels = 1;
|
||||
final boolean signedData = true;
|
||||
final boolean isBigEndian = false;
|
||||
|
||||
AudioFormat audioFormat = new AudioFormat(
|
||||
SAMPLE_RATE, sampleSizeInBits, numberChannels, signedData, isBigEndian);
|
||||
|
||||
try {
|
||||
sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
|
||||
sourceDataLine.open(audioFormat);
|
||||
} catch (LineUnavailableException lue) {
|
||||
lue.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static SourceDataLine sourceDataLine;
|
||||
|
||||
private static float SAMPLE_RATE = 8_000.0F;
|
||||
private static final int BYTE_OFFSET = 0;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,83 +1,83 @@
|
|||
var globalAudioContext = new webkitAudioContext();
|
||||
|
||||
function morsecode(text, unit, freq) {
|
||||
'use strict';
|
||||
'use strict';
|
||||
|
||||
// defaults
|
||||
unit = unit ? unit : 0.05;
|
||||
freq = freq ? freq : 700;
|
||||
var cont = globalAudioContext;
|
||||
var time = cont.currentTime;
|
||||
// defaults
|
||||
unit = unit ? unit : 0.05;
|
||||
freq = freq ? freq : 700;
|
||||
var cont = globalAudioContext;
|
||||
var time = cont.currentTime;
|
||||
|
||||
// morsecode
|
||||
var code = {
|
||||
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: '____.'
|
||||
};
|
||||
// morsecode
|
||||
var code = {
|
||||
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: '____.'
|
||||
};
|
||||
|
||||
// generate code for text
|
||||
function makecode(data) {
|
||||
for (var i = 0; i <= data.length; i ++) {
|
||||
var codedata = data.substr(i, 1).toLowerCase();
|
||||
codedata = code[codedata];
|
||||
// recognised character
|
||||
if (codedata !== undefined) {
|
||||
maketime(codedata);
|
||||
}
|
||||
// unrecognised character
|
||||
else {
|
||||
time += unit * 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
// generate code for text
|
||||
function makecode(data) {
|
||||
for (var i = 0; i <= data.length; i ++) {
|
||||
var codedata = data.substr(i, 1).toLowerCase();
|
||||
codedata = code[codedata];
|
||||
// recognised character
|
||||
if (codedata !== undefined) {
|
||||
maketime(codedata);
|
||||
}
|
||||
// unrecognised character
|
||||
else {
|
||||
time += unit * 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// generate time for code
|
||||
function maketime(data) {
|
||||
for (var i = 0; i <= data.length; i ++) {
|
||||
var timedata = data.substr(i, 1);
|
||||
timedata = (timedata === '.') ? 1 : (timedata === '_') ? 3 : 0;
|
||||
timedata *= unit;
|
||||
if (timedata > 0) {
|
||||
maketone(timedata);
|
||||
time += timedata;
|
||||
// tone gap
|
||||
time += unit * 1;
|
||||
}
|
||||
}
|
||||
// char gap
|
||||
time += unit * 2;
|
||||
}
|
||||
// generate time for code
|
||||
function maketime(data) {
|
||||
for (var i = 0; i <= data.length; i ++) {
|
||||
var timedata = data.substr(i, 1);
|
||||
timedata = (timedata === '.') ? 1 : (timedata === '_') ? 3 : 0;
|
||||
timedata *= unit;
|
||||
if (timedata > 0) {
|
||||
maketone(timedata);
|
||||
time += timedata;
|
||||
// tone gap
|
||||
time += unit * 1;
|
||||
}
|
||||
}
|
||||
// char gap
|
||||
time += unit * 2;
|
||||
}
|
||||
|
||||
// generate tone for time
|
||||
function maketone(data) {
|
||||
var start = time;
|
||||
var stop = time + data;
|
||||
// filter: envelope the tone slightly
|
||||
gain.gain.linearRampToValueAtTime(0, start);
|
||||
gain.gain.linearRampToValueAtTime(1, start + (unit / 8));
|
||||
gain.gain.linearRampToValueAtTime(1, stop - (unit / 16));
|
||||
gain.gain.linearRampToValueAtTime(0, stop);
|
||||
}
|
||||
// generate tone for time
|
||||
function maketone(data) {
|
||||
var start = time;
|
||||
var stop = time + data;
|
||||
// filter: envelope the tone slightly
|
||||
gain.gain.linearRampToValueAtTime(0, start);
|
||||
gain.gain.linearRampToValueAtTime(1, start + (unit / 8));
|
||||
gain.gain.linearRampToValueAtTime(1, stop - (unit / 16));
|
||||
gain.gain.linearRampToValueAtTime(0, stop);
|
||||
}
|
||||
|
||||
// create: oscillator, gain, destination
|
||||
var osci = cont.createOscillator();
|
||||
osci.frequency.value = freq;
|
||||
var gain = cont.createGainNode();
|
||||
gain.gain.value = 0;
|
||||
var dest = cont.destination;
|
||||
// connect: oscillator -> gain -> destination
|
||||
osci.connect(gain);
|
||||
gain.connect(dest);
|
||||
// start oscillator
|
||||
osci.start(time);
|
||||
// create: oscillator, gain, destination
|
||||
var osci = cont.createOscillator();
|
||||
osci.frequency.value = freq;
|
||||
var gain = cont.createGainNode();
|
||||
gain.gain.value = 0;
|
||||
var dest = cont.destination;
|
||||
// connect: oscillator -> gain -> destination
|
||||
osci.connect(gain);
|
||||
gain.connect(dest);
|
||||
// start oscillator
|
||||
osci.start(time);
|
||||
|
||||
// begin encoding: text -> code -> time -> tone
|
||||
makecode(text);
|
||||
// begin encoding: text -> code -> time -> tone
|
||||
makecode(text);
|
||||
|
||||
// return web audio context for reuse / control
|
||||
return cont;
|
||||
// return web audio context for reuse / control
|
||||
return cont;
|
||||
}
|
||||
|
|
|
|||
59
Task/Morse-code/Langur/morse-code.langur
Normal file
59
Task/Morse-code/Langur/morse-code.langur
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
val morse_code = {
|
||||
"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": "----.",
|
||||
|
||||
".": ".-.-.-",
|
||||
" ": " ",
|
||||
}
|
||||
|
||||
val conv = fn(s string) {
|
||||
for[=""] i of s {
|
||||
_for ~= morse_code[cp2s(ucase(s[i])); "........"]
|
||||
if i < len(s): _for ~= " "
|
||||
}
|
||||
}
|
||||
|
||||
val test = fn*(s string) {
|
||||
# with 2 different methods of transliteration
|
||||
writeln "orig. string: ", s
|
||||
writeln "morse code1 : ", conv(s)
|
||||
writeln "morse code2 : ", tran(ucase(s), with=morse_code, delim=" ")
|
||||
writeln()
|
||||
}
|
||||
|
||||
test("This is a test.")
|
||||
|
|
@ -1,58 +1,58 @@
|
|||
Module Morse_code {
|
||||
declare Json JsonObject
|
||||
json$={{
|
||||
"!": "---.", "\"": ".-..-.", "$": "...-..-", "'": ".----.",
|
||||
"(": "-.--.", ")": "-.--.-", "+": ".-.-.", ",": "--..--",
|
||||
"-": "-....-", ".": ".-.-.-", "/": "-..-.",
|
||||
"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": "--..",
|
||||
"[": "-.--.", "]": "-.--.-", "_": "..--.-",
|
||||
}}
|
||||
e = 50 ' Element time in ms. one dit is on for e then off for e
|
||||
f = 1280 ' Tone freq. in hertz
|
||||
chargap = 1*e ' Time between characters of a word
|
||||
wordgap = 7*e ' Time between words
|
||||
|
||||
method json, "parser", json$ as json
|
||||
with json, "itempath" as json.path$()
|
||||
|
||||
|
||||
Input "Send Message:";a$
|
||||
a$=trim$(a$)
|
||||
if len(a$)=0 then exit
|
||||
a$=ucase$(a$)
|
||||
|
||||
for i=1 to len(a$)
|
||||
L$=mid$(a$, i, 1)
|
||||
Print L$;
|
||||
Send(json.path$(L$))
|
||||
next
|
||||
|
||||
sub Send(a$)
|
||||
if len(a$)=0 then wait wordgap : exit sub
|
||||
local i
|
||||
for i=1 to len(a$)
|
||||
select case mid$(a$, i, 1)
|
||||
case "."
|
||||
tone e, f
|
||||
case "-"
|
||||
tone 3*e, f
|
||||
case else
|
||||
tone 3*e, f mod 2
|
||||
end select
|
||||
wait chargap
|
||||
next
|
||||
end sub
|
||||
declare Json JsonObject
|
||||
json$={{
|
||||
"!": "---.", "\"": ".-..-.", "$": "...-..-", "'": ".----.",
|
||||
"(": "-.--.", ")": "-.--.-", "+": ".-.-.", ",": "--..--",
|
||||
"-": "-....-", ".": ".-.-.-", "/": "-..-.",
|
||||
"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": "--..",
|
||||
"[": "-.--.", "]": "-.--.-", "_": "..--.-",
|
||||
}}
|
||||
e = 50 ' Element time in ms. one dit is on for e then off for e
|
||||
f = 1280 ' Tone freq. in hertz
|
||||
chargap = 1*e ' Time between characters of a word
|
||||
wordgap = 7*e ' Time between words
|
||||
|
||||
method json, "parser", json$ as json
|
||||
with json, "itempath" as json.path$()
|
||||
|
||||
|
||||
Input "Send Message:";a$
|
||||
a$=trim$(a$)
|
||||
if len(a$)=0 then exit
|
||||
a$=ucase$(a$)
|
||||
|
||||
for i=1 to len(a$)
|
||||
L$=mid$(a$, i, 1)
|
||||
Print L$;
|
||||
Send(json.path$(L$))
|
||||
next
|
||||
|
||||
sub Send(a$)
|
||||
if len(a$)=0 then wait wordgap : exit sub
|
||||
local i
|
||||
for i=1 to len(a$)
|
||||
select case mid$(a$, i, 1)
|
||||
case "."
|
||||
tone e, f
|
||||
case "-"
|
||||
tone 3*e, f
|
||||
case else
|
||||
tone 3*e, f mod 2
|
||||
end select
|
||||
wait chargap
|
||||
next
|
||||
end sub
|
||||
}
|
||||
Keyboard "This is Morse_code", 13
|
||||
Morse_code
|
||||
|
|
|
|||
|
|
@ -1,121 +1,121 @@
|
|||
{$mode delphi}
|
||||
PROGRAM cw;
|
||||
// Output a string as Morse code and CW.
|
||||
// Cross-platform PCM audio uses OpenAL
|
||||
USES OpenAL, HRTimer;
|
||||
// Output a string as Morse code and CW.
|
||||
// Cross-platform PCM audio uses OpenAL
|
||||
USES OpenAL, HRTimer;
|
||||
|
||||
// Intl. Morse codes in ASCII order
|
||||
CONST Morse: ARRAY [32..95] OF STRING = (' ','-.-.--','.-..-.','#','...-..-','%','.-...','.----.','-.--.','-.--.-','*','.-.-.','--..--','-....-','.-.-.-','-..-.','-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','---...','-.-.-.','>','-...-','<','..--..','.--.-.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','-.--.','\','-.--.-','~','..--.-');
|
||||
// lengthen dah by this fraction of dit:
|
||||
// best = 0.4; also lengthens pauses
|
||||
doh = 0.4;
|
||||
// an 0.05 sec dit is around 26 wpm
|
||||
dit = 0.05;
|
||||
dah = 3 * dit + doh * dit;
|
||||
// Intl. Morse codes in ASCII order
|
||||
CONST Morse: ARRAY [32..95] OF STRING = (' ','-.-.--','.-..-.','#','...-..-','%','.-...','.----.','-.--.','-.--.-','*','.-.-.','--..--','-....-','.-.-.-','-..-.','-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','---...','-.-.-.','>','-...-','<','..--..','.--.-.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','-.--.','\','-.--.-','~','..--.-');
|
||||
// lengthen dah by this fraction of dit:
|
||||
// best = 0.4; also lengthens pauses
|
||||
doh = 0.4;
|
||||
// an 0.05 sec dit is around 26 wpm
|
||||
dit = 0.05;
|
||||
dah = 3 * dit + doh * dit;
|
||||
|
||||
VAR // OpenAL variables
|
||||
buffer : TALuint;
|
||||
source : TALuint;
|
||||
sourcepos: ARRAY [0..2] OF TALfloat= ( 0.0, 0.0, 0.0 );
|
||||
sourcevel: ARRAY [0..2] OF TALfloat= ( 0.0, 0.0, 0.0 );
|
||||
VAR // OpenAL variables
|
||||
buffer : TALuint;
|
||||
source : TALuint;
|
||||
sourcepos: ARRAY [0..2] OF TALfloat= ( 0.0, 0.0, 0.0 );
|
||||
sourcevel: ARRAY [0..2] OF TALfloat= ( 0.0, 0.0, 0.0 );
|
||||
|
||||
argv: ARRAY OF PalByte;
|
||||
format: TALEnum;
|
||||
size: TALSizei;
|
||||
freq: TALSizei;
|
||||
loop: TALInt;
|
||||
data: TALVoid;
|
||||
argv: ARRAY OF PalByte;
|
||||
format: TALEnum;
|
||||
size: TALSizei;
|
||||
freq: TALSizei;
|
||||
loop: TALInt;
|
||||
data: TALVoid;
|
||||
|
||||
// rewinding has an effect on the output:
|
||||
// <with> and <without> sound rather different
|
||||
rewind : BOOLEAN = FALSE;
|
||||
// the high-res timer is from Wolfgang Ehrhardt
|
||||
// http://www.wolfgang-ehrhardt.de/misc_en.html
|
||||
t : THRTimer;
|
||||
msg : STRING = 'the quick brown fox jumps over the lazy dog.';
|
||||
|
||||
// rewinding has an effect on the output:
|
||||
// <with> and <without> sound rather different
|
||||
rewind : BOOLEAN = FALSE;
|
||||
// the high-res timer is from Wolfgang Ehrhardt
|
||||
// http://www.wolfgang-ehrhardt.de/misc_en.html
|
||||
t : THRTimer;
|
||||
msg : STRING = 'the quick brown fox jumps over the lazy dog.';
|
||||
|
||||
PROCEDURE PlayS(s: Extended);
|
||||
BEGIN
|
||||
StartTimer(t);
|
||||
AlSourcePlay(source);
|
||||
WHILE readseconds(t) < s DO BEGIN END;
|
||||
IF rewind THEN AlSourceRewind(source);
|
||||
AlSourceStop(source);
|
||||
END;
|
||||
|
||||
PROCEDURE Pause(s: Extended);
|
||||
BEGIN
|
||||
StartTimer(t);
|
||||
WHILE readseconds(t) < s DO BEGIN END;
|
||||
END;
|
||||
|
||||
PROCEDURE doDit;
|
||||
BEGIN
|
||||
PlayS(dit);
|
||||
Pause(dit);
|
||||
END;
|
||||
|
||||
PROCEDURE doDah;
|
||||
BEGIN
|
||||
PlayS(dah);
|
||||
Pause(dit);
|
||||
END;
|
||||
|
||||
// ASCII char to Morse CW
|
||||
FUNCTION AtoM(ch: CHAR): STRING;
|
||||
VAR i: Integer;
|
||||
u: CHAR;
|
||||
BEGIN
|
||||
u := ch;
|
||||
IF ch IN ['a'..'z'] THEN u := chr(ord(ch) AND $5F);
|
||||
result := Morse[ord(u)];
|
||||
FOR i := 1 TO Length(result) DO
|
||||
CASE result[i] OF
|
||||
'.': BEGIN doDit; Write('. ') END;
|
||||
'-': BEGIN doDah; Write('_ ') END;
|
||||
END;
|
||||
Pause(dah);
|
||||
Write(' ');
|
||||
IF u = ' ' THEN Write(' ');
|
||||
END;
|
||||
PROCEDURE PlayS(s: Extended);
|
||||
BEGIN
|
||||
StartTimer(t);
|
||||
AlSourcePlay(source);
|
||||
WHILE readseconds(t) < s DO BEGIN END;
|
||||
IF rewind THEN AlSourceRewind(source);
|
||||
AlSourceStop(source);
|
||||
END;
|
||||
|
||||
PROCEDURE Pause(s: Extended);
|
||||
BEGIN
|
||||
StartTimer(t);
|
||||
WHILE readseconds(t) < s DO BEGIN END;
|
||||
END;
|
||||
|
||||
PROCEDURE doDit;
|
||||
BEGIN
|
||||
PlayS(dit);
|
||||
Pause(dit);
|
||||
END;
|
||||
|
||||
PROCEDURE doDah;
|
||||
BEGIN
|
||||
PlayS(dah);
|
||||
Pause(dit);
|
||||
END;
|
||||
|
||||
// ASCII char to Morse CW
|
||||
FUNCTION AtoM(ch: CHAR): STRING;
|
||||
VAR i: Integer;
|
||||
u: CHAR;
|
||||
BEGIN
|
||||
u := ch;
|
||||
IF ch IN ['a'..'z'] THEN u := chr(ord(ch) AND $5F);
|
||||
result := Morse[ord(u)];
|
||||
FOR i := 1 TO Length(result) DO
|
||||
CASE result[i] OF
|
||||
'.': BEGIN doDit; Write('. ') END;
|
||||
'-': BEGIN doDah; Write('_ ') END;
|
||||
END;
|
||||
Pause(dah);
|
||||
Write(' ');
|
||||
IF u = ' ' THEN Write(' ');
|
||||
END;
|
||||
|
||||
// ASCII string to Morse CW
|
||||
PROCEDURE StoM(s: STRING);
|
||||
VAR i: Integer;
|
||||
BEGIN
|
||||
FOR i := 1 TO Length(s) DO AtoM(s[i]);
|
||||
END;
|
||||
|
||||
// ASCII string to Morse CW
|
||||
PROCEDURE StoM(s: STRING);
|
||||
VAR i: Integer;
|
||||
BEGIN
|
||||
FOR i := 1 TO Length(s) DO AtoM(s[i]);
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
// OpenAL preparation
|
||||
InitOpenAL;
|
||||
AlutInit(nil,argv);
|
||||
|
||||
AlGenBuffers(1, @buffer);
|
||||
// load the 500 Hz 1 sec sine-wave file
|
||||
// get it from http://audiocheck.net
|
||||
AlutLoadWavFile('audiocheck.net_sin_500Hz_-3dBFS_1s.wav', format, data, size, freq, loop);
|
||||
AlBufferData(buffer, format, data, size, freq);
|
||||
AlutUnloadWav(format, data, size, freq);
|
||||
|
||||
AlGenSources(1, @source);
|
||||
AlSourcei ( source, AL_BUFFER, buffer);
|
||||
AlSourcef ( source, AL_PITCH, 1.0 );
|
||||
AlSourcef ( source, AL_GAIN, 1.0 );
|
||||
AlSourcefv ( source, AL_POSITION, @sourcepos);
|
||||
AlSourcefv ( source, AL_VELOCITY, @sourcevel);
|
||||
AlSourcei ( source, AL_LOOPING, AL_TRUE);
|
||||
|
||||
// Sound and print the Morse
|
||||
StoM(msg);
|
||||
Pause(1.0);
|
||||
|
||||
AlSourceRewind(source);
|
||||
AlSourceStop(source);
|
||||
|
||||
// Clean up
|
||||
AlDeleteBuffers(1, @buffer);
|
||||
AlDeleteSources(1, @source);
|
||||
AlutExit();
|
||||
// OpenAL preparation
|
||||
InitOpenAL;
|
||||
AlutInit(nil,argv);
|
||||
|
||||
AlGenBuffers(1, @buffer);
|
||||
// load the 500 Hz 1 sec sine-wave file
|
||||
// get it from http://audiocheck.net
|
||||
AlutLoadWavFile('audiocheck.net_sin_500Hz_-3dBFS_1s.wav', format, data, size, freq, loop);
|
||||
AlBufferData(buffer, format, data, size, freq);
|
||||
AlutUnloadWav(format, data, size, freq);
|
||||
|
||||
AlGenSources(1, @source);
|
||||
AlSourcei ( source, AL_BUFFER, buffer);
|
||||
AlSourcef ( source, AL_PITCH, 1.0 );
|
||||
AlSourcef ( source, AL_GAIN, 1.0 );
|
||||
AlSourcefv ( source, AL_POSITION, @sourcepos);
|
||||
AlSourcefv ( source, AL_VELOCITY, @sourcevel);
|
||||
AlSourcei ( source, AL_LOOPING, AL_TRUE);
|
||||
|
||||
// Sound and print the Morse
|
||||
StoM(msg);
|
||||
Pause(1.0);
|
||||
|
||||
AlSourceRewind(source);
|
||||
AlSourceStop(source);
|
||||
|
||||
// Clean up
|
||||
AlDeleteBuffers(1, @buffer);
|
||||
AlDeleteSources(1, @source);
|
||||
AlutExit();
|
||||
END.
|
||||
|
|
|
|||
60
Task/Morse-code/Pluto/morse-code.pluto
Normal file
60
Task/Morse-code/Pluto/morse-code.pluto
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
local audio = require "audio"
|
||||
|
||||
local char_to_morse = {
|
||||
["!"] = "---.", ['"'] = ".-..-.", ["$"] = "...-..-", ["'"] = ".----.",
|
||||
["("] = "-.--.", [")"] = "-.--.-", ["+"] = ".-.-.", [","] = "--..--",
|
||||
["-"] = "-....-", ["."] = ".-.-.-", ["/"] = "-..-.",
|
||||
["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"] = "--..",
|
||||
["["] = "-.--.", ["]"] = "-.--.-", ["_"] = "..--.-"
|
||||
}
|
||||
|
||||
local function text_to_morse(text)
|
||||
text = text:upper()
|
||||
local morse = ""
|
||||
for i = 1, #text do
|
||||
local c = text[i]
|
||||
if c == " " then
|
||||
morse ..= string.rep(" ", 7)
|
||||
else
|
||||
local m = char_to_morse[c]
|
||||
if m then morse ..= m:split(""):concat(" ") .. " " end
|
||||
end
|
||||
end
|
||||
return morse:rstrip()
|
||||
end
|
||||
|
||||
local morse = text_to_morse("Hello world!")
|
||||
print(morse) -- print to terminal
|
||||
|
||||
-- Now create a .wav file.
|
||||
morse = morse:replace("-", "...") -- replace 'dash' with 3 'dot's
|
||||
local data = {}
|
||||
local sample_rate = 44100
|
||||
local samples = 0.2 * sample_rate -- number of samples assuming 'dot' takes 200 ms
|
||||
local freq = 500 -- say
|
||||
local omega = 2 * math.pi * freq
|
||||
for i = 1, #morse do
|
||||
local c = morse[i]
|
||||
if c == "." then
|
||||
for s = 0, samples -1 do
|
||||
local value = math.round(32 * math.sin(omega * s / sample_rate)) & 255
|
||||
data:insert(value)
|
||||
end
|
||||
else
|
||||
for _ = 1, samples do data:insert(0) end
|
||||
end
|
||||
end
|
||||
local filepath = "morse_code.wav"
|
||||
audio.create(filepath, data, sample_rate)
|
||||
audio.play(filepath)
|
||||
59
Task/Morse-code/PowerShell/morse-code-1.ps1
Normal file
59
Task/Morse-code/PowerShell/morse-code-1.ps1
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
function Send-MorseCode
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([string])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipeline=$true,
|
||||
Position=0)]
|
||||
[string]
|
||||
$Message,
|
||||
|
||||
[switch]
|
||||
$ShowCode
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$morseCode = @{
|
||||
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 = "----."
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
foreach ($word in $Message)
|
||||
{
|
||||
$word.Split(" ",[StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
|
||||
|
||||
foreach ($char in $_.ToCharArray())
|
||||
{
|
||||
if ($char -in $morseCode.Keys)
|
||||
{
|
||||
foreach ($code in ($morseCode."$char").ToCharArray())
|
||||
{
|
||||
if ($code -eq ".") {$duration = 250} else {$duration = 750}
|
||||
|
||||
[System.Console]::Beep(1000, $duration)
|
||||
Start-Sleep -Milliseconds 50
|
||||
}
|
||||
|
||||
if ($ShowCode) {Write-Host ("{0,-6}" -f ("{0,6}" -f $morseCode."$char")) -NoNewLine}
|
||||
}
|
||||
}
|
||||
|
||||
if ($ShowCode) {Write-Host}
|
||||
}
|
||||
|
||||
if ($ShowCode) {Write-Host}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Task/Morse-code/PowerShell/morse-code-2.ps1
Normal file
1
Task/Morse-code/PowerShell/morse-code-2.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Send-MorseCode -Message "S.O.S" -ShowCode
|
||||
1
Task/Morse-code/PowerShell/morse-code-3.ps1
Normal file
1
Task/Morse-code/PowerShell/morse-code-3.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
"S.O.S", "Goodbye, cruel world!" | Send-MorseCode -ShowCode
|
||||
|
|
@ -6,18 +6,18 @@
|
|||
% There is a space between chars and double space between words
|
||||
%
|
||||
text2morse(Text, Morse) :-
|
||||
string_lower(Text, TextLower), % rules are in lower case
|
||||
string_chars(TextLower, Chars), % convert string into list of chars
|
||||
chars2morse(Chars, MorseChars), % convert each char into morse
|
||||
string_chars(MorsePlusSpace, MorseChars), % append returned string list into single string
|
||||
string_concat(Morse, ' ', MorsePlusSpace). % Remove trailing space
|
||||
string_lower(Text, TextLower), % rules are in lower case
|
||||
string_chars(TextLower, Chars), % convert string into list of chars
|
||||
chars2morse(Chars, MorseChars), % convert each char into morse
|
||||
string_chars(MorsePlusSpace, MorseChars), % append returned string list into single string
|
||||
string_concat(Morse, ' ', MorsePlusSpace). % Remove trailing space
|
||||
|
||||
chars2morse([], "").
|
||||
chars2morse([H|CharTail], Morse) :-
|
||||
morse(H, M),
|
||||
chars2morse(CharTail, MorseTail),
|
||||
string_concat(M,' ', MorseSpace),
|
||||
string_concat(MorseSpace, MorseTail, Morse).
|
||||
morse(H, M),
|
||||
chars2morse(CharTail, MorseTail),
|
||||
string_concat(M,' ', MorseSpace),
|
||||
string_concat(MorseSpace, MorseTail, Morse).
|
||||
|
||||
% space
|
||||
morse(' ', " ").
|
||||
|
|
|
|||
|
|
@ -1,60 +1,60 @@
|
|||
my %m = ' ', '_ _ ',
|
||||
|<
|
||||
! ---.
|
||||
" .-..-.
|
||||
$ ...-..-
|
||||
' .----.
|
||||
( -.--.
|
||||
) -.--.-
|
||||
+ .-.-.
|
||||
, --..--
|
||||
- -....-
|
||||
. .-.-.-
|
||||
/ -..-.
|
||||
: ---...
|
||||
; -.-.-.
|
||||
= -...-
|
||||
? ..--..
|
||||
@ .--.-.
|
||||
[ -.--.
|
||||
] -.--.-
|
||||
_ ..--.-
|
||||
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 --..
|
||||
! ---.
|
||||
" .-..-.
|
||||
$ ...-..-
|
||||
' .----.
|
||||
( -.--.
|
||||
) -.--.-
|
||||
+ .-.-.
|
||||
, --..--
|
||||
- -....-
|
||||
. .-.-.-
|
||||
/ -..-.
|
||||
: ---...
|
||||
; -.-.-.
|
||||
= -...-
|
||||
? ..--..
|
||||
@ .--.-.
|
||||
[ -.--.
|
||||
] -.--.-
|
||||
_ ..--.-
|
||||
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 --..
|
||||
>.map: -> $c, $m is copy {
|
||||
$m.=subst(rx/'-'/, 'BGAAACK!!! ', :g);
|
||||
$m.=subst(rx/'.'/, 'buck ', :g);
|
||||
|
|
|
|||
68
Task/Morse-code/Rebol/morse-code.rebol
Normal file
68
Task/Morse-code/Rebol/morse-code.rebol
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Morse code"
|
||||
file: %Morse_code.r3
|
||||
url: https://rosettacode.org/wiki/Morse_code
|
||||
]
|
||||
|
||||
play-morse: function/with [
|
||||
"Play Morse code"
|
||||
text [string!]
|
||||
][
|
||||
foreach line split-lines text [
|
||||
print rejoin ["^/Transmitting: " line "^/"]
|
||||
foreach c line [
|
||||
code: select morse-map c
|
||||
either code [
|
||||
prin [c ": "]
|
||||
foreach sym code [
|
||||
prin sym
|
||||
either sym = #"." [dot][dash]
|
||||
]
|
||||
print ""
|
||||
wait 0.3
|
||||
][
|
||||
if c = #" " [
|
||||
print "[word gap]"
|
||||
wait 0.3
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
print "^/Done."
|
||||
][
|
||||
;; Morse code dictionary
|
||||
morse-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" "----."
|
||||
]
|
||||
;-- Sound functions...
|
||||
;@@ https://github.com/Oldes/Rebol-MiniAudio
|
||||
audio: import 'miniaudio
|
||||
with audio [
|
||||
;; initialize an audio device...
|
||||
device: init-playback 1
|
||||
;; create a waveform
|
||||
wave: make-waveform-node type_sine 0.5 500.0
|
||||
;; start the sound to be reused for the beep (paused)
|
||||
stop snd: play :wave
|
||||
;; beep function accepting time how long
|
||||
beep: function[time [decimal! time!]][
|
||||
start :snd
|
||||
wait time
|
||||
stop :snd
|
||||
wait 0.1
|
||||
]
|
||||
dot: does[beep 0.1]
|
||||
dash: does[beep 0.3]
|
||||
]
|
||||
]
|
||||
|
||||
play-morse "SOS, Hello Rebol!"
|
||||
|
|
@ -24,21 +24,21 @@ interp alias {} dit {} beep 1
|
|||
interp alias {} dah {} beep 3
|
||||
|
||||
set MORSE_CODE {
|
||||
"!" "---." "\"" ".-..-." "$" "...-..-" "'" ".----."
|
||||
"(" "-.--." ")" "-.--.-" "+" ".-.-." "," "--..--"
|
||||
"-" "-....-" "." ".-.-.-" "/" "-..-."
|
||||
":" "---..." ";" "-.-.-." "=" "-...-" "?" "..--.."
|
||||
"@" ".--.-." "[" "-.--." "]" "-.--.-" "_" "..--.-"
|
||||
"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" "--.."
|
||||
"!" "---." "\"" ".-..-." "$" "...-..-" "'" ".----."
|
||||
"(" "-.--." ")" "-.--.-" "+" ".-.-." "," "--..--"
|
||||
"-" "-....-" "." ".-.-.-" "/" "-..-."
|
||||
":" "---..." ";" "-.-.-." "=" "-...-" "?" "..--.."
|
||||
"@" ".--.-." "[" "-.--." "]" "-.--.-" "_" "..--.-"
|
||||
"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" "--.."
|
||||
}
|
||||
|
||||
# The code to translate text to morse code and play it
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#!/bin/bash
|
||||
# michaeltd 2019-11-29 https://github.com/michaeltd/dots/blob/master/dot.files/.bashrc.d/.var/morse.sh
|
||||
# michaeltd 2019-11-29 https://github.com/michaeltd/dots/blob/master/dot.files/.bashrc.d/.var/morse.sh
|
||||
# https://en.wikipedia.org/wiki/Morse_code
|
||||
# International Morse Code
|
||||
# 1. Length of dot is 1 unit
|
||||
|
|
|
|||
|
|
@ -29,59 +29,59 @@ set wordgap 7
|
|||
|
||||
|
||||
def gap (int n)
|
||||
sleep (* n e)
|
||||
sleep (* n e)
|
||||
end gap
|
||||
decl function off
|
||||
set off gap
|
||||
|
||||
|
||||
def on (int n)
|
||||
snd.beep f (/ (* n e) 1000)
|
||||
snd.beep f (/ (* n e) 1000)
|
||||
end on
|
||||
|
||||
|
||||
def dot ()
|
||||
on 1
|
||||
off 1
|
||||
on 1
|
||||
off 1
|
||||
end dot
|
||||
|
||||
|
||||
def dash ()
|
||||
on 3
|
||||
off 1
|
||||
on 3
|
||||
off 1
|
||||
end dash
|
||||
|
||||
|
||||
def bloop (int n)
|
||||
snd.beep (/ f 2) (/ (* n e) 1000)
|
||||
snd.beep (/ f 2) (/ (* n e) 1000)
|
||||
end bloop
|
||||
|
||||
|
||||
def encode_morse (string text)
|
||||
decl string<> words
|
||||
set words (split (upper (trim text)) " ")
|
||||
decl int i j k
|
||||
for () (< i (size words)) (inc i)
|
||||
for (set j 0) (< j (size words<i>)) (inc j)
|
||||
decl int loc
|
||||
set loc (locate words<i><j> chars)
|
||||
if (= loc -1)
|
||||
bloop 3
|
||||
else
|
||||
for (set k 0) (< k (size morse<loc>)) (inc k)
|
||||
if (= morse<loc><k> "-")
|
||||
dash
|
||||
elif (= morse<loc><k> ".")
|
||||
dot
|
||||
else
|
||||
bloop 3
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
gap chargap
|
||||
end for
|
||||
gap wordgap
|
||||
end for
|
||||
decl string<> words
|
||||
set words (split (upper (trim text)) " ")
|
||||
decl int i j k
|
||||
for () (< i (size words)) (inc i)
|
||||
for (set j 0) (< j (size words<i>)) (inc j)
|
||||
decl int loc
|
||||
set loc (locate words<i><j> chars)
|
||||
if (= loc -1)
|
||||
bloop 3
|
||||
else
|
||||
for (set k 0) (< k (size morse<loc>)) (inc k)
|
||||
if (= morse<loc><k> "-")
|
||||
dash
|
||||
elif (= morse<loc><k> ".")
|
||||
dot
|
||||
else
|
||||
bloop 3
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
gap chargap
|
||||
end for
|
||||
gap wordgap
|
||||
end for
|
||||
end encode_morse
|
||||
|
||||
|
||||
|
|
@ -92,6 +92,6 @@ end encode_morse
|
|||
|
||||
|
||||
while true
|
||||
out "A string to change into morse: " console
|
||||
encode_morse (in string console)
|
||||
out "A string to change into morse: " console
|
||||
encode_morse (in string console)
|
||||
end while
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
const morse_code = [["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", "----."]]
|
||||
["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", "----."]]
|
||||
|
||||
fn main() {
|
||||
str := "this is a test text"
|
||||
mut strmorse := ""
|
||||
for n, _ in str {
|
||||
if str[n].ascii_str() ==" " {strmorse += " "}
|
||||
for m, _ in morse_code {
|
||||
if morse_code[m][0] == str[n].ascii_str() {strmorse += morse_code[m][1] + "|"}
|
||||
}
|
||||
}
|
||||
println(strmorse.all_before_last("|"))
|
||||
str := "this is a test text"
|
||||
mut strmorse := ""
|
||||
for n, _ in str {
|
||||
if str[n].ascii_str() ==" " {strmorse += " "}
|
||||
for m, _ in morse_code {
|
||||
if morse_code[m][0] == str[n].ascii_str() {strmorse += morse_code[m][1] + "|"}
|
||||
}
|
||||
}
|
||||
println(strmorse.all_before_last("|"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue