Ran everything through the 'black' formatter

This commit is contained in:
Jono Targett 2023-06-15 15:16:24 +09:30
parent 873b5ad6b5
commit 3206a9b1ff
10 changed files with 444 additions and 382 deletions

View File

@ -6,7 +6,7 @@ import os
class FileRadio(Streamer): class FileRadio(Streamer):
REST_PATH = 'sample' REST_PATH = "sample"
def __init__(self, path): def __init__(self, path):
super().__init__() super().__init__()
@ -17,22 +17,26 @@ class FileRadio(Streamer):
def _stream_thread(self): def _stream_thread(self):
self.playback = subprocess.Popen( self.playback = subprocess.Popen(
[ [
'/usr/bin/ffmpeg', "/usr/bin/ffmpeg",
'-re', # http://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag "-re", # http://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag
'-stream_loop', '-1', # Loop the stream indefinitely "-stream_loop", # Loop the stream -
'-i', self.path, "-1", # ...indefinitely
'-c', 'copy', "-i",
'-f', 'rtsp', self.path,
self.stream_address('localhost') "-c",
"copy",
"-f",
"rtsp",
self.stream_address("localhost"),
], ],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL stderr=subprocess.DEVNULL,
) )
while self.run: while self.run:
if not is_alive(self.playback): if not is_alive(self.playback):
print('Playback failed, aborting stream.', file=sys.stderr) print("Playback failed, aborting stream.", file=sys.stderr)
break break
time.sleep(0.1) time.sleep(0.1)
@ -43,9 +47,8 @@ class FileRadio(Streamer):
self.playback = None self.playback = None
if __name__ == "__main__":
if __name__ == '__main__': fr = FileRadio("./data/sampleaudio/taunt.mp3")
fr = FileRadio('./data/sampleaudio/taunt.mp3')
fr.start_stream() fr.start_stream()
while True: while True:

View File

