Sunday, August 25, 2013

Transmit Voice From Microphone

I copied this from a post on the Raspberry Pi forums found here.

I figured out how to broadcast your voice through the radio using the raspberry pi, i.e. speak through a mic and tune your radio to a frequency to hear it, in real time!

It's actually quite simple.
First, follow the instructions here to get the FM transmitter setup on your raspberry pi. This transmits .wav files through a specified frequency and requires no extra hardware besides an optional antenna! These guys that did this are brilliant, and it's open source! (This link is a cached version because the original site is down).
http://webcache.googleusercontent.com/search?q=cache:2sSuOAJQhN4J:www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter+raspberry+pi+fm+transmitter&cd=1&hl=en&ct=clnk&gl=us

Second, you need the ALSA sound library installed which most of you should already have this. The executable out of this we will use is 'arecord'.

Plug in your mic and attach an antenna to GPIO4 if desired. 

Then here is the command you will use to start the broadcast. Each piece will be explained. Note that this command may need modified to work for your mic, just keep reading.
CODE: SELECT ALL
arecord -fS16_LE -r 22050 -Dplughw:1,0 - | sudo ./pifm - 100.1 22050


Alright so:
CODE: SELECT ALL
arecord

Program we are using to record audio.
---------------------------------------------------------

CODE: SELECT ALL
-fS16_LE

Output 16-bit data. Needed this way for PiFM to read it.
---------------------------------------------------------

CODE: SELECT ALL
-r 22050

This specifies sampling rate to output recording. 22,050 is a good balance for speed and quality.
---------------------------------------------------------

CODE: SELECT ALL
-Dplughw:1,0

This is where you may need to modify. The '1' specifies card number and '0' is the device number I think. To find this out for your device, use
CODE: SELECT ALL
arecord -l
to see all of the audio devices connected.
---------------------------------------------------------

CODE: SELECT ALL
-

Here it means to print output to standard out.
---------------------------------------------------------

CODE: SELECT ALL
|

Pipe the standard out from previous command to next command's standard input.
---------------------------------------------------------

CODE: SELECT ALL
sudo ./pifm - 100.1 22050

This is explained in the link at the top. '-' means use standard input for "file"(Remember we piped it). '100.1' is the frequency in Mhz to transmit and '22050' is the sampling rate of the input. Actually if you change the sampling rate lower it will output a low pitch sound and higher will do a high pitch sound, kind of a voice changer.

If you did everything right, after you run the command you should be able to tune your radio to 100.1 Mhz and hear yourself talking through the mic!

Good luck! And don't amplify your signal or you could risk getting a fine from the FCC or whatever the agency is for your country!

Stream Playlists from VLC

I use the transmitter with the vlc media player, which allows me to transmit almost anything (internet radio stream, mic, music from my NAS-Drive).

How i did it:
You need:
-VLC Media player ("sudo apt-get install vlc")
-PiFM (http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter)
Make a FIFO-File:
CODE: SELECT ALL
mkfifo /tmp/radio


Open two terminals
Terminal 1:
CODE: SELECT ALL
 vlc YOUR_FILE_STREAM_OR_DEVICE_HERE vlc://quit --intf=dummy --no-sout -smem-time-sync --sout='#transcode{acodec=s16l,ab=32,samplerate=44100,channels=1}:std{access=file,mux=dummy,dst=-}' > /tmp/radio


Terminal 2 (cd into the PiFM dir):
CODE: SELECT ALL
sudo ./pifm /tmp/radio YOUR_FREQ 44100

How to pick location/ frequency

Before you make an antenna, pick out the place that you will put it. The most important thing to remember about FM, is that it works best with Line Of Sight, meaning the more obstacles you have between you and the antenna, the faster the signal will degrade. With that in mind, choosing the optimal placement for your antenna is quiet simple. Anywhere with a good view of the surrounding area will suffice. I live on the side of a hill overlooking a valley, so I out my antenna on my chimney. With a commanding view of the valley, I get strong reception through out the area. If you live in an apartment, The roof of the complex is the perfect place for the antenna. If you cant put it on your house, anywhere with access to power will work. Try putting the antenna on a tree in near your living area. 

