Skip to content

Commit

Permalink
Added more clear description
Browse files Browse the repository at this point in the history
Fixed issue #59
  • Loading branch information
markondej committed Dec 29, 2018
1 parent 71e7e23 commit 121982a
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 239 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.o
*.swp
fm_transmitter
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
# fm_transmitter

Use Raspberry Pi as FM transmitter. Works on any RPi board.
Use Raspberry Pi as FM transmitter. Works on any Raspberry Pi board.

This project uses the general clock output to produce frequency modulated radio communication. It is based on idea originaly posted here: [http://icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter](http://icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter), but does not use DMA controller in order to distribute samples to output (clock generator), so sound quality is worse as in PiFm project and only mono transmition is available but this makes possible to run it on all kind of boards.

## How to use it

To compile this project use commands below:
To use this project You will have to buid it. First, clone this repository, then use "make" command as shown below:
```
sudo apt-get install make gcc g++
git clone https://github.com/markondej/fm_transmitter
cd fm_transmitter
make
```

Then you can use it by typing:
After successful build You can start transmitting by typing:
```
sudo ./fm_transmitter [-f frequency] [-r] filename
sudo ./fm_transmitter -f 102.0 acoustic_guitar_duet.wav
```
where:
* -f frequency - Specifies the frequency in MHz, default is 100.0
* -r - Loops the file
* filename - WAVE file name
Where:
* -f 102.0 - Specifies the frequency in MHz, if not passed default is 100.0
* acoustic_guitar_duet.wav - Sample WAVE file, You can use your own

### WAVE Files
You can open uncompressed WAVE (.wav) files or read audio data from stdin, eg.:
### Supported audio files
You can transmitt uncompressed WAVE (.wav) files directly or read audio data from stdin, eg.:
```
sox star_wars.wav -r 22050 -c 1 -b 16 -t wav - | sudo ./fm_transmitter -f 100.6 -
```

Notice only uncompressed WAVE files are supported. If You expire "corrupted data" error try converting file, eg. by using SoX:
```
sox my-audio.mp3 -r 22050 -c 1 -b 16 -t wav my-converted-audio.wav
sudo ./fm_transmitter -f 100.6 my-converted-audio.wav
```
### USB microphone
To use a USB sound card microphone input use arecord, eg.:
```
arecord -D hw:1,0 -c1 -d 0 -r 22050 -f S16_LE | sudo ./fm_transmitter -f 100.6 -
```
In case of performance dropdown use ```plughw:1,0``` instead of ```hw:1,0```.
In case of performance drop down use ```plughw:1,0``` instead of ```hw:1,0```.

## Legal note
Please keep in mind that transmitting on certain frequencies without special permissions may be illegal in your country.

## New features
* works on RPi 1, 2 and 3
* works on any Raspberry Pi model
* reads mono and stereo files
* reads data from stdin
* based on threads

Included sample audio was created by [graham_makes](https://freesound.org/people/graham_makes/sounds/449409/) and published on [freesound.org](https://freesound.org/)
Binary file added acoustic_guitar_duet.wav
Binary file not shown.
44 changes: 0 additions & 44 deletions audio_format.h

This file was deleted.

4 changes: 2 additions & 2 deletions error_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class ErrorReporter : public exception
{
public:
explicit ErrorReporter(string message);
virtual ~ErrorReporter() throw();
virtual ~ErrorReporter() throw();

virtual const char* what() const throw();
virtual const char* what() const throw();
protected:
string errorMessage;
};
Expand Down
60 changes: 23 additions & 37 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,17 @@
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <iostream>
#include "wave_reader.h"
#include "transmitter.h"
#include <cstdlib>
#include <csignal>
#include <iostream>
#include <unistd.h>

using namespace std;

bool stop = false;
Transmitter* transmitter = NULL;

AudioFormat* getFormat(string filename) {
stop = false;
WaveReader* reader = new WaveReader(filename, stop);
AudioFormat* format = reader->getFormat();
delete reader;
return format;
}

void sigIntHandler(int sigNum)
{
if (transmitter != NULL) {
Expand All @@ -64,23 +56,23 @@ int main(int argc, char** argv)
double frequency = 100.0;
bool loop = false;
string filename;

bool showUsage = true;
for (int i = 1; i < argc; i++) {
if (string("-f") == argv[i]) {
if (i < argc - 1) {
frequency = ::atof(argv[i + 1]);
i++;
}
} else if (string("-r") == argv[i]) {
loop = true;
} else {
if (i == argc - 1) {
showUsage = false;
filename = argv[i];
}
int opt;

while ((opt = getopt(argc, argv, "rf:")) != -1) {
switch (opt) {
case 'r':
loop = true;
break;
case 'f':
frequency = ::atof(optarg);
break;
}
}
if (optind < argc) {
filename = argv[optind];
showUsage = false;
}
if (showUsage) {
cout << "Usage: " << argv[0] << " [-f frequency] [-r] FILE" << endl;
return 0;
Expand All @@ -90,19 +82,13 @@ int main(int argc, char** argv)

try {
transmitter = Transmitter::getInstance();

if (filename != "-") {
AudioFormat* format = getFormat(filename);
cout << "Playing: " << filename << ", "
<< format->sampleRate << " Hz, "
<< format->bitsPerSample << " bits, "
<< ((format->channels > 0x01) ? "stereo" : "mono") << endl;
delete format;
} else {
cout << "Playing: STDIN" << endl;
}

transmitter->play(filename, frequency, loop);
WaveReader reader(filename != "-" ? filename : string(), stop);
PCMWaveHeader header = reader.getHeader();
cout << "Playing: " << reader.getFilename() << ", "
<< header.sampleRate << " Hz, "
<< header.bitsPerSample << " bits, "
<< ((header.channels > 0x01) ? "stereo" : "mono") << endl;
transmitter->play(&reader, frequency, 0, loop);
} catch (exception &error) {
cout << "Error: " << error.what() << endl;
return 1;
Expand Down
14 changes: 8 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
CFLAGS += -Wall -fexceptions -pthread -lm -O3 -fpermissive -fno-strict-aliasing
FLAGS = -Wall -fexceptions -pthread -O3 -fpermissive -fno-strict-aliasing
INCLUDES = -I/opt/vc/include -L/opt/vc/lib
LIBS = -lm -lbcm_host
TARGET = fm_transmitter

CPP=$(CCPREFIX)g++

all: main.o error_reporter.o wave_reader.o transmitter.o
$(CPP) $(CFLAGS) -o $(TARGET) main.o error_reporter.o wave_reader.o transmitter.o
$(CPP) $(FLAGS) $(INCLUDES) $(LIBS) -o $(TARGET) main.o error_reporter.o wave_reader.o transmitter.o

wave_reader.o: wave_reader.cpp wave_reader.h
$(CPP) $(CFLAGS) -c wave_reader.cpp
$(CPP) $(FLAGS) $(INCLUDES) $(LIBS) -c wave_reader.cpp

error_reporter.o: error_reporter.cpp error_reporter.h
$(CPP) $(CFLAGS) -c error_reporter.cpp
$(CPP) $(FLAGS) $(INCLUDES) $(LIBS) -c error_reporter.cpp

transmitter.o: transmitter.cpp transmitter.h
$(CPP) $(CFLAGS) -c transmitter.cpp
$(CPP) $(FLAGS) $(INCLUDES) $(LIBS) -c transmitter.cpp

main.o: main.cpp
$(CPP) $(CFLAGS) -c main.cpp
$(CPP) $(FLAGS) $(INCLUDES) $(LIBS) -c main.cpp

clean:
rm *.o
Binary file removed star_wars.wav
Binary file not shown.
Loading

0 comments on commit 121982a

Please sign in to comment.