# Genuary2024_SDF

# CircularThoughts

The prompt for **Genuary 2024 Day 29 is SDF (Signed Distance Function)**

Within the **SonicP**i Composition of **"Circular Thoughts"**, the SDF controls the bpm, rate, beat\_stretch and the notes being played

## Poem

```ocaml
The Circles are Rotating
Their Inner Thoughts are Gyrating
The tone  is Glitchy
The thoughts are plenty
As the Circular Thoughts are explaining
What their inner Thoughts are Saying
```

## Audio

<iframe width="100%" height="300" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/1729840875&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>

[IllestPreacha](https://soundcloud.com/llestreacha) · [CircularThoughts](https://soundcloud.com/llestreacha/circularthoughts)

## SonicPi Code

```ruby
#getting the sign distance function
#Formula = d(x,y)=  Math.sqrt(x**2 + y**2) - radius
#which can be written as Math.hypot(x,y)

#x,y coordinates and the r for radius
def SDF_circle(x, y, r)
  Math.hypot(x,y) - r
end


r = 75

live_loop :soundOfSDF do
  
  #random seed based on time
  use_random_seed Time.now.to_i
  
 use_bpm (dist_sdf).abs * 10 + 2
  
  #angle from -75 to beyond 360, to test the sdf
  
  angle = rrand_i(-75,400)
  
  # r * angleInRadians = (angleInDegrees * PI) / 180; gives a coordinate for X and Y
  
  #use cos for both as it allows for more points outside the circle to be played
  
  x = r * Math.cos(angle * Math::PI / 180)
  y = r * Math.cos(angle * Math::PI / 180)
  
  dist_sdf = SDF_circle(x,y,r)
  
  #if the sign distance value is negative, it plays the disance in prophet
  #if the sign distance value is positive, it plays the distance in piano
  
  if (dist_sdf < 1)
    with_fx [:ping_pong,:flanger].choose, mix: rrand(0.4,0.7) do
      use_synth :prophet
      play (dist_sdf).abs
      sample [:bass_dnb_f,:perc_snap,:perc_swoosh].tick, 
             rate: (r/dist_sdf) if spread(7,12).tick
    end
    
  else
    with_fx [:ixi_techno,:whammy].choose, mix: rrand(0.1,0.9) do
      use_synth :piano
      play (dist_sdf)
      sample [:drum_bass_hard,:tabla_ghe1].choose, 
            beat_stretch: (r/dist_sdf).abs if spread(5,12).tick
    end
    
  end
  sleep 0.5
end



```
