yue/yue.scm

38 lines
1,007 B
Scheme
Raw Normal View History

2023-03-17 02:59:41 +00:00
(use-modules (ice-9 binary-ports))
; Bitrate is the number time to sample the music function each second
(define bitrate 8000)
2023-01-07 01:09:14 +00:00
; Triangle wave with a period of 1 second
2023-01-07 01:25:27 +00:00
(define (tri t)
(let ((m (floor-remainder (+ t (/ 1 4)) 1)))
(if (< m 1/2)
(- (* 4 m) 1)
2023-01-07 01:25:27 +00:00
(- 3 (* 4 m)))))
2023-01-22 02:18:06 +00:00
; Square wave with a period of 1 second
(define (square t)
(let ((m (floor-remainder t 1)))
(if (< m 0.5) 1 -1)))
2022-12-18 05:00:16 +00:00
; Creates a note
2023-01-07 01:25:27 +00:00
(define (note freq start len)
(lambda (t) (
if (or (< t start) (>= t (+ start len)))
0
2023-01-22 02:58:22 +00:00
(* 1/4 (tri (* t freq))))))
2022-12-18 05:00:16 +00:00
; Gets the frequency of a particular pitch
2023-01-07 01:25:27 +00:00
(define (getfreq octave pitch)
2023-01-07 02:13:43 +00:00
(* 55 (ash 1 octave) (expt 2 (/ pitch 12))))
2023-03-17 02:59:41 +00:00
; Get the music as a list sampled at the bitrate
(define (play t end)
(cons ((lambda (a)
(let ((b (modulo (inexact->exact (round (* (+ a 2) 32768))) 65536))) cons
(put-u8 (current-output-port) (modulo b 256))
(put-u8 (current-output-port) (quotient b 256)))) (music t))
(if (< t end)
(play (+ t (/ 1 bitrate)) end)
'())))