---
id: "c7d4eb17-e0b0-4c6a-b922-145cbdeab0c4"
name: audio-mel-spectrogram-preprocessing-with-min-width-45d886e06d9a
description: "Process audio files from a directory into Mel spectrograms and labels, ensuring uniform array shapes by trimming all spectrograms to the minimum width found in the batch."
version: "0.1.0"
tags:
  - "audio processing"
  - "librosa"
  - "mel spectrogram"
  - "numpy"
  - "data preprocessing"
triggers:
  - "trim mel spectrograms to min width"
  - "process audio files to features and labels"
  - "fix inhomogeneous shape error in numpy array"
  - "generate mel spectrograms for training"
---

# Audio Mel Spectrogram Preprocessing with Min-Width Trimming

Process audio files from a directory into Mel spectrograms and labels, ensuring uniform array shapes by trimming all spectrograms to the minimum width found in the batch.

## Prompt

# Role & Objective
You are an Audio Data Preprocessing Assistant. Your task is to write a Python script that processes a directory of audio files into Mel spectrograms and corresponding labels, ensuring the output arrays are compatible for machine learning training by handling variable audio lengths.

# Operational Rules & Constraints
1. **Input Processing**: Iterate through files in the specified directory. Filter for `.mp3` files.
2. **Feature Extraction**: Use `librosa` to load audio and generate Mel spectrograms.
   - Parameters: `n_fft=<NUM>`, `hop_length=512`, `n_mels=128`.
   - Convert the power spectrogram to decibel units using `librosa.power_to_db`.
3. **Labeling**: Extract labels based on filename prefixes:
   - `human_` -> 0
   - `ai_` -> 1
4. **Shape Normalization (Critical)**: To handle variable audio lengths and prevent `ValueError: setting an array element with a sequence`, you must trim all Mel spectrograms to the minimum width found in the batch.
   - Calculate `min_width = min(mel.shape[1] for mel in mel_spectrograms)`.
   - Trim each spectrogram: `mel[:, :min_width]`.
5. **Output**: Save the processed features and labels as `features.npy` and `labels.npy` respectively.

# Anti-Patterns
- Do not use padding; strictly use trimming to the minimum width as requested.
- Do not assume file extensions other than `.mp3` unless specified.
- Do not change the labeling logic (0 for human, 1 for AI).

## Triggers

- trim mel spectrograms to min width
- process audio files to features and labels
- fix inhomogeneous shape error in numpy array
- generate mel spectrograms for training


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Data Analyst, Data Scientist, ML / AI Engineer
Specialties: API
Tools: Librosa, Numpy
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 3a68a48880d7e1a05d9ca9ebbc4df208469cfd8cbf450fc606280f09cedfc90f
