Skip to content

Instantly share code, notes, and snippets.

@balsama
Created August 4, 2021 22:57
Show Gist options
  • Save balsama/8be5c3bf8f25766243bc1b016b0ab109 to your computer and use it in GitHub Desktop.
Save balsama/8be5c3bf8f25766243bc1b016b0ab109 to your computer and use it in GitHub Desktop.
Counts the number of Katydid chirp groups and the number of chirps per group in a .wav file.
#!/usr/bin/env python
import sys
from pydub import AudioSegment
from pydub.silence import split_on_silence
sound_file = AudioSegment.from_wav("./wav/2021-08-03--72--002.wav")
# First chunk the audio file into groups of chirps.
audio_chunks = split_on_silence(sound_file,
# Must be silent for a little less than 1/5th of a second.
min_silence_len=180,
# Consider it silent if quieter than -37 dBFS
silence_thresh=-37
)
# Then, count the number of chirps per chunk.
pulses_string = ""
pulses_array = []
for i, chunk in enumerate(audio_chunks):
# Save all of the chunks/groups of chirps out int their own file for debugging.
out_file = ".//splitAudio//chunk{0}.wav".format(i)
chunk.export(out_file, format="wav")
pulses = split_on_silence(chunk,
# There's usually about 40 ms between chirps.
min_silence_len=30,
# And it doesnt get as quiet between chirps as it does between groups/chunks of chirps.
silence_thresh=-22
)
# Count and print the number of chirps in each group of chirps.
pulses_count = len(pulses)
print (pulses_count)
# Make a string out of the pulses to look for patterns later.
pulses_string = pulses_string + str(pulses_count)
#print (pulses_string)
print (pulses_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment