Skip to content

Instantly share code, notes, and snippets.

@hyperknot
Last active April 23, 2022 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hyperknot/46ff18b46c43269a70e637d1f7158b74 to your computer and use it in GitHub Desktop.
Save hyperknot/46ff18b46c43269a70e637d1f7158b74 to your computer and use it in GitHub Desktop.
DSD to FLAC conversion using SoX DSD. Tries best gain (+6, +5, +4, etc.) which doesn't clip. You can get SoX DSD binaries from https://audiodigitale.eu/repo/sox/
#!/usr/bin/env python3
import pathlib
import subprocess
import sys
import shutil
root = pathlib.Path(__file__).parent.resolve()
target_root = root / 'dsd'
def convert_folder(folder, gain):
print(f' gain: {gain}')
files = folder.glob('*.dsf')
for file in files:
relative_dir = file.parent.relative_to(root)
target_dir = target_root / relative_dir
target_dir.mkdir(exist_ok=True, parents=True)
tmp_path = target_dir / 'tmp.flac'
tmp_path.unlink(missing_ok=True)
target_path = target_dir / (file.stem + '.flac')
if target_path.is_file():
continue
print(f' {target_path}')
cmd = [
'soxdsd',
'-V3',
file,
'--bits',
'24',
tmp_path,
#
'rate',
'-ub74',
'88200',
#
'gain',
str(gain),
#
'sinc',
'-25k',
]
p = subprocess.run(cmd, text=True, capture_output=True)
if 'decrease volume' in p.stderr:
return dict(success=False, target_dir=target_dir)
tmp_path.rename(target_path)
return dict(success=True, target_dir=target_dir)
def main():
dsd_folders = set()
dsd_files = root.glob('**/*.dsf')
for file in dsd_files:
folder = file.parent
dsd_folders.add(folder)
for folder in sorted(dsd_folders):
print(f'\n\nProcessing {folder}')
for gain in range(6, -7, -1):
r = convert_folder(folder, gain)
if r['success']:
break
shutil.rmtree(r['target_dir'])
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment