diablofx commited on
Commit
d0dbc19
1 Parent(s): ba7c3c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -4
app.py CHANGED
@@ -15,15 +15,18 @@ def main():
15
  with gr.Row():
16
  with gr.Column():
17
  audio_input = gr.Audio(type='filepath')
18
- create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
 
19
  with gr.Column():
20
  output_markdown = gr.Markdown(value="", visible=True)
 
21
 
22
- create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
 
23
 
24
  app.queue(max_size=1022).launch(share=True)
25
 
26
- def get_audio_file_info(audio_file):
27
  # Read the audio data from the file
28
  audio_data, sample_rate = sf.read(audio_file)
29
 
@@ -55,8 +58,34 @@ def get_audio_file_info(audio_file):
55
 
56
  """
57
 
58
- # Return the info table
59
  return info_table
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Create the Gradio interface
62
  main()
 
15
  with gr.Row():
16
  with gr.Column():
17
  audio_input = gr.Audio(type='filepath')
18
+ get_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
19
+ interval_input = gr.Number(value=5, label="Enter Interval (seconds):", min=1, step=1)
20
  with gr.Column():
21
  output_markdown = gr.Markdown(value="", visible=True)
22
+ cut_audio_butt = gr.Button(value='Cut and Compile Audio', variant='success', disabled=True)
23
 
24
+ get_info_butt.click(fn=get_audio_file_info, inputs=[audio_input, interval_input], outputs=[output_markdown, cut_audio_butt])
25
+ cut_audio_butt.click(fn=cut_and_compile_audio, inputs=[audio_input, interval_input], outputs=[output_markdown])
26
 
27
  app.queue(max_size=1022).launch(share=True)
28
 
29
+ def get_audio_file_info(audio_file, interval):
30
  # Read the audio data from the file
31
  audio_data, sample_rate = sf.read(audio_file)
32
 
 
58
 
59
  """
60
 
61
+ # Enable the button for cutting audio
62
  return info_table
63
 
64
+ def cut_and_compile_audio(audio_file, interval):
65
+ # Read the audio data from the file
66
+ audio_data, sample_rate = sf.read(audio_file)
67
+
68
+ # Convert to mono if it's not mono
69
+ if len(audio_data.shape) > 1:
70
+ audio_data = np.mean(audio_data, axis=1)
71
+
72
+ # Calculate the number of frames for the specified interval
73
+ frames_per_interval = int(sample_rate * interval)
74
+
75
+ # Cut the audio into intervals
76
+ audio_clips = [audio_data[i:i + frames_per_interval] for i in range(0, len(audio_data), frames_per_interval)]
77
+
78
+ # Compile audio clips into a single array
79
+ compiled_audio = np.concatenate(audio_clips)
80
+
81
+ # Save the compiled audio to a new file
82
+ compiled_file_path = 'compiled_audio.wav'
83
+ sf.write(compiled_file_path, compiled_audio, sample_rate)
84
+
85
+ # Provide a download link for the compiled audio
86
+ download_link = f"[Download Compiled Audio](sandbox:/mnt/data/{compiled_file_path})"
87
+
88
+ return f"Audio cut and compiled successfully. {download_link}"
89
+
90
  # Create the Gradio interface
91
  main()