diff options
author | Anthony Wang | 2022-12-17 18:37:07 -0600 |
---|---|---|
committer | Anthony Wang | 2022-12-17 18:37:07 -0600 |
commit | 3da3a7f6c5ee83373ef4e6e3232cc5a371137e68 (patch) | |
tree | 454427a3f00f9e34e06362f03b3441b825989b19 | |
parent | f11992a8fca36b7be77c7225a0007468bf6a29cb (diff) |
Split into multiple files and rewrite README
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | lambeat.scm | 20 | ||||
-rw-r--r-- | lib.scm | 18 | ||||
-rw-r--r-- | main.scm | 41 | ||||
-rw-r--r-- | music.scm | 18 |
5 files changed, 59 insertions, 43 deletions
@@ -1,4 +1,5 @@ # Lambeat -Bytebeat in Scheme +Lambeat is a new way to make music using functional programming. It's heavily influenced by [Bytebeat](https://dollchan.net/bytebeat) and initially started out as an reimplementation of Bytebeat in Scheme. Since then, it's grown to be a delightful new way to make music with code. -You can use `guile main.scm | play -r 8000 -t s16 -v 0.25 -` to try it out. +## Get started +First, install [Sox](https://sox.sourceforge.net/) and clone this repo. Write some music in `music.scm`. Enjoy your music with `guile lambeat.scm | play -r 8000 -t s16 -`! diff --git a/lambeat.scm b/lambeat.scm new file mode 100644 index 0000000..ac3bb76 --- /dev/null +++ b/lambeat.scm @@ -0,0 +1,20 @@ +(use-modules (ice-9 binary-ports)) +(include "music.scm") + +(define bitrate 8000) + +; Get the music as a list sampled at the bitrate +(define (play t) ( + cons (music t) (if (< t 30) + (play (+ t (/ 1 bitrate))) + '() + ) +)) + +; Output the list in the s16 raw audio format +(for-each (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)) +)) (play 0)) @@ -0,0 +1,18 @@ +(define (saw t) ( + let ((m (floor-remainder (+ t (/ 1 4)) 1))) + (if (< m 1/2) + (- (* 4 m) 1) + (- 3 (* 4 m))) +)) + +(define (note freq start len) ( + lambda (t) ( + if (or (< t start) (>= t (+ start len))) + 0 + (saw (* freq t)) + ) +)) + +(define (getfreq octave pitch) ( + * 55 (ash 1 octave) (expt 2 (/ pitch 13)) +)) diff --git a/main.scm b/main.scm deleted file mode 100644 index a3e3788..0000000 --- a/main.scm +++ /dev/null @@ -1,41 +0,0 @@ -(use-modules (ice-9 binary-ports)) - -(define bitrate 8000) - -(define (saw t) ( - let ((m (floor-remainder (+ t (/ 1 4)) 1))) - (if (< m 1/2) - (- (* 4 m) 1) - (- 3 (* 4 m))) -)) - -(define (note freq start len) ( - lambda (t) ( - if (or (< t start) (>= t (+ start len))) - 0 - (saw (* freq t)) - ) -)) - -(define (music t) (+ - ((note 523.25 0 1) t) - ((note 587.33 1 1) t) - ((note 659.25 2 1) t) - ((note 698.46 3 1) t) - ((note 783.99 4 1) t) -)) - -(define (play t) ( - cons (music t) (if (< t 5) - (play (+ t (/ 1 bitrate))) - '() - ) -)) - -;(display (play 0)) -(for-each (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)) -)) (play 0)) diff --git a/music.scm b/music.scm new file mode 100644 index 0000000..a45111f --- /dev/null +++ b/music.scm @@ -0,0 +1,18 @@ +(include "lib.scm") + +(define (melody t) (+ + ((note (getfreq 3 4) 0 1/4) t) + ((note (getfreq 3 8) 1/4 1/4) t) + ((note (getfreq 3 4) 3/4 1/4) t) + ((note (getfreq 3 11) 1 1/4) t) + ((note (getfreq 3 4) 5/4 1/4) t) + ((note (getfreq 3 2) 3/2 1/4) t) + ((note (getfreq 3 8) 7/4 1/4) t) + ((note (getfreq 3 4) 9/4 1/4) t) + ((note (getfreq 3 11) 5/2 1/4) t) + ((note (getfreq 3 4) 11/4 1/4) t) +)) + +(define (music t) ( + melody (floor-remainder t 3) +)) |