]>
git.sthu.org Git - echo-analysis.git/blob - util/echorec.py
2 """A wrapper module for echorec."""
4 import subprocess
as sp
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."""
14 exe
= exe
or 'echorec'
16 args
+= ['--count=%d' % count
]
18 ret
= sp
.run([exe
] + args
, stdout
=sp
.PIPE
, stderr
=sp
.PIPE
, input=play_buffer
)
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]."""
29 tsamples
= np
.arange(starttime
, endtime
, 1.0/rate
)
31 channel
= [amp
* play_signal(t
) for t
in tsamples
]
32 play_buffer
= np
.array([channel
, channel
], dtype
=np
.int16
).T
.flatten()
34 args
= ['--rate=%d' % rate
]
36 return echorec_buffer(play_buffer
.tobytes(), args
=args
, *pargs
, **kwargs
)