Ultrasonic Sensor - Midi - desperately need help with some tweaking of the code

Hi guys!
SO, I have a big question to ask. I am working on a sound art installation in where I use my arduino and an ultrasonic distance sensor to send midi information to ableton. Previously I've used it as a theremin, but now I want to do something quite different.
The code I am using now sends a midi note every time the sensor detects a value (a distance between 1-30 cm). So it works like a regular theremin would - with pitch. What I would like it to do for my next project is - to not hit a new note every time the sensor detects something but to turn the volume on every time the sensor detects something and turn it off every time it does not detect something. So I would have a clip playing in the background continuously - but it would be muted except for when the sensor detects something (people passing by, me putting my hand in front of it etc.)
The code I am using is this -Made by Moritz Geist for Sonicrobots:

#include <MIDI.h>
int pingPin = 13;
int inPin = 12;
long duration;
int cm_new = 20;
int cm_old = 20;
int sens = 5; // sensivity range when to launch a new  note

MIDI_CREATE_DEFAULT_INSTANCE();


void setup() {

  Serial.begin(9600); // MIDI Begin
  pinMode(pingPin, OUTPUT); // setup the Pins
  pinMode(inPin, INPUT); // setup the Pins

}

void loop()
{


// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH); // the singal from the Sensor is measured. The length of the pulse describes the distance form an object to the sensor.

cm_new = map(constrain(duration, 20, 3000), 50, 2000, 96, 48); // contrain --> set a threshold to cut values of the sensor that are to far away of to short (20 .. 3000) MAP --> Map the sensor range to a note values vom 96 (A high C) to 48 (a low C)

if (cm_new != cm_old) // only if a new sensor value is detected, a new nite will be send. IF the object stays stable infront of the sensor, no note will be send.
{
 MIDI.sendNoteOff(cm_old,0,1);   // Stop the old note
 MIDI.sendNoteOn(cm_new,127,1);  // Send a new Note (pitch , velocity 127, on channel 1)


cm_old = cm_new;
}
delay(30); // for staiblity
}

The code I am using now sends a midi note every time the sensor detects a value (a distance between 1-30 cm).

Sorry, this is nonsense. The ultrasonic sensor is continuously detecting values. Most of the time, there is nothing to detect, because there are no pulses being echoed by surfaces in front of the sensor, because the sensor is not continuously sending pulses.

Also, the sensor does not return a distance.

It would certainly be better to put the code to make the sensor send a pulse, and to time how long it takes to get an echo, into a function, so you can separate the "get a distance" code from the "now use that distance" code.

What I would like it to do for my next project is - to not hit a new note every time the sensor detects something

So, no calls to MIDI.sendNoteOn() or MIDI.sendNoteOff(). What function do you want to call? Does such a function exist?

Two duplicates of this topic DELETED.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.