在Raspberry Pi上流式调频收音机,无需重启即可更改频率

时间:2017-07-12 03:57:11

标签: python raspberry-pi software-defined-radio

我正在播放调频收音机并尝试在流式播放时更改我的Raspberry Pi 3的RTL-SDR频率(即无需关闭程序并重新启动)

它当前正在播放电台,我可以慢慢增加电台/频率。但是,如果我增加得太快,SDR就会停止播放。

我正在使用:

  • Raspberry Pi 3,用Python 2.7.9编写
  • NooElec R820T SDR& DVB-T USB加密狗
  • 旋转编码器输入所需频率
  • pi-encoder图书馆
  • rtl_fm接收和玩游戏

我的代码目前是:

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()

python子进程是否容忍这种用途?

是否有另一种方式(命令,方法,库)可以更平稳快速地改变不会影响比赛或停止SDR播放的电台/频率?

1 个答案:

答案 0 :(得分:0)

在使用updateStation调用启动新进程之前,您可能需要找到一种方法等待process.kill()完成并关闭管道(它是异步发生的)。在任何kill之后尝试在主while()循环中放置一个小延迟,以防止它比OS可以可靠地处理更快地发送进程命令。