aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--options.bqn6
-rw-r--r--oscillator.bqn32
2 files changed, 37 insertions, 1 deletions
diff --git a/options.bqn b/options.bqn
index 7e2aa25..2cc055a 100644
--- a/options.bqn
+++ b/options.bqn
@@ -3,8 +3,12 @@ fmt ⇐ 1‿16 # Format: 16-bit integer
freq ⇐ 44100 # Frequency: 44.1kHz
Set ⇐ {fmt‿freq↩𝕩}
+# For generating random waveforms; result is a shape 𝕩 array of uniform
+# random numbers between 0 and 1
+RandFloats ⇐ •RAND∘⥊⟜0
+
# For reading/writing wave files
warn_dither ⇐ 0 # Whether to warn on non-integer signal
warn_clip ⇐ 1 # Whether to warn on out-of-bounds signal
-Dither ⇐ ⌊ (-˝ 0 •RAND∘⥊˜ 2∾≢)⊸+
+Dither ⇐ ⌊ (-˝ ·RandFloats 2∾≢)⊸+
Resample ⇐ "No resampling function specified"!0˙
diff --git a/oscillator.bqn b/oscillator.bqn
new file mode 100644
index 0000000..8df742d
--- /dev/null
+++ b/oscillator.bqn
@@ -0,0 +1,32 @@
+# 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]
+SP ← (4⊸×÷5⊸-) 4׬⊸× # Sine π×𝕩 by Bhaskara I
+Rand ← Ce {o.RandFloats 𝕩}
+
+# Basic waveforms
+Id ← {𝕨++`𝕩÷o.freq} # Identity
+P ← 1|Id # Phase
+
+# Deterministic waveforms for synthesis
+Saw ⇐ Ce∘P # 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.Sine (2×π) × Id # Sine wave
+Sine ⇐ (××SP∘|) {𝕨+0.5}Saw⊢ # Sine wave, more or less
+
+# Random waveforms
+White ⇐ Rand ≢ # White noise
+Brown ⇐ +` White # Brown noise
+# Pink noise
+#Pink ⇐ ÷⟜(⌈´|) (2⊸/⊸+˜´∘⌽ · Rand¨ 2⋆↕)⊸÷∘(1+·⌈2⋆⁼⊢)⊸(↑˜)∘≠
+Pink ⇐ ÷⟜(⌈´|) (+` ·⥊∘⍉∘≍´ ·-⟜«∘Rand¨ 2⋆1⌈⌽∘↕)∘(1+·⌈2⋆⁼⊢)⊸(↑˜)∘≠