Created
September 22, 2017 07:19
-
-
Save MaverickTse/b6b29e6ca1b2280207f6f3f2beea2152 to your computer and use it in GitHub Desktop.
Scan a mono 16bit signed PCM wave file for sound level exceeding certain threshold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy as np | |
import argparse | |
import scipy.io.wavfile as w | |
def GetHMS(total_seconds:int): | |
hr = np.floor(total_seconds/3600) | |
q1 = total_seconds % 3600 | |
m = np.floor(q1/60) | |
s = q1 % 60 | |
return int(hr), int(m), int(s) | |
if __name__ == "__main__": | |
cli_parser = argparse.ArgumentParser( | |
description="Search for sound level above certain level in a wav file") | |
cli_parser.add_argument("folder", help="Folder containing mono uncompressed wav files") | |
cli_parser.add_argument("--threshold", type=float, default=-0.025, | |
help="Sound threshold. Lower value gives less higher sensitivity") | |
cli_parser.add_argument("--min_interval", type=int, default=60, | |
help="minimum interval between reporting points") | |
program_options = vars(cli_parser.parse_args()) | |
if not os.path.exists(program_options["folder"]): | |
print("Folder not found") | |
exit(1) | |
os.chdir(program_options["folder"]) | |
arr_wav = [f for f in os.listdir() if f.endswith(".wav")] | |
for f in arr_wav: | |
rate, data = w.read(f, mmap=True) | |
dn = np.abs((data - 32768)/32768) | |
clean = np.nan_to_num(dn) | |
total_time = np.floor(data.shape[0]/rate) | |
db_array = [] | |
for x in np.linspace(0, total_time, total_time+1): | |
start_index = int(x * rate) | |
end_index = int(start_index + rate) | |
data_subset = clean[start_index:end_index] | |
if np.isnan(data_subset.shape[0]): | |
continue | |
sq = data_subset ** 2 | |
sum_of_square = np.sum(sq) | |
rms = np.sqrt(sum_of_square/sq.shape[0]) | |
db = 20 * np.log10(rms) | |
db_array.append(db) | |
noisy_point = [] | |
np_db = np.array(db_array) | |
for i, v in enumerate(np_db): | |
if v > program_options["threshold"]: | |
if len(noisy_point) == 0: | |
noisy_point.append(i + 1) | |
if i - noisy_point[-1] > program_options["min_interval"]: | |
noisy_point.append(i+1) | |
log_content = "Auto noise detection\n" | |
for p in noisy_point: | |
h, m, s = GetHMS(p) | |
log_content += "{:0>2}:{:0>2}:{:0>2}\n".format(h, m, s) | |
print("{:0>2}:{:0>2}:{:0>2}".format(h, m, s)) | |
out_file_name = f + ".txt" | |
with open(out_file_name, "w") as outfile: | |
outfile.write(log_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment