Mercurial > hg > simscene-py
view simscene.py @ 5:42f189846ba8
More options parsing, can read scores in csv,json,xls as well as annotation data
author | Emmanouil Thoefanis Chourdakis <e.t.chourdakis@qmul.ac.uk> |
---|---|
date | Mon, 02 Oct 2017 15:54:26 +0100 |
parents | 94eb0280ad4a |
children | f5edaa5ca167 |
line wrap: on
line source
#!/bin/python # -*- coding: utf-8 -*- # For licensing please see: LICENSE # Copyright (c) Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk> import argparse import logging import pandas as pd import sys from tabulate import tabulate def read_events_file(fname): if fname[-3:].lower() == 'xls': df = pd.read_excel(fname) elif fname[-4:].lower() == 'json': df = pd.read_json(fname) elif fname[-3:].lower() in ['txt', 'csv']: with open(fname) as f: header = f.readline() s = f.readline() f.seek(0,0) if ',' in s: sep = ',' elif '\t' in s: sep = '\t' else: sep = ' ' if sep in header: logging.warning('Probably no header or malformed .csv. Will try to parse it raw.') df = pd.read_csv(f, header=None, sep=sep) else: df = pd.read_csv(f, sep=sep) df = None df.columns = ['label','sampleid','ebr','ebr_stddev','mean_time_between_instances','time_between_instances_stddev','start_time','end_time','fade_in_time','fade_out_time'] logging.info('Using input:\n'+tabulate(df, headers='keys', tablefmt='psql')) return df def read_backgrounds_file(fname): if fname[-3:].lower() == 'xls': df = pd.read_excel(fname) elif fname[-4:].lower() == 'json': df = pd.read_json(fname) elif fname[-3:].lower() in ['txt', 'csv']: with open(fname) as f: header = f.readline() s = f.readline() f.seek(0,0) if ',' in s: sep = ',' elif '\t' in s: sep = '\t' else: sep = ' ' if sep in header: logging.warning('Probably no header or malformed .csv. Will try to parse it raw.') df = pd.read_csv(f, header=None, sep=sep) else: df = pd.read_csv(f, sep=sep) df = None df.columns = ['label','sampleid','snr'] logging.info('Using input:\n'+tabulate(df, headers='keys', tablefmt='psql')) return df def read_annotations_file(fname): if fname[-3:].lower() == 'xls': df = pd.read_excel(fname) elif fname[-4:].lower() == 'json': df = pd.read_json(fname) elif fname[-3:].lower() in ['txt', 'csv']: with open(fname) as f: header = f.readline() s = f.readline() f.seek(0,0) if ',' in s: sep = ',' elif '\t' in s: sep = '\t' else: sep = ' ' if sep in header: logging.warning('Probably no header or malformed .csv. Will try to parse it raw.') df = pd.read_csv(f, header=None, sep=sep) df.columns = ['start', 'stop', 'class'] else: df.columns = ['start', 'stop', 'class'] df = pd.read_csv(f, sep=sep) df = None logging.info('Using input:\n'+tabulate(df, headers='keys', tablefmt='psql')) return df def run_demo(): print("TODO: Implement run_demo()") def simscene(input_path, output_path, scene_duration, score_events, score_backgrounds, **kwargs): logging.info('simscene() is not yet implemented') def not_implemented(): print("TODO: not implemented") if __name__=="__main__": """ Main function, parses options and calls the simscene generation function or a demo. The options given are almost identical to Lagrange et al's simscene. """ argparser = argparse.ArgumentParser( description="SimScene.py acoustic scene generator", ) argparser.add_argument( 'input_path', type=str, help="Path of a directory containing wave files for sound backgrounds (in the `background' sub-directory) or events (in `event')" ) argparser.add_argument( 'output_path', type=str, help="The directory the generated scenes and annotations will reside." ) argparser.add_argument( 'scene_duration', type=float, help="Duration of scene in seconds", ) scene_duration = None argparser.add_argument( '-e', '--score-events', type=str, help="Score events file as a comma-separated text file (.csv, .txt), JSON (.json), or Excel (.xls) file" ) score_events = None argparser.add_argument( '-b', '--score-backgrounds', type=str, help="Score backgrounds file as a comma-separated text file (.csv, .txt), JSON (.json), or Excel (.xls) file" ) score_backgrounds = None argparser.add_argument( '-t', '--time-mode', type=str, help="Mode of spacing between events. `generate': values must be set for each track in the score files. `abstract': values are computed from an abstract representation of an existing acoustic scene. `replicate': values are replicated from an existing acousting scene.", choices=['generate', 'abstract', 'replicate'] ) time_mode = 'generate' argparser.add_argument( '-R', '--ebr-mode', type=str, help="Mode for Event to Background power level ratio. `generate': values must be set for each track in the score files. `abstract': values are computed from an abstract representation of an existing acoustic scene. `replicate': values are replicated from an existing acousting scene.", choices=['generate', 'abstract', 'replicate'] ) ebr_mode = 'generate' argparser.add_argument( '-A', '--annotation-file', type=float, help="If -R or -m are selected, this provides the source for sourcing the times or EBRs from ANNOTATION_FILE. ANNOTATION_FILE must be comma-separated text file (.csv, .txt), JSON (.json), or Excel (.xls)." ) annotation_file = None argparser.add_argument( '-a', '--audio-file', type=float, help="If -R or -m are selected, this provides the source for sourcing the times or EBRs from AUDIO_FILE. AUDIO_FILE must be a 44100Hz .wav file." ) audio_file = None argparser.add_argument( '-v', '--figure-verbosity', action='count', help="Increase figure verbosity. (Default) 0 - Don't save or display figures, 1 - Save pictures but do not display them, 2 - Save and display figures" ) figure_verbosity = None argparser.add_argument( '-C', '--channel-mode', type=str, help="number of audio channels contained in file. (Default) 'mono' - 1 channel (mono), 'classes' - As many channels as sound classes (events+textures), 'separate' - Same as 'classes', each channel is saved in a separate .wav file.", choices=['mono', 'classes', 'separate'] ) channel_mode = None argparser.add_argument( '-m', '--min-space', type=float, help="Minimum space allowed between successive events (seconds). If -1, then allow overlapping between events." ) min_space = None argparser.add_argument( '-c', '--end-cut', action='store_true', help="If the last sample ends after the scene ends then: if enabled, cut the sample to duration, else remove the sample." ) end_cut = None logging.basicConfig(level=logging.DEBUG) args = argparser.parse_args() if args.input_path: input_path = args.input_path logging.debug("Using `{}' as input path".format(input_path)) if args.output_path: output_path = args.output_path logging.debug("Saving to `{}'".format(output_path)) if args.scene_duration: if not (args.score_backgrounds or args.score_events): print("You must provide one of -e or -b") else: if args.ebr_mode: ebr_mode = args.ebr_mode if ebr_mode not in ['generate']: logging.warning("`{}' not yet implemented for EBR_MODE, using default.".format(ebr_mode)) ebr_mode = 'generate' if args.time_mode: time_mode = args.time_mode if time_mode not in ['generate']: logging.warning("`{}' not yet implemented for TIME_MODE, using default.".format(time_mode)) time_mode = 'generate' if args.annotation_file: annotations = read_annotations_file(args.annotation_file) scene_duration = float(args.scene_duration) if args.score_backgrounds: score_backgrounds = read_backgrounds_file(args.score_backgrounds) if args.score_events: score_events = read_events_file(args.score_events) simscene(input_path, output_path, scene_duration, score_events, score_backgrounds, time_mode=time_mode, ebr_mode=ebr_mode, channel_mode=channel_mode, annotation_file=annotation_file, audio_file=audio_file, figure_verbosity=figure_verbosity, min_space=min_space, end_cut=end_cut)