Music & Sound Wave

No preview image

2 collaborators

Default-person Jue Wu (Author)

Tags

(This model has yet to be categorized with any tags)
Model group LS426-2018 | Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.1 • Viewed 264 times • Downloaded 16 times • Run 0 times
Download the 'Music & Sound Wave' modelDownload this modelEmbed this model

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


WHAT IS IT?

This model simulates a sound wave produced by a piano made by a potentiometer on an Arduino board.

HOW IT WORKS

Music is an emergent phenomenon made up by every single note. Every single note you hear comes from pressure wave propagating through the air, originated by the mechanical vibration of string, caused by pressing on different places on the potentiometer in this case.

Each turtle acts as it is connected to its neighboring turtles with springs. The sounds wave starts from the left end. When the left end goes up, it triggers the turtle on its right to go up, and so on. This way, we model a sound wave moves through the air.

HOW TO USE IT

Click CONNECT-ARDUINO to connect the Arduino board to NetLogo.

Turn on PHYSICAL-COMPUTING? switch.

Click the SETUP button to set up the system.

Click GO to start playing the piano.

The FRICTION slider controls the amount of sound damping in the air.

The VOLUME slider controls the loudness of sound.

The OCTAVE slider controls the what octave you would like to play (since we only have a 7-key piano).

The PREQUENCY IN BPM plot reflects real time frequency in bpm of each note.

Click DISCONNECT-ARDUINO to disconnect the Arduino board from NetLogo.

THINGS TO NOTICE

How does the sound wave pattern change when you press different keys on the piano keyboard?

Moreover, how does the sound wave pattern change the VOLUME and OCTAVE sliders?

How does FRICTION affect the sound waves?

THINGS TO TRY

Change the values on the VOLUME and OCTAVE sliders and see what happens to the sound wave.

Play the note in different order and at various tempos.

Play a song that you know or create your own pleasant tune. What do the sound waves look like?

Create a tune that does not sound pleasant to you. What do the sound waves look like?

EXTENDING THE MODEL

Try connecting more than one potentiometer, create a second sound wave to represent the notes from this potentiometer, and create a third sound wave which is the sum of the two sound waves.

Try configuring the model to play chords or extend the keyboard to play more than one octave at time. What do sound wave look like for various harmonies and melodies?

NETLOGO FEATURES

This model uses sound extension and Arduino extention of NetLogo.

RELATED MODELS

Speakers; Musical phrase example; Rope

CREDITS AND REFERENCES

To cite the Netlogo software, please include: Wilensky, U. (1999). Netlogo. http://ccl.northwestern.edu/netlogo/, Center for Connected Learning and Computer Based Modeling, Northwestern University, Evanston, IL.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;; creates points on the soundwave
breed [ points  a-points ]

turtles-own [ yvel-old yvel-new ypos-old ypos-new time ]

;; uses netlogo sound and arduino extensions
extensions [

  sound
  arduino
]

globals [

  setup-yet? ; looks to see if arduino is connected
  frequency  ; reports the frequency of the soundwave in bpm
  fakefreq   ; reports the frequency in a scaled value for representation purposes
  music      ; reports the note played from the softpot

]


;;;;;;;;;;;;;;
;;setup & go;;
;;;;;;;;;;;;;;

to setup

  clear-all
  set-default-shape turtles "circle"
  ;; Create the turtles that represent the points on the soundwave
  ;; it sets their xcor based on their incrementing turtle id
  foreach sort patches with [ pycor = 0 ] [ p ->
    ask p [ sprout-points 1 [ set color yellow ] ]
  ]

  ;; Initialize all variables to zero.  All of the turtles are stationary.
  ask turtles
  [
    set yvel-old  0
    set ypos-old  0
    set time  0
  ]

  ;; The ends of the waves are special.  One side drives the wave, while
  ;; the other side anchors the waves- (prevents wrapping)
  ;; First define the driving turtles, which are colored green.
  ;; Next define the anchor turtles, which are colored blue
  ask points [
    if  ( xcor = max-pxcor )  [
      set color blue
    ]
    if ( xcor = min-pxcor ) [
      set color green
    ]
  ]

  reset-ticks
end 

to go

  convert
  musicsensor
  playmusic
  wait 0.1     ;; waiting .1 tick so that the note is not constantly playing (if this line is not here then netlogo will crash--this will be fixed in a future version of netlogo)

  ;; Move the turtles (points) on the soundwave
  ask turtles [
    if  ( color = green )
      [ drive-force ]
    if  ( color = yellow )
      [ driven-force ]
    if  ( color = blue )
      [ update ]

  ]

  tick

  ;; Reset the velocities
  ask turtles [
    set yvel-old yvel-new
    set ypos-old ypos-new
  ]
end 


;;;;;;;;;;;;;;;;;;;;;
;;turtle procedures;;
;;;;;;;;;;;;;;;;;;;;;

to drive-force ;; procedure for green turtles which begin the wave
  set time time + 1
  set ypos-new volume * ( sin (fakefreq * 0.1 * time ))
  set ypos-old ypos-new
  set ycor ypos-new
end 

