简体   繁体   中英

Stream FM Radio on Raspberry Pi, Changing Frequency without Restarting

I am playing FM radio and trying to change the frequency of my RTL-SDR for my Raspberry Pi 3, while streaming (ie, without having to shut down the program and restart)

It currently plays the station and I can increment the station/frequency slowly. However, if I increment it too fast, the SDR stops playing.

I'm using:

  • Raspberry Pi 3, writing in Python 2.7.9
  • NooElec R820T SDR & DVB-T USB dongle
  • Rotary Encoder to input desired frequency
  • pi-encoder library
  • rtl_fm to receive and sox to play

My code is currently:

import os
import time
import subprocess

from pi_encoder import *

def updateStation(station_addr, kill=0):
    print 'creating pipe1a'
    pipe1a = subprocess.Popen(['rtl_fm', '-M', 'wbfm', '-f', str(station_addr) + 'M', '-g', '10'], stdout=subprocess.PIPE)
    print 'creating pipe1b'
    pipe1b = subprocess.Popen(['play', '-r', '32k', '-t', 'raw', '-e', 's', '-b', '16', '-c', '1', '-V1', '-'], stdin=pipe1a.stdout, stdout=subprocess.PIPE)
    print 'creating pipe1c'
    pipe1c = pipe1b.stdout

    pid_a = pipe1a.pid
    return pid_a, pipe1a

# Export LD_LIBRARY_PATH; doesn't seem to retain
os.system('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib')

## Rotary encoder GPIO PIN assignments are set in 'pi_encoder' module

## SET INITIAL VALUES
tuner_value = 0
station_addr = 95.1
max_tuner_addr = 109.1
min_tuner_addr = 88.0

## INITIALIZE ROTARY ENCODER WORKER THREAD
tuner_encoder = EncoderWorker(SwitchEncoder(TUNER_CLK_PIN, TUNER_DT_PIN, TUNER_SW_PIN))
tuner_encoder.start()

## MAIN LOOP
try:
    print 'Starting main loop'
    pid, process = updateStation(station_addr)
    while True:
        delta_tuner = tuner_encoder.get_delta()

        if delta_tuner!=0:
            tuner_value = tuner_value + delta_tuner
            print "tuner value", tuner_value
            station_addr = station_addr + (delta_tuner/10.0)
            if station_addr > max_tuner_addr:
                station_addr = max_tuner_addr
            elif station_addr < min_tuner_addr:
                station_addr = min_tuner_addr
            print 'station_address: ', station_addr

            print 'terminating process:'
            process.terminate()
            try:
                os.kill(pid, 0)
                process.kill()
            except OSError, e:
                print 'Terminated gracefully'

            pid, process = updateStation(station_addr)

        if tuner_encoder.get_upEvent():
            print "tuner up!"

        if tuner_encoder.get_downEvent():
            print "tuner down!"

finally:
    print 'Cleaning up GPIO...'
    IO.cleanup()

Is python subprocess tolerant to this use?

Is there another way (command, method, library) that would allow a more smooth and quick change of station/frequency that wouldn't affect play or stop the SDR from playing?

You may need to find a way wait for process.kill() to finish and the pipes to close (it happens asynchronously), before starting the a new process with your updateStation call. Try putting a small delay in your main while() loop after any kill to prevent it from sending process commands faster than the OS can reliably handle.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM