]> git.sthu.org Git - echo-analysis.git/blob - util/echorec.py
First commit
[echo-analysis.git] / util / echorec.py
1 #!/usr/bin/env python3
2 """A wrapper module for echorec."""
3
4 import subprocess as sp
5 import numpy as np
6
7
8 def echorec_buffer(play_buffer, count=1, args=None, exe=None):
9 """Invokes echorec. It passes play_buffer to stdin of rechorec and has to
10 be a byte sequence. The output from stdout is captured and returned.
11 Additional options are to be passed by means of subprocess.run(). The path
12 to the echorec executeable can be passed via exe."""
13
14 exe = exe or 'echorec'
15 args = args or []
16 args += ['--count=%d' % count]
17
18 ret = sp.run([exe] + args, stdout=sp.PIPE, stderr=sp.PIPE, input=play_buffer)
19 return ret.stdout
20
21
22 def echorec_lambda(play_signal, starttime, endtime, rate, *pargs, **kwargs):
23 """Invokes echorec by means of echorec_buffer(). The play buffer is formed
24 by the function play_signal that is evaluated on the interval [startime,
25 endtime) with the given sample rate. It is assumed that play_signal
26 produces values in the interval [-1, 1]."""
27
28 amp = (1 << 15) - 1
29 tsamples = np.arange(starttime, endtime, 1.0/rate)
30
31 channel = [amp * play_signal(t) for t in tsamples]
32 play_buffer = np.array([channel, channel], dtype=np.int16).T.flatten()
33
34 args = ['--rate=%d' % rate]
35
36 return echorec_buffer(play_buffer.tobytes(), args=args, *pargs, **kwargs)