Gloria Dal Santo commited on
Commit
56e1924
·
1 Parent(s): 5613ddf

Format output

Browse files
Files changed (2) hide show
  1. app.py +51 -6
  2. src/reverb.py +11 -10
app.py CHANGED
@@ -1,4 +1,8 @@
1
  import gradio as gr
 
 
 
 
2
  from src.config import BaseConfig
3
  from src.reverb import BaseFDN
4
  from flamo.optimize.trainer import Trainer
@@ -76,16 +80,53 @@ def process_fdn(N, delay_lengths):
76
  # Train the model
77
  print("Starting training...")
78
  trainer.train(train_loader, valid_loader)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  else:
82
- return "Error: No delay lengths provided"
83
 
84
  except Exception as e:
85
  print(f"Error: {e}")
86
  import traceback
87
  traceback.print_exc()
88
- return f"Error processing inputs: {str(e)}"
89
 
90
  demo = gr.Interface(
91
  fn=process_fdn,
@@ -99,9 +140,13 @@ demo = gr.Interface(
99
  label="Delay Lengths (N integer values)"
100
  )
101
  ],
102
- outputs=gr.Textbox(label="Output"),
103
- title="Feedback Delay Network Configuration",
104
- description="Configure your feedback delay network by specifying N (number of delay lines) and their corresponding delay lengths."
 
 
 
 
105
  )
106
 
107
  demo.launch(debug=True)
 
1
  import gradio as gr
2
+ import json
3
+ import torch
4
+ import numpy as np
5
+ import soundfile as sf
6
  from src.config import BaseConfig
7
  from src.reverb import BaseFDN
8
  from flamo.optimize.trainer import Trainer
 
80
  # Train the model
81
  print("Starting training...")
82
  trainer.train(train_loader, valid_loader)
83
+ est_param = model.get_params()
84
+
85
+ # Convert parameters to JSON format
86
+ # Assuming est_param is a dict or can be converted to one
87
+ param_dict = {}
88
+ for key, value in est_param.items():
89
+ # Convert tensors to lists for JSON serialization
90
+ if hasattr(value, 'cpu'):
91
+ param_dict[key] = value.cpu().detach().numpy().tolist()
92
+ else:
93
+ param_dict[key] = value
94
+
95
+ # Save to JSON file
96
+ output_path = "estimated_parameters.json"
97
+ with open(output_path, 'w') as f:
98
+ json.dump(param_dict, f, indent=2)
99
 
100
+ ir = model.shell.get_time_response()
101
+
102
+ # Convert ir to audio format for Gradio
103
+ ir_audio = ir.cpu().detach().numpy()
104
+
105
+ # Ensure proper shape (1D array)
106
+ if ir_audio.ndim > 1:
107
+ ir_audio = ir_audio.squeeze()
108
+
109
+ # Normalize to [-1, 1] range to prevent overflow
110
+ max_val = np.abs(ir_audio).max()
111
+ if max_val > 0:
112
+ ir_audio = ir_audio / max_val
113
+
114
+ # Get the sample rate from config
115
+ sample_rate = getattr(config, 'fs', 48000)
116
+
117
+ # Save audio to file using soundfile (avoids Gradio's conversion issues)
118
+ audio_path = "impulse_response.wav"
119
+ sf.write(audio_path, ir_audio, sample_rate)
120
+
121
+ return result, output_path, audio_path
122
  else:
123
+ return "Error: No delay lengths provided", None, None
124
 
125
  except Exception as e:
126
  print(f"Error: {e}")
127
  import traceback
128
  traceback.print_exc()
129
+ return f"Error processing inputs: {str(e)}", None, None
130
 
131
  demo = gr.Interface(
132
  fn=process_fdn,
 
140
  label="Delay Lengths (N integer values)"
141
  )
142
  ],
143
+ outputs=[
144
+ gr.Textbox(label="Output"),
145
+ gr.File(label="Estimated Parameters (JSON)"),
146
+ gr.Audio(label="Impulse Response", type="numpy")
147
+ ],
148
+ title="Feedback Delay Network Optimization",
149
+ description="Configure your homogeneous feedback delay network by specifying N (number of delay lines) and their corresponding delay lengths. Submit the values to run optimization and obtain estimated parameters and playback the resulting impulse response."
150
  )
151
 
152
  demo.launch(debug=True)
src/reverb.py CHANGED
@@ -104,7 +104,7 @@ class BaseFDN(nn.Module):
104
  - The returned parameters can be used to recreate or modify the FDN
105
  """
106
  core = self.shell.get_core()
107
-
108
  params = OrderedDict()
109
  params["delays"] = self.delay_lengths.cpu().numpy().tolist()
110
  params["onset_time"] = self.onset
@@ -112,23 +112,24 @@ class BaseFDN(nn.Module):
112
  core.branchB.early_reflections.param.cpu().detach().numpy().tolist()
113
  )
114
  params["input_gains"] = (
115
- core.branchA.input_gain.param.cpu().detach().numpy().tolist()
116
  )
117
  params["output_gains"] = (
118
- core.branchA.output_gain.param[0].cpu().detach().numpy().tolist()
119
  )
120
  params["feedback_matrix"] = (
121
- core.branchA.feedback_loop.feedback.mixing_matrix.param.cpu()
122
- .detach()
123
- .numpy()
124
- .tolist()
125
- )
126
- params["attenuation"] = (
127
- core.branchA.feedback_loop.feedback.attenuation.param.cpu()
128
  .detach()
 
129
  .numpy()
130
  .tolist()
131
  )
 
 
 
 
 
 
132
  return params
133
 
134
  def _validate_delays(self, config: BaseConfig, delay_lengths: List[int]) -> None:
 
104
  - The returned parameters can be used to recreate or modify the FDN
105
  """
106
  core = self.shell.get_core()
107
+ map_matrix = core.branchA.feedback_loop.feedback.mixing_matrix.map
108
  params = OrderedDict()
109
  params["delays"] = self.delay_lengths.cpu().numpy().tolist()
110
  params["onset_time"] = self.onset
 
112
  core.branchB.early_reflections.param.cpu().detach().numpy().tolist()
113
  )
114
  params["input_gains"] = (
115
+ core.branchA.input_gain.param.cpu().squeeze().detach().numpy().tolist()
116
  )
117
  params["output_gains"] = (
118
+ core.branchA.output_gain.param[0].cpu().squeeze().detach().numpy().tolist()
119
  )
120
  params["feedback_matrix"] = (
121
+ map_matrix(core.branchA.feedback_loop.feedback.mixing_matrix.param).cpu()
 
 
 
 
 
 
122
  .detach()
123
+ .squeeze()
124
  .numpy()
125
  .tolist()
126
  )
127
+ # params["attenuation"] = (
128
+ # core.branchA.feedback_loop.feedback.attenuation.param.cpu()
129
+ # .detach()
130
+ # .numpy()
131
+ # .tolist()
132
+ # )
133
  return params
134
 
135
  def _validate_delays(self, config: BaseConfig, delay_lengths: List[int]) -> None: