Crow: Coding help needed: sample&hold in Lua for "Harp Interface"

Hello!

Coding noob here, making my first LUA script for Crow.

A “Harp Interface” for Mutable Rings, utilizing a 2hp Switch module and a quad quantizer.

I can’t figure out how to make a working sample & hold function. I basically need “in2” to be sampled and sent out of “out4”, everytime a pulse is sent out of “out3”.

The idea is that when I move a fader (in1) and it crosses one of 4 virtual “strings”, a short trigger is fired to trigger Rings, while the pitch of the “string”, is sent from a quad quantizer via 2hp Switch.

Here is the code:

--- Harp Interface
-- in1: Controller Output
-- in2: 2hp Switch Out (1v/oct)
-- out1: 2hp Switch Selector
-- out2:
-- out3: Trigger Out
-- out4: Sample & Hold Out


--- input modes

function init()
  input[1].mode('stream', 0.001)             -- Read input1 from controller
  input[2].mode('stream', 0.001)             -- Read input2 from 2hp Switch


--- virtual string thresholds

  string1 = 0.58 
  string2 = 1.80
  string3 = 3.20
  string4 = 4.38

  position = 1

end


--- Send switch CV from in1 to out1

input[1].stream = function(switch)          -- Make switch function
  output[1].volts = switch                  -- Send switch CV to out1


--- Send a pulse to out3 when crossing virtual strings

    if switch < string1 and position ~= 1
      then
        output[3] (pulse(0.01,8))
        position = 1
      end

    if switch > string1 and switch < string2 and position ~= 2
      then
        output[3] (pulse(0.01,8))
        position = 2
      end

    if switch > string2 and switch < string3 and position ~= 3
      then
        output[3] (pulse(0.01,8))
        position = 3
      end

    if switch > string3 and switch < string4 and position ~= 4
      then
        output[3] (pulse(0.01,8))
        position = 4
      end

    if switch > string4 and position ~= 5
      then
        output[3] (pulse(0.01,8))
        position = 5
      end

end

Does adding output[4].volts = input[2].volts following each output[3](pulse(0.01,8)) do what you expect?

As a side note, if you have Crow firmware version 2.0 or higher, this would be a perfect script to rewrite using the 'window' input mode. It’s designed for doing exactly this kind of thing, where you have a bunch of mutually exclusive “windows” in the voltage range on an input where something should happen when inside them.

Yes! It worked! What a simple solution. I thought I already tried that, but something else must have been wrong then. Thank you so much!

Regarding 'window' mode - it doesn’t seem to be possible to use at the same time I’m using 'stream' mode, right? I actually started doing the code with 'window' mode, but I wanted to have a fast continuous stream of data from the position of the input fader. Do you have a suggestion for another way to check if the fader input crosses the “virtual harp strings” instead of 'stream' mode?

Btw - output[4].volts = input[2].volts was what worked.!

Ah I didn’t notice that you were using the stream function to mult the fader CV to output 1. You’re right, in this case the window wouldn’t work as far as I know.

I suppose you could use a metro to do that instead, and just sample the voltage with output[1].volts = input[1].volts, but that could potentially be less performant or reliable depending on how fast you want to sample. (Just speculating, as I have not actually looked at how those features are implemented).

1 Like

Yes that’s what I meant, glad it worked out.

1 Like