Skip to content

Instantly share code, notes, and snippets.

@blacklight
Created October 30, 2020 21:02
Show Gist options
  • Save blacklight/698ac9d2823319a338c143dc5eb4404d to your computer and use it in GitHub Desktop.
Save blacklight/698ac9d2823319a338c143dc5eb4404d to your computer and use it in GitHub Desktop.
Generate a micmon spectrum dataset from a set of raw labelled audio files
import os
from micmon.audio import AudioDirectory, AudioPlayer, AudioFile
from micmon.dataset import DatasetWriter
basedir = os.path.expanduser('~/datasets/sound-detect')
audio_dir = os.path.join(basedir, 'audio')
datasets_dir = os.path.join(basedir, 'data')
cutoff_frequencies = [250, 2500]
# Scan the base audio_dir for labelled audio samples
audio_dirs = AudioDirectory.scan(audio_dir)
# Save the spectrum information and labels of the samples to a
# different compressed file for each audio file.
for audio_dir in audio_dirs:
dataset_file = os.path.join(datasets_dir, os.path.basename(audio_dir.path) + '.npz')
print(f'Processing audio sample {audio_dir.path}')
with AudioFile(audio_dir) as reader, \
DatasetWriter(dataset_file,
low_freq=cutoff_frequencies[0],
high_freq=cutoff_frequencies[1]) as writer:
for sample in reader:
writer += sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment