Split into multiple files and rewrite README

This commit is contained in:
Anthony Wang 2022-12-17 18:37:07 -06:00
parent f11992a8fc
commit 3da3a7f6c5
Signed by: a
GPG key ID: 42A5B952E6DD8D38
5 changed files with 59 additions and 43 deletions

View file

@ -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 -`!

20
lambeat.scm Normal file
View file

@ -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))

18
lib.scm Normal file
View file

@ -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))
))

View file

@ -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))

18
music.scm Normal file
View file

@ -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)
))