2019独角兽企业重金招聘Python工程师标准>>>
用树莓派2架设微型FM电台程序(同时支持Pi1、Pi2)
目录
- 原理简述
- 安装fm_transmitter
- 使用方法
- 相关参考
原理简述
树莓派支持GPIO
,1代板上有26个引出的管脚,2代板上有40个引出的管脚,其中有一个可以作为时钟信号输出,于是就有人把把音频信号通过树莓派进行FM
调制后从该引脚发出,于是树莓派就成了一台微型FM电台,可以自己指定频率,打开调频收音机,调到对应频道就能收到你的树莓派播放的微型 FM
广播。
安装fm_transmitter
在网络上看到很多文章介绍树莓派的微型FM
电台功能,软件普遍是 pifm
,下载了不少版本的 pifm
,试用后发现在我的树莓派2上没效果,搜索了半天,终于找到一款可以同时用于树莓派1和树莓派2的软件:
fm_transmitter
直接到 github 下载:
sudo git clone https://github.com/markondej/fm_transmitter
需要自己编译,要安装好 make 、gcc 和 c++
编译工具在我的 2015-05-05
版本的 Raspbian
上已经安装好了,可以直接使用,没有安装的朋友可以用下面这行命令来安装:
sudo apt-get install make gcc c++
然后进入 fm_transmitter
目录,执行:
sudo make
很快就编译完成,得到一个可执行文件 fm_transmitter
使用方法
可以直接执行,设置调频频率为100.0 MHz
调用该目录下自带的音频文件 命令如下:
sudo ./fm_transmitter ./star_wars.wav 100.0
也可以通过管道命令这么用,当然需要事先安装好sox
sox star_wars.wav -r 44100 -c 1 -b 16 -t wav - | sudo ./fm_transmitter - 100.0
arecord -D hw:1,0 -c1 -d 0 -r 44100 -f S16_LE | sudo ./fm_transmitter - 100.6
还有人尝试这么用,直接播放 mp3 不过我试验的结果暂时没成功,提示不支持 mp3 错误:
sox -t mp3 http://www.linuxvoice.com/episodes/lv_s02e01.mp3 -t wav - | sudo ./fm_transmitter - 100.
通过 ffmpeg
来本地播放MP3,可以播放,不过音质不太好,节奏有些赶
ffmpeg -i input.mp3 -f s16le -ar 22.05k -ac 1 - | sudo ./fm_transmitter - 100.0
网络播放MP3,这个可以播放出来,但是声音变形严重,速度加快很多,可能是某些参数需要调整
ffmpeg -i http://www.linuxvoice.com/episodes/lv_s02e01.mp3 -f s16le -ar 22.05k -ac 1 - | sudo ./fm_transmitter - 100.0
通过 mpg123
本地播放 mp3,可以播放,节奏正常,不过背景沙沙声比较大
mpg123 -m -C -q -s sound.mp3 | sudo ./fm_transmitter - 100.0
这个是通过一个 USB 麦克风实时播音(因为手头没有USB麦克风,所以没试验)
arecord -d0 -c2 -f S16_LE -r 22050 -twav -D copy | sudo ./fm_transmitter - 100.0
把你的收音机打开,调到 100 MHz
,就可以听到声音了,支持FM
的手机也可以收听,如果不用杜邦线做天线,1米之内可以清晰收听,在GPIO4
(树莓派2有两个指示灯,一个绿色,一个红色,从靠近这两个灯的管脚开始数,绿灯同侧,第4个管脚)上上连一根杜邦线,FM
信号就能穿墙了,wiki建议杜邦线长度为70cm,20cm也可以,不过我用了根18cm左右的,感觉效果也可以,只要你住的地方不是大型别墅,基本上每个房间都可以收听。
相关参考:
1、几个 github 资源,无法编译,勉强编译后页无法使用:
https://github.com/ChristopheJacquet/PiFmRds
2、sox无法播放MP3,错误提示:
sox FAIL formats: no handler for file extension `mp3'
原因参见其官网描述,因为MP3不是一种开源格式,所以 sox 为保证不侵权就无法直接对MP3提供支持,如果希望能支持,则需要自己重新编译 sox,把mp3相关的库编译进去。
.mp3, .mp2 (optional read, optional write)
MP3 compressed audio; MP3 (MPEG Layer 3) is a part of the patent-encumbered MPEG standards for audio and video compression. It is a lossy compression format that achieves good compression rates with little quality loss.
Because MP3 is patented, SoX cannot be distributed with MP3 support without incurring the patent holder’s fees. Users who require SoX with MP3 support must currently compile and build SoX with the MP3 libraries (LAME & MAD) from source code, or, in some cases, obtain pre-built dynamically loadable libraries.
When reading MP3 files, up to 28 bits of precision is stored although only 16 bits is reported to user. This is to allow default behavior of writing 16 bit output files. A user can specify a higher precision for the output file to prevent lossing this extra information. MP3 output files will use up to 24 bits of precision while encoding.
MP3 compression parameters can be selected using SoX’s −C option as follows (note that the current syntax is subject to change):
The primary parameter to the LAME encoder is the bit rate. If the value of the −C value is a positive integer, it’s taken as the bitrate in kbps (e.g. if you specify 128, it uses 128 kbps).
The second most important parameter is probably "quality" (really performance), which allows balancing encoding speed vs. quality. In LAME, 0 specifies highest quality but is very slow, while 9 selects poor quality, but is fast. (5 is the default and 2 is recommended as a good trade-off for high quality encodes.)
Because the −C value is a float, the fractional part is used to select quality. 128.2 selects 128 kbps encoding with a quality of 2. There is one problem with this approach. We need 128 to specify 128 kbps encoding with default quality, so 0 means use default. Instead of 0 you have to use .01 (or .99) to specify the highest quality (128.01 or 128.99).
LAME uses bitrate to specify a constant bitrate, but higher quality can be achieved using Variable Bit Rate (VBR). VBR quality (really size) is selected using a number from 0 to 9. Use a value of 0 for high quality, larger files, and 9 for smaller files of lower quality. 4 is the default.
In order to squeeze the selection of VBR into the the −C value float we use negative numbers to select VRR. -4.2 would select default VBR encoding (size) with high quality (speed). One special case is 0, which is a valid VBR encoding parameter but not a valid bitrate. Compression value of 0 is always treated as a high quality vbr, as a result both -0.2 and 0.2 are treated as highest quality VBR (size) and high quality (speed).
See also Ogg Vorbis for a similar format.
3、[wiki相关描述-Turning_the_Raspberry_Pi_Into_an_FM_Transmitter] (http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter)
内容如下:
from a post on MAKE by Matt Richardson
[hide]
1 Steps to play sound:
2 New! Now with stereo
3 How to change the broadcast frequency
4 The details of how it works
4.1 Accessing Hardware
(Created by Oliver Mattos and Oskar Weigl. Code is GPL)
sudo python
>>> import PiFm
>>> PiFm.play_sound("sound.wav")
Now connect a 70cm (optimally, ~20cm will do) or so plain wire to GPIO 4 (which is pin 7 on header P1) to act as an antenna, and tune an FM radio to 103.3Mhz.
Download the module here:
[Download Now!]
(this contains both source and a ready to go binary. Just run the above code in the same folder. The antenna is optional, but range is reduced from ~100 meters to ~10cm without the antenna. The sound file must be 16 bit mono wav format. )
sudo ./pifm left_right.wav 103.3 22050 stereo
# Example command lines
# play an MP3
ffmpeg -i input.mp3 -f s16le -ar 22.05k -ac 1 - | sudo ./pifm -
# Broadcast from a usb microphone (see arecord manual page for config)
arecord -d0 -c2 -f S16_LE -r 22050 -twav -D copy | sudo ./pifm -
Run the ./pifm binary with no command line arguments to find usage.
The second command line argument is the frequency to transmit on, as a number in Mhz. Eg. This will transmit on 100.0
sudo ./pifm sound.wav 100.0
It will work from about 1Mhz up to 250Mhz, although the useful FM band is 88 Mhz to 108 Mhz in most countries.
Most radio receivers want a signal to be an odd multiple of 0.1 MHz to work properly.
Below is some code that was hacked together over a few hours at the Code Club pihack. It uses the hardware on the raspberry pi that is actually meant to generate spread-spectrum clock signals on the GPIO pins to output FM Radio energy. This means that all you need to do to turn the Raspberry-Pi into a (ridiculously powerful) FM Transmitter is to plug in a wire as the antenna (as little as 20cm will do) into GPIO pin 4 and run the code posted below. It transmits on 100.0 MHz.
When testing, the signal only started to break up after we went through several conference rooms with heavy walls, at least 50m away, and crouched behind a heavy metal cabinet. The sound quality is ok, but not amazing, as it currently plays some clicks when the CPU gets switched away to do anything else than play the music. The plan was to make a kernel mode driver that would be able to use the DMA controller to offload the CPU and play smooth music without loading the CPU, but we ran out of time. Now Done and working, DMA from userspace is awesome and awful at the same time!
If you're v. smart, you might be able to get stereo going! Done!
The python library calls a C program (provided both precompiled and in source form). The C program maps the Peripheral Bus (0x20000000) in physical memory into virtual address space using /dev/mem and mmap. To do this it needs root access, hence the sudo. Next it sets the clock generator module to enabled and sets it to output on GPIO4 (no other accessible pins can be used). It also sets the frequency to 100.0Mhz (provided from PLLD@500Mhz, divided by 5), which provides a carrier. At this point, radios will stop making a "fuzz" noise, and become silent.
Modulation is done by adjusting the frequency using the fractional divider between 100.025Mhz and 99.975Mhz, which makes the audio signal. The fractional divider doesn't have enough resolution to produce more than ~6 bit audio, but since the PI is very fast, we can do oversampling to provide about 9.5 bit audio by using 128 subsamples per real audio sample. We were being naieve with our subsampling algorithm - you can now get full 16 bit quality sound, and it even does FM pre-emphasis so that the result doesn't sound bass-heavy.
本文最新版本链接: http://my.oschina.net/freeblues/blog/497252
==end==