Created
April 4, 2018 22:51
-
-
Save jboynyc/b620590647c2e8a8dc34325cfd1ba1cf to your computer and use it in GitHub Desktop.
robots making sounds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require rsound) | |
(define *midi-notes* (range 38 85)) | |
(define (choice lst) | |
(list-ref lst (random (length lst)))) | |
(define (sublist lst midpoint width) | |
(let ([begin-index (if (> midpoint width) | |
(- midpoint width) | |
0)] | |
[end-index (if (<= midpoint (- (length lst) width)) | |
(+ midpoint width) | |
(length lst))]) | |
(for/list ([i (in-range begin-index end-index)]) | |
(list-ref lst i)))) | |
(define (limited-choice lst prev-elem) | |
(let* ([prev-index (index-of lst prev-elem)] | |
[new-lst (sublist lst prev-index 4)]) | |
(choice new-lst))) | |
(define (play-note note) | |
(play (synth-note "vgame" | |
17 | |
note | |
(/ (default-sample-rate) 4)))) | |
(define-syntax-rule (make-sound-bot bot-name peer) | |
(define bot-name | |
(thread | |
(λ () | |
(let loop () | |
(let ([msg (thread-receive)]) | |
(match msg | |
[number? (let ([chosen-note (limited-choice *midi-notes* msg)]) | |
(play-note chosen-note) | |
(sleep 0.33) | |
(thread-send peer chosen-note) | |
(loop))] | |
['quit null]))))))) | |
(make-sound-bot bot-1 bot-2) | |
(make-sound-bot bot-2 bot-1) | |
(thread-send bot-1 (choice *midi-notes*)) | |
(sleep 90) | |
(kill-thread bot-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment