Beatbox

Beatbox preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 470 times • Downloaded 98 times • Run 1 time
Download the 'Beatbox' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Click to Run Model

;; this model uses the following command and reporters that
;; are part of the sound extension:
;;    drums -- reports a list of the names of all available MIDI drums
;;    play-drum -- hits a drum
;; and that's it!  see the "Sound" section of the NetLogo User Manual
;; for more details on the sound extension.
extensions [ sound ]

;; this is used to detect separate mouse clicks. once the mouse button is
;; pressed, we want that to be considered as only a single click until the
;; button is released again.
globals [mouse-released?]

;; the "drummers" are the little white circles that move across the graphics
;; window.  the "notes" are the colored squares.  when a drummer passes
;; over a note, the drummer plays its drum.
breed [ drummers drummer ]
breed [ notes note ]

;; "instrument" is the name of a drummer's drum
drummers-own [instrument]

to setup
  clear-all
  set mouse-released? true
  set-default-shape drummers "circle"
  set-default-shape notes "square"

  ;; make the grid lines to help the user see the structure of the beat
  ask patches
    [ set plabel-color gray + 1 ]
  ask patches with [pycor mod 2 = 0 and pxcor != min-pxcor ]
    [ set pcolor gray - 4.8 ]

  turn-all-drums-on
  create-drummers world-height - 1 [
    set color white
    set shape "circle"
    set size 0.2
    set xcor -1 * max-pxcor
    set heading 90
    ;; "who" is the turtle's id number.  id numbers are assigned
    ;; starting from zero.  each turtle has a different number, so
    ;; each turtle occupies a different row and gets a different drum.
    set ycor max-pycor - who - 1
    set instrument item who sound:drums
    ask patch-ahead 8 [ set plabel [instrument] of myself ]
  ]
  label-beats
  reset-ticks
end 

;; this puts text in the top row and draws gray columns;
;; this is to help the user place drum hits

to label-beats
  let text ["1" "e" "+" "a" "2" "e" "+" "a"
            "3" "e" "+" "a" "4" "e" "+" "a"]
  ;; this gets a bit tricky.  basically we're stepping
  ;; through the string a character at a time, placing
  ;; one character on every other patch along the top edge.
  ;; when we hit a number, we turn that whole column
  ;; dark gray.
  let n 0
  foreach text [
    let x (-1 * max-pxcor + n + 1)
    ;; "?" is used inside foreach to refer to the
    ;; current item of the list we're looping through
    ask patch x max-pycor [ set plabel ? ]
    if member? ? ["1" "2" "3" "4"]
      [ ask patches with [pxcor = x]
          ;; subtracting from a color makes it darker
          [ set pcolor gray - 4.8 ] ]
    set n n + 2
  ]
end 

to turn-all-drums-on
  ask patches with [pxcor = min-pxcor and pycor != max-pycor]
    [ set pcolor white ]
end 

to turn-all-drums-off
  ask patches with [pxcor = min-pxcor]
    [ set pcolor black ]
end 

to go
  ;; there are eight patches per "beat" (four beats to a measure)
  ;; and sixty seconds in a minute, hence the 7.5 here
  ;; (because 60 divided by 8 is 7.5)
  every 7.5 / bpm [
    ask drummers [
      ;; check the white square on the left to see if this drum
      ;; is "on" or not.  if it is, and if we're over a note,
      ;; then hit our drum
      if ([pcolor] of patch min-pxcor pycor = white) and
      (any? notes-here)
        [ sound:play-drum instrument velocity ]
      fd 1
      ;; when we wrap around the right edge of the world, we
      ;; need to move an extra square in order to skip the first column
      if xcor = -1 * max-pxcor
        [ fd 1 ]
    ]
    tick
  ]
  ;; the edit procedure handles mouse clicks
  edit
end 

to edit
  if not mouse-down?
    [ set mouse-released? true ]
  if mouse-released? and mouse-down? [
    ask patch mouse-xcor mouse-ycor
      [ toggle ]
    set mouse-released? false
  ]
end 

;; if the user clicks on a note, remove it.
;; if the user clicks where there is no note, make one.
;; if the user clicks on the left column, turn
;;   the patch white or black to indicate whether that drum
;;   is on or off.

to toggle  ;; patch procedure
  if pycor != max-pycor [
    ifelse pxcor = min-pxcor
      [ set pcolor ifelse-value (pcolor = black) [white] [black] ]
      [ ifelse any? notes-here
          [ ask notes-here [ die ] ]
          [ sprout-notes 1 ] ] ]
end 

;; return to the beginning of the measure (the left
;; side of the view)

to rewind
  ask drummers [
    set xcor -1 * max-pxcor
  ]
  reset-ticks
end 

to load-file
  ;; we don't want the value the user chose for "file" to change when
  ;; the user hits the "load-file" button.  "file" is a global variable,
  ;; and "import-world" wipes out the values of all global variables,
  ;; so we need to store the old value temporarily in a local variable,
  ;; in order to restore it once "import-world" is done.
  let old-value-of-file file
  import-world file
  set file old-value-of-file
  rewind
end 

;; we could just tell the user to choose "Export World" on the File
;; menu, but it seems a bit friendlier to provide this in a button

to save-file
  export-world user-new-file
end 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky about 12 years ago Updated version tag Download this version
Uri Wilensky almost 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 14 years ago Beatbox Download this version
Uri Wilensky over 14 years ago Beatbox Download this version

Attached files

File Type Description Last updated
Beatbox.png preview Preview for 'Beatbox' over 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.