Use apply and map for melody function

This commit is contained in:
Anthony Wang 2022-12-17 23:00:16 -06:00
parent 3da3a7f6c5
commit 55b6ce6d80
Signed by: a
GPG Key ID: 42A5B952E6DD8D38
3 changed files with 17 additions and 13 deletions

View File

@ -2,4 +2,4 @@
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.
## 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 -`!
First, install [Sox](https://sox.sourceforge.net/) and clone this repo. Write some music in `music.scm`. Enjoy your music with `guile --fresh-auto-compile lambeat.scm | play -r 8000 -t s16 -`!

View File

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

View File

@ -1,18 +1,19 @@
(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 (melody t) (
apply + (map
(lambda (octave pitch start len) ((note (getfreq octave pitch) start len) t))
;'(3)
;(cons (* 3 (sin t)) '())
;'(0)
;'(6.28)
'(3 3 3 3 3 3 3 3 3 3)
'(4 8 4 11 4 2 8 4 11 4)
'(0 1/4 3/4 1 5/4 3/2 7/4 9/4 5/2 11/4)
'(1/4 1/4 1/4 1/4 1/4 1/4 1/4 1/4 1/4 1/4)
)
))
(define (music t) (
melody (floor-remainder t 3)
melody (floor-remainder t 6.28)
))