Funny :w moments

Signed-off-by: Skylar "The Cobra" Widulski <cobra@vern.cc>
This commit is contained in:
Skylar "The Cobra" Widulski 2023-12-21 21:58:18 -05:00
parent e8aa120b31
commit 63c3ef1135
Signed by: cobra
GPG Key ID: 4FD8F812083FF6F9
1 changed files with 0 additions and 193 deletions

193
\
View File

@ -1,193 +0,0 @@
;;;
;;; Copyright (C) 2023 Skylar Widulski
;;;
;;; This file is part of Yammer.
;;;
;;; Yammer is free software: you can redistribute it and/or modify it under the
;;; terms of the GNU General Public License as published by the Free Software
;;; Foundation, either version 3 of the License, or (at your option) any later
;;; version.
;;;
;;; Yammer is distributed in the hope that it will be useful, but WITHOUT ANY
;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;;; details.
;;;
;;; You should have received a copy of the GNU General Public License along with
;;; Yammer. If not, see <https://www.gnu.org/licenses/>.
;;;
(define-module (yammer)
#:declarative? #f
#:use-module (sdl2)
#:use-module (sdl2 blend-mode)
#:use-module (sdl2 events)
#:use-module (sdl2 render)
#:use-module (sdl2 rect)
#:use-module (sdl2 surface)
#:use-module (sdl2 video)
#:use-module (ice-9 binary-ports)
#:use-module (ice-9 futures)
#:use-module (ice-9 receive)
#:use-module (ice-9 threads)
#:use-module (ice-9 q)
#:use-module (rnrs bytevectors)
#:use-module (scheme base)
#:use-module (srfi srfi-1)
#:use-module (system foreign)
#:use-module (yammer fftw))
(add-to-load-path ".")
(define moving-average 0)
(load "config.scm")
(define recip-fps (/ 1 fps))
(define sample-size (* 4 (round (* 3 (* (/ 44100 4) recip-fps)))))
(define vsize (exact (/ sample-size 4)))
(define plan #f)
(if fft (monitor (set! plan (init_plan vsize))))
(define window #f)
(define win-id #f)
(define renderer #f)
(define bv (make-bytevector sample-size))
(define queue (make-q))
(define (chunk2 lst)
(if (>= (length lst) 2)
(cons (take lst 2)
(chunk2 (drop lst 2)))
'()))
(define (open-fifo filename)
(define fd (open-input-file filename))
(fcntl fd F_SETFL (logior O_NONBLOCK
(fcntl fd F_GETFL)))
(setvbuf fd 'block sample-size)
fd)
(define (read-fifo fd)
(if (port-closed? fd)
(set! fd (open-fifo fifo-file)))
(define new-bv #vu8())
(define bytes-read 0)
(while (< bytes-read sample-size)
(set! new-bv (bytevector-append new-bv (bytevector (read-u8 fd))))
(set! bytes-read (1+ bytes-read))
(set! new-bv (bytevector-append
new-bv (get-bytevector-n fd (- sample-size bytes-read))))
(set! bytes-read (bytevector-length new-bv)))
(enq! queue new-bv)
(if (>= (q-length queue) queue-size)
(set! bv (deq! queue))))
(define (draw ren plst)
(clear-renderer ren)
(define init-surf #f)
(define surf #f)
(receive (x y) (window-size window)
(set! init-surf (make-rgb-surface x y 32))
(set! surf (convert-surface-format init-surf 'rgba8888))
(delete-surface! init-surf)
(fill-rect surf #nil color)
(let ((y-mid (round/ y 2)))
(do ((i 0 (+ i (/ x (length plst))))
(j 0 (1+ j)))
((>= j (length plst)))
(begin
(fill-rect
surf
(make-rect
(floor i) 0
(ceiling/ x (length plst))
(- y-mid (abs (round/ (* y (car (list-ref plst j)))
(/ 76800
(if (= smoothing-mode moving-average)
(/ i scale)
scale))))))
bgcolor)
(let ((n (abs (round/ (* y (cadr (list-ref plst j)))
(/ 76800
(if (= smoothing-mode moving-average)
(/ i scale)
scale))))))
(fill-rect
surf
(make-rect
(floor i) (+ y-mid n)
(ceiling/ x (length plst))
(- y-mid n))
bgcolor))))))
(define texture (surface->texture ren surf))
(render-copy ren texture)
(present-renderer ren)
(delete-texture! texture)
(delete-surface! surf))
(define (smooth lst type)
(define ret '())
(define window 0)
(cond
((= type moving-average)
(do ((i 1 (1+ i)))
((>= i (- (length lst) moving-average-block-size)))
(set! window 0)
(do ((j 0 (1+ j)))
((>= j moving-average-block-size))
(set! window (+ window (list-ref lst (+ i j)))))
(set! window (round/ window moving-average-block-size))
(set! ret (append ret (list window))))))
ret)
(define (lp ren fft?)
(define read-future #f)
(define sleep-future #f)
(define plst '())
(while (not (window-closed-event? (poll-event)))
(set! sleep-future (future (usleep (round (* recip-fps 1000)))))
(if (= (bytevector-length bv) sample-size)
(begin
(set! plst (chunk2 (bytevector->sint-list
bv (native-endianness) 2)))
(set! read-future (future (read-fifo fd)))
(if fft?
(if (not (equal? smoothing-mode #f))
(draw ren
(map list
(smooth (s16vector->list
(do-dft plan (map car plst)
(length plst)))
smoothing-mode)
(smooth (s16vector->list
(do-dft plan (map cadr plst)
(length plst)))
smoothing-mode)))
(draw ren
(map list
(s16vector->list
(do-dft plan (map car plst)
(length plst)))
(s16vector->list
(do-dft plan (map cadr plst)
(length plst))))))
(draw ren plst))))
(touch sleep-future)))
(define fd (open-fifo fifo-file))
(sdl-init '(video events))
(set! window (make-window #:title "Yammer"))
(set! win-id (window-id window))
(set-window-resizable! window #t)
(set! renderer (make-renderer window))
(lp renderer fft)
(delete-renderer! renderer)
(close-window! window)
(sdl-quit)
(close-port fd)
(if fft (monitor (fftw_destroy_plan plan)))