Icaro Bombonato commited on
Commit
4bebc03
·
1 Parent(s): 8e7360f

converting audio to wav pcm, accept name with spaces

Browse files
Files changed (2) hide show
  1. Rocha_teste.wav +0 -0
  2. app.py +22 -17
Rocha_teste.wav DELETED
Binary file (3.15 MB)
 
app.py CHANGED
@@ -15,21 +15,26 @@ matplotlib.use('Agg')
15
  logging.basicConfig(level=logging.INFO)
16
  logging.getLogger()
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def get_chunk_times(in_filename, silence_threshold, silence_duration=1):
19
 
20
  silence_duration_re = re.compile('silence_duration: (\d+.\d+)')
21
  silence_end_re = re.compile('silence_end: (\d+.\d+)\s')
22
 
23
- logging.info(f"File {in_filename} exists? = {os.path.exists(in_filename)}")
24
-
25
- fpath = Path(in_filename).absolute()
26
-
27
- logging.info(f"Absolue path: {fpath}")
28
-
29
  command = f"ffmpeg -i {in_filename} -af silencedetect=n=-{silence_threshold}dB:d={silence_duration} -f null - "
30
- #out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
31
-
32
- #stdout, stderr = out.communicate()
33
 
34
  out = subprocess.run(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
35
  stdout = out.stdout
@@ -62,7 +67,7 @@ def get_audio_plot(filename, chunks):
62
 
63
  duration = len(audioBuffer)/sampleRate
64
 
65
- time = np.arange(0,duration,1/sampleRate) #time vector
66
 
67
  ax.plot(time,audioBuffer)
68
  y1 = min(audioBuffer)
@@ -74,20 +79,20 @@ def get_audio_plot(filename, chunks):
74
 
75
  plt.xlabel('Time [s]')
76
  plt.ylabel('Amplitude')
77
- plt.title(os.path.basename(filename))
78
 
79
  return plt.gcf()
80
 
81
 
82
  def get_audio_info(audio):
83
- audio = "Rocha_teste.wav"
84
- ts, chunks = get_chunk_times(audio, 30, 1)
85
- p = get_audio_plot(audio, chunks)
86
  return str(ts), p
87
 
88
- otext = gr.outputs.Textbox(type="auto", label="Silence time")
89
 
90
- oplot = gr.outputs.Image(type="plot", label=None)
91
 
92
  iaudio = gr.inputs.Audio(source="upload", type="filepath", label=None)
93
 
@@ -95,7 +100,7 @@ iface = gr.Interface(
95
  get_audio_info,
96
  iaudio,
97
  [otext, oplot],
98
- description="Enter .WAV audio to view silence areas",
99
  )
100
 
101
  iface.launch()
 
15
  logging.basicConfig(level=logging.INFO)
16
  logging.getLogger()
17
 
18
+ def convert_to_wav(filename):
19
+
20
+ os.rename(filename, filename.replace(" ", "_"))
21
+
22
+ filename = filename.replace(" ", "_")
23
+
24
+ new_name = f"{os.path.splitext(filename)[0]}_converted.wav".replace(" ", "_")
25
+
26
+ command = f"ffmpeg -i {filename} -f wav -bitexact -acodec pcm_s16le -ar 22050 -ac 1 {new_name} -y"
27
+
28
+ subprocess.run(command.split())
29
+
30
+ return new_name
31
+
32
  def get_chunk_times(in_filename, silence_threshold, silence_duration=1):
33
 
34
  silence_duration_re = re.compile('silence_duration: (\d+.\d+)')
35
  silence_end_re = re.compile('silence_end: (\d+.\d+)\s')
36
 
 
 
 
 
 
 
37
  command = f"ffmpeg -i {in_filename} -af silencedetect=n=-{silence_threshold}dB:d={silence_duration} -f null - "
 
 
 
38
 
39
  out = subprocess.run(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
40
  stdout = out.stdout
 
67
 
68
  duration = len(audioBuffer)/sampleRate
69
 
70
+ time = np.arange(0,duration,1/sampleRate)
71
 
72
  ax.plot(time,audioBuffer)
73
  y1 = min(audioBuffer)
 
79
 
80
  plt.xlabel('Time [s]')
81
  plt.ylabel('Amplitude')
82
+ plt.title("Audio with silence marks")
83
 
84
  return plt.gcf()
85
 
86
 
87
  def get_audio_info(audio):
88
+ new_audio = convert_to_wav(audio)
89
+ ts, chunks = get_chunk_times(new_audio, 30, 1)
90
+ p = get_audio_plot(new_audio, chunks)
91
  return str(ts), p
92
 
93
+ otext = gr.outputs.Textbox(type="auto", label="Silence time:")
94
 
95
+ oplot = gr.outputs.Image(type="plot", label="Audio with silence in gray areas")
96
 
97
  iaudio = gr.inputs.Audio(source="upload", type="filepath", label=None)
98
 
 
100
  get_audio_info,
101
  iaudio,
102
  [otext, oplot],
103
+ description="Enter audio to view silence areas",
104
  )
105
 
106
  iface.launch()