用树莓派2架设微型FM电台程序(同时支持Pi1、Pi2)

用树莓派2架设微型FM电台程序(同时支持Pi1、Pi2)

目录

原理简述

树莓派支持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==

转载于:https://my.oschina.net/freeblues/blog/497252

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于这个项目 您可以通过一个小小的盒子在互联网上收听数千个免费电台。 互联网上有成千上万的免费电台,通过这个项目,你可以从一个小小的盒子里听到所有的电台。所以,让我们使用Raspberry Pi,扬声器和几个简单易用的软件轻松构建我们的流媒体收音机。 硬件组件: 树莓派2型号B× 1 扬声器:0.25W,8欧姆× 1 项目概况: 电缆的连接: 连接到22和27 步骤1 首先,我们需要准备我们的Pi。使用Raspbian和Pi连接到互联网,打开一个终端并切换到root用户: sudo su 并更新您的软件包列表,然后将您的Pi升级到最新的软件: apt-get update && apt-get upgrade -y 第2步 安装一些额外的包。 我们需要安装Python包来访问GPIO。在以root身份登录的终端中,输入以下内容: apt-get install python-rpi.gpio 现在安装MPlayer,这将播放我们的音频。 apt-get install mplayer 第3步 我们创建了一个txt文件:wpa,它保存了我们的热点地址和密码。 步骤4 我们需要使用网址来使用网络收音机。我们使用https://www.internet-radio.com/ 。随着开关的变化。 [在URL中,应该下载一个pls文件并输入到我们的地址栏中。] 第5步 将收音机配置为在引导时启动。 在终端中,以root用户身份导航到/etc/init.d/,然后使用nano创建一个名为radio的文件。 nano radio 在该文件中,输入以下内容: #! /bin/bash modprobe snd_bcm2835 amixer cset numid=3 1 python /home/pi/radio.py 这会加载声卡的内核模块; Amixer设置扬声器的输出。 第6步 我们在服务上编写我们的代码; 当操作系统启动服务时,这将是工作。在etc / init.d中,我们定义了我们的服务。在这个目录中,我们定义了基于kill和start(使用nano)的服务。同样在我们的代码中,我们使用GPIO的pull_up_down。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值