⋯ 2 hidden lines@@ -3,47 +3,40 @@ package vkbd 3 import (3 import (4 "math/rand/v2" 4 "math/rand/v2" 5 "time" 5 "time" 6 - "unicode" 7 ) 6 ) 8 7 9 - // Natural typing goes word by word: the letters of a word come quickly and 8 + // Natural typing follows how a sentence is read aloud: the words of a 10 - // steadily, the pauses fall between words and grow at punctuation. Every 9 + // phrase come in one steady flow, spaces included, and the pauses fall only 11 - // pause is drawn at random, so no two dictations type the same way. 10 + // at punctuation. Each phrase gets its own tempo, drawn again after every 11 + // punctuation mark, and every pause is drawn on its own, so no two phrases 12 + // and no two dictations type the same way. 12 const (13 const (13 - letterMin = 4 * time.Millisecond // inside a word 14 + letterMin = 4 * time.Millisecond // letters and spaces, at tempo 1 14 - letterMax = 9 * time.Millisecond 15 + letterMax = 9 * time.Millisecond 15 - wordMin = 30 * time.Millisecond // after a space 16 + tempoMin = 0.6 // phrase speed factor: below 1 is faster 16 - wordMax = 110 * time.Millisecond 17 + tempoMax = 1.7 17 - commaMin = 140 * time.Millisecond // after , ; : 18 + commaMin = 140 * time.Millisecond // after , ; : 18 - commaMax = 280 * time.Millisecond 19 + commaMax = 280 * time.Millisecond 19 - sentenceMin = 260 * time.Millisecond // after . ! ? … 20 + sentenceMin = 260 * time.Millisecond // after . ! ? … 20 - sentenceMax = 480 * time.Millisecond 21 + sentenceMax = 480 * time.Millisecond 21 - hesitOdds = 0.08 // chance to hesitate before a long word 22 - hesitMin = 90 * time.Millisecond 23 - hesitMax = 220 * time.Millisecond 24 - longWordRune = 8 25 ) 22 ) 26 23 27 // natural returns the pause after each rune of text. No pause goes below 24 // natural returns the pause after each rune of text. No pause goes below 28 - // floor. When the total exceeds limit (0: no limit), letters speed up 25 + // floor. When the total exceeds limit (0: no limit), everything speeds up in 29 - // first, then the pauses between words shrink, so the rhythm survives. 26 + // the same proportion, so the rhythm survives. 30 func natural(text []rune, floor, limit time.Duration, rnd *rand.Rand) []time.Duration {27 func natural(text []rune, floor, limit time.Duration, rnd *rand.Rand) []time.Duration {31 letters := make([]time.Duration, len(text)) 28 letters := make([]time.Duration, len(text)) 32 pauses := make([]time.Duration, len(text)) 29 pauses := make([]time.Duration, len(text)) 33 - // The pause after a word's last letter comes from the punctuation or 30 + tempo := newTempo(rnd) 34 - // space that follows: pauses belong to the rune they follow. 35 for i, r := range text { 31 for i, r := range text { 36 - letters[i] = between(rnd, letterMin, letterMax) 32 + letters[i] = time.Duration(float64(between(rnd, letterMin, letterMax)) * tempo) 37 - switch { 33 + switch r { 38 - case r == ',' || r == ';' || r == ':' : 34 + case ',' , ';' , ':' : 39 pauses[i] = between(rnd, commaMin, commaMax) 35 pauses[i] = between(rnd, commaMin, commaMax) 40 - case r == '.' || r == '!' || r == '?' || r == '…' : 36 + tempo = newTempo(rnd) // the next phrase 37 + case '.' , '!' , '?' , '…' : 41 pauses[i] = between(rnd, sentenceMin, sentenceMax) 38 pauses[i] = between(rnd, sentenceMin, sentenceMax) 42 - case unicode.IsSpace(r) && !afterPunct(text, i): 39 + tempo = newTempo(rnd) 43 - pauses[i] = between(rnd, wordMin, wordMax) 44 - if wordLen(text, i+1) >= longWordRune && rnd.Float64() < hesitOdds { 45 - pauses[i] += between(rnd, hesitMin, hesitMax) 46 - } 47 } 40 } 48 } 41 } 49 // Each key waits floor + letter + pause; letters and pauses only 42 // Each key waits floor + letter + pause; letters and pauses only ⋯ 3 hidden lines@@ -53,10 +46,14 @@ func natural(text []rune, floor, limit time.Duration, rnd *rand.Rand) []time.Dur 53 letters[i] = max(letters[i]-floor, 0) 46 letters[i] = max(letters[i]-floor, 0) 54 } 47 } 55 if limit > 0 { 48 if limit > 0 { 49 + // Letters and pauses shrink together, so the tempo of each phrase 50 + // and the length of each pause keep their proportions. 56 room := limit - time.Duration(n)*floor 51 room := limit - time.Duration(n)*floor 57 - // Letters give way first, then the pauses, so the rhythm survives. 52 + if total := sum(letters[:n]) + sum(pauses[:n]); total > room { 58 - fit(letters[:n], room-sum(pauses[:n])) 53 + s := float64(max(room, 0)) / float64(total) 59 - fit(pauses[:n], room-sum(letters[:n])) 54 + scale(letters[:n], s) 55 + scale(pauses[:n], s) 56 + } 60 } 57 } 61 out := make([]time.Duration, len(text)) 58 out := make([]time.Duration, len(text)) 62 for i := range n { 59 for i := range n {