@ -10,8 +10,9 @@
import numpy, math, sys, time import numpy, math, sys, time
from numpy import fft from numpy import fft
def impulse(mask): def impulse(mask):
''' Convert frequency domain mask to time-domain ''' """Convert frequency domain mask to time-domain"""
# Negative side, a mirror of positive side # Negative side, a mirror of positive side
negatives = mask[1:-1] negatives = mask[1:-1]
negatives.reverse() negatives.reverse()
@ -30,7 +31,7 @@ def impulse(mask):
def lo_mask(sample_rate, tap_count, freq, dboct): def lo_mask(sample_rate, tap_count, freq, dboct):
''' Create a freq domain mask for a lowpass filter ''' """Create a freq domain mask for a lowpass filter"""
order = dboct / 6 order = dboct / 6
max_freq = sample_rate / 2.0 max_freq = sample_rate / 2.0
f2s = max_freq / (tap_count / 2.0) f2s = max_freq / (tap_count / 2.0)
@ -45,7 +46,7 @@ def lo_mask(sample_rate, tap_count, freq, dboct):
def hi_mask(sample_rate, tap_count, freq, dboct): def hi_mask(sample_rate, tap_count, freq, dboct):
''' Create a freq domain mask for a highpass filter ''' """Create a freq domain mask for a highpass filter"""
order = dboct / 6 order = dboct / 6
max_freq = sample_rate / 2.0 max_freq = sample_rate / 2.0
f2s = max_freq / (tap_count / 2.0) f2s = max_freq / (tap_count / 2.0)
@ -60,7 +61,7 @@ def hi_mask(sample_rate, tap_count, freq, dboct):
def combine_masks(mask1, mask2): def combine_masks(mask1, mask2):
''' Combine two filter masks ''' """Combine two filter masks"""
assert len(mask1) == len(mask2) assert len(mask1) == len(mask2)
return [mask1[i] * mask2[i] for i in range(0, len(mask1))] return [mask1[i] * mask2[i] for i in range(0, len(mask1))]
@ -88,7 +89,7 @@ class filter:
def feed(self, original): def feed(self, original):
unfiltered = numpy.concatenate((self.buf, original)) unfiltered = numpy.concatenate((self.buf, original))
self.buf = unfiltered[-len(self.coefs) :] self.buf = unfiltered[-len(self.coefs) :]
filtered = numpy.convolve(unfiltered, self.coefs, mode='valid') filtered = numpy.convolve(unfiltered, self.coefs, mode="valid")
assert len(filtered) == len(original) + 1 assert len(filtered) == len(original) + 1
return filtered[1:] return filtered[1:]
@ -111,8 +112,9 @@ class high_pass(filter):
class band_pass(filter): class band_pass(filter):
def __init__(self, sample_rate, lo, hi, dbo): def __init__(self, sample_rate, lo, hi, dbo):
tap_count = max(taps(sample_rate, lo, dbo, True), tap_count = max(
taps(sample_rate, hi, dbo, False)) taps(sample_rate, lo, dbo, True), taps(sample_rate, hi, dbo, False)
)
lomask = lo_mask(sample_rate, tap_count, hi, dbo) lomask = lo_mask(sample_rate, tap_count, hi, dbo)
himask = hi_mask(sample_rate, tap_count, lo, dbo) himask = hi_mask(sample_rate, tap_count, lo, dbo)
mask = combine_masks(lomask, himask) mask = combine_masks(lomask, himask)
@ -131,8 +133,9 @@ class deemphasis(filter):
# slope in dB/octave of deemphasis filter # slope in dB/octave of deemphasis filter
dedbo = 10 / octaves dedbo = 10 / octaves
tap_count = max(taps(sample_rate, lo, dedbo, False), tap_count = max(
taps(sample_rate, hi, final_dbo, False)) taps(sample_rate, lo, dedbo, False), taps(sample_rate, hi, final_dbo, False)
)
# Calculate deemphasis filter # Calculate deemphasis filter
demask = lo_mask(sample_rate, tap_count, lo, dedbo) demask = lo_mask(sample_rate, tap_count, lo, dedbo)
@ -155,10 +158,10 @@ class decimator(filter):
# Gets the last n-th sample of every n (n = factor) # Gets the last n-th sample of every n (n = factor)
# If e.g. gets 12 samples, gets s[4] and s[9], and # If e.g. gets 12 samples, gets s[4] and s[9], and
# stoves s[10:] to the next round # stoves s[10:] to the next round
''' """
decimated = [ original[ self.factor * i + self.factor - 1 ] \ decimated = [ original[ self.factor * i + self.factor - 1 ] \
for i in range(0, len(original) // self.factor) ] for i in range(0, len(original) // self.factor) ]
''' """
decimated = original[(self.factor - 1) :: self.factor] decimated = original[(self.factor - 1) :: self.factor]
self.buf2 = original[: -len(original) % self.factor] self.buf2 = original[: -len(original) % self.factor]

View File

@ -19,10 +19,22 @@ from formats import FORMATS
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', help='Print additional informational output', action='store_true') parser.add_argument(
parser.add_argument('-f', '--format', choices=list(FORMATS.keys()), help='Input sample format', required=True) "-v", "--verbose", help="Print additional informational output", action="store_true"
parser.add_argument('-s', '--sample-rate', metavar='rate', help='Source sample rate (Hz)', required=True) )
parser.add_argument('-d', '--demod-rate', metavar='rate', help='Output sample rate (Hz)', required=True) parser.add_argument(
"-f",
"--format",
choices=list(FORMATS.keys()),
help="Input sample format",
required=True,
)
parser.add_argument(
"-s", "--sample-rate", metavar="rate", help="Source sample rate (Hz)", required=True
)
parser.add_argument(
"-d", "--demod-rate", metavar="rate", help="Output sample rate (Hz)", required=True
)
# TODO JMT: Output to file # TODO JMT: Output to file
# parser.add_argument('-o', metavar='file', help='Specify an output file for demodulated audio. Omit for stdout or use \'-\'.') # parser.add_argument('-o', metavar='file', help='Specify an output file for demodulated audio. Omit for stdout or use \'-\'.')
args = parser.parse_args() args = parser.parse_args()
@ -32,7 +44,10 @@ OUTPUT_RATE = int(prefixed.Float(args.demod_rate))
DECIMATION = INPUT_RATE / OUTPUT_RATE DECIMATION = INPUT_RATE / OUTPUT_RATE
if DECIMATION != math.floor(DECIMATION): if DECIMATION != math.floor(DECIMATION):
print(f'The output rate must be an integer divisor of the input rate: {INPUT_RATE}/{OUTPUT_RATE} = {DECIMATION}', file=sys.stderr) print(
f"The output rate must be an integer divisor of the input rate: {INPUT_RATE}/{OUTPUT_RATE} = {DECIMATION}",
file=sys.stderr,
)
sys.exit(1) sys.exit(1)
FORMAT = FORMATS[args.format].numpy FORMAT = FORMATS[args.format].numpy
@ -63,15 +78,17 @@ decimate2 = filters.decimator(DECIMATION)
lo_r = filters.deemphasis(INPUT_RATE, 75, FM_BANDWIDTH, 120) lo_r = filters.deemphasis(INPUT_RATE, 75, FM_BANDWIDTH, 120)
# Band-pass filter for stereo (L-R) modulated audio # Band-pass filter for stereo (L-R) modulated audio
hi = filters.band_pass(INPUT_RATE, hi = filters.band_pass(
STEREO_CARRIER - FM_BANDWIDTH, STEREO_CARRIER + FM_BANDWIDTH, 120) INPUT_RATE, STEREO_CARRIER - FM_BANDWIDTH, STEREO_CARRIER + FM_BANDWIDTH, 120
)
# Filter to extract pilot signal # Filter to extract pilot signal
pilot = filters.band_pass(INPUT_RATE, pilot = filters.band_pass(
STEREO_CARRIER / 2 - 100, STEREO_CARRIER / 2 + 100, 120) INPUT_RATE, STEREO_CARRIER / 2 - 100, STEREO_CARRIER / 2 + 100, 120
)
last_angle = 0.0 last_angle = 0.0
remaining_data = b'' remaining_data = b""
while True: while True:
# Ingest 0.1s worth of data # Ingest 0.1s worth of data
@ -173,8 +190,7 @@ while True:
last_deviation_avg = 0.0 last_deviation_avg = 0.0
# Translate rotation to frequency deviation # Translate rotation to frequency deviation
STEREO_CARRIER /= (1 + (rotation * 1.05) / tau) STEREO_CARRIER /= 1 + (rotation * 1.05) / tau
# Downsample, Low-pass/deemphasis demodulated L-R # Downsample, Low-pass/deemphasis demodulated L-R
output_jstereo = lo_r.feed(output_jstereo) output_jstereo = lo_r.feed(output_jstereo)
@ -196,4 +212,4 @@ while True:
output[1::2] = output_right output[1::2] = output_right
output = output.astype(int) output = output.astype(int)
sys.stdout.buffer.write(struct.pack('<%dh' % len(output), *output)) sys.stdout.buffer.write(struct.pack("<%dh" % len(output), *output))

View File

@ -2,6 +2,7 @@ import numpy as np
from SoapySDR import * from SoapySDR import *
from dataclasses import dataclass from dataclasses import dataclass
@dataclass @dataclass
class FormatSpec: class FormatSpec:
name: str name: str
@ -9,9 +10,10 @@ class FormatSpec:
numpy: np.dtype numpy: np.dtype
packing: str packing: str
FORMATS = { FORMATS = {
'CU8': FormatSpec('CU8', SOAPY_SDR_CU8, np.uint8, '=%dB'), "CU8": FormatSpec("CU8", SOAPY_SDR_CU8, np.uint8, "=%dB"),
'CS8': FormatSpec('CS8', SOAPY_SDR_CS8, np.int8, '=%db'), "CS8": FormatSpec("CS8", SOAPY_SDR_CS8, np.int8, "=%db"),
'CS16': FormatSpec('CS16', SOAPY_SDR_CS16, np.int16, '=%dh'), "CS16": FormatSpec("CS16", SOAPY_SDR_CS16, np.int16, "=%dh"),
'CF32': FormatSpec('CF32', SOAPY_SDR_CF32, np.float32, '=%df'), "CF32": FormatSpec("CF32", SOAPY_SDR_CF32, np.float32, "=%df"),
} }

View File

@ -17,7 +17,8 @@ swag = Swagger(app)
radios = {} radios = {}
@app.route('/report')
@app.route("/report")
def report(): def report():
"""Get streams report from the RTSP relay. """Get streams report from the RTSP relay.
--- ---
@ -30,17 +31,16 @@ def report():
try: try:
r = requests.get("http://localhost:9997/v1/paths/list") r = requests.get("http://localhost:9997/v1/paths/list")
j = r.json() j = r.json()
for item in j['items']: for item in j["items"]:
del j['items'][item]['conf'] del j["items"][item]["conf"]
del j['items'][item]['confName'] del j["items"][item]["confName"]
return jsonify(j) return jsonify(j)
except Exception as e: except Exception as e:
return _error_message_json(e.message), 500 return _error_message_json(e.message), 500
@app.route('/radio/report') @app.route("/radio/report")
def radio_report(): def radio_report():
"""List radio devices available to the system. """List radio devices available to the system.
--- ---
@ -52,7 +52,8 @@ def radio_report():
devices = [dict(device) for device in soapy.Device.enumerate()] devices = [dict(device) for device in soapy.Device.enumerate()]
return jsonify(devices) return jsonify(devices)
@app.route('/radio/<radio>/connect')
@app.route("/radio/<radio>/connect")
def connect(radio): def connect(radio):
"""Connect to a radio device, by driver name or serial number. """Connect to a radio device, by driver name or serial number.
--- ---
@ -83,10 +84,10 @@ def connect(radio):
radios.pop(radio) radios.pop(radio)
return _error_message_json(e.message), 500 return _error_message_json(e.message), 500
return "Radio device not found", 400 return "Radio device not found", 400
@app.route('/radio/<radio>/disconnect')
@app.route("/radio/<radio>/disconnect")
def disconnect(radio): def disconnect(radio):
"""Disconnect from a radio device. """Disconnect from a radio device.
--- ---
@ -110,7 +111,8 @@ def disconnect(radio):
else: else:
return _error_message_json("Radio not connected"), 400 return _error_message_json("Radio not connected"), 400
@app.route('/radio/<radio>/configure/<frequency>')
@app.route("/radio/<radio>/configure/<frequency>")
def configure(radio, frequency): def configure(radio, frequency):
"""Tune the radio to a frequency. """Tune the radio to a frequency.
You must connect to the radio before attempting to configure it. You must connect to the radio before attempting to configure it.
@ -138,7 +140,8 @@ def configure(radio, frequency):
else: else:
return _error_message_json("Radio not connected"), 400 return _error_message_json("Radio not connected"), 400
@app.route('/radio/<radio>/start')
@app.route("/radio/<radio>/start")
def start_stream(radio): def start_stream(radio):
"""Start the radio stream. """Start the radio stream.
Once the stream has been started, connect to the stream at: Once the stream has been started, connect to the stream at:
@ -162,7 +165,8 @@ def start_stream(radio):
except Exception as e: except Exception as e:
return _error_message_json(e.message), 400 return _error_message_json(e.message), 400
@app.route('/radio/<radio>/end')
@app.route("/radio/<radio>/end")
def end_stream(radio): def end_stream(radio):
"""Terminate the radio stream. """Terminate the radio stream.
--- ---
@ -185,7 +189,8 @@ def end_stream(radio):
error_message = {"error_message": e.message} error_message = {"error_message": e.message}
return _error_message_json(e.message), 400 return _error_message_json(e.message), 400
@app.route('/radio/<radio>/info')
@app.route("/radio/<radio>/info")
def radio_info(radio): def radio_info(radio):
"""Get information about a radio. """Get information about a radio.
--- ---
@ -209,7 +214,8 @@ def radio_info(radio):
tubes = {} tubes = {}
@app.route('/tuuube/<id>/start')
@app.route("/tuuube/<id>/start")
def start_tuuube_stream(id): def start_tuuube_stream(id):
"""Start streaming from a youtube source. """Start streaming from a youtube source.
Once the stream has been started, connect to the stream at: Once the stream has been started, connect to the stream at:
@ -242,7 +248,8 @@ def start_tuuube_stream(id):
except Exception as e: except Exception as e:
return _error_message_json(e.message), 400 return _error_message_json(e.message), 400
@app.route('/tuuube/<id>/end')
@app.route("/tuuube/<id>/end")
def end_tuuube_stream(id): def end_tuuube_stream(id):
"""Terminate the youtube stream. """Terminate the youtube stream.
--- ---
@ -265,42 +272,37 @@ def end_tuuube_stream(id):
return _error_message_json(e.message), 400 return _error_message_json(e.message), 400
""" """
Helper function to return a JSON error message Helper function to return a JSON error message
parameters: error_message - the error message to return parameters: error_message - the error message to return
returns: a JSON object with the error message returns: a JSON object with the error message
""" """
def _error_message_json(error_message: str): def _error_message_json(error_message: str):
error_message = {"error_message": error_message} error_message = {"error_message": error_message}
return jsonify(error_message) return jsonify(error_message)
if __name__ == '__main__':
if __name__ == "__main__":
import subprocess import subprocess
rtsp_relay = subprocess.Popen( rtsp_relay = subprocess.Popen(
[ ["./dependencies/mediamtx/mediamtx", "./mediamtx.yml"],
'./dependencies/mediamtx/mediamtx',
'./mediamtx.yml'
],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL stderr=subprocess.DEVNULL,
) )
for path, _, files in os.walk('./data/sampleaudio'): for path, _, files in os.walk("./data/sampleaudio"):
for file in files: for file in files:
name, ext = os.path.splitext(file) name, ext = os.path.splitext(file)
if ext == '.mp3': if ext == ".mp3":
tubes[name] = FileRadio(f"{path}/{file}") tubes[name] = FileRadio(f"{path}/{file}")
tubes[name].start_stream() tubes[name].start_stream()
app.run( app.run(host="0.0.0.0", threaded=True, debug=False)
host='0.0.0.0',
threaded=True,
debug=False
)
print('Stopping any currently streaming radios...') print("Stopping any currently streaming radios...")
for radio in radios: for radio in radios:
if radios[radio].is_streaming(): if radios[radio].is_streaming():
radios[radio].end_stream() radios[radio].end_stream()
@ -311,6 +313,6 @@ if __name__ == '__main__':
tubes[tube].end_stream() tubes[tube].end_stream()
tubes = None tubes = None
print('Killing RTSP relay...') print("Killing RTSP relay...")
rtsp_relay.kill() rtsp_relay.kill()
rtsp_relay.wait() # Necessary? rtsp_relay.wait() # Necessary?

123
radio.py
View File

@ -11,8 +11,8 @@ from streamer import Streamer, is_alive
class Radio(Streamer): class Radio(Streamer):
REST_PATH = 'radio' REST_PATH = "radio"
FORMAT = 'CS16' FORMAT = "CS16"
SAMPLES = 8192 SAMPLES = 8192
def __init__(self, name, device_info): def __init__(self, name, device_info):
@ -33,7 +33,7 @@ class Radio(Streamer):
frequency = int(prefixed.Float(frequency)) frequency = int(prefixed.Float(frequency))
bandwidth = 200000 bandwidth = 200000
sample_rates = preferred_sample_rates(self.capabilities['rx']['sample-rates']) sample_rates = preferred_sample_rates(self.capabilities["rx"]["sample-rates"])
if len(sample_rates) == 0: if len(sample_rates) == 0:
raise RuntimeError("No suitable sample rates are available") raise RuntimeError("No suitable sample rates are available")
self.sample_rate, self.output_rate = sample_rates[0] self.sample_rate, self.output_rate = sample_rates[0]
@ -46,41 +46,43 @@ class Radio(Streamer):
self.device.setGainMode(soapy.SOAPY_SDR_RX, 0, True) self.device.setGainMode(soapy.SOAPY_SDR_RX, 0, True)
return { return {
'frequency': self.device.getFrequency(soapy.SOAPY_SDR_RX, 0), "frequency": self.device.getFrequency(soapy.SOAPY_SDR_RX, 0),
'sample-rate': self.device.getSampleRate(soapy.SOAPY_SDR_RX, 0), "sample-rate": self.device.getSampleRate(soapy.SOAPY_SDR_RX, 0),
'bandwidth': self.device.getBandwidth(soapy.SOAPY_SDR_RX, 0), "bandwidth": self.device.getBandwidth(soapy.SOAPY_SDR_RX, 0),
'gain-mode': 'auto' if self.device.getGainMode(soapy.SOAPY_SDR_RX, 0) else 'manual', "gain-mode": "auto"
if self.device.getGainMode(soapy.SOAPY_SDR_RX, 0)
else "manual",
} }
def get_info(self): def get_info(self):
return { return {
'name': self.name, "name": self.name,
'device': self.device_info, "device": self.device_info,
'capabilities': self.capabilities, "capabilities": self.capabilities,
'stream-path': self._stream_path(), "stream-path": self._stream_path(),
'streaming': self.is_streaming(), "streaming": self.is_streaming(),
} }
def _get_capabilities(self): def _get_capabilities(self):
def get_direction_capabilities(direction): def get_direction_capabilities(direction):
return { return {
'antennas': self.device.listAntennas(direction, 0), "antennas": self.device.listAntennas(direction, 0),
'gains': self.device.listGains(direction, 0), "gains": self.device.listGains(direction, 0),
'frequencies': self.device.listFrequencies(direction, 0), "frequencies": self.device.listFrequencies(direction, 0),
'sample-rates': self.device.listSampleRates(direction, 0), "sample-rates": self.device.listSampleRates(direction, 0),
'bandwidths': self.device.listBandwidths(direction, 0), "bandwidths": self.device.listBandwidths(direction, 0),
'sensors': self.device.listSensors(direction, 0), "sensors": self.device.listSensors(direction, 0),
'formats': self.device.getStreamFormats(direction, 0), "formats": self.device.getStreamFormats(direction, 0),
} }
return { return {
'rx': get_direction_capabilities(soapy.SOAPY_SDR_RX), "rx": get_direction_capabilities(soapy.SOAPY_SDR_RX),
'tx': get_direction_capabilities(soapy.SOAPY_SDR_TX), "tx": get_direction_capabilities(soapy.SOAPY_SDR_TX),
'clock-sources': self.device.listClockSources(), "clock-sources": self.device.listClockSources(),
'time-sources': self.device.listTimeSources(), "time-sources": self.device.listTimeSources(),
'register-interfaces': self.device.listRegisterInterfaces(), "register-interfaces": self.device.listRegisterInterfaces(),
'gpios': self.device.listGPIOBanks(), "gpios": self.device.listGPIOBanks(),
'uarts': self.device.listUARTs(), "uarts": self.device.listUARTs(),
} }
def _stream_thread(self): def _stream_thread(self):
@ -89,7 +91,7 @@ class Radio(Streamer):
while self.run: while self.run:
# Check that the child processes are still running # Check that the child processes are still running
if (not is_alive(self.demod)) or (not is_alive(self.playback)): if (not is_alive(self.demod)) or (not is_alive(self.playback)):
print('DSP chain failed, aborting stream.', file=sys.stderr) print("DSP chain failed, aborting stream.", file=sys.stderr)
break break
result = self.device.readStream(self.stream, [self.buffer], Radio.SAMPLES) result = self.device.readStream(self.stream, [self.buffer], Radio.SAMPLES)
@ -99,14 +101,18 @@ class Radio(Streamer):
elif result.ret < 0: elif result.ret < 0:
error = SoapyError(result.ret) error = SoapyError(result.ret)
if error is not SoapyError.Timeout: if error is not SoapyError.Timeout:
print("Stream read failed, aborting stream:", error, file=sys.stderr) print(
"Stream read failed, aborting stream:", error, file=sys.stderr
)
break break
continue continue
else: else:
read_size = int(result.ret * 2) read_size = int(result.ret * 2)
self.demod.stdin.write( self.demod.stdin.write(
struct.pack(FORMATS[Radio.FORMAT].packing % read_size, struct.pack(
*self.buffer[:read_size]) FORMATS[Radio.FORMAT].packing % read_size,
*self.buffer[:read_size],
)
) )
self._cleanup_stream() self._cleanup_stream()
@ -114,32 +120,44 @@ class Radio(Streamer):
def _init_stream(self): def _init_stream(self):
self.playback = subprocess.Popen( self.playback = subprocess.Popen(
[ [
'/usr/bin/ffmpeg', "/usr/bin/ffmpeg",
'-f', 's16le', "-f",
'-ar', str(self.output_rate), "s16le",
'-ac', '2', "-ar",
'-i', '-', str(self.output_rate),
'-f', 'rtsp', self.stream_address() "-ac",
"2",
"-i",
"-",
"-f",
"rtsp",
self.stream_address(),
], ],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL stderr=subprocess.DEVNULL,
) )
self.demod = subprocess.Popen( self.demod = subprocess.Popen(
[ [
'/usr/bin/python3', 'fm_demod.py', "/usr/bin/python3",
'-f', 'CS16', "fm_demod.py",
'-s', str(self.sample_rate), "-f",
'-d', str(self.output_rate) "CS16",
"-s",
str(self.sample_rate),
"-d",
str(self.output_rate),
], ],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=self.playback.stdin, stdout=self.playback.stdin,
stderr=subprocess.DEVNULL stderr=subprocess.DEVNULL,
) )
self.buffer = np.array([0] * Radio.SAMPLES * 2, FORMATS[Radio.FORMAT].numpy) self.buffer = np.array([0] * Radio.SAMPLES * 2, FORMATS[Radio.FORMAT].numpy)
self.stream = self.device.setupStream(soapy.SOAPY_SDR_RX, FORMATS[Radio.FORMAT].soapy) self.stream = self.device.setupStream(
soapy.SOAPY_SDR_RX, FORMATS[Radio.FORMAT].soapy
)
result = self.device.activateStream(self.stream) result = self.device.activateStream(self.stream)
if result != 0: if result != 0:
@ -164,26 +182,25 @@ class Radio(Streamer):
self.playback = None self.playback = None
""" """
Quick and dirty test of the Radio class. Quick and dirty test of the Radio class.
""" """
if __name__ == '__main__': if __name__ == "__main__":
import time import time
sdr = Radio('demo', {'driver': 'sdrplay'}) sdr = Radio("demo", {"driver": "sdrplay"})
print('Configuring...') print("Configuring...")
sdr.configure('105.5M') sdr.configure("105.5M")
print('Configured.') print("Configured.")
print('Starting stream...') print("Starting stream...")
sdr.start_stream() sdr.start_stream()
print('Stream started.') print("Stream started.")
# Let the stream play for a while # Let the stream play for a while
time.sleep(15) time.sleep(15)
print('Ending stream...') print("Ending stream...")
sdr.end_stream() sdr.end_stream()
print('Stream ended.') print("Stream ended.")

View File

@ -17,6 +17,7 @@ supported_ouput_rates = [
192000, # Too much. 192000, # Too much.
] ]
def score(pair, target_output=32000, target_ratio=10): def score(pair, target_output=32000, target_ratio=10):
""" """
Heuristic for scoring input & output sample rates. The criteria are: Heuristic for scoring input & output sample rates. The criteria are:
@ -36,29 +37,38 @@ def score(pair, target_output=32000, target_ratio=10):
ratio = pair[0] // pair[1] ratio = pair[0] // pair[1]
return abs(pair[1] - target_output)/2500 \ return (
+ max(0, target_output - pair[1])/2500 \ abs(pair[1] - target_output) / 2500
+ abs(ratio - target_ratio)**0.8 \ + max(0, target_output - pair[1]) / 2500
+ abs(ratio - target_ratio) ** 0.8
+ max(0, target_ratio - ratio) ** 2 + max(0, target_ratio - ratio) ** 2
)
def flatten(l): def flatten(l):
return [item for sublist in l for item in sublist] return [item for sublist in l for item in sublist]
def flatten_dict(d): def flatten_dict(d):
return [(key, value) for key, rates in d.items() for value in rates] return [(key, value) for key, rates in d.items() for value in rates]
def get_pairs(input_rate): def get_pairs(input_rate):
return [ return [
(input_rate, rate) (input_rate, rate) for rate in supported_ouput_rates if (input_rate % rate == 0)
for rate in supported_ouput_rates
if (input_rate % rate == 0)
] ]
def supported_sample_rates(supported_input_rates): def supported_sample_rates(supported_input_rates):
return { return {
in_rate: [out_rate for out_rate in supported_ouput_rates if in_rate % out_rate == 0] in_rate: [
out_rate for out_rate in supported_ouput_rates if in_rate % out_rate == 0
]
for in_rate in supported_input_rates for in_rate in supported_input_rates
} }
def preferred_sample_rates(supported_input_rates): def preferred_sample_rates(supported_input_rates):
return sorted(flatten_dict(supported_sample_rates(supported_input_rates)), key=score) return sorted(
flatten_dict(supported_sample_rates(supported_input_rates)), key=score
)

View File

@ -1,6 +1,7 @@
import SoapySDR as soapy import SoapySDR as soapy
from enum import IntEnum from enum import IntEnum
class SoapyError(IntEnum): class SoapyError(IntEnum):
Timeout = soapy.SOAPY_SDR_TIMEOUT Timeout = soapy.SOAPY_SDR_TIMEOUT
StreamError = soapy.SOAPY_SDR_STREAM_ERROR StreamError = soapy.SOAPY_SDR_STREAM_ERROR
@ -10,6 +11,7 @@ class SoapyError(IntEnum):
TimeError = soapy.SOAPY_SDR_TIME_ERROR TimeError = soapy.SOAPY_SDR_TIME_ERROR
Underflow = soapy.SOAPY_SDR_UNDERFLOW Underflow = soapy.SOAPY_SDR_UNDERFLOW
class SoapyFlag(IntEnum): class SoapyFlag(IntEnum):
EndBurst = soapy.SOAPY_SDR_END_BURST EndBurst = soapy.SOAPY_SDR_END_BURST
HasTime = soapy.SOAPY_SDR_HAS_TIME HasTime = soapy.SOAPY_SDR_HAS_TIME

View File

@ -2,7 +2,7 @@ from threading import Thread
import yaml import yaml
with open('mediamtx.yml', 'r') as config_file: with open("mediamtx.yml", "r") as config_file:
MEDIASERVER_CONFIG = yaml.safe_load(config_file) MEDIASERVER_CONFIG = yaml.safe_load(config_file)
@ -11,8 +11,8 @@ def is_alive(subprocess):
class Streamer: class Streamer:
PROTOCOL = 'rtsp' PROTOCOL = "rtsp"
REST_PATH = 'stream' REST_PATH = "stream"
def __init__(self): def __init__(self):
self.run = False self.run = False
@ -30,7 +30,7 @@ class Streamer:
def start_stream(self): def start_stream(self):
if self.is_streaming(): if self.is_streaming():
raise RuntimeError('Stream thread is already running') raise RuntimeError("Stream thread is already running")
self.run = True self.run = True
self.thread = Thread(target=self._stream_thread, daemon=True, args=()) self.thread = Thread(target=self._stream_thread, daemon=True, args=())
@ -38,14 +38,14 @@ class Streamer:
def end_stream(self): def end_stream(self):
if self.thread is None: if self.thread is None:
raise RuntimeError('No stream thread to terminate') raise RuntimeError("No stream thread to terminate")
self.run = False self.run = False
self.thread.join() self.thread.join()
self.thread = None self.thread = None
if __name__ == "__main__":
if __name__ == '__main__':
from pprint import pprint from pprint import pprint
pprint(MEDIASERVER_CONFIG) pprint(MEDIASERVER_CONFIG)

View File

@ -17,7 +17,7 @@ import os
class Tuuube(Streamer): class Tuuube(Streamer):
REST_PATH = 'tuuube' REST_PATH = "tuuube"
def __init__(self, name): def __init__(self, name):
super().__init__() super().__init__()
@ -28,43 +28,51 @@ class Tuuube(Streamer):
return f"/tmp/{self.name}.mp3" return f"/tmp/{self.name}.mp3"
def _stream_thread(self): def _stream_thread(self):
if not os.path.exists(self.source_path()) or not os.path.isfile(self.source_path()): if not os.path.exists(self.source_path()) or not os.path.isfile(
self.source_path()
):
ydl_opts = { ydl_opts = {
'format': 'bestaudio/best', "format": "bestaudio/best",
'outtmpl': f'/tmp/{self.name}.%(ext)s', # yt_dlp will append %(ext) if not specified, "outtmpl": f"/tmp/{self.name}.%(ext)s", # yt_dlp will append %(ext) if not specified,
'postprocessors': [{ # resulting in `/tmp/file.mp3.mp3` :/ "postprocessors": [ # resulting in `/tmp/file.mp3.mp3` :/
'key': 'FFmpegExtractAudio', {
'preferredcodec': 'mp3', "key": "FFmpegExtractAudio",
'preferredquality': '192', "preferredcodec": "mp3",
}], "preferredquality": "192",
}
],
} }
try: try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([f'https://www.youtube.com/watch?v={self.name}']) ydl.download([f"https://www.youtube.com/watch?v={self.name}"])
except Exception as e: except Exception as e:
print(f'File sourcing failed, aborting stream. {e}', file=sys.stderr) print(f"File sourcing failed, aborting stream. {e}", file=sys.stderr)
self.run = False self.run = False
return return
self.playback = subprocess.Popen( self.playback = subprocess.Popen(
[ [
'/usr/bin/ffmpeg', "/usr/bin/ffmpeg",
'-re', "-re",
'-stream_loop', '-1', "-stream_loop",
'-i', self.source_path(), "-1",
'-c', 'copy', "-i",
'-f', 'rtsp', self.source_path(),
self.stream_address('localhost') "-c",
"copy",
"-f",
"rtsp",
self.stream_address("localhost"),
], ],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL stderr=subprocess.DEVNULL,
) )
while self.run: while self.run:
if not is_alive(self.playback): if not is_alive(self.playback):
print('Playback failed, aborting stream.', file=sys.stderr) print("Playback failed, aborting stream.", file=sys.stderr)
break break
time.sleep(0.1) time.sleep(0.1)
@ -75,9 +83,8 @@ class Tuuube(Streamer):
self.playback = None self.playback = None
if __name__ == "__main__":
if __name__ == '__main__': tube = Tuuube("BaW_jenozKc")
tube = Tuuube('BaW_jenozKc')
tube.start_stream() tube.start_stream()
while True: while True: