MerlijnWajer/SNARP — pull request #1 by casebeer (2013-09-08 23:48:04+00:00)
Title: Command line interface for SNARP

Hi,
First off, sorry to submit such a big, monolithic pull request. I've modified your SNARP script to accept options from the command line.

Since the original script was based pretty heavily around using hardcoded input specs to read data from an arecord subprocess, this involved pretty substantial changes, but I think I've managed to retain essentially all of the functionality.

Most of the INPUT global dict settings can now be specified on the command line, read using argparse. (Note for Python 2.6 and below, this means you must pip install argparse; Python 2.7 should have argparse already).

This includes allowing overriding of the Wave format standard endianness and signedness settings, just as the old INPUT dict allowed.

Other metadata is now read directly from the input wave file header rather than the INPUT config data. It's possible to read input wave data either from a disk file via, a CLI flag, or from STDIN, the default behavior.

I've stripped out the arecord subprocess handling, but I've added documentation and shell scripts that contain the previously hardcoded arecord params and pass its data in to SNARP over STDIN. The old snarp.py default behavior (record from `hw:0,0` using arecord) can be obtained by calling:

scripts/snarp-arecord-default.sh

Basic usage of the new CLI looks like:

cat input.wav | python snarp.py output.wav

or

python snarp.py output.wav < input.wav

or

python snarp.py -i input.wav output.wav

or

rec -t wav - | python snarp.py --silence-min -1024 --silence-max 1024 output.wav

or

python snarp.py -h

For a listing of options.

At the Python API level, these command line options I've discussed can be set via context managers: I've created managers to wrap the module global variables I've retained for INPUT_ENDIANNESS, INPUT_SIGNEDNESS (an override; defaults to allowing the wave bit depth to control the signedness, per the spec), SILENCE_MIN, and SILENCE_MAX.

The new code API goes something like:

```python
import snarp

with open("input.wav", "rb") as input:
with open("output.wav", "wb") as output:
with snarp.silence_levels(120, 135):
snarp.remove_silences(input, output)
```

To use the endianness and signedness overrides, just use their context managers similarly:

```python
...
with snarp.input_endianness('big'):
with snarp.input_signedness('unsigned'):
snarp.remove_silences(...)
...
```

I've also added README documentation, a Makefile for basic cleanup, and some extremely rudimentary unit tests (along with test WAV data).

I'm next intending to tweak the silence detection algorithm for clarity and flexibility (e.g. easier or automatic spec of max/min levels, e.g. generators for input samples rather than iterating over blocks, etc), and to create a distutils package for easier CLI install. In particular, I think allowing specification of a max sample delta (i.e. max(sample_max, sample_min) rather than an explicit max and min could be a bit more robust. It's probably also a good idea to scale the max/min/delta limits to match the sample bit depth and endianness automatically.

These changes are actually probably more straightforward than this pull request's, but I figured it'd be better to clean up the input and output settings before submitting changes to the algorithm.

If you have any questions or suggestions, please feel free to contact me.

Thanks,

Chris