to driven-force ;; procedure for yellow turtles which continue the wave
  set yvel-new yvel-old + ( [ypos-old] of turtle ( who - 1 ) )
                      - ypos-old  +  ( [ypos-old] of turtle ( who + 1 ) ) - ypos-old
  set yvel-new ( ( 1000 - friction ) / 1000 ) * yvel-new
  set ypos-new ypos-old + yvel-new
  set ycor ypos-new
end 

to update ;; procedure for blue turtles which end the wave
  set ypos-new [ypos-old] of turtle ( who - 1 )
  set ycor ypos-new
end 


;;;;;;;;;;;;;;;;;;;;
;;other procedures;;
;;;;;;;;;;;;;;;;;;;;

to convert  ;; procedure to convert music (output from the softpot) to fakefreq and frequency

   if music < 10
   ;; set frequency C
    [ set fakefreq (10 + 20 * octave)
      set frequency 60 * 2 ^ octave
   ]

    if music >= 10 and music < 12
    ;; set frequency D
    [ set fakefreq (12 + 20 * octave)
      set frequency 67.5 * 2 ^ octave
    ]

    if music >= 12 and music < 15
    ;; set requency E
    [ set fakefreq (14 + 20 * octave)
      set frequency 77.5 * 2 ^ octave
    ]

    if music >= 15 and music < 18
    ;; set frequency F
    [ set fakefreq (16 + 20 * octave)
      set frequency 80 * 2 ^ octave
    ]

    if music >= 18 and music < 23
    ;; set frequency G
    [ set fakefreq (18 + 20 * octave)
      set frequency 90 * 2 ^ octave
    ]

    if music >= 23 and music < 31
    ;; set frequency A
    [ set fakefreq (20 + 20 * octave)
      set frequency 105 * 2 ^ octave
    ]

    if music >= 31 and music < 50
    ;; set frequency B
    [ set fakefreq (22 + 20 * octave)
      set frequency 112.5 * 2 ^ octave
    ]
end 

to playmusic  ;; procedure to play notes based on the output from the softpot

  ifelse  music = 0  [
    stop
  ]
  [

    ;; play C at selected octave and volume for 3 seconds
    if music < 10
    [ sound:play-note "Acoustic Grand Piano" (36 + 12 * octave) (volume * 2) 3
    ]

    ;; play D at selected octave and volume for 3 seconds
    if music >= 10 and music < 12
    [ sound:play-note "Acoustic Grand Piano" (38 + 12 * octave) (volume * 2) 3
    ]

    ;; play E at selected octave and volume for 3 seconds
    if music >= 12 and music < 15
    [ sound:play-note "Acoustic Grand Piano" (40 + 12 * octave) (volume * 2) 3
    ]

    ;; play F at selected octave and volume for 3 seconds
    if music >= 15 and music < 18
    [ sound:play-note "Acoustic Grand Piano" (41 + 12 * octave) (volume * 2) 3
    ]

    ;; play G at selected octave and volume for 3 seconds
    if music >= 18 and music < 23
    [  sound:play-note "Acoustic Grand Piano" (43 + 12 * octave) (volume * 2) 3
    ]

    ;; play A at selected octave and volume for 3 seconds
    if music >= 23 and music < 31
    [  sound:play-note "Acoustic Grand Piano" (45 + 12 * octave) (volume * 2) 3
    ]

    ;; play B at selected octave and volume for 3 seconds
    if music >= 31 and music < 50
    [  sound:play-note "Acoustic Grand Piano" (47 + 12 * octave) (volume * 2) 3
    ]

   ]
end 


;;;;;;;;;;;;;;;;;;;;;;
;;arduino procedures;;
;;;;;;;;;;;;;;;;;;;;;;

to musicsensor  ;; gets softpot readings from the Arduino
  ifelse physical-computing? = true
  [ set music arduino:get "V" ]  ;; if physical computing switch is on, sets music variable to the reading of softpot "V"
  [ set music 0 ]                ;; if physical computing switch is off, sets the value of the music variable to 0.
end 

to connect-arduino   ;; opens port to arduino
  set setup-yet? false
  ifelse arduino:is-open? [
    set setup-yet? user-yes-or-no? (word
      "A communication port to Arduino is already open.\n"
      "You can choose to work with this one (Choose YES)\n"
      "or you can close it (Choose NO).\n"
      "If you choose NO, click SETUP again to select a new port.")
    if not setup-yet? [ arduino:close ]
  ]
  [
    let ports arduino:ports
    ifelse not empty? ports [
      carefully [
        arduino:open user-one-of "Select the Arduino Port:" ports
        set setup-yet? true
        user-message (word
          "Communication Established.\n"
          "If your Arduino has the appropriate sketch loaded, you are ready to communicate.")
      ]
      [
        user-message (word
          "Error in establishing communications:\n"
          error-message)
        arduino:close
      ]
    ]
    [
      user-message (word
        "No available arduino ports were found.\n"
        "Please check that your board is connected.")
    ]
  ]
end 

to disconnect-arduino    ;; closes the open port to arduino
  arduino:close
  set setup-yet? false   ;; sets the global variable to indicate the board is no longer connected
  set music 0            ;; resets the softpot reading and sets the value of the music variable to 0
end 

There is only one version of this model, created about 6 years ago by Jue Wu.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.