簡體   English   中英

在Raspberry Pi上流FM廣播,無需重新啟動即可更改頻率

[英]Stream FM Radio on Raspberry Pi, Changing Frequency without Restarting

我正在播放FM廣播,並嘗試在流傳輸時更改Raspberry Pi 3的RTL-SDR頻率(即,無需關閉程序並重新啟動)

當前正在播放電台,我可以緩慢增加電台/頻率。 但是,如果我增加得太快,SDR將停止播放。

我正在使用:

  • Raspberry Pi 3,用Python 2.7.9編寫
  • NooElec R820T SDR和DVB-T USB軟件狗
  • 旋轉編碼器輸入所需的頻率
  • pi編碼器
  • rtl_fm接收和sox播放

我的代碼當前為:

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?

在使用updateStation調用啟動新進程之前,您可能需要找到一種方法來等待process.kill()完成和管道關閉(異步發生)。 嘗試在執行任何殺死操作后在main while()循環中稍加延遲,以防止其發送進程命令的速度超過操作系統能夠可靠處理的速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM