#!/usr/bin/env python
import os
import argparse
import audio_degrader


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="""Process audio with a sequence of degradations
    Accepted degradadations:
        start,time: Remove audio until start. Value in seconds.
        mp3,quality: Mp3 compression. Value is quality (1-5)
        gain,db: Gain. Value is dB (e.g. gain,-20.3).
        normalize,percentage: Normalize. Percentage in 0.0-1.0 (1.0=full range)
        mix,"sound_path"//snr: Mix with sound at a specified SNR.
                               See --list-resources to check installed sounds.
        impulse-response,"impulse_response_path"//level: Apply impulse response
                                                         Level 0.0-1.0
                                See --list-resources to check installed sounds.
        dr-compression,degree: Dynamic range compression. Degree 1,2 or 3.
        time-stretching,ratio: Apply time streting.
        pitch-shifting,cents: Apply pitch shifting.
        eq,freq_hz//bw_hz//gain_db: Apply equalization with sox.
        """,
        formatter_class=argparse.RawTextHelpFormatter,
        epilog="Note: all audios are transcoded to mono, pcm_s16le")

    parser.add_argument('--in-wav',
                        type=str,
                        help='Input audio wav')
    parser.add_argument('--degradations', metavar='degradation,value',
                        type=str,
                        nargs='*',
                        help='List of sequential degradations')
    parser.add_argument('--out-wav',
                        type=str,
                        help='Output audio wav')
    parser.add_argument('--list-resources', action='store_true',
                        dest='list_resources',
                        help='List all available resources')
    args = vars(parser.parse_args())
    try:
        if args['list_resources']:
            print "Available resources"
            print "(see 'mix' and 'impulse-response' degradations):"
            for root, dirs, fnames in os.walk(
                    os.path.join(audio_degrader.__path__[0], 'resources')):
                for fname in fnames:
                    if os.path.splitext(fname)[1] == '.wav':
                        print "  " + os.path.join(root, fname)

            exit(0)

        audio_degrader.main(args['in_wav'],
                            args['degradations'],
                            args['out_wav'])
    except Exception:
        parser.print_help()
