Measuring duration of voltage peak

I use the following method in sound to light programs

// pin for analog sound sensor
#define soundSensor A0
// set to your threshold level
int threshold = 500;
unsigned long timeNow;
unsigned long timePassed;

void setup() {
}

void loop() {
  // measures the time it takes for the signal amplitude to return to the threshold level
  // thus measures the peak duration or period of a sine wave
  // the longer the time the lower the frequency so can be used to
  // crudely measure frequency
  
  if(analogRead(soundSensor) > threshold) {
    // timing started
    timeNow = millis();
    while (analogRead(soundSensor) > threshold) {
      // do nothing
    }
    timePassed = millis() - timeNow;
  }
}