现在网上有不少针对树莓派架设电台的项目,但大部分在Pi2下不能用。我找到一个在Pi2下能用的,效果还不错。
Github:https://github.com/ChristopheJacquet/PiFmRds
Pi-FM-RDS
FM-RDS transmitter using the Raspberry Pi
This program generates an FM modulation, with RDS (Radio Data System) data generated in real time. It can include monophonic or stereophonic audio.
It is based on the FM transmitter created by Oliver Mattos and Oskar Weigl, and later adapted to using DMA by Richard Hirst. Christophe Jacquet adapted it and added the RDS data generator and modulator. The transmitter uses the Raspberry Pi's PWM generator to produce VHF signals.
It is compatible with both the Raspberry Pi 1 (the original one) and the Raspberry Pi 2.
How to use it?
Pi-FM-RDS, depends on the sndfile
library. To install this library on Debian-like distributions, for instance Raspbian, runsudo apt-get install libsndfile1-dev
.
Pi-FM-RDS also depends on the Linux rpi-mailbox
driver, so you need a recent Linux kernel. The Raspbian releases from August 2015 have this.
Important. The binaries compiled for the Raspberry Pi 1 are not compatible with the Raspberry Pi 2, and conversely. Always re-compile when switching models, so do not skip the make clean
step in the instructions below!
Clone the source repository and run make
in the src
directory:
git clone https://github.com/ChristopheJacquet/PiFmRds.git
cd PiFmRds/src
make clean
make
Then you can just run:
sudo ./pi_fm_rds
This will generate an FM transmission on 107.9 MHz, with default station name (PS), radiotext (RT) and PI-code, without audio. The radiofrequency signal is emitted on GPIO 4 (pin 7 on header P1).
You can add monophonic or stereophonic audio by referencing an audio file as follows:
sudo ./pi_fm_rds -audio sound.wav
To test stereophonic audio, you can try the file stereo_44100.wav
provided.
The more general syntax for running Pi-FM-RDS is as follows:
pi_fm_rds [-freq freq] [-audio file] [-ppm ppm_error] [-pi pi_code] [-ps ps_text] [-rt rt_text]
All arguments are optional:
-freq
specifies the carrier frequency (in MHz). Example:-freq 107.9
.-audio
specifies an audio file to play as audio. The sample rate does not matter: Pi-FM-RDS will resample and filter it. If a stereo file is provided, Pi-FM-RDS will produce an FM-Stereo signal. Example:-audio sound.wav
. The supported formats depend onlibsndfile
. This includes WAV and Ogg/Vorbis (among others) but not MP3. Specify-
as the file name to read audio data on standard input (useful for piping audio into Pi-FM-RDS, see below).-pi
specifies the PI-code of the RDS broadcast. 4 hexadecimal digits. Example:-pi FFFF
.-ps
specifies the station name (Program Service name, PS) of the RDS broadcast. Limit: 8 characters. Example:-ps RASP-PI
.-rt
specifies the radiotext (RT) to be transmitted. Limit: 64 characters. Example:-rt 'Hello, world!'
.-ctl
specifies a named pipe (FIFO) to use as a control channel to change PS and RT at run-time (see below).-ppm
specifies your Raspberry Pi's oscillator error in parts per million (ppm), see below.
By default the PS changes back and forth between Pi-FmRds
and a sequence number, starting at 00000000
. The PS changes around one time per second.
Clock calibration (only if experiencing difficulties)
The RDS standards states that the error for the 57 kHz subcarrier must be less than ± 6 Hz, i.e. less than 105 ppm (parts per million). The Raspberry Pi's oscillator error may be above this figure. That is where the -ppm
parameter comes into play: you specify your Pi's error and Pi-FM-RDS adjusts the clock dividers accordingly.
In practice, I found that Pi-FM-RDS works okay even without using the -ppm
parameter. I suppose the receivers are more tolerant than stated in the RDS spec.
One way to measure the ppm error is to play the pulses.wav
file: it will play a pulse for precisely 1 second, then play a 1-second silence, and so on. Record the audio output from a radio with a good audio card. Say you sample at 44.1 kHz. Measure 10 intervals. Using Audacity for example determine the number of samples of these 10 intervals: in the absence of clock error, it should be 441,000 samples. With my Pi, I found 441,132 samples. Therefore, my ppm error is (441132-441000)/441000 * 1e6 = 299 ppm, assuming that my sampling device (audio card) has no clock error...
Piping audio into Pi-FM-RDS
If you use the argument -audio -
, Pi-FM-RDS reads audio data on standard input. This allows you to pipe the output of a program into Pi-FM-RDS. For instance, this can be used to read MP3 files using Sox:
sox -t mp3 http://www.linuxvoice.com/episodes/lv_s02e01.mp3 -t wav - | sudo ./pi_fm_rds -audio -
Or to pipe the AUX input of a sound card into Pi-FM-RDS:
sudo arecord -fS16_LE -r 44100 -Dplughw:1,0 -c 2 - | sudo ./pi_fm_rds -audio -
Changing PS, RT and TA at run-time
You can control PS, RT and TA (Traffic Announcement flag) at run-time using a named pipe (FIFO). For this run Pi-FM-RDS with the -ctl
argument.
Example:
mkfifo rds_ctl
sudo ./pi_fm_rds -ctl rds_ctl
Then you can send “commands” to change PS, RT and TA:
cat >rds_ctl
PS MyText
RT A text to be sent as radiotext
TA ON
PS OtherTxt
TA OFF
...
Every line must start with either PS
, RT
or TA
, followed by one space character, and the desired value. Any other line format is silently ignored. TA ON
switches the Traffic Announcement flag to on, any other value switches it to off.