Convert samples to mono
| #!/usr/bin/env python | |
| """ | |
| mo' noise | |
| monome + noise | |
| mono-ise | |
| """ | |
| # | |
| # You will need sox and pysox | |
| # https://github.com/rabitt/pysox#install | |
| # | |
| # | |
| # Run in the directory containing the samples, with | |
| # | |
| # python /wherever/you/saved/monoise.py | |
| # | |
| # or make it executable and add the location to your PATH. | |
| # | |
| # | |
| # The defaults are WAV file inputs and a target of 48Khz / 24-bit files. | |
| # You can change this with the switches -e (or --ext), -r (or --rate), and -b | |
| # (or --bits): | |
| # | |
| # python -e aiff -r 44100 -b 16 /wherever/you/saved/monoise.py | |
| # | |
| # There is a shortcut for 44.1KHz / 16 bits, -R (or --radio). | |
| # | |
| # | |
| # This will create a "monoise" directory at that location, and recreate | |
| # the original directory structure inside that, with all files converted | |
| # to mono. It will refuse to run if the "monoise" directory already exists. | |
| # | |
| import os | |
| import sys | |
| from argparse import ArgumentParser | |
| from shutil import copyfile | |
| import sox | |
| P = ArgumentParser() | |
| P.add_argument('-e', '--ext', type=str, default='wav', | |
| help='File extension to look for') | |
| P.add_argument('-r', '--rate', type=int, default=48000, | |
| help='Target sample rate') | |
| P.add_argument('-b', '--bits', type=int, default=24, | |
| help='Target bit rate') | |
| P.add_argument('-R', '--radio', action='store_true') | |
| ARGS = P.parse_args() | |
| EXT = f'.{ARGS.ext}' | |
| TARGET_EXT = 'wav' | |
| RADIO_RATE = 44100 | |
| RADIO_BITS = 16 | |
| OUTPUT_DIR = 'monoise' | |
| if ARGS.radio: | |
| RATE = RADIO_RATE | |
| BITS = RADIO_BITS | |
| else: | |
| RATE = ARGS.rate | |
| BITS = ARGS.bits | |
| if os.path.exists(OUTPUT_DIR): | |
| print(f'A "{OUTPUT_DIR}" directory already exists, bailing out.') | |
| sys.exit(1) | |
| else: | |
| os.mkdir(OUTPUT_DIR) | |
| TFM = sox.Transformer() | |
| TFM.set_output_format(file_type=TARGET_EXT, rate=RATE, bits=BITS, channels=1) | |
| # 1. walk dir and look for input files | |
| # 3. if the file has more than 1 channel | |
| # 4. convert to mono | |
| # 5. write out to 'monoise/...' # easier than looking for them afterward | |
| # 6. else | |
| # 7. copy to 'monoise/...' | |
| for root, _, files in os.walk('.'): | |
| if root.startswith(os.path.join('.', OUTPUT_DIR)): | |
| continue | |
| for file in files: | |
| if file.endswith(EXT): | |
| current = os.path.join(root, file) | |
| dir_name = root.replace('./', '') | |
| target_dir = os.path.join('.', OUTPUT_DIR, dir_name) | |
| file_name, _ = os.path.splitext(file) | |
| mono_file = os.path.join(target_dir, f'{file_name}.{TARGET_EXT}') | |
| if not os.path.exists(target_dir): | |
| os.makedirs(target_dir) | |
| if ( | |
| sox.file_info.channels(current) != 1 | |
| or sox.file_info.sample_rate(current) != RATE | |
| or sox.file_info.bitdepth(current) != BITS | |
| ): | |
| TFM.build(current, mono_file) | |
| else: | |
| copyfile(current, mono_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment