Skip to content

Commit

Permalink
Add support for no AI and preprocessed stems
Browse files Browse the repository at this point in the history
  • Loading branch information
starscouts committed Jul 29, 2024
1 parent 3f8906a commit 1cd2398
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ test.m4a
separated
srdr_work
__pycache__
stems
27 changes: 5 additions & 22 deletions base.sds
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%Srdr-1.0
%Srdr-1.1

'
' Surrounder base scene file
Expand All @@ -13,19 +13,9 @@ Chs: CHANNELS_5_1; 16; 44100
Lay: LAYOUT_5_1

' Splitting into stems
Sep: STEMS_ALL
Sep: STEMS_SIMPLE

' Original version of the song in the front channels
Map: STEMS_VOCALS_L; 0; 1
Map: STEMS_VOCALS_R; 1; 1
Map: STEMS_PIANO_L; 0; 1
Map: STEMS_PIANO_R; 1; 1
Map: STEMS_GUITAR_L; 0; 1
Map: STEMS_GUITAR_R; 1; 1
Map: STEMS_BASS_L; 0; 1
Map: STEMS_BASS_R; 1; 1
Map: STEMS_DRUMS_L; 0; 1
Map: STEMS_DRUMS_R; 1; 1
Map: STEMS_OTHER_L; 0; 1
Map: STEMS_OTHER_R; 1; 1

Expand All @@ -38,18 +28,11 @@ Map: STEMS_LFE_L; 3; 1
Map: STEMS_LFE_R; 3; 1

' Quieter version of the song in the back channels
Map: STEMS_VOCALS_L; 4; 0.75
Map: STEMS_VOCALS_R; 5; 0.75
Map: STEMS_PIANO_L; 4; 0.75
Map: STEMS_PIANO_R; 5; 0.75
Map: STEMS_GUITAR_L; 4; 0.75
Map: STEMS_GUITAR_R; 5; 0.75
Map: STEMS_BASS_L; 4; 0.75
Map: STEMS_BASS_R; 5; 0.75
Map: STEMS_DRUMS_L; 4; 0.75
Map: STEMS_DRUMS_R; 5; 0.75
Map: STEMS_OTHER_L; 4; 0.75
Map: STEMS_OTHER_R; 5; 0.75

' New in 0.2.0: Adding distance between the back channels
Dis: 4; 5

' FLAC output
Out: OUTPUT_FLAC
58 changes: 58 additions & 0 deletions base_ai.sds
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
%Srdr-1.1

'
' Surrounder base scene file
'
' This is the primary scene file tested during development
' of Surrounder. It produces 5.1 surround output from stereo
' input.
'

' Base configuration
Chs: CHANNELS_5_1; 16; 44100
Lay: LAYOUT_5_1

' Splitting into stems
Sep: STEMS_ALL

' Original version of the song in the front channels
Map: STEMS_VOCALS_L; 0; 1
Map: STEMS_VOCALS_R; 1; 1
Map: STEMS_PIANO_L; 0; 1
Map: STEMS_PIANO_R; 1; 1
Map: STEMS_GUITAR_L; 0; 1
Map: STEMS_GUITAR_R; 1; 1
Map: STEMS_BASS_L; 0; 1
Map: STEMS_BASS_R; 1; 1
Map: STEMS_DRUMS_L; 0; 1
Map: STEMS_DRUMS_R; 1; 1
Map: STEMS_OTHER_L; 0; 1
Map: STEMS_OTHER_R; 1; 1

' Vocals on the center channel
Map: STEMS_VOCALS_L; 2; 0.5
Map: STEMS_VOCALS_R; 2; 0.5

' Low frequency elements
Map: STEMS_LFE_L; 3; 1
Map: STEMS_LFE_R; 3; 1

' Quieter version of the song in the back channels
Map: STEMS_VOCALS_L; 4; 0.75
Map: STEMS_VOCALS_R; 5; 0.75
Map: STEMS_PIANO_L; 4; 0.75
Map: STEMS_PIANO_R; 5; 0.75
Map: STEMS_GUITAR_L; 4; 0.75
Map: STEMS_GUITAR_R; 5; 0.75
Map: STEMS_BASS_L; 4; 0.75
Map: STEMS_BASS_R; 5; 0.75
Map: STEMS_DRUMS_L; 4; 0.75
Map: STEMS_DRUMS_R; 5; 0.75
Map: STEMS_OTHER_L; 4; 0.75
Map: STEMS_OTHER_R; 5; 0.75

' New in 0.2.0: Adding distance to the back channels
Dis: 4; 5

' FLAC output
Out: OUTPUT_FLAC
58 changes: 58 additions & 0 deletions base_manual.sds
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
%Srdr-1.1

