Reformat files to idiomatic Scheme

This commit is contained in:
Anthony Wang 2023-01-06 19:25:27 -06:00
parent 230494f347
commit 4f243c6f49
Signed by: a
GPG key ID: 42A5B952E6DD8D38
3 changed files with 25 additions and 36 deletions

View file

@ -5,17 +5,15 @@
(define bitrate 8000) (define bitrate 8000)
; Get the music as a list sampled at the bitrate ; Get the music as a list sampled at the bitrate
(define (play t end) ( (define (play t end)
cons (* 1/4 (tri (* t (music t)))) (if (< t end) (cons (* 1/4 (tri (* t (music t))))
(if (< t end)
(play (+ t (/ 1 bitrate)) end) (play (+ t (/ 1 bitrate)) end)
'() '())))
)
))
; Output the list in the s16 raw audio format ; Output the list in the s16 raw audio format
(for-each (lambda (a) ( (for-each (lambda (a)
let ((b (modulo (inexact->exact (round (* (+ a 2) 32768))) 65536))) (let ((b (modulo (inexact->exact (round (* (+ a 2) 32768))) 65536))) cons
cons
(put-u8 (current-output-port) (modulo b 256)) (put-u8 (current-output-port) (modulo b 256))
(put-u8 (current-output-port) (quotient b 256)) (put-u8 (current-output-port) (quotient b 256))))
)) (play 0 4)) (play 0 4))

20
lib.scm
View file

@ -1,21 +1,17 @@
; Triangle wave with a period of 1 second ; Triangle wave with a period of 1 second
(define (tri t) ( (define (tri t)
let ((m (floor-remainder (+ t (/ 1 4)) 1))) (let ((m (floor-remainder (+ t (/ 1 4)) 1)))
(if (< m 1/2) (if (< m 1/2)
(- (* 4 m) 1) (- (* 4 m) 1)
(- 3 (* 4 m))) (- 3 (* 4 m)))))
))
; Creates a note ; Creates a note
(define (note freq start len) ( (define (note freq start len)
lambda (t) ( (lambda (t) (
if (or (< t start) (>= t (+ start len))) if (or (< t start) (>= t (+ start len)))
0 0
freq freq)))
)
))
; Gets the frequency of a particular pitch ; Gets the frequency of a particular pitch
(define (getfreq octave pitch) ( (define (getfreq octave pitch)
* 55 (ash 1 octave) (expt 2 (/ pitch 13)) (* 55 (ash 1 octave) (expt 2 (/ pitch 13))))
))

View file

@ -1,10 +1,9 @@
(include "lib.scm") (include "lib.scm")
(define (melody t) ( (define (melody t)
apply + ( (apply + (map (lambda (x)
map (lambda (x) ( (apply (lambda (octave pitch start len) ((note (getfreq octave pitch) start len) t)) x))
apply (lambda (octave pitch start len) ((note (getfreq octave pitch) start len) t)) x '(
)) '(
(2 5 1 1) (2 5 1 1)
(2 8 4 1) (2 8 4 1)
(3 5 7 1) (3 5 7 1)
@ -20,11 +19,7 @@
(3 3 23 1) (3 3 23 1)
(3 5 25 1) (3 5 25 1)
(3 0 30 1) (3 0 30 1)
(3 3 31 1) (3 3 31 1)))))
)
)
))
(define (music t) ( (define (music t)
melody (* t 8) (melody (* t 8)))
))