Fm transmitter in software


65 posts   Page 2 of 3   1, 2, 3
by Yggdrasil » Sun Dec 16, 2012 7:18 pm
Hi Rgh,

I did not look on your code until now, but it is secure to abort the program with Ctrl+C?

Regards,
Yggdrasil
Posts: 132
Joined: Sun Aug 26, 2012 8:45 pm
by rgh » Sun Dec 16, 2012 7:28 pm
Yggdrasil wrote:Hi Rgh,

I did not look on your code until now, but it is secure to abort the program with Ctrl+C?

Regards,
Yggdrasil

Yes, it safe to abort with Ctrl+C.
Posts: 220
Joined: Fri Nov 25, 2011 3:53 pm
by chris.meyers.fsu » Sun Dec 16, 2012 7:32 pm
Very nice! Could you put this code on your github? I feel like it will get lost in the forum.
Posts: 27
Joined: Sat Jun 02, 2012 7:46 pm
by shinyquagsire23 » Mon Dec 17, 2012 5:11 am
I tried the DMA version, and it definitely works well. However, I should note that if you overclock, the sample rate must increase with the processor speed. For example, my wav's must be encoded at 88.2 KHz, twice the default 44.1 kHz, and quadruple the amount the author specified.
Posts: 1
Joined: Mon Dec 17, 2012 5:08 am
Location: Las Vegas, NV
by mikerr » Mon Dec 17, 2012 2:37 pm
rgh wrote:Here's a version I've written that uses DMA and so improves quality and reduces CPU use from 100% to about 1.6% :-)

and turns it into something you can run continuously now it's no longer a cpu hog. Good job!
Got a Pi Camera? View it in my android app - Raspicam Remote ! No software required on the pi
User avatar
Posts: 1162
Joined: Thu Jan 12, 2012 12:46 pm
Location: NorthWest, UK
by dom » Mon Dec 17, 2012 2:47 pm
Just need FM transmitting ALSA driver now!
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3995
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by rgh » Mon Dec 17, 2012 6:29 pm
chris.meyers.fsu wrote:Very nice! Could you put this code on your github? I feel like it will get lost in the forum.

Done, see https://github.com/richardghirst/PiBits/tree/master/PiFmDma
Posts: 220
Joined: Fri Nov 25, 2011 3:53 pm
by mahjongg » Mon Dec 17, 2012 6:41 pm
Perhaps you can also put it into the new PI store, with a warning that using it may be still illegal in some places! :| perhaps as part of an app that broadcasts MP3 music. :)
User avatar
Forum Moderator
Forum Moderator
Posts: 4929
Joined: Sun Mar 11, 2012 12:19 am
by haxr3 » Mon Dec 17, 2012 7:24 pm
Is there any way of enabling this to broadcast a live stream in any way?

My idea was to add a USB wifi module to be able to 'dial in' remotely and send the audio from another computer whilst it is being 'created'. Any idea where to start with this?
Posts: 17
Joined: Wed Jul 11, 2012 6:42 pm
by Yggdrasil » Mon Dec 17, 2012 7:56 pm
dom wrote:Just need FM transmitting ALSA driver now!


haxr3 wrote:Is there any way of enabling this to broadcast a live stream in any way?

My idea was to add a USB wifi module to be able to 'dial in' remotely and send the audio from another computer whilst it is being 'created'. Any idea where to start with this?


I simpler, but still useful, option could be the usage of a named fifo. This could be used to
share wav data between mplayer and PiFm.

The mplayer side is just something like that
Code: Select all
# mkfifo /dev/shm/aufifo
mplayer -ao pcm:waveheader:file=/dev/shm/aufifo $*


Here is a example code snippet for the fifo reading on PiFm side:
http://stackoverflow.com/questions/8657 ... t-and-exit

Regards Yggdrasil
Posts: 132
Joined: Sun Aug 26, 2012 8:45 pm
by poglad » Mon Dec 17, 2012 8:02 pm
This is very cool. But how much power does it put out? If the antenna is not connected, won't the Pi be at risk of damage? I would never operate one of my transmitters with no antenna connected.
User avatar
Posts: 102
Joined: Tue Jul 31, 2012 8:47 am
Location: Aberdeen, Scotland
by redhawk » Mon Dec 17, 2012 8:24 pm
With no antenna the range is about 20cm with an antenna wire a hell of a lot more.
I reckon with the correct length of wire and filter circuit I could push it up to a mile, my cheap Chinese FM USB dongle could manage a cool 0.8 miles with the same arrangement.

