Skip to content

Commit

Permalink
pifmrds reads from stdin
Browse files Browse the repository at this point in the history
./pifmrds now can read raw data from stdin
use option -raw

examples:
sudo ./pifmrds -freq 102.0 -audio - < <(while true; do cat src/pifmrds/stereo_44100.wav; done)
sudo ./pifmrds -raw -freq 102.0 -audio - < <(while true; do cat src/pifmrds/stereo_44100.wav; done)

based on this pull request: ChristopheJacquet/PiFmRds#23
  • Loading branch information
F3lda committed Dec 4, 2023
1 parent cce1fe6 commit a62c696
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
18 changes: 12 additions & 6 deletions src/pifmrds/fm_mpx.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ float *alloc_empty_buffer(size_t length) {
}


int fm_mpx_open(char *filename, size_t len) {
int fm_mpx_open(char *filename, size_t len, int raw) {
length = len;

if(filename != NULL) {
// Open the input file
SF_INFO sfinfo;

if(raw) {
sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
sfinfo.samplerate = 44100;
sfinfo.channels = 2;
}

// stdin or file on the filesystem?
if(filename[0] == '-') {
Expand Down Expand Up @@ -118,7 +124,7 @@ int fm_mpx_open(char *filename, size_t len) {

// Choose a cutoff frequency for the low-pass FIR filter
float cutoff_freq = 15700;
//float cutoff_freq = 3000; //For NBFM
//float cutoff_freq = 3000; //For NBFM
if(in_samplerate/2 < cutoff_freq) cutoff_freq = in_samplerate/2 * .8;


Expand Down Expand Up @@ -205,7 +211,7 @@ int fm_mpx_get_samples(float *mpx_buffer) {
if(audio_len == 0) {
if( sf_seek(inf, 0, SEEK_SET) < 0 ) {
fprintf(stderr, "Could not rewind in audio file, terminating\n");
return -1;
return -1;
}
} else {
break;
Expand Down Expand Up @@ -234,10 +240,10 @@ int fm_mpx_get_samples(float *mpx_buffer) {
int iphase = ((int) (audio_pos*FIR_PHASES/downsample_factor) );// I think this is correct
//int iphase=FIR_PHASES-1; // test override
//printf("%d %d \n",fir_index,iphase); // diagnostics
// Sanity checks
// Sanity checks
if ( iphase < 0 ) {iphase=0; printf("low\n"); }// Seems to run faster with these checks in place
if ( iphase >= FIR_PHASES ) {iphase=FIR_PHASES-2; printf("high\n"); }
if( channels > 1 )
{
for(int fi=0; fi<FIR_TAPS; fi++) // fi = Filter Index
Expand All @@ -253,7 +259,7 @@ int fm_mpx_get_samples(float *mpx_buffer) {
out_left+=low_pass_fir[iphase][fi] * fir_buffer_left[(fir_index-fi)&(FIR_TAPS-1)];
}
}
// Simple broadcast compressor
//
// The goal is to get the loudest sounding audio while
Expand Down
2 changes: 1 addition & 1 deletion src/pifmrds/fm_mpx.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

extern int fm_mpx_open(char *filename, size_t len);
extern int fm_mpx_open(char *filename, size_t len, int raw);
extern int fm_mpx_get_samples(float *mpx_buffer);
extern int fm_mpx_close();
28 changes: 16 additions & 12 deletions src/pifmrds/pi_fm_rds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ terminate(int num)
fm_mpx_close();
close_control_pipe();


exit(num);
}

Expand All @@ -151,7 +150,7 @@ fatal(char *fmt, ...)
#define DATA_SIZE 5000


int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt, float ppm, char *control_pipe) {
int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt, float ppm, char *control_pipe, int raw) {
// Catch all signals possible - it is vital we kill the DMA engine
// on process exit!
for (int i = 0; i < 64; i++) {
Expand All @@ -171,7 +170,7 @@ int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt,
int data_index = 0;

// Initialize the baseband generator
if(fm_mpx_open(audio_file, DATA_SIZE) < 0) return 1;
if(fm_mpx_open(audio_file, DATA_SIZE, raw) < 0) return 1;

// Initialize the RDS modulator
char myps[9] = {0};
Expand Down Expand Up @@ -232,18 +231,20 @@ int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt,
varying_ps = 0;
}

if( fm_mpx_get_samples(data) < 0 ) {
terminate(0);
}
data_len = DATA_SIZE;
for(int i=0;i< data_len;i++)
if( fm_mpx_get_samples(data) < 0 ) {
//terminate(0);
return 0;
} else {
data_len = DATA_SIZE;
for(int i=0;i< data_len;i++)
{

devfreq[i] = data[i]*deviation_scale_factor;
devfreq[i] = data[i]*deviation_scale_factor;


}
}
fmmod->SetFrequencySamples(devfreq,data_len);
}
}

return 0;
Expand All @@ -259,6 +260,7 @@ int main(int argc, char **argv) {
uint16_t pi = 0x1234;

float ppm = 0;
int raw = 0;


// Parse command-line arguments
Expand Down Expand Up @@ -291,16 +293,18 @@ int main(int argc, char **argv) {
} else if(strcmp("-ctl", arg)==0 && param != NULL) {
i++;
control_pipe = param;
} else if(strcmp("-raw", arg)==0) { //expect raw input of 44.1khz, 2 channels, 16 bit pcm.
raw = 1;
}
else {
fatal("Unrecognised argument: %s.\n"
"Syntax: pi_fm_rds [-freq freq] [-audio file] [-ppm ppm_error] [-pi pi_code]\n"
" [-ps ps_text] [-rt rt_text] [-ctl control_pipe]\n", arg);
" [-ps ps_text] [-rt rt_text] [-ctl control_pipe] [-raw]\n", arg);
}
}
int FifoSize=DATA_SIZE*2;
fmmod=new ngfmdmasync(carrier_freq,228000,14,FifoSize);
int errcode = tx(carrier_freq, audio_file, pi, ps, rt, ppm, control_pipe);
int errcode = tx(carrier_freq, audio_file, pi, ps, rt, ppm, control_pipe, raw);

terminate(errcode);
}
2 changes: 1 addition & 1 deletion src/pifmrds/rds_wav.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main(int argc, char **argv) {
char *in_file = argv[1];
if(strcmp("NONE", argv[1]) == 0) in_file = NULL;

if(fm_mpx_open(in_file, LENGTH) != 0) {
if(fm_mpx_open(in_file, LENGTH, 0) != 0) {
printf("Could not setup FM mulitplex generator.\n");
return EXIT_FAILURE;
}
Expand Down

0 comments on commit a62c696

Please sign in to comment.