CLI is great - some early examples

Okay one more for fun.

This is a server in python that can dispatch asynchronous calls to your shell. A folder is watched for any files that are added to it and if it detects a new file it automatically calls the shell to run hpss on it. You can decide if you want the output to be relative to the file or in an absolute location. The best part is, you don’t have to wait for one process to finish before you add another file - you can just keep adding files to the folder and it will queue it up. The next stage is to make it run multiple processes simultaneously :slight_smile:

This one isn’t super friendly if you haven’t used python before and I definitely haven’t tested it. It will try and hpss anything you put in the file, and there is no protections against this however the script probably wont execute which won’t stop the overall server from running.

This requires Python 3.6 and watchdog which can be installed with pip install watchdog

import logging
import sys
import time
import os
import subprocess

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

logging.basicConfig(level=logging.DEBUG)

class MyEventHandler(FileSystemEventHandler):
    def __init__(self):
        self.creation_rule = ''
        self.file_name = ''
        self.only_file = ''
        self.dir_name = ''
        self.base = ''
        self.abs_dir = None

    def catch_all_handler(self, event):
        logging.debug(event)

    def on_moved(self, event):
        self.catch_all_handler(event)

    def on_created(self, event):
        self.catch_all_handler(event)
        self.file_name = str(event.src_path)
        self.base = os.path.basename(self.file_name)
        self.only_file = os.path.splitext(self.base)[0]

        if self.creation_rule == 'Relative':
            self.dir_name = f'{os.path.dirname(self.file_name)}/{self.only_file}_proc'
            try:
                os.makedirs(self.dir_name)
            except FileExistsError:
                print('Files possibly being overwritten or already exist')
                pass

        elif self.creation_rule == 'Absolute':
            self.dir_name = f'{self.abs_dir}/{self.only_file}_proc/'
            if self.abs_dir != None:
                try:
                    os.makedirs(self.dir_name)
                except FileExistsError:
                    print('Files possibly being overwritten or already exist')
                    pass

        cmd = ['hpss', '-source', self.file_name, '-harmonic', f'{self.dir_name}/{self.only_file}-harm.wav', '-percussive', f'{self.dir_name}/{self.only_file}-perc.wav']
        subprocess.call(cmd)

    def on_deleted(self, event):
        self.catch_all_handler(event)

    def on_modified(self, event):
        self.catch_all_handler(event)

### Set your path here that you want to watch. Make sure you leave the backslash off at the end
path = '/Users/jamesbradbury/dev'

event_handler = MyEventHandler()
event_handler.creation_rule = 'Absolute' ### You can set this string to 'Relative' or 'Absolute' to change how the output file structure works
event_handler.abs_dir = '/Users/jamesbradbury/_abs_processing'
observer = Observer()
observer.schedule(event_handler, path, recursive=False)
observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()
2 Likes