aboutsummaryrefslogtreecommitdiff
path: root/yue.scm
diff options
context:
space:
mode:
authorAnthony Wang2023-03-16 22:59:41 -0400
committerAnthony Wang2023-03-16 22:59:41 -0400
commit0da46c8a33e7a9b61b564cce5c4c4526fe3d84dc (patch)
tree1c3948cdb3a6cd6119f2ede76279cde26e464eba /yue.scm
parent46911993234b7c9cc07546f56843b35621b7f811 (diff)
Clean up code and rename to yue
Diffstat (limited to 'yue.scm')
-rw-r--r--yue.scm37
1 files changed, 37 insertions, 0 deletions
diff --git a/yue.scm b/yue.scm
new file mode 100644
index 0000000..6b692ab
--- /dev/null
+++ b/yue.scm
@@ -0,0 +1,37 @@
+(use-modules (ice-9 binary-ports))
+
+; Bitrate is the number time to sample the music function each second
+(define bitrate 8000)
+
+; Triangle wave with a period of 1 second
+(define (tri t)
+ (let ((m (floor-remainder (+ t (/ 1 4)) 1)))
+ (if (< m 1/2)
+ (- (* 4 m) 1)
+ (- 3 (* 4 m)))))
+
+; Square wave with a period of 1 second
+(define (square t)
+ (let ((m (floor-remainder t 1)))
+ (if (< m 0.5) 1 -1)))
+
+; Creates a note
+(define (note freq start len)
+ (lambda (t) (
+ if (or (< t start) (>= t (+ start len)))
+ 0
+ (* 1/4 (tri (* t freq))))))
+
+; Gets the frequency of a particular pitch
+(define (getfreq octave pitch)
+ (* 55 (ash 1 octave) (expt 2 (/ pitch 12))))
+
+; 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)
+ '())))