'
' Surrounder base scene file
'
' This is the primary scene file tested during development
' of Surrounder. It produces 5.1 surround output from stereo
' input.
'

' Base configuration
Chs: CHANNELS_5_1; 16; 44100
Lay: LAYOUT_5_1

' Splitting into stems
Sep: STEMS_MANUAL

' Original version of the song in the front channels
Map: STEMS_VOCALS_L; 0; 1
Map: STEMS_VOCALS_R; 1; 1
Map: STEMS_PIANO_L; 0; 1
Map: STEMS_PIANO_R; 1; 1
Map: STEMS_GUITAR_L; 0; 1
Map: STEMS_GUITAR_R; 1; 1
Map: STEMS_BASS_L; 0; 1
Map: STEMS_BASS_R; 1; 1
Map: STEMS_DRUMS_L; 0; 1
Map: STEMS_DRUMS_R; 1; 1
Map: STEMS_OTHER_L; 0; 1
Map: STEMS_OTHER_R; 1; 1

' Vocals on the center channel
Map: STEMS_VOCALS_L; 2; 0.5
Map: STEMS_VOCALS_R; 2; 0.5

' Low frequency elements
Map: STEMS_LFE_L; 3; 1
Map: STEMS_LFE_R; 3; 1

' Quieter version of the song in the back channels
Map: STEMS_VOCALS_L; 4; 0.75
Map: STEMS_VOCALS_R; 5; 0.75
Map: STEMS_PIANO_L; 4; 0.75
Map: STEMS_PIANO_R; 5; 0.75
Map: STEMS_GUITAR_L; 4; 0.75
Map: STEMS_GUITAR_R; 5; 0.75
Map: STEMS_BASS_L; 4; 0.75
Map: STEMS_BASS_R; 5; 0.75
Map: STEMS_DRUMS_L; 4; 0.75
Map: STEMS_DRUMS_R; 5; 0.75
Map: STEMS_OTHER_L; 4; 0.75
Map: STEMS_OTHER_R; 5; 0.75

' New in 0.2.0: Adding distance to the back channels
Dis: 4; 5

' FLAC output
Out: OUTPUT_FLAC
5 changes: 4 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def value_to_constant(value, filter_str=None):
return keys[0]

Versions = [
"1.0"
"1.0",
"1.1"
]

