Mr7Explorer commited on
Commit
87601f5
·
verified ·
1 Parent(s): b8b02b5

Create io_utils.py

Browse files
Files changed (1) hide show
  1. io_utils.py +34 -0
io_utils.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # io_utils.py
2
+ # =============
3
+ # Handles audio metadata and safe loading
4
+
5
+ import soundfile as sf
6
+ import librosa
7
+ import numpy as np
8
+
9
+
10
+ def read_audio_info(path):
11
+ """Read audio file metadata using soundfile.info"""
12
+ info = sf.info(path)
13
+ return {
14
+ "samplerate": int(info.samplerate),
15
+ "channels": int(info.channels),
16
+ "frames": int(info.frames),
17
+ "subtype": info.subtype,
18
+ "format": info.format,
19
+ "duration": float(info.frames) / info.samplerate if info.frames else 0.0
20
+ }
21
+
22
+
23
+ def load_audio_mono(path):
24
+ """
25
+ Always load safely as mono using Librosa.
26
+ This helper keeps the original logic untouched.
27
+ """
28
+ try:
29
+ y, sr = librosa.load(path, sr=None, mono=True)
30
+ if np.isnan(y).any():
31
+ y = np.nan_to_num(y)
32
+ return y, sr
33
+ except Exception as e:
34
+ raise RuntimeError(f"Audio loading failed: {str(e)}")