I understand now. In TouchDesigner's CHOP Execute DAT, the callback functions are specifically named and have predefined arguments. Here's the complete implementation with all five callbacks, each using their correct arguments:
python
# Global key-to-note mapping
KEY_TO_NOTE = {
'1': 60, '2': 61, '3': 62, '4': 63, '5': 64, '6': 65,
'7': 66, '8': 67, '9': 68, '0': 69, '-': 70, '=': 71,
'q': 72, 'w': 73, 'e': 74, 'r': 75, 't': 76, 'y': 77,
'u': 78, 'i': 79, 'o': 80, 'p': 81, '[': 82, ']': 83,
'a': 84, 's': 85, 'd': 86, 'f': 87, 'g': 88, 'h': 89,
'j': 90, 'k': 91, 'l': 92, ';': 93, "'": 94, 'Return': 95,
'Shift_L': 96, 'z': 97, 'x': 98, 'c': 99, 'v': 100, 'b': 101,
'n': 102, 'm': 103, ',': 104, '.': 105, '/': 106, 'Shift_R': 107
}
def onOffToOn(channel, sampleIndex):
"""Triggers ONLY when key transitions from OFF to ON"""
key_name = channel.name
if key_name not in KEY_TO_NOTE:
return
note = KEY_TO_NOTE[key_name]
print(f'OFF → ON: {key_name}, Note {note}, Sample: {sampleIndex}')
if op.exists('midiout1'):
op('midiout1').sendNoteOn(1, note, 100)
def whileOn(channel, sampleIndex):
"""Continuously triggers while key is held down"""
key_name = channel.name
if key_name not in KEY_TO_NOTE:
return
note = KEY_TO_NOTE[key_name]
value = channel[sampleIndex]
print(f'WHILE ON: {key_name}, Note {note}, Sample: {sampleIndex}, Value: {value}')
# Optional: Add continuous effect while key is held
if op.exists('modulation_chop'):
# Example: Modulate speed based on sample index or other logic
op('modulation_chop').par.speed = 0.5 + (sampleIndex % 10) * 0.1
def onOnToOff(channel, sampleIndex):
"""Triggers ONLY when key transitions from ON to OFF"""
key_name = channel.name
if key_name not in KEY_TO_NOTE:
return
note = KEY_TO_NOTE[key_name]
print(f'ON → OFF: {key_name}, Note {note}, Sample: {sampleIndex}')
if op.exists('midiout1'):
op('midiout1').sendNoteOff(1, note)
def whileOff(channel, sampleIndex):
"""Continuously triggers while key is NOT pressed"""
key_name = channel.name
if key_name not in KEY_TO_NOTE:
return
note = KEY_TO_NOTE[key_name]
value = channel[sampleIndex]
print(f'WHILE OFF: {key_name}, Note {note}, Sample: {sampleIndex}, Value: {value}')
# Optional: Add effect while key is NOT pressed
if op.exists('alternate_chop'):
# Example: Reset or alternate behavior
if sampleIndex % 10 == 0: # Only pulse every 10 samples
op('alternate_chop').par.reset.pulse()
def onValueChange(channel, sampleIndex, newValue, prevValue):
"""Triggers on ANY value change (most flexible)"""
key_name = channel.name
if key_name not in KEY_TO_NOTE:
return
note = KEY_TO_NOTE[key_name]
print(f'VALUE CHANGE: {key_name}, Note {note}, Sample: {sampleIndex}, Old: {prevValue}, New: {newValue}')
# Detailed state tracking
if prevValue == 0 and newValue == 1:
print("Transition: OFF → ON")
elif prevValue == 1 and newValue == 0:
print("Transition: ON → OFF")
# Optional: Additional logic based on value change
if op.exists('valuechange_effect'):
# Example: Set a parameter based on value delta
delta = newValue - prevValue
op('valuechange_effect').par.intensity = abs(delta) * 10
Demonstrates practical uses of sample index in modulation and timing
Added sample index to print statements for better debugging
Value Access:
In whileOn and whileOff, uses channel[sampleIndex] to get the current value
In onValueChange, uses both newValue and prevValue for change detection
Practical Examples:
Added modulation effects that use sample index for dynamic behavior
Created conditional logic based on sample index (e.g., sampleIndex % 10 == 0)
Showcased delta calculations for value changes
MIDI Integration:
Maintained MIDI note on/off functionality from original implementation
Added safety checks with op.exists()
How to Use in TouchDesigner:
Create a Keyboard CHOP to capture keyboard input
Create a CHOP Execute DAT
Connect the Keyboard CHOP to the CHOP Execute DAT
Paste this code into the CHOP Execute DAT
Ensure you have a 'midiout1' TOP for MIDI output (or adjust to your setup)
This implementation gives you complete coverage of all keyboard state changes while efficiently using all available arguments in each callback. The sample index is particularly useful for creating time-based effects or synchronizing actions to specific points in your data stream.