F5OEO-rpitx/fm/pifm.c

118 wiersze
2.7 KiB
C
Czysty Zwykły widok Historia

2015-10-21 16:08:45 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sndfile.h>
#define BUFFER_LEN 1024*8
int FileFreqTiming;
// Test program using SNDFILE
2015-10-21 16:08:45 +00:00
// see http://www.mega-nerd.com/libsndfile/api.html for API
void WriteTone(double Frequency,uint32_t Timing)
{
typedef struct {
double Frequency;
uint32_t WaitForThisSample;
} samplerf_t;
samplerf_t RfSample;
2015-10-21 16:08:45 +00:00
RfSample.Frequency=Frequency;
RfSample.WaitForThisSample=Timing; //en 100 de nanosecond
//printf("Freq =%f Timing=%d\n",RfSample.Frequency,RfSample.WaitForThisSample);
if (write(FileFreqTiming,&RfSample,sizeof(samplerf_t)) != sizeof(samplerf_t)) {
fprintf(stderr, "Unable to write sample\n");
}
2015-10-21 16:08:45 +00:00
}
int main(int argc, char **argv) {
float data [2*BUFFER_LEN] ;
float data_filtered[2*BUFFER_LEN] ; // we generate complex I/Q samples
SNDFILE *infile, *outfile ;
SF_INFO sfinfo ;
2015-10-21 16:08:45 +00:00
int readcount, nb_samples ;
char *infilename ;
char *outfilename ;
int k ;
float x ;
2015-10-21 16:08:45 +00:00
if( argc < 2 ) {
printf("Usage : %s in.wav [out.wav]\n", argv[0]);
return(1);
}
2015-10-21 16:08:45 +00:00
infilename = argv[1] ;
if( argc == 3 ) {
outfilename = argv[2] ;
} else {
outfilename = (char *)malloc( 128 );
sprintf( outfilename, "%s", "out.ft");
}
if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
{ /* Open failed so print an error message. */
printf ("Not able to open input file %s.\n", infilename) ;
/* Print the error message from libsndfile. */
puts (sf_strerror (NULL)) ;
return 1 ;
} ;
2015-10-21 16:08:45 +00:00
if( sfinfo.samplerate != 48000 ) {
printf("Input rate must be 48K.\n");
return 1 ;
}
FileFreqTiming = open(outfilename, O_WRONLY|O_CREAT, 0644);
2015-10-21 16:08:45 +00:00
/** **/
printf ("Reading file : %s\n", infilename ) ;
printf ("Sample Rate : %d\n", sfinfo.samplerate) ;
printf ("Channels : %d\n", sfinfo.channels) ;
printf ("----------------------------------------\n") ;
printf ("Writing file : %s\n", outfilename ) ;
2015-10-21 16:08:45 +00:00
/* While there are.frames in the input file, read them, process
** them and write them to the output file.
*/
2015-12-14 12:28:04 +00:00
int Excursion=6000;
2015-10-21 16:08:45 +00:00
while ((readcount = sf_readf_float(infile, data, BUFFER_LEN)))
{
nb_samples = readcount / sfinfo.channels ;
2015-10-21 16:08:45 +00:00
for( k=0 ; k < nb_samples ; k++ ) {
x = data[k*sfinfo.channels] ;
if( sfinfo.channels == 2 ) {
// stereo file, avg left + right
x += data[k*sfinfo.channels+1] ;
x /= 2 ;
}
//printf("%f \n",x);
WriteTone(x*Excursion*2.0,1e9/48000.0);
2015-10-21 16:08:45 +00:00
}
2015-10-21 16:08:45 +00:00
} ;
/* Close input and output files. */
sf_close (infile) ;
close(FileFreqTiming);
2015-10-21 16:08:45 +00:00
return 0;
}