Variable with memory longer then one sample inside Gen Codebox

    Jun 08 2016 | 2:07 pm
    Hi everybody,
    I'm a real beginner with MAX/MSP. Currently I use it as a prototyping playground to get a bit more used with digital audio Processing.
    Right now, I'd like to generate a sort of clipping, that holds on outputting the clipped value for some more samples, after the input Signal already got back under the threshold value. This "hold"-time should be half of the time the input signal was over the threshold. To do that, I thought of something like that:
    threshold = 0.6; //fixed value
    samples_over=0; //initial value
    if (in1>threshold){
    else {
    if (samples_over>0){
    else {
    But if I noticed everything right, the code in the codebox is looped on a sample basis, so assigning 0 to "samples_over" as an initial value just resets its value with every new sample. How do I create a variable with a memory that lasts longer then one loop cycle and just gets one initial value once the signal processing got started? I'm sure this is a basic and simple task - most probably I just don't know the right keyword to google for ;)
    Thank you in advance

    • Jun 08 2016 | 2:34 pm
      You want the 'History' object(it stores 1 sample value in memory through each perform loop, if you ever need more than 1 sample history use the 'Data' object(and there's also 'Buffer' to store in a buffer~ which resides outside the gen~ patcher)). You also don't need a nested if statement, because the ordering of your conditionals can be satisfied with an 'else if' instead. Here's the code you want:
      History samples_over(0); //initial value
      threshold = 0.6; //fixed value
      if (in1>threshold)
      { out1 = threshold; samples_over += 1; }
      else if (samples_over > 0)
      { samples_over -= 2; out1 = threshold;  }
      else { samples_over = 0; out1 = in1; }
      Interesting concept in action, here's my test in a patcher:
    • Jun 09 2016 | 7:35 am
      Great, this was exactly what I was looking for! Works exactly like I expected.
      I even came across the "History" object while searching for a solution to my problem but I didn't get the Idea of using it this way...