I'm trying to pipe the output of sox into a python program like so:
sox <audio file name>.flac --type raw --encoding signed-integer - | python3 <file name>.py | head
I'm very unfamiliar with the command line, but I know I have to do it this way. I just have no idea how to actually access the data I'm piping into my program. IIRC when you pipe into a program it comes through sys.stdin instead of sys.argv, so what I've tried doing is:
pcm = sys.stdin.buffer.read().decode('utf-16')
but that throws the error: "UnicodeDecodeError: 'utf-16-be' codec can't decode bytes in position 8598-8599: illegal UTF-16 surrogate"
I also tried open(sys.stdin, 'rb')
but that gives me an error that's something like "expected str or ospath like object not '_io.TextIOWrapper' object"
I would like to be able to read the hexadecimal input in groups of 16bits, but I'm really lost. Would appreciate any help here. Thanks!
This should work fine:
./Generate16BitData | ./Readstdin.py
where the Python code is:
#!/usr/bin/env python3
import sys
import numpy as np
# Read entire input stream
data = sys.stdin.buffer.read()
print(f'Read {len(data)} bytes')
# Convert into Numpy array of np.uint16
na = np.frombuffer(data, dtype=np.uint16)
print(f'Numpy array shape: {na.shape}, dtype: {na.dtype}')
That runs like this:
dd if=/dev/urandom bs=1024 count=10 | ./Readstdin.py
10+0 records in
10+0 records out
10240 bytes transferred in 0.000087 secs (117670337 bytes/sec)
Read 10240 bytes
Numpy array shape: (5120,), dtype: uint16
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.