Skip to content

Instantly share code, notes, and snippets.

@fuxoft
Created June 21, 2024 10:03
Show Gist options
  • Save fuxoft/dcc200bb237c64fcee8479007df6300b to your computer and use it in GitHub Desktop.
Save fuxoft/dcc200bb237c64fcee8479007df6300b to your computer and use it in GitHub Desktop.
Terminates after 2 seconds, cannot be stopped using Ctrl+C
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
(scl 12)
(load "@lib/math.l")
(def '2pi (+ pi pi))
# Returns 0.01 for note 69.0, i.e. is hardcoded for 44.1 kHz sample rate
(de noteToStep (Note.)
~(assert (> Note. 0.1))
(*/ (pow 2.0 (*/ 1.0 Note. 12.0)) 0.0001858136117 1.0))
(println (noteToStep 69.0) "should be almost" 0.01)
(println (noteToStep 57.0) "should be almost" 0.005)
(de floatToBytes (N)
~(assert (and (<= N 1.0) (>= N -1.0)))
(let (signed (*/ N 32767 1.0) unsigned (if (lt0 signed) (+ 65536 signed) signed))
# (println 'unsigned unsigned)
(cons (& unsigned 255) (>> 8 unsigned))))
(setq masterOutMono
(pipe
(let (Step 0.01 Position 0)
(loop
(pr (sin (*/ 2pi Position 1.0)))
(inc 'Position Step)
(setq Step (*/ Step 0.99999 1.0))
(setq Position (% Position 1.0) ) ) ) ) )
#(for N 4410000 (and (co 'masterOut T)))
(println (floatToBytes 10))
(println (floatToBytes -0.99))
(out
'("aplay" "-Dhw:CARD=SoundCard,DEV=0" "-f" "S16_LE" "-c" "2" "-r" "44100")
(in masterOutMono
(for N 90000
(let bytes (floatToBytes (rd))
#(println bytes)
(wr (car bytes) (cdr bytes))
(wr (car bytes) (cdr bytes)) ) ) ) )
(bye)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment