mirror of
https://github.com/romanz/amodem.git
synced 2026-04-19 04:36:02 +08:00
use wave.py to play and record audio at test.sh
This commit is contained in:
20
test.sh
20
test.sh
@@ -1,9 +1,19 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -u
|
set -u
|
||||||
set -x
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
dd if=/dev/urandom of=data.send bs=1024 count=128
|
dd if=/dev/urandom of=data.send bs=1024 count=1
|
||||||
python send.py
|
./send.py
|
||||||
python recv.py
|
|
||||||
python errors.py data.* #python show.py tx.int16 rx.int16
|
./wave.py record rx.int16 &
|
||||||
|
PID=$!
|
||||||
|
|
||||||
|
./wave.py play tx.int16
|
||||||
|
sleep 1
|
||||||
|
kill -INT $PID
|
||||||
|
|
||||||
|
./recv.py
|
||||||
|
./errors.py data.*
|
||||||
|
if [ -z $? ]; then
|
||||||
|
./show.py tx.int16 rx.int16
|
||||||
|
fi
|
||||||
28
wave.py
Normal file → Executable file
28
wave.py
Normal file → Executable file
@@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import subprocess as sp
|
import subprocess as sp
|
||||||
@@ -6,15 +7,38 @@ import logging
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
from common import Fs
|
from common import Fs
|
||||||
|
Fs = int(Fs)
|
||||||
|
|
||||||
def play(fname, **kwargs):
|
def play(fname, **kwargs):
|
||||||
return launch('aplay', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', str(int(Fs)), **kwargs)
|
return launch('aplay', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', Fs, **kwargs)
|
||||||
|
|
||||||
def record(fname, **kwargs):
|
def record(fname, **kwargs):
|
||||||
return launch('arecord', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', str(int(Fs)), **kwargs)
|
return launch('arecord', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', Fs, **kwargs)
|
||||||
|
|
||||||
def launch(*args, **kwargs):
|
def launch(*args, **kwargs):
|
||||||
|
args = map(str, args)
|
||||||
log.debug('$ %s', ' '.join(args))
|
log.debug('$ %s', ' '.join(args))
|
||||||
p = sp.Popen(args=args, **kwargs)
|
p = sp.Popen(args=args, **kwargs)
|
||||||
p.stop = lambda: os.kill(p.pid, signal.SIGINT)
|
p.stop = lambda: os.kill(p.pid, signal.SIGINT)
|
||||||
return p
|
return p
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import logging
|
||||||
|
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
|
||||||
|
import argparse
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
subparsers = parser.add_subparsers()
|
||||||
|
fmt = 'a raw audio file (16 bits at {:.1f} kHz)'.format(Fs / 1e3)
|
||||||
|
recorder = subparsers.add_parser('record', help='record ' + fmt)
|
||||||
|
recorder.add_argument('filename', default='-', help='path to the audio file to record (otherwise, use stdout)')
|
||||||
|
recorder.set_defaults(func=record)
|
||||||
|
|
||||||
|
player = subparsers.add_parser('play', help='play ' + fmt)
|
||||||
|
player.add_argument('filename', default='-', help='path to the audio file to play (otherwise, use stdin)')
|
||||||
|
player.set_defaults(func=play)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
p = args.func(args.filename)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
sys.exit(p.wait())
|
||||||
|
|||||||
Reference in New Issue
Block a user