樹莓派支持GPIO
,1代板上有26個引出的管腳,2代板上有40個引出的管腳,其中有一個能夠做爲時鐘信號輸出,因而就有人把把音頻信號經過樹莓派進行FM
調製後從該引腳發出,因而樹莓派就成了一臺微型FM電臺,能夠本身指定頻率,打開調頻收音機,調到對應頻道就能收到你的樹莓派播放的微型 FM
廣播。php
在網絡上看到不少文章介紹樹莓派的微型FM
電臺功能,軟件廣泛是 pifm
,下載了很多版本的 pifm
,試用後發如今個人樹莓派2上沒效果,搜索了半天,終於找到一款能夠同時用於樹莓派1和樹莓派2的軟件:html
fm_transmitter
直接到 github 下載:python
sudo git clone https://github.com/markondej/fm_transmitter
須要本身編譯,要安裝好 make 、gcc 和 c++
linux
編譯工具在個人 2015-05-05
版本的 Raspbian
上已經安裝好了,能夠直接使用,沒有安裝的朋友能夠用下面這行命令來安裝:ios
sudo apt-get install make gcc c++
而後進入 fm_transmitter
目錄,執行:c++
sudo make
很快就編譯完成,獲得一個可執行文件 fm_transmitter
git
能夠直接執行,設置調頻頻率爲100.0 MHz
調用該目錄下自帶的音頻文件 命令以下:github
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 錯誤:app
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左右的,感受效果也能夠,只要你住的地方不是大型別墅,基本上每一個房間均可以收聽。
一、幾個 github 資源,沒法編譯,勉強編譯後頁沒法使用:
https://github.com/ChristopheJacquet/PiFmRds
二、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.
三、[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==