Zw07 commited on
Commit
768647a
โ€ข
1 Parent(s): 50abadb

add read_single_channel function

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py CHANGED
@@ -28,6 +28,56 @@ from pydub import AudioSegment
28
  # for p in predictions:
29
  # col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def create_default_value():
32
  if "def_value" not in st.session_state:
33
  def_val_npy = np.random.choice([0, 1], size=32 - len_start_bit)
@@ -97,6 +147,7 @@ def main():
97
  if add_watermark_button: # ็‚นๅ‡ปๆŒ‰้’ฎๅŽๆ‰ง่กŒ็š„
98
  if audio_file and watermark_text:
99
  with st.spinner("Adding Watermark..."):
 
100
  watermark = model.get_watermark(wav, default_sr)
101
  # watermarked_audio, encode_time_cost = add_watermark(tmp_input_audio_file, watermark_text)
102
  # st.write("Watermarked Audio:")
 
28
  # for p in predictions:
29
  # col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
30
 
31
+ def read_as_single_channel_16k(audio_file, def_sr=16000, verbose=True, aim_second=None):
32
+ assert os.path.exists(audio_file)
33
+
34
+ file_extension = os.path.splitext(audio_file)[1].lower()
35
+
36
+ if file_extension == ".mp3":
37
+ data, origin_sr = librosa.load(audio_file, sr=None)
38
+ elif file_extension in [".wav", ".flac"]:
39
+ data, origin_sr = soundfile.read(audio_file)
40
+ else:
41
+ raise Exception("unsupported file:" + file_extension)
42
+
43
+ # channel check
44
+ if len(data.shape) == 2:
45
+ left_channel = data[:, 0]
46
+ if verbose:
47
+ print("Warning! the input audio has multiple chanel, this tool only use the first channel!")
48
+ data = left_channel
49
+
50
+ # sample rate check
51
+ if origin_sr != def_sr:
52
+ data = resampy.resample(data, origin_sr, def_sr)
53
+ if verbose:
54
+ print("Warning! The original samplerate is not 16Khz; the watermarked audio will be re-sampled to 16KHz")
55
+
56
+ sr = def_sr
57
+ audio_length_second = 1.0 * len(data) / sr
58
+ # if verbose:
59
+ # print("input length :%d second" % audio_length_second)
60
+
61
+ if aim_second is not None:
62
+ signal = data
63
+ assert len(signal) > 0
64
+ current_second = len(signal) / sr
65
+ if current_second < aim_second:
66
+ repeat_count = int(aim_second / current_second) + 1
67
+ signal = np.repeat(signal, repeat_count)
68
+ data = signal[0:sr * aim_second]
69
+
70
+ return data, sr, audio_length_second
71
+
72
+
73
+ def my_read_file(audio_path, max_second):
74
+ signal, sr, audio_length_second = read_as_single_channel_16k(audio_path, default_sr)
75
+ if audio_length_second > max_second:
76
+ signal = signal[0:default_sr * max_second]
77
+ audio_length_second = max_second
78
+
79
+ return signal, sr, audio_length_second
80
+
81
  def create_default_value():
82
  if "def_value" not in st.session_state:
83
  def_val_npy = np.random.choice([0, 1], size=32 - len_start_bit)
 
147
  if add_watermark_button: # ็‚นๅ‡ปๆŒ‰้’ฎๅŽๆ‰ง่กŒ็š„
148
  if audio_file and watermark_text:
149
  with st.spinner("Adding Watermark..."):
150
+ wav = my_read_file(wav,max_second_encode)
151
  watermark = model.get_watermark(wav, default_sr)
152
  # watermarked_audio, encode_time_cost = add_watermark(tmp_input_audio_file, watermark_text)
153
  # st.write("Watermarked Audio:")