Channels = [
Expand Down Expand Up @@ -122,6 +123,8 @@ def value_to_constant(value, filter_str=None):
'LAYOUT_ATMOS_11_1_4': 'FL; FR; FC; LFE; BL; BR; FLC; FRC; BC; SL; SR; TBL; TBR',

# Stem configurations
'STEMS_MANUAL': -2, # Supported for Sep
'STEMS_SIMPLE': -1, # Supported for Sep
'STEMS_ALL': 0, # Supported for Sep
'STEMS_VOCALS': 1, # Supported for Sep
'STEMS_BASS': 2, # Supported for Sep
Expand Down
2 changes: 1 addition & 1 deletion legacy.sds
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%Srdr-1.0
%Srdr-1.1

Chs: CHANNELS_5_1; 16; 44100
Sep: STEMS_ALL
Expand Down
119 changes: 112 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import shutil
import subprocess
import json
import demucs.separate

parser = argparse.ArgumentParser(
prog='surrounder',
Expand All @@ -21,6 +20,10 @@
print(f'Input file {args.input} not found')
exit(1)

if os.path.isdir(args.input):
print(f"Notice: {args.input} is a directory, using the first file in this directory as the reference input.")
args.input = list(os.scandir(args.input))[0].path

if not os.path.exists(args.scene):
print(f'Scene file {args.scene} not found')
exit(1)
Expand Down Expand Up @@ -102,6 +105,10 @@
for (name, parameters) in operations:
match name:
case "Chs":
if len(parameters) != 3:
print(f"Expected 3 parameters but got {len(parameters)}.")
exit(2)

try:
channels = int(parameters[0])

Expand Down Expand Up @@ -168,6 +175,10 @@
f"./srdr_work/channels/{i}.wav"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
case "Sep":
if len(parameters) != 1:
print(f"Expected 1 parameter but got {len(parameters)}.")
exit(2)

if not os.path.exists(f"./srdr_work/input.wav"):
print(f"Separation is not ready: not configured.")
exit(2)
Expand All @@ -180,7 +191,15 @@
print(f"Invalid stem format value: {parameters[0]}")
exit(2)

if stem_format != 0:
if stem_format == -2:
if not os.path.exists(f"./stems"):
print("Could not find stems in a 'stems' folder.")
exit(2)

shutil.copytree(f"./stems", f"./srdr_work/stems")
elif stem_format == -1:
pass
elif stem_format != 0:
match stem_format:
case 1:
print("Isolating vocals")
Expand All @@ -206,11 +225,26 @@

args += ["./srdr_work/input.wav"]

print("Separating, this might take a while.")
demucs.separate.main(args)

os.rename("./srdr_work/stems_tmp/htdemucs_6s/input", "./srdr_work/stems")
shutil.rmtree('./srdr_work/stems_tmp')
if stem_format > -1:
import demucs.separate
print("Separating using machine learning, this might take a while.")
demucs.separate.main(args)
os.rename("./srdr_work/stems_tmp/htdemucs_6s/input", "./srdr_work/stems")
shutil.rmtree('./srdr_work/stems_tmp')
elif stem_format == -1:
os.mkdir("./srdr_work/stems")
subprocess.run([
"sox",
"./srdr_work/input.wav",
"./srdr_work/stems/other.wav"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run([
"sox",
"./srdr_work/input.wav",
"./srdr_work/stems/vocals.wav",
"highpass", "300",
"lowpass", "3500"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

if os.path.exists("./srdr_work/stems/vocals.wav"):
os.rename("./srdr_work/stems/vocals.wav", "./srdr_work/stems/1.wav")
Expand Down Expand Up @@ -302,6 +336,10 @@
"./srdr_work/stems/21.wav"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
case "Map":
if len(parameters) != 3:
print(f"Expected 3 parameters but got {len(parameters)}.")
exit(2)

try:
stem = int(parameters[0])
except ValueError:
Expand Down Expand Up @@ -357,6 +395,69 @@
os.unlink(f"./srdr_work/channels/{channel}.wav")
os.unlink(f"./srdr_work/stems/{stem}_2.wav")
os.rename(f"./srdr_work/channels/{channel}_2.wav", f"./srdr_work/channels/{channel}.wav")
case "Dis":
if len(parameters) != 2:
print(f"Expected 2 parameters but got {len(parameters)}.")
exit(2)

try:
channel1 = int(parameters[0])
except ValueError:
print(f"Invalid channel value: {parameters[0]}")
exit(2)

try:
channel2 = int(parameters[1])
except ValueError:
print(f"Invalid channel value: {parameters[1]}")
exit(2)

if not os.path.exists(f"./srdr_work/channels/{channel1}.wav"):
print(f"Invalid or nonexistent channel: {channel1}")
exit(2)

if not os.path.exists(f"./srdr_work/channels/{channel2}.wav"):
print(f"Invalid or nonexistent channel: {channel2}")
exit(2)

if layout is not None:
print(f"Distance: Channel {channel1} ({layout[channel1]}) <-> Channel {channel2} ({layout[channel2]})")
else:
print(f"Distance: Channel {channel1} <-> Channel {channel2}")

subprocess.run([
"sox",
"-M",
f"./srdr_work/channels/{channel1}.wav",
f"./srdr_work/channels/{channel2}.wav",
f"./srdr_work/channels/_2.wav",
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

subprocess.run([
"sox",
f"./srdr_work/channels/_2.wav",
f"./srdr_work/channels/_3.wav",
"remix", "1v-0.8718,2v0.4898", "1v0.4898,2v-0.8718",
"delay", "0.15"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

subprocess.run([
"ffmpeg",
"-y",
"-i", "./srdr_work/channels/_3.wav",
"-filter_complex", "[0:0]pan=1|c0=c0[left];[0:0]pan=1|c0=c1[right]",
"-map", "[left]",
f"./srdr_work/channels/{channel1}_2.wav",
"-map", "[right]",
f"./srdr_work/channels/{channel2}_2.wav",
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

os.unlink(f"./srdr_work/channels/{channel1}.wav")
os.unlink(f"./srdr_work/channels/{channel2}.wav")
os.unlink(f"./srdr_work/channels/_2.wav")
os.unlink(f"./srdr_work/channels/_3.wav")
os.rename(f"./srdr_work/channels/{channel1}_2.wav", f"./srdr_work/channels/{channel1}.wav")
os.rename(f"./srdr_work/channels/{channel2}_2.wav", f"./srdr_work/channels/{channel2}.wav")
case "Lay":
layout = parameters

Expand All @@ -367,6 +468,10 @@

print(f"Channel layout: {', '.join(layout)}")
case "Out":
if len(parameters) != 1:
print(f"Expected 1 parameter but got {len(parameters)}.")
exit(2)

if not os.path.exists(f"./srdr_work/input.wav"):
print(f"Output is not ready: not configured.")
exit(2)
Expand Down

0 comments on commit 1cd2398

Please sign in to comment.