RexChan commited on
Commit
8c57520
·
verified ·
1 Parent(s): d303e1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -3
app.py CHANGED
@@ -8,14 +8,67 @@ from pydub import AudioSegment
8
  from IPython.display import Audio
9
  import os
10
  import accelerate
 
 
11
 
 
 
 
 
 
12
 
13
  # preprocess and crop audio file
14
- def audio_preprocess(input_file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # separate music and vocal
16
- separator = Separator('spleeter:2stems')
17
  #separator.separate_to_file(input_file, output_file)
18
- separated_audio = separator.separate(input_file)
19
 
20
  # Crop the audio
21
  start_time = 60000 # e.g. 30 seconds, 30000
 
8
  from IPython.display import Audio
9
  import os
10
  import accelerate
11
+ import pyaudio
12
+ import numpy as np
13
 
14
+ # Create PyAudio object
15
+ p = pyaudio.PyAudio()
16
+ CHUNK_SIZE = 1024
17
+ SAMPLING_RATE = 16000
18
+ vocals_data = bytes()
19
 
20
  # preprocess and crop audio file
21
+ def audio_preprocess(input_file, in_data, frame_count, time_info, status):
22
+
23
+ # Define callback function for audio processing
24
+
25
+ global vocals_data
26
+ # Convert input data to numpy array
27
+ audio_array = np.frombuffer(input_file, dtype=np.int16)
28
+
29
+ # Perform vocal removal on the audio input
30
+ # Pass the audio array as waveform to separate() method
31
+ vocals = Separator('spleeter:2stems').separate(audio_array)
32
+
33
+ # Convert vocals to audio data
34
+ vocals_data = vocals['vocals'].flatten().astype(np.int16).tobytes()
35
+
36
+ # Return processed data for output
37
+ return vocals_data, pyaudio.paContinue
38
+
39
+ # Open stream for recording
40
+ stream = p.open(format=pyaudio.paInt16, channels=1, rate=SAMPLING_RATE, input=True, output=True,
41
+ frames_per_buffer=CHUNK_SIZE, stream_callback=process_audio)
42
+
43
+ # Start stream
44
+ stream.start_stream()
45
+
46
+ # Create stream for playback
47
+ playback_stream = p.open(format=pyaudio.paInt16, channels=1, rate=SAMPLING_RATE, output=True)
48
+
49
+ # Play processed data in real-time
50
+ while stream.is_active():
51
+ if len(vocals_data) >= CHUNK_SIZE:
52
+ playback_stream.write(vocals_data[:CHUNK_SIZE])
53
+ vocals_data = vocals_data[CHUNK_SIZE:]
54
+
55
+ # Stop streams
56
+ stream.stop_stream()
57
+ stream.close()
58
+ playback_stream.stop_stream()
59
+ playback_stream.close()
60
+
61
+ # Terminate PyAudio object
62
+ p.terminate()
63
+
64
+ # Now 'processed_file' contains the separated vocals
65
+ separated_audio = vocals_data
66
+
67
+
68
  # separate music and vocal
69
+ #separator = Separator('spleeter:2stems')
70
  #separator.separate_to_file(input_file, output_file)
71
+ #separated_audio = separator.separate(input_file)
72
 
73
  # Crop the audio
74
  start_time = 60000 # e.g. 30 seconds, 30000