Update infer/lib/audio.py
Browse files- infer/lib/audio.py +15 -4
infer/lib/audio.py
CHANGED
@@ -6,7 +6,6 @@ import av
|
|
6 |
import traceback
|
7 |
from io import BytesIO
|
8 |
|
9 |
-
|
10 |
def wav2(i, o, format):
|
11 |
inp = av.open(i, "rb")
|
12 |
if format == "m4a":
|
@@ -29,7 +28,6 @@ def wav2(i, o, format):
|
|
29 |
out.close()
|
30 |
inp.close()
|
31 |
|
32 |
-
|
33 |
def load_audio(file, sr):
|
34 |
try:
|
35 |
# Clean the file path
|
@@ -47,10 +45,23 @@ def load_audio(file, sr):
|
|
47 |
traceback.print_exc()
|
48 |
raise RuntimeError(f"Failed to load audio: {e}")
|
49 |
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
def clean_path(path_str):
|
54 |
if platform.system() == "Windows":
|
55 |
path_str = path_str.replace("/", "\\")
|
56 |
return path_str.strip().strip('"').strip("\n").strip('"').strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import traceback
|
7 |
from io import BytesIO
|
8 |
|
|
|
9 |
def wav2(i, o, format):
|
10 |
inp = av.open(i, "rb")
|
11 |
if format == "m4a":
|
|
|
28 |
out.close()
|
29 |
inp.close()
|
30 |
|
|
|
31 |
def load_audio(file, sr):
|
32 |
try:
|
33 |
# Clean the file path
|
|
|
45 |
traceback.print_exc()
|
46 |
raise RuntimeError(f"Failed to load audio: {e}")
|
47 |
|
48 |
+
audio_data = np.frombuffer(out, np.float32).flatten()
|
49 |
+
|
50 |
+
# Ensure the function always returns a numpy array, even if it's empty
|
51 |
+
if audio_data is None or len(audio_data) == 0:
|
52 |
+
raise RuntimeError("Failed to load audio: no data returned.")
|
53 |
+
|
54 |
+
return audio_data
|
55 |
|
56 |
def clean_path(path_str):
|
57 |
if platform.system() == "Windows":
|
58 |
path_str = path_str.replace("/", "\\")
|
59 |
return path_str.strip().strip('"').strip("\n").strip('"').strip()
|
60 |
+
|
61 |
+
# Example usage
|
62 |
+
if __name__ == "__main__":
|
63 |
+
try:
|
64 |
+
audio_data = load_audio("example.wav", 16000)
|
65 |
+
print("Audio data loaded successfully")
|
66 |
+
except RuntimeError as e:
|
67 |
+
print(e)
|