Esempio n. 1
0
def test_evaluate(backend):
    with TemporaryDirectory() as directory:
        generate_fake_eval_dataset(directory)
        p = create_argument_parser()
        arguments = p.parse_args(["evaluate", "-p", "spleeter:4stems", "--mus_dir", directory, "-B", backend])
        params = load_configuration(arguments.configuration)
        metrics = evaluate.entrypoint(arguments, params)
        for instrument, metric in metrics.items():
            for m, value in metric.items():
                assert np.allclose(np.median(value), res_4stems[instrument][m], atol=1e-3)
Esempio n. 2
0
def test_evaluate(path="FAKE_MUSDB_DIR"):
    generate_fake_eval_dataset(path)
    p = create_argument_parser()
    arguments = p.parse_args(
        ["evaluate", "-p", "spleeter:4stems", "--mus_dir", path])
    params = load_configuration(arguments.configuration)
    metrics = evaluate.entrypoint(arguments, params)
    for instrument, metric in metrics.items():
        for metric, value in metric.items():
            assert np.allclose(np.median(value),
                               res_4stems[instrument][metric],
                               atol=1e-3)
Esempio n. 3
0
def test_train():


    with TemporaryDirectory() as path:

        # generate training dataset
        generate_fake_training_dataset(path)

        # set training command aruments
        p = create_argument_parser()
        arguments = p.parse_args(["train", "-p", "useless_config.json", "-d", path])
        TRAIN_CONFIG["train_csv"] = join(path, "train", "train.csv")
        TRAIN_CONFIG["validation_csv"] = join(path, "train", "train.csv")
        TRAIN_CONFIG["model_dir"] = join(path, "model")
        TRAIN_CONFIG["training_cache"] = join(path, "cache", "training")
        TRAIN_CONFIG["validation_cache"] = join(path, "cache", "validation")

        # execute training
        res = train.entrypoint(arguments, TRAIN_CONFIG)

        # assert that model checkpoint was created.
        assert os.path.exists(join(path,'model','model.ckpt-10.index'))
        assert os.path.exists(join(path,'model','checkpoint'))
        assert os.path.exists(join(path,'model','model.ckpt-0.meta'))
