title: Smoothly "Audialize" Recursion in Python with Turtle and PyAudio
body: <p>I'd like to be able to reproduce the idea behind the code below, but with sound that is not horribly harsh to listen to. Ideally I'd like a solution that uses code that school kids can understand (the play_note function can be imported so they don't have to worry about how that works). One answer <a href="https://stackoverflow.com/a/35075923/3042018">here</a> suggested the clicking between consecutive notes is due to incomplete cycles of sound , but I don't know how to fix that for changing durations.</p>
  
  <p>Can anyone help with this please? Can it be made to work with some tweaking or is the approach flawed somehow?</p>
  
  <pre><code>import turtle
  import pyaudio
  import numpy as np
  
  def play_note(freq, dur):
  
    p = pyaudio.PyAudio()
  
    volume = 0.5     # range [0.0, 1.0]
    fs = 44100       # sampling rate, Hz, must be integer
    duration = dur   # in seconds, may be float
    f = freq       # sine frequency, Hz, may be float
  
    # generate samples, note conversion to float32 array
    samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
  
    # for paFloat32 sample values must be in range [-1.0, 1.0]
    stream = p.open(format=pyaudio.paFloat32,
                    channels=1,
                    rate=fs,
                    output=True)
  
    # play. May repeat with different volume values (if done interactively) 
    stream.write(volume*samples)
  
    stream.stop_stream()
    stream.close()
  
    p.terminate()
  
  def tree(branchLen,t):
      if branchLen &gt; 5:
          freq = branchLen * 2 + 400
          dur = branchLen / 100.0
          print freq, dur
          play_note(freq, dur)
          t.forward(branchLen)
          t.right(20)
          tree(branchLen-15,t)
          t.left(40)
          tree(branchLen-15,t)
          t.right(20)
          t.backward(branchLen)
  
  def main():
      t = turtle.Turtle()
      t.speed(0)
      myWin = turtle.Screen()
      t.left(90)
      t.up()
      t.backward(100)
      t.down()
      t.color("green")
      tree(75,t)
      myWin.exitonclick()
  
  main()
  </code></pre>