Now that you have a location for your antenna, chose a frequency. This tool  shows a list of unused frequency's in the area around you. Try to pick the one with the least signal pollution. Once you have it, run over to my post about how to make your antenna. 

PiFm- Learning To Transmit

At the heart of every radio station there is the transmitter. This is used to transmit your music and prerecorded shows to the world, or at least your neighborhood. The Raspberry Pi is the perfect transmitter for a localized radio station for several key reasons;

  1. Size- with both the computer housing the audio and the transmitter built into a form the size of a credit card, you can put up a radio station anywhere there is power in under a minute. 
  2. Cost- at $35 for the Raspberry Pi V2, the Raspberry is definitely the most cost effective solution on the market for a beginner to radio.
  3. Ease of use- Built on the open source Linux platform, any number of modifications can be made to the source code, and an intuitive GUI makes things easier then ever.
They key to transmitting on the FM spectrum is PiFm, a program made by the Imperial College Robotics Society at a code hack fest. The official wiki for the project can be viewed here, but I will be covering all the steps needed to use PiFm in a much easier way.

When you have constructed an antenna, connect it to your GPIO 4 pin. Click this to download the PiFm package to your raspberry pi. Save it to your home directory and unpack it there. That's it! PiFm comes with a sound.wav file so that you can start using it right away. To make sure that you installed PiFm correctly, open up a new terminal window and type

sudo ./pifm sound.wav 100.0

This will play sound.wav at 100mhz. If you tune a radio to that frequency, you should hear the star wars theme song playing! Good job! It is important to know that in most areas, 100.0FM will be used by an existing radio station. To change the frequency, simply change the 100.0 to any frequency that is open. For example

sudo ./pifm sound.wav 87.9

That is the frequency that I use. If you want to change the file that is playing, simply change sound.wav to the file name. The file does have to be in the home directory- were you unpacked PiFm.

Saturday, August 24, 2013

Antenna that doesn't suck

One of the easiest and most important ways to increase signal range is through the antenna. I accomplished this by building a dipole antenna. It is important to note that before this, I had little to no experience constructing anything- I was a DIY virgin. Luckily, this pdf  gives easy and clear instructions on how to easily construct a dipole antenna-which is what I did. Dipole antennas are the cheapest and easiest antenna to make, and also save space.

Before you construct your antenna, you want to decide on a radio frequency that you will be broadcasting on. This directly affects the size of the antenna as to give as clear a signal as possible. There are many online calculators that will help you find the length of the wire needed, but I used this one. Once you have the length of your antenna, print out the PDF above and write the number on it. Bring this to your local hardware store, they'll help you get everything you need. All together the materials for the antenna detailed above cost about $20. Don't forget to have them cut down the PVC to size, unless you want to do it yourself. I had to do some last minute cutting, and was easily able to cut through the PVC with just a handsaw.

The construction of the antenna took only a couple of hours, I finished mine in a single afternoon. After you have your antenna, you've done the hardest part! Connect the antenna to the GPIO 4 pin on the Raspberry Pi with an alligator clip and copper wire. I got mine from Radio Shack.


My very own radio station?!

I created this website to catalog my efforts at creating a FM radio station with my Raspberry Pi V2.0
Starting out I had no experience with Linux distros and the command line interface. My knowledge about radio was lacking, and I need to learn a lot.

Will be moving this to a collaborative wiki as soon as I have time!

Audio File Format

These instructions are assuming you are using Audacity. If you are not, I recommend you switch. Audacity is a free, open source audio editing tool that is available for download here. The audio file has to be in a 22050Hz format. This can be accomplished by selecting the Project Rate (Hz) tab on the bottom left hand of the screen and selecting 22050 from the drop down menu. That's it! Your audio is now ready for PiFm broadcasting.