Square Wave Oscillator
Nucli

Square Wave Oscillator

Generates a square wave with adjustable frequency, pulse width and output level. Identical signal is sent to both stereo channels.
LanguageNucli
Added16 Oct 2026
Views30

Free to use for anything. No credit needed.

No ratings yet

Log in to rate this code.

Code 27 lines


param freq = 220 (20 .. 2000)
param level = 0.3 (0 .. 1)
param pulse = 0.5 (0.01 .. 0.99)

history phase = 0

let increment = freq / samplerate
let nextPhase = phase + increment

if (nextPhase >= 1) {
  nextPhase = nextPhase - 1
}

let square = 0
if (nextPhase < pulse) {
  square = 1
} else {
  square = -1
}

let output = square * level

out1 = output
out2 = output

phase = nextPhase

Comments

No comments yet.

← Back to code