Created
September 29, 2022 06:39
-
-
Save controversial/009ddbfedef2aa0f932c1c9ea03ff516 to your computer and use it in GitHub Desktop.
Code for a TouchDesigner Script CHOP that can generate a signal comparable to the built in “Audio Oscillator” CHOP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# press 'Setup Parameters' in the OP to call this function to re-create the parameters. | |
# The CHOP will not work until you do | |
def onSetupParameters(scriptOp): | |
page = scriptOp.appendCustomPage('Custom') | |
p = page.appendInt('Samplerate', label='Sample Rate') | |
p.val = 44100 | |
p2 = page.appendFloat('Time', label='Time') | |
p2.expr = 'absTime.seconds' | |
p.normMin = 1 | |
p.normMax = 44100 | |
scriptOp.isTimeSlice = True | |
return | |
def onCook(scriptOp): | |
scriptOp.clear() | |
# Measure timing | |
sampleRate = scriptOp.par.Samplerate.eval() | |
numSamplesElapsed = int(absTime.stepSeconds * sampleRate) | |
# Set CHOP settings (sample rate, start / end index) - this logic is designed to mimic the | |
# observed behavior of the “audio oscillator” CHOP. | |
scriptOp.start = int(scriptOp.time.seconds * sampleRate) | |
scriptOp.end = scriptOp.start + numSamplesElapsed - 1 | |
scriptOp.rate = sampleRate | |
# Generate sine wave samples in numpy array | |
freq = 440 # Hz | |
phase = (2 * np.pi * freq * absTime.seconds) % (2 * np.pi) | |
samples = np.sin(2 * np.pi * freq * np.arange(numSamplesElapsed, dtype=np.float32) / sampleRate + phase) | |
c1 = scriptOp.appendChan('c1') | |
c1.copyNumpyArray(samples) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment