Skip to content

Instantly share code, notes, and snippets.

@altbodhi
Created October 15, 2023 13:19
Show Gist options
  • Save altbodhi/04e65d44aaa67b92bb4e7bccff01ae07 to your computer and use it in GitHub Desktop.
Save altbodhi/04e65d44aaa67b92bb4e7bccff01ae07 to your computer and use it in GitHub Desktop.
Simple timer for zen meditation written on F#
module ZenUtil
#r "nuget: NAudio"
#r "nuget: System.Speech"
open NAudio.Wave
open System
module WindowsSpeech =
open System.Speech
let speak (text :string) =
use speak = new Synthesis.SpeechSynthesizer()
speak.Speak text
module Utils =
open System.Diagnostics
let exec program args =
let startInfo = ProcessStartInfo(
FileName = program,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
CreateNoWindow = true,
Arguments = args)
Process.Start(startInfo).WaitForExit()
module ESpeak =
let speak text =
Utils.exec "espeak" $"-v ru {text}"
let isWindows = Environment.OSVersion.Platform = PlatformID.Win32NT
let speak text =
if isWindows then
WindowsSpeech.speak text
else ESpeak.speak text
let url = "http://www.kwanumzen.sk/img/sound.mp3"
let playSound url =
async {
use mf = new MediaFoundationReader(url)
use wo = new WasapiOut()
wo.Init(mf)
wo.Play()
while (wo.PlaybackState = PlaybackState.Playing) do
do! Async.Sleep 100
}
let play url =
if isWindows then
playSound url
else async { do Utils.exec "mpv" url }
let display (i: int, count) =
let time = DateTime.Now.ToString("HH:mm")
let position = i.ToString("00")
let remain = (count - i).ToString("00")
printfn $"{time} {position} {remain}"
let doIt url count pauseSec =
async {
speak "Начали."
do! Async.Sleep 1000
for i = 1 to count do
display (i, count)
do! play url
do! Async.Sleep(1000 * pauseSec)
do! play url
speak "Закончили."
}
|> Async.RunSynchronously
let smallZen () = doIt url 10 60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment