-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_utils.py
More file actions
113 lines (93 loc) · 3 KB
/
Copy pathexport_utils.py
File metadata and controls
113 lines (93 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
export_utils.py — Export utilities for Function Sonifier.
Provides functions to export graphs as PNG, audio as WAV,
and save/load project settings as JSON files.
"""
import json
import numpy as np
from scipy.io import wavfile
from matplotlib.figure import Figure
from typing import Optional
from settings import ProjectSettings
def export_graph_as_png(fig: Figure, filepath: str) -> tuple[bool, str]:
"""
Export a Matplotlib figure as a PNG image.
Args:
fig: The Matplotlib Figure to export.
filepath: Destination file path (should end in .png).
Returns:
Tuple of (success, message).
"""
try:
fig.savefig(
filepath,
dpi=200,
bbox_inches='tight',
facecolor='#ffffff',
edgecolor='none',
)
return True, f"Graph exported to {filepath}"
except Exception as e:
return False, f"Export failed: {str(e)}"
def export_audio_as_wav(
audio_data: np.ndarray,
sample_rate: int,
filepath: str
) -> tuple[bool, str]:
"""
Export audio data as a WAV file.
Args:
audio_data: NumPy array of audio samples (mono or stereo).
sample_rate: Sample rate in Hz.
filepath: Destination file path (should end in .wav).
Returns:
Tuple of (success, message).
"""
try:
# Normalize to 16-bit integer range
if audio_data.dtype != np.int16:
peak = np.max(np.abs(audio_data))
if peak > 0:
normalized = audio_data / peak
else:
normalized = audio_data
int_data = (normalized * 32767).astype(np.int16)
else:
int_data = audio_data
wavfile.write(filepath, sample_rate, int_data)
return True, f"Audio exported to {filepath}"
except Exception as e:
return False, f"Export failed: {str(e)}"
def save_project_settings(
settings: ProjectSettings,
filepath: str
) -> tuple[bool, str]:
"""
Save project settings to a JSON file.
Args:
settings: ProjectSettings instance to serialize.
filepath: Destination file path (should end in .json).
Returns:
Tuple of (success, message).
"""
try:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(settings.to_json())
return True, f"Settings saved to {filepath}"
except Exception as e:
return False, f"Save failed: {str(e)}"
def load_project_settings(filepath: str) -> tuple[bool, Optional[ProjectSettings], str]:
"""
Load project settings from a JSON file.
Args:
filepath: Source file path to read.
Returns:
Tuple of (success, settings_or_None, message).
"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
settings = ProjectSettings.from_json(content)
return True, settings, f"Settings loaded from {filepath}"
except Exception as e:
return False, None, f"Load failed: {str(e)}"