def main():
    # Figure out where we are
    working = str((os.getcwdb()))
    try:
        os.system("title Spleeter GUI by Dozza")
        logging.info("""  
         ██████  ██▓███   ██▓    ▓█████ ▓█████▄▄▄█████▓▓█████  ██▀███  
        ▒██    ▒ ▓██░  ██▒▓██▒    ▓█   ▀ ▓█   ▀▓  ██▒ ▓▒▓█   ▀ ▓██ ▒ ██▒
        ░ ▓██▄   ▓██░ ██▓▒▒██░    ▒███   ▒███  ▒ ▓██░ ▒░▒███   ▓██ ░▄█ ▒
          ▒   ██▒▒██▄█▓▒ ▒▒██░    ▒▓█  ▄ ▒▓█  ▄░ ▓██▓ ░ ▒▓█  ▄ ▒██▀▀█▄  
        ▒██████▒▒▒██▒ ░  ░░██████▒░▒████▒░▒████▒ ▒██▒ ░ ░▒████▒░██▓ ▒██▒
        ▒ ▒▓▒ ▒ ░▒▓▒░ ░  ░░ ▒░▓  ░░░ ▒░ ░░░ ▒░ ░ ▒ ░░   ░░ ▒░ ░░ ▒▓ ░▒▓░
        ░ ░▒  ░ ░░▒ ░     ░ ░ ▒  ░ ░ ░  ░ ░ ░  ░   ░     ░ ░  ░  ░▒ ░ ▒░
        ░  ░  ░  ░░         ░ ░      ░      ░    ░         ░     ░░   ░ 
              ░               ░  ░   ░  ░   ░  ░           ░  ░   ░ 
        GUI by Dozza""")
        # Leave this code here cos it gives us a nice default argument to then parse shit into
        mode = ""
        while mode not in ["1", "2"]:
            mode = input(
                "\nWhich mode would you like?\n"
                "    1. Spleeter from MP3 file\n"
                "    2. Search youtube and download mp3 from top 10 list.")
        if mode == "1":  # Spleeter from mp3 mode.
            # make a TK instance and minimise it, ask for a file name to return to spleeter, then exit.
            root = Tk()
            root.withdraw()
            filename = filedialog.askopenfilename(
                initialdir=os.environ["HOMEPATH"] + "/Desktop",
                title="Select file",
                filetypes=(("mp3 audio", ".mp3"), ("all files", ".*")))
            root.destroy()

        if mode == "2":
            # input the search term for VideosSearch, store the top 10 results by name and Url
            searchterm = input("Enter search term:")
            search = VideosSearch(searchterm, limit=10).result()["result"]
            results = []
            # Unpack the videosearch generator.
            for i, x in enumerate(search):
                results.append(x)

            #parse the results of the search into useful parameters.
            for index, result in enumerate(results):
                print(
                    f"    Result {index}: {result['title']}, {result['duration']}"
                )

            # Choose a result.
            while True:
                selected_option = input(
                    "Which option would you like to pick?\n")
                if 0 <= int(selected_option) < 10:
                    break

            selected_result = results[int(selected_option)]

            # setup for youtube downloader. we can use high quality here since it is volatile and spleeter
            ydl_opts = {
                'format':
                'bestaudio/best',
                'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '320',
                }],
                'verbose':
                True,
                'outtmpl':
                '%(title)s.%(ext)s'
            }
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                downloaded_filename = ydl.prepare_filename(
                    selected_result)[:-3] + '.mp3'
                logging.info(f"Saving file as {downloaded_filename}")
                ydl.download([selected_result['link']])
            # the mp3 should be present in the working directory now.
            # get the filename

            filename = os.path.join(os.getcwd(), downloaded_filename)
            if not os.path.isfile(filename):
                root = Tk()
                root.withdraw()
                filename = filedialog.askopenfilename(
                    initialdir=os.cwd(),
                    title="Select file",
                    filetypes=(("supporter audio", ".mp3 .wav "), ("all files",
                                                                   ".*")))
                root.destroy()
        #
        logging.debug(f"Output full path: {filename}")
        if filename is "":
            # avoid errors when quitting the spleeter GUI
            sys.exit(0)
        [head, tail] = os.path.split(filename)
        arguments = create_argument_parser().parse_args([
            'separate', '-i', filename, '-o', 'audio_output', '-p',
            'spleeter:5stems-16kHz'
        ])
        # ok so now we add our own spicy c-c-custom code.
        arguments.codec = tail.split(".")[1]
        # arguments.command = 'separate'
        # arguments.configuration = 'spleeter:5stems-16kHz'
        # arguments.duration = 600
        arguments.bitrate = '256k'
        arguments.filename_format = '{filename}/{instrument}.{codec}'
        # arguments.offset = 0.0
        # arguments.output_path = 'audio_output'
        arguments.verbose = True
        logging.debug(f"Loaded {tail} from {head}")
        logging.debug("Processing...")
        enable_logging()
        if arguments.verbose:
            enable_tensorflow_logging()
        if arguments.command == 'separate':
            from spleeter.commands.separate import entrypoint
        params = load_configuration(arguments.configuration)
        logging.info("Spleeting...")
        entrypoint(arguments, params)
        logging.info("Done!")

        #remove the downloaded mp3 afterwards
        if mode == "2":
            os.remove(filename)

        #pop open the output folder
        outputdir = '"' + working[
            2:-1] + '\\' + arguments.output_path + '\\' + tail[
                0:-4] + '\\' + '"'
        os.startfile(outputdir)

        # audacity time
        os.startfile(AUDACITY_PATH)
        if sys.platform == 'win32':
            logging.debug("pipe-test.py, running on windows")
            TONAME = '\\\\.\\pipe\\ToSrvPipe'
            FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
            EOL = '\r\n\0'
        else:
            logging.debug("pipe-test.py, running on linux or mac")
            TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
            FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
            EOL = '\n'

        logging.debug("Write to  \"" + TONAME + "\"")
        while not os.path.exists(TONAME):
            logging.error(
                " ..does not exist.  Ensure Audacity is running with mod-script-pipe."
            )
            time.sleep(1)

        logging.debug("Read from \"" + FROMNAME + "\"")
        while not os.path.exists(FROMNAME):
            logging.error(
                " ..does not exist.  Ensure Audacity is running with mod-script-pipe."
            )
            time.sleep(1)

        time.sleep(5)

        logging.debug("-- Both pipes exist.  Good.")

        TOFILE = open(TONAME, 'w')
        logging.debug("-- File to write to has been opened")
        FROMFILE = open(FROMNAME, 'rt')
        logging.debug("-- File to read from has now been opened too\r\n")

        def send_command(command):
            """Send a single command."""
            logging.debug("Send: >>> \n" + command)
            TOFILE.write(command + EOL)
            TOFILE.flush()

        def get_response():
            """Return the command response."""
            result = ''
            line = ''
            while True:
                result += line
                line = FROMFILE.readline()
                if line == '\n' and len(result) > 0:
                    break
            return result

        def do_command(command):
            """Send one command, and return the response."""
            send_command(command)
            response = get_response()
            logging.debug("Rcvd: <<< \n" + response)
            return response

        # construct import paths for audio
        importPath = working[2:-1] + '/' + arguments.output_path + '/' + tail[
            0:-4] + '/'
        importPath = os.path.normpath(importPath)
        do_command('Import2: Filename="' +
                   os.path.join(importPath, 'bass.mp3"'))
        do_command('Import2: Filename="' +
                   os.path.join(importPath, 'vocals.mp3"'))
        do_command('Import2: Filename="' +
                   os.path.join(importPath, 'piano.mp3"'))
        do_command('Import2: Filename="' +
                   os.path.join(importPath, 'other.mp3"'))
        do_command('Import2: Filename="' +
                   os.path.join(importPath, 'drums.mp3"'))

        # Imported!
        logging.info("files imported successfully...")
        sys.exit(1)

    except SpleeterError as e:
        get_logger().error(e)