From 32fe925b3c88052cc41566585db3e6e0e09ccbf6 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Wed, 28 Aug 2024 17:43:44 -0500 Subject: [PATCH] Added -r flag to allow repeating sections like numbers stations typically do --- numberstation.go | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/numberstation.go b/numberstation.go index 73515f5..4111c44 100644 --- a/numberstation.go +++ b/numberstation.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" "time" + "flag" "github.com/chzyer/readline" "github.com/gopxl/beep" @@ -22,6 +23,10 @@ const ( ) func main() { + // Define and parse the -r flag + repeat := flag.Bool("r", false, "Repeat each section separated by space, '-', or '_'") + flag.Parse() + // Create a map to store sound data for each character soundMap := make(map[rune][]byte) @@ -75,18 +80,39 @@ func main() { input := inputBuilder.String() input = strings.ToLower(strings.TrimSpace(input)) - for _, c := range input { - if c == '-' || c == '_' || c == ' ' || c == '\n' { - time.Sleep(longPause) - } else { - if soundData, ok := soundMap[c]; ok { - playSound(soundData) - } else { - fmt.Printf("Invalid character (skipping): %c\n", c) - } - time.Sleep(pauseBetweenCharacters) + // Split the input into sections and process each section + sections := strings.FieldsFunc(input, func(r rune) bool { + return r == ' ' || r == '-' || r == '_' + }) + + fmt.Println("Playback starting...") + for _, section := range sections { + // Determine the number of times to read each section + numRepeats := 1 + if *repeat { + numRepeats = 2 } + + // Read and optionally repeat each section + for i := 0; i < numRepeats; i++ { + for _, c := range section { + if soundData, ok := soundMap[c]; ok { + playSound(soundData) + } else { + fmt.Printf("Invalid character (skipping): %c\n", c) + } + time.Sleep(pauseBetweenCharacters) + } + // Pause between repeats if repeating + if *repeat && i == 0 { + time.Sleep(longPause) + } + } + // Long pause between sections + time.Sleep(longPause) } + + fmt.Println("Playback finished. Exiting.") } // Function to play sound