Just be aware that in some countries FM broadcast and especially long distance FM broadcast could get yourself into trouble with the law, stray FM broadcasts over Airband aviation is also strictly forbidden.
Play it safe don't let your PI go too far outside your house especially not your whole neighbourhood.
:)

One of the problems with this transmitter design is it must operate on 100MHz this produces the cleanest signal.
If the frequency strays away from 100MHz then the PI would appear several times on the FM band as well as reduced power output for the intended frequency.

Richard S.
User avatar
Posts: 3317
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1
by haxr3 » Mon Dec 17, 2012 9:25 pm
redhawk wrote:
, stray FM broadcasts over Airband aviation is also strictly forbidden.
Play it safe don't let your PI go too far outside your house especially not your whole neighbourhood.
:)

One of the problems with this transmitter design is it must operate on 100MHz this produces the cleanest signal.
If the frequency strays away from 100MHz then the PI would appear several times on the FM band as well as reduced power output for the intended frequency.

Richard S.


A low pass filter is really really required for this as it has strong harmonics at the 3rd, 5th 7th and 9th which sit in licensed and rather essential bands, ie GSM, HAM, emergency services and others. Polluting these frequencies is immoral and dangerous, whereas "breaking in" on FM bands is just plain illegal.

100mhz will just overtake classic fm which isn't really an issue :P Just dont get caught, this GPIO use has the potential to exceed the legal limits by about 2000% with a proper aerial.


As for that code, bit confusing, probably bit too much to ask for a compiled binary or something similar? I have electrics knowledge but no knowledge of code at all.
Posts: 17
Joined: Wed Jul 11, 2012 6:42 pm
by Yggdrasil » Mon Dec 17, 2012 9:33 pm
haxr3 wrote:As for that code, bit confusing, probably bit too much to ask for a compiled binary or something similar? I have electrics knowledge but no knowledge of code at all.


Hi Haxr3,

the gcc compiler is already installed in the Raspbian os as default. Thus, you just need to transfer the files of the above Github project on your pi and type make in a terminal.
Thats all.

P.S. And thanks for your warnings. I already assumed that its would be problematic. I've read
from this near field FM transmitter for car radios. They have very low transmission power. But if we reduce the sending output, the range would be shrink near zero, didn't it?
Posts: 132
Joined: Sun Aug 26, 2012 8:45 pm
by rgh » Mon Dec 17, 2012 10:41 pm
I've updated the code in github (https://github.com/richardghirst/PiBits/tree/master/PiFmDma) so that it reads stdin if no file is provided. You can now do things like:
Code: Select all
sox BatOutOfHell.ogg -c 1 -r 22050 -t wav - | sudo ./PiFmDma

Or you can make your Pi listen for incoming wav files on port 2345 using netcat:
Code: Select all
nc -k -l 2345 | sudo ./PiFmDma

And then send a file from elsewhere on the network (assuming the Pi is at 192.168.1.22):
Code: Select all
sox BatOutOfHell.ogg -c 1 -r 22050 -t wav - | nc 192.168.1.22 2345

PiFmDma.c could use some work to make it send silence when it ran out of input data.

I'd also like to thank haxr3 and redhawk for their warnings, above.
Posts: 220
Joined: Fri Nov 25, 2011 3:53 pm
by pygmy_giant » Mon Dec 17, 2012 11:33 pm
The worst hell I have experienced when monkeying about with DMAness on the Pi in the past is that invisible background tasks continue to run, choking up the Pi which then will not re-boot unless the power is disconnected - which could corrupt the SD Card as it has not been shut down properly. I would advise backing up the SD image.
Posts: 1569
Joined: Sun Mar 04, 2012 12:49 am
by Yggdrasil » Tue Dec 18, 2012 1:09 am
@Rgh: Great :-)

haxr3 wrote:As for that code, bit confusing, probably bit too much to ask for a compiled binary or something similar? I have electrics knowledge but no knowledge of code at all.


Here is a binary of the last version of Rgh.
Attachments
PiFmDma.tar.gz
PiFmDma just zipped for Forum Upload.
(9.11 KiB) Downloaded 259 times
Posts: 132
Joined: Sun Aug 26, 2012 8:45 pm
by sim_tcr » Tue Dec 18, 2012 9:55 am
works great.

Few questions,
I tried an mp3 file and i hear lot of static noise. Does that mean mp3 is not supported?
Is there a way i can create a playlist and play continuous or let PiFmDma play all music file in the folder?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by redhawk » Tue Dec 18, 2012 10:26 am
I tried an mp3 file and i hear lot of static noise. Does that mean mp3 is not supported?

