From 3da3a7f6c5ee83373ef4e6e3232cc5a371137e68 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Sat, 17 Dec 2022 18:37:07 -0600 Subject: Split into multiple files and rewrite README --- README.md | 5 +++-- lambeat.scm | 20 ++++++++++++++++++++ lib.scm | 18 ++++++++++++++++++ main.scm | 41 ----------------------------------------- music.scm | 18 ++++++++++++++++++ 5 files changed, 59 insertions(+), 43 deletions(-) create mode 100644 lambeat.scm create mode 100644 lib.scm delete mode 100644 main.scm create mode 100644 music.scm diff --git a/README.md b/README.md index adaf2e0..035afd2 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/lib.scm b/lib.scm new file mode 100644 index 0000000..66d2a0e --- /dev/null +++ b/lib.scm @@ -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) +)) -- cgit v1.2.3-70-g09d2