From 2d4f9323c6e8cadbe8171b36a0d27f41485437d3 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Sat, 21 Jan 2023 21:18:06 -0500 Subject: Fix buffering issue --- lambeat.scm | 14 ++++----- lib.scm | 8 +++++- music.scm | 95 +++++++++++++++++-------------------------------------------- 3 files changed, 39 insertions(+), 78 deletions(-) diff --git a/lambeat.scm b/lambeat.scm index 10b08b5..c6b506b 100644 --- a/lambeat.scm +++ b/lambeat.scm @@ -2,18 +2,16 @@ (include "music.scm") ; Bitrate is the number time to sample the music function each second -(define bitrate 8000) +(define bitrate 44100) ; Get the music as a list sampled at the bitrate (define (play t end) - (cons (music t) + (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) '()))) -; 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 16)) +(play 0 100) diff --git a/lib.scm b/lib.scm index f0f9c6a..0038c58 100644 --- a/lib.scm +++ b/lib.scm @@ -5,12 +5,18 @@ (- (* 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 - freq))) + (* 1/4 (tri (* 1.5 t freq)))))) +; ; Gets the frequency of a particular pitch (define (getfreq octave pitch) diff --git a/music.scm b/music.scm index 51b948d..3680c2a 100644 --- a/music.scm +++ b/music.scm @@ -1,75 +1,32 @@ (include "lib.scm") -; https://musiclab.chromeexperiments.com/Song-Maker/song/5761928473608192 -; https://musiclab.chromeexperiments.com/Song-Maker/song/6414430911070208 -(define (melody t) - (apply + (map (lambda (x) - (apply (lambda (octave pitch start len) ((note (getfreq octave pitch) start len) (- t 32))) x)) - '( - ; (2 5 0 1/4) - ; (3 0 1 1/2) - ; (2 8 3 1/4) - ; (2 7 4 1/2) - ; (2 5 6 1/4) - ; (3 1 7 1/4) - ; (3 0 8 1/4) - - ; (2 5 0 4) - ; (3 0 8 4) - ; (2 3 18 1) - ; (2 10 20 1) - ; (3 0 22 1) - ; (3 1 24 1) - ; (3 0 26 1) - ; (2 10 27 1) - ; (2 5 28 1) - ; (3 0 30 1) - ; (2 5 32 4) - ; (3 0 38 4) - ; (2 3 42 1) - ; (3 1 44 1) - ; (3 1 48 1) - ; (2 10 49 1) - ; (3 0 50 1) - ; (3 1 51 1) - ; (2 10 52 1) - ; (2 8 53 1) - ; (2 10 54 1) - ; (3 0 56 1) - ; (3 1 58 1) - ; (3 0 60 1) - ; (2 8 62 1) - (2 5 1 2) - (3 0 7 2) - (2 10 15 2) - (3 1 19 2) - (3 0 21 2) - (2 10 29 1) - (3 0 30 2) - )))) - -(define (base t) +(define (music t) (apply + (map (lambda (x) (apply (lambda (octave pitch start len) ((note (getfreq octave pitch) start len) t)) x)) '( - (1 5 1 1) - (1 8 4 1) - (2 5 7 1) - (2 0 9 1) - (1 10 10 1) - (1 8 12 1) - (1 7 15 1) - (1 8 17 1) - (1 7 18 1) - (2 3 19 1) - (1 8 21 1) - (2 0 22 1) - (2 3 23 1) - (2 5 25 1) - (2 5 29 1) - (2 0 30 1) - (2 3 31 1))))) + ; opening notes + (2 2 0 .1) + (2 6 .1 .1) + (2 9 .2 .1) + (3 1 .3 1.2) + (2 0 2 .1) + (2 4 2.1 .1) + (2 7 2.2 .1) + (2 11 2.3 .6) + (1 10 3 .1) + (2 2 3.1 .1) + (2 5 3.2 .1) + (2 9 3.3 .6) + (1 7 4 .1) + (1 11 4.1 .1) + (2 2 4.2 .1) + (2 6 4.3 4.2) -(define (music t) - (+ (* 1/2 (tri (* t (melody (floor-remainder (* t 8) 64))))) - (* 1/4 (tri (* t (base (floor-remainder (* t 8) 32))))))) + ; A melody + (2 6 9 2.5) + (2 7 11.5 .5) + (2 6 12 .5) + (2 2 12.5 .5) + (2 9 13 1) + + )))) -- cgit v1.2.3-70-g09d2