This software supports only wav files therefore it's playing mp3 compressed data as wav data.

Is there a way i can create a playlist and play continuous or let PiFmDma play all music file in the folder?

If you want to listen to music on your FM radio in perfect stereo with the PI then buy yourself a proper FM transmitter like this one - http://www.ebay.co.uk/itm/230669438036? (I own one and it works well).

To play all music files in a folder you could do this using the moc / mocp (Music On Console) application or qmmp for X-Windows.

Richard S.
User avatar
Posts: 3317
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1
by Danbert » Tue Dec 18, 2012 11:12 am
@rgh:
I'm not following what you are using the PWM for in this.
If the transmitter frequency is adjusted away from 100MHz, does the PWM need to be adjusted to match?

Cheers,
Dan
Posts: 17
Joined: Mon Dec 05, 2011 2:52 pm
by rgh » Tue Dec 18, 2012 1:27 pm
@Dan:
The code needs to update the 100MHz clock frequency every 4.54us (10 times per sample, for 22050Hz). To do that with the DMA controller it needs some way to control the rate at which the DMA controller does things. If we just gave the DMA controller a big buffer of values and told it to use them to update the 100MHz clock, it'd go as fast as it could (a lot faster than one every 5.45us, and also not at a very consistent rate as it fought for bus and memory bandwidth with other users). So, to slow down the DMA controller we make it alternate between writing one word to the 100MHz clock control and one word to the PWM FIFO. It can only write to the PWM FIFO when there is space in the FIFO, so by controlling the PWM clock rate we can effectively control how fast its FIFO empties and so control how frequently the DMA controller will write words to the 100MHz clock control. So, we need the PWM clock (and its RNG1 value) setting such that there will be space for one word in the FIFO every 4.54us. The PWM block is used purely to implement this delay; I could probably have used the PCM block instead, for example. Hope that clarifies.
Posts: 220
Joined: Fri Nov 25, 2011 3:53 pm
by Danbert » Tue Dec 18, 2012 2:20 pm
Thanks for that.

So basically the fact that we are running the PWM at 100MHz and the GPIO clock (transmitter frequency) at 100MHz is a coincidence - if we wanted to transmit at 88MHz we would still run the PWM at 100MHz as a rate control. Have I got that right?
Posts: 17
Joined: Mon Dec 05, 2011 2:52 pm
by rgh » Tue Dec 18, 2012 2:54 pm
@Danbert: yes, that's correct.
Posts: 220
Joined: Fri Nov 25, 2011 3:53 pm
by dom » Tue Dec 18, 2012 2:58 pm
rgh wrote:@Dan:
The code needs to update the 100MHz clock frequency every 4.54us (10 times per sample, for 22050Hz). To do that with the DMA controller it needs some way to control the rate at which the DMA controller does things. If we just gave the DMA controller a big buffer of values and told it to use them to update the 100MHz clock, it'd go as fast as it could (a lot faster than one every 5.45us, and also not at a very consistent rate as it fought for bus and memory bandwidth with other users). So, to slow down the DMA controller we make it alternate between writing one word to the 100MHz clock control and one word to the PWM FIFO. It can only write to the PWM FIFO when there is space in the FIFO, so by controlling the PWM clock rate we can effectively control how fast its FIFO empties and so control how frequently the DMA controller will write words to the 100MHz clock control. So, we need the PWM clock (and its RNG1 value) setting such that there will be space for one word in the FIFO every 4.54us. The PWM block is used purely to implement this delay; I could probably have used the PCM block instead, for example. Hope that clarifies.

Very clever.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3995
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by Hello1024 » Tue Dec 18, 2012 5:31 pm
Hi,

It seems I was working in parallel with you on this...

Here is my DMA version:
http://www.icrobotics.co.uk/wiki/images ... ifm.tar.gz


Advantages over the above:
* It uses pulse width modulation to get an effective 18 bit resolution (input audio is only 16 bits though). Sound is now silky smooth!
* FM pre-emphisis is implemented, which reduces the noise heard at high frequencies, and makes the audio you hear on the radio match what you intended to play.
* You can pipe audio into it, so you can stream audio from a text-to-speech engine or similar.
* Transmission frequency and sample rate are now adjustable.

Disadvantages:
* Documentation is sparse
* The PWM channel used for timing is also used by the graphics hardware to output sound (crazy, yes), so if you use sound the FM radio stops working.
Posts: 3
Joined: Wed Dec 12, 2012 11:57 am