Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1. (require rsound)
  2. (require 2htdp/universe)
  3.  
  4. ;;; PRE PROCESSING ;;;
  5.  
  6. (define TICK_RATE 1/40)
  7. (define SAMPLE (resample-to-rate FRAME-RATE (rs-read "sample.wav")))
  8. (define FRAMES (rs-frames SAMPLE))
  9. (define NUMBER_OF_INTERVALS (floor (/ FRAMES (* TICK_RATE FRAME-RATE))))
  10. (define INTERVAL_LENGTH (* TICK_RATE FRAME-RATE))
  11. (define TRACK (rs-append SAMPLE (silence 48000))) ; might not be needed, but the padding might be necessary. keep for now just in case
  12.  
  13. ;;; HELPER FUNCTIONS ;;;
  14.  
  15. ; return the frame value of the amount of seconds given
  16. ; Number -> Natural Number
  17. (define (sec x) (* x FRAME-RATE))
  18. (check-expect (sec 1) FRAME-RATE)
  19. (check-expect (sec 2) (* FRAME-RATE 2))
  20.  
  21. ; return the tick rate times a given value
  22. ; Number -> Number
  23. (define (interval x) (* x TICK_RATE))
  24. (check-expect (interval 1) TICK_RATE)
  25. (check-expect (interval 2) (* 2 TICK_RATE))
  26.  
  27.  
  28. ; return the sum of the squares of all rs-ith values in the given frame interval
  29. ; Natural Number, Natural Number -> Number
  30. (define (sum-of-squares upper lower)
  31. (cond [(<= upper lower) 0]
  32. [else
  33. (+ (expt (rs-ith/left SAMPLE upper) 2) (sum-of-squares (- upper 1) lower))
  34. ]
  35. ))
  36. (check-within (sum-of-squares (sec 10) (sec 9)) 1703 10)
  37.  
  38. ; a list of intervals has a length of NUMBER_OF_INTERVALS
  39. ; the 0th entry is the sum of squares between (1/20 * FRAMES) and (0/20 * FRAMES)
  40. ; the 1st entry is the sum of squares between (2/20 * FRAMES) and (1/20 * FRAMES)
  41. ; the 2nd entry is the sum of squares between (3/20 * FRAMES) and (2/20 * FRAMES)
  42. ; the 3rd entry is the sum of squares between (4/20 * FRAMES) and (3/20 * FRAMES)
  43. ; and so on
  44.  
  45. ; a list-of-intervals is one of:
  46. ; - '()
  47. ; - (cons Number list-of-intervals)
  48. ; ex:
  49. (cons 5 '())
  50.  
  51. ; Task: develop a function that will return a list of intervals that follows the above pattern
  52.  
  53. ; generate a list of intervals of length NUMBER_OF_INTERVALS - ***NUMBER_OF_INTERVALS IS THE ONLY ACCEPTABLE ARGUMENT***
  54. ; NUMBER_OF_INTERVALS -> list-of-intervals
  55. (define (genIntervalList max)
  56. (cond [(= max 0) '()]
  57. [else
  58. (cons (sum-of-squares (floor (* FRAME-RATE max TICK_RATE)) (floor (* FRAME-RATE (- max 1) TICK_RATE))) (genIntervalList (- max 1)))
  59. ]
  60. ))
  61.  
  62. (define TRACK_INTERVAL_LIST (genIntervalList NUMBER_OF_INTERVALS))
  63.  
  64.  
  65. ;;; END OF PRE-PROCESSING ;;;
  66.  
  67. ;;; playing sound stuff ;;;
  68.  
  69. (define PLAY_SECONDS 1/20)
  70. (define PLAY_FRAMES (* PLAY_SECONDS FRAME-RATE))
  71. (define PLAY_POSNFRAC (/ PLAY_SECONDS (/ (rs-frames SAMPLE) FRAME-RATE)))
  72. (define MAX_QUEUE_INTERVAL (* 3/80 FRAME-RATE))
  73. (define STREAM (make-pstream))
  74.  
  75. ;;; Drawing Stuff ;;;
  76.  
  77. ;;; button box stuff ;;;
  78. (define BUTTON_BOX_WIDTH 1080)
  79. (define BUTTON_BOX_HEIGHT 90)
  80. (define BUTTON_BOX (rectangle BUTTON_BOX_WIDTH
  81. BUTTON_BOX_HEIGHT
  82. "outline"
  83. "black"))
  84.  
  85. ;;; button stuff ;;;
  86. (define BUTTON_WIDTH 150)
  87. (define BUTTON_HEIGHT 80)
  88.  
  89. ; make a button array
  90. ; mode -> Image
  91. (define (buttons mode)
  92. (cond [(= mode 1)
  93. (beside
  94. (rectangle BUTTON_WIDTH
  95. BUTTON_HEIGHT
  96. "solid"
  97. "green")
  98. (rectangle BUTTON_WIDTH
  99. BUTTON_HEIGHT
  100. "solid"
  101. "red")
  102. (rectangle BUTTON_WIDTH
  103. BUTTON_HEIGHT
  104. "solid"
  105. "red")
  106. (rectangle BUTTON_WIDTH
  107. BUTTON_HEIGHT
  108. "solid"
  109. "red")
  110. )
  111. ]
  112. [(= mode 2)
  113. (beside
  114. (rectangle BUTTON_WIDTH
  115. BUTTON_HEIGHT
  116. "solid"
  117. "red")
  118. (rectangle BUTTON_WIDTH
  119. BUTTON_HEIGHT
  120. "solid"
  121. "green")
  122. (rectangle BUTTON_WIDTH
  123. BUTTON_HEIGHT
  124. "solid"
  125. "red")
  126. (rectangle BUTTON_WIDTH
  127. BUTTON_HEIGHT
  128. "solid"
  129. "red")
  130. )
  131. ]
  132. [(= mode 3)
  133. (beside
  134. (rectangle BUTTON_WIDTH
  135. BUTTON_HEIGHT
  136. "solid"
  137. "red")
  138. (rectangle BUTTON_WIDTH
  139. BUTTON_HEIGHT
  140. "solid"
  141. "red")
  142. (rectangle BUTTON_WIDTH
  143. BUTTON_HEIGHT
  144. "solid"
  145. "green")
  146. (rectangle BUTTON_WIDTH
  147. BUTTON_HEIGHT
  148. "solid"
  149. "red")
  150. )
  151. ]
  152. [(= mode 4)
  153. (beside
  154. (rectangle BUTTON_WIDTH
  155. BUTTON_HEIGHT
  156. "solid"
  157. "red")
  158. (rectangle BUTTON_WIDTH
  159. BUTTON_HEIGHT
  160. "solid"
  161. "red")
  162. (rectangle BUTTON_WIDTH
  163. BUTTON_HEIGHT
  164. "solid"
  165. "red")
  166. (rectangle BUTTON_WIDTH
  167. BUTTON_HEIGHT
  168. "solid"
  169. "green")
  170. )
  171. ]
  172. ))
  173.  
  174. ;;; visualizer box stuff ;;;
  175. (define VISUALIZER_BOX_WIDTH 1080)
  176. (define VISUALIZER_BOX_HEIGHT 560)
  177. (define VISUALIZER_BOX (rectangle VISUALIZER_BOX_WIDTH
  178. VISUALIZER_BOX_HEIGHT
  179. "outline"
  180. "black"))
  181.  
  182. ;;; slider box stuff ;;;
  183. (define SLIDER_BOX_WIDTH 1080)
  184. (define SLIDER_BOX_HEIGHT 90)
  185. (define SLIDER_BOX (rectangle SLIDER_BOX_WIDTH
  186. SLIDER_BOX_HEIGHT
  187. "outline"
  188. "black"))
  189.  
  190. ;;; slider stuff ;;;
  191. (define SLIDER_HEIGHT 160)
  192. (define SLIDER_WIDTH 9)
  193. (define SLIDER (rectangle SLIDER_WIDTH
  194. SLIDER_HEIGHT
  195. "solid"
  196. "black"))
  197.  
  198.  
  199. ; a posn-list is one of:
  200. ; - '()
  201. ; - (cons (make-posn x y) posn-list)
  202. ; ex:
  203. (cons (make-posn 5 10) '())
  204.  
  205. ; a vstate is a (make-vstate mode slider-frac last-frame points current-sample index)
  206. ; where mode is a Number between 1 and 4
  207. ; and slider-frac is a Number between 0 and 1
  208. ; and last-frame is a Natural Number
  209. ; and points is a posn-list
  210. ; and current-sample is a Number
  211. ; and index is a number
  212. (define-struct vstate (mode slider-frac last-frame points current-sample index))
  213. ; ex:
  214. (make-vstate 1 0.0 0 (cons (make-posn 5 10) '()) 0.0 0)
  215. (define INIT (make-vstate 1
  216. 0.0
  217. 0
  218. '()
  219. (list-ref TRACK_INTERVAL_LIST 0)
  220. 0))
  221.  
  222. ; switch the mode of the given vs to the given number
  223. ; vs number -> vs
  224. (define (vstate-switch-mode vs n)
  225. (make-vstate n
  226. (vstate-slider-frac vs)
  227. (vstate-last-frame vs)
  228. (vstate-points vs)
  229. (vstate-current-sample vs)
  230. (vstate-index vs)))
  231.  
  232. ; reset the slider frac and index of the given vs to 0
  233. ; vs -> vs
  234. (define (vstate-reset-slider-frac&index&points vs)
  235. (make-vstate (vstate-mode vs)
  236. 0.0
  237. (vstate-last-frame vs)
  238. '()
  239. (vstate-current-sample vs)
  240. 0))
  241.  
  242. ; increment index by 1
  243. ; vs -> vs
  244. (define (vstate-index+ vs)
  245. (make-vstate (vstate-mode vs)
  246. (vstate-slider-frac vs)
  247. (vstate-last-frame vs)
  248. (vstate-points vs)
  249. (list-ref TRACK_INTERVAL_LIST (vstate-index vs))
  250. (+ 1 (vstate-index vs))))
  251.  
  252. ; a Signal is a number between
  253. ; -1 and 1
  254. ; representing audio
  255.  
  256. ; get percentage from sum of squares of signals
  257. ; Number -> Number
  258. (define (sumofsquares% s)
  259. (* (/ s (* FRAME-RATE TICK_RATE 10)) 100)
  260. )
  261.  
  262. ;;; TASK: Develop a function that accepts a list of posns and draws them all
  263.  
  264. ; a list-of-posns is one of
  265. ; - '()
  266. ; - (cons (make-posn x y) list-of-posns)
  267. ; example
  268. (cons (make-posn 10 8) '())
  269.  
  270. ; draw the entirety of a list of posns onto a surface
  271. ; list-of-posns -> Image
  272. (define (drawPosns lop)
  273. (cond [(empty? lop) (add-line VISUALIZER_BOX 1 1 1 1 "black")]
  274. [else
  275. (if (empty? (rest lop)) (add-line VISUALIZER_BOX 1 1 1 1 "black")
  276. (place-image
  277. (add-line VISUALIZER_BOX (posn-x (first lop)) (posn-y (first lop)) (posn-x (first (rest lop))) (posn-y (first (rest lop))) "black")
  278. (/ VISUALIZER_BOX_WIDTH 2)
  279. (/ VISUALIZER_BOX_HEIGHT 2)
  280. (drawPosns (rest lop))))
  281. ]))
  282.  
  283.  
  284. ; do this stuff whenever big bang wakes up
  285. ; vs -> vs
  286. (define (tock vs) ; wake up
  287. (cond [(queue? (vstate-last-frame vs)
  288. (pstream-current-frame STREAM)) ; determine whether its go-time
  289. (if (< (+ (round (* (vstate-slider-frac vs)
  290. FRAMES)) PLAY_FRAMES) FRAMES)
  291. (queue-next-fragment
  292. (round (* (vstate-slider-frac vs)
  293. FRAMES))
  294. (vstate-last-frame vs)
  295. (make-vstate (vstate-mode vs)
  296. (+ (vstate-slider-frac vs) PLAY_POSNFRAC)
  297. (+ (vstate-last-frame vs) PLAY_FRAMES)
  298. (append (vstate-points vs) (list (make-posn
  299. (* (vstate-slider-frac vs) VISUALIZER_BOX_WIDTH)
  300. (* VISUALIZER_BOX_HEIGHT (sumofsquares% (vstate-current-sample vs)))
  301. )))
  302. (vstate-current-sample vs)
  303. (vstate-index vs)))
  304. (vstate-reset-slider-frac&index&points vs))]
  305. [else (vstate-index+ vs)]))
  306.  
  307. ; determine whether it's time to queue up a fragment
  308. ; number number -> boolean
  309. (define (queue? last-frame current-frame)
  310. (< (- last-frame current-frame) MAX_QUEUE_INTERVAL))
  311.  
  312. ; queue the next song fragment
  313. ; number number vs -> vs
  314. (define (queue-next-fragment song-frame frame-to-play val)
  315. (andqueue STREAM
  316. (clip TRACK song-frame (+ song-frame PLAY_FRAMES))
  317. frame-to-play
  318. (vstate-index+ val)))
  319.  
  320. ; draw this on the screen whenever vs changes
  321. ; vs -> Image
  322. (define (render vs) (above
  323. (place-image (text (number->string (vstate-current-sample vs)) 24 "black") 150 50 (place-image (buttons (vstate-mode vs))
  324. 700
  325. 60
  326. BUTTON_BOX))
  327. (cond [(= (vstate-mode vs) 1)
  328. (place-image (circle (* 2 (floor (vstate-current-sample vs))) "solid" "red") (/ VISUALIZER_BOX_WIDTH 2) (/ VISUALIZER_BOX_HEIGHT 2)
  329. VISUALIZER_BOX)
  330. ]
  331. [(= (vstate-mode vs) 2)
  332. (add-line VISUALIZER_BOX
  333. (/ VISUALIZER_BOX_WIDTH 2)
  334. (/ VISUALIZER_BOX_HEIGHT 2)
  335. (* (vstate-slider-frac vs) VISUALIZER_BOX_WIDTH)
  336. (* VISUALIZER_BOX_HEIGHT (sumofsquares% (vstate-current-sample vs)))
  337. "black")
  338. ]
  339. [(= (vstate-mode vs) 3)
  340.  
  341. (drawPosns (vstate-points vs))
  342.  
  343. ]
  344. [(= (vstate-mode vs) 4)
  345. VISUALIZER_BOX
  346. ]
  347. [else VISUALIZER_BOX])
  348.  
  349. (place-image SLIDER (* (vstate-slider-frac vs) SLIDER_BOX_WIDTH) 90 SLIDER_BOX)
  350. ))
  351.  
  352. ; return a new vs based on key input
  353. ; vs ke -> vs
  354. (define (keyhandler vs ke)
  355. (cond [(string=? ke "1") (vstate-switch-mode vs 1)]
  356. [(string=? ke "2") (vstate-switch-mode vs 2)]
  357. [(string=? ke "3") (vstate-switch-mode vs 3)]
  358. [(string=? ke "4") (vstate-switch-mode vs 4)]
  359. [else vs]))
  360.  
  361. (define (main x)
  362. (big-bang INIT
  363. [on-tick tock TICK_RATE]
  364. [to-draw render]
  365. [on-key keyhandler]))
  366. (main 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement