aboutsummaryrefslogtreecommitdiff
path: root/oscillator.bqn
blob: e918072a59386c4347fb32f2a0c04998d65ce3ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Standard oscillators for (usually subtractive) synthesis
# In each case, the right argument argument is a list giving the
# frequency for each sample.
# If not otherwise specified, the left argument is optional and gives
# the phase offset.

"oscillator.bqn takes a single option namespace, or no arguments" ! 1≥≠•args
o  •Import"options.bqn",  •args

# Utilities
Ce  1-˜2×                   # Center: transform [0,1] to [¯1,1]
Rand  Ce {o.RandFloats 𝕩}

# Basic waveforms
Id  {𝕨++`𝕩÷o.freq}           # Identity
P  1|Id                      # Phase

# Deterministic waveforms for synthesis
Saw    CeP                  # Sawtooth wave //////
Triangle  Ce| {𝕨+0.25}Saw⊢  # Triangle wave ´\/\/\
Square Ce 0.5<P              # Square wave   ⊔¯⊔¯⊔¯
Pulse  Ce <P                # Pulse wave with duty cycle 𝕨
Sine   •math.Sin (2×π) × Id  # Sine wave

# Random waveforms
White  Rand                 # White noise
Brown  +` White              # Brown noise
# Pink noise
#Pink  ⇐ ÷⟜(⌈´|) (2⊸/⊸+˜´∘⌽ · Rand¨ 2⋆↕)⊸÷∘(1+·⌈2⋆⁼⊢)⊸(↑˜)∘≠
Pink   {÷(´|) +` 𝕩  ´ -«Rand¨ (1⌈↕)(2)2𝕩}

# Karplus-Stong algorithm for a plucked string
Pluck  { 𝕊 flenatt: # Frequency, length, attenuation
  p  o.freq÷f
   ((÷2×16att÷p)×1+)(↕⌈len÷p) Pink p0
}