Using functions in AppleScriptObjC

Hi, I need a way to determine the sound level of mp3 files. These files are internet radio recordings (12 hours each), but some of them only contain low level noise. I’d like to find those “bad” recordings in order to delete them.

Found this Objective-C code on stackoverflow but I can’t find a way to use functions like MIN, sqrtf, log10f in AppleScriptObjC. Is this possible at all? If not, does anyone have vanilla AppleScript equivalents?

Also, what’s the equivalnt of HUGE_VALF in AppleScriptObjC?

Here’s the Objective-C part where I’m stuck in translating:

    float maxDecible = 0;
    float minDecible = HUGE_VALF;
    NSMutableArray * sd = [NSMutableArray new]; //used for standard deviation
    for (int n = 0; n < MIN(buffer.format.channelCount, 2); n++){ //go through channels
        NSMutableArray * decibles = [NSMutableArray new]; //holds actual decible values
        
        //go through pulling the decibles
        for (int i = 0; i < deciblesCount; i++){
            
            int offset = frameIncrement * i; //grab offset
            //equation from stack, no idea the maths
            float sqr = sqrtf(buffer.floatChannelData[n][offset * buffer.stride]/(float)buffer.frameLength);
            float decible = 20 * log10f(sqr);
            
            decible += 160; //make positive
            decible = (isnan(decible) || decible < 0) ? 0 : decible; //if it's not a number or silent, make it zero
            if (decible > 0){ //if it has volume
                [sd addObject:@(decible)];
            }
            [decibles addObject:@(decible)];//add to decibles array
            
            maxDecible = MAX(maxDecible, decible); //grab biggest
            minDecible = MIN(minDecible, decible); //grab smallest
        }
        
        [channels addObject:decibles]; //add to channels array
    }

The min and sqrtf can be pretty easily implemented using AppleScript:

on sqrtf(base)
    return base ^ 0.5
end sqrtf

on min(num1, num2)
  if num1 < num2 then
      return num1
  else
      return num2
  end if
end min

I wasn’t sure if log10 could be achieved similarly but Googling for a solution yields this idea to use the shell tool “bc” to achieve the result:

-- https://www.macscripter.net/t/can-i-use-logarithms-in-math-equations/48269/6
on log10 (TheNumber)
  set natural_log to (do shell script ("echo 'l(" & (TheNumber as string) & ")' | bc -l")) as real
  set natural_log_of_10 to (do shell script ("echo 'l(10)' | bc -l")) as real  
  set common_log to natural_log / natural_log_of_10
  return common_log
end log10

Hope those help!

I am not an expert in trigonometry but this post could make sense to you:

You could use JavaScript or Python to get math functions.

set theNumber to 64
log "sqrt: " & (do shell script "/usr/bin/python3 <<EOF
import math
print(math.sqrt(" & theNumber & "))
EOF
")

set theNumber to 5
log "log10: " & (do shell script "/usr/bin/python3 <<EOF
import math
print(math.log10(" & theNumber & "))
EOF
")