2006-01-15 15:00:56 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/soundcard.h>
|
2006-01-15 17:17:12 -05:00
|
|
|
#include <string.h>
|
2006-01-15 15:00:56 -05:00
|
|
|
|
|
|
|
#define AUDIOBUFSIZE 4096
|
|
|
|
#define FRAMESPERBUFFER 1024
|
|
|
|
#define TIMEOUT 1000L /* select time out for audio device */
|
|
|
|
|
2006-08-31 21:40:03 -04:00
|
|
|
/* XXX probably safer to use a local buffer due to the wsjt threaded nature. */
|
|
|
|
static char rcv_buf[AUDIOBUFSIZE];
|
|
|
|
static char tx_buf[AUDIOBUFSIZE];
|
2006-01-15 15:00:56 -05:00
|
|
|
|
2006-08-31 21:40:03 -04:00
|
|
|
#define MAXDSPNAME 16
|
2006-01-15 15:00:56 -05:00
|
|
|
|
|
|
|
extern void decode1_(int *iarg);
|
|
|
|
void oss_loop(int *iarg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* local state data referencing some gcom common fortran variables as well
|
|
|
|
*/
|
2006-09-01 13:34:21 -04:00
|
|
|
static struct audio_data {
|
2006-01-15 15:00:56 -05:00
|
|
|
int fd_in; /* Audio fd in; used only locally in this function */
|
|
|
|
int fd_out; /* Audio fd out; used only locally in this function */
|
|
|
|
double *Tsec; /* Present time SoundIn,SoundOut */
|
|
|
|
double *tbuf; /* Tsec at time of input callback SoundIn */
|
|
|
|
int *iwrite; /* Write pointer to Rx ring buffer SoundIn */
|
|
|
|
int *ibuf; /* Most recent input buffer# SoundIn */
|
|
|
|
int *TxOK; /* OK to transmit? SoundIn */
|
|
|
|
int *ndebug; /* Write debugging info? GUI */
|
|
|
|
int *ndsec; /* Dsec in units of 0.1 s GUI */
|
|
|
|
int *Transmitting; /* Actually transmitting? SoundOut */
|
|
|
|
int *nwave; /* Number of samples in iwave SoundIn */
|
|
|
|
int *nmode; /* Which WSJT mode? GUI */
|
|
|
|
int *trperiod; /* Tx or Rx period in seconds GUI */
|
|
|
|
int nbuflen;
|
|
|
|
int nfs;
|
|
|
|
int16_t *y1; /* Ring buffer for audio channel 0 SoundIn */
|
|
|
|
int16_t *y2; /* Ring buffer for audio channel 1 SoundIn */
|
|
|
|
short *iwave;
|
|
|
|
}data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start_threads()
|
|
|
|
* inputs - ndevin device number for input
|
|
|
|
* - ndevout device number for output
|
|
|
|
* - y1 short int array for channel 0
|
|
|
|
* - y2 short int array for channel 1
|
|
|
|
* - nmax
|
|
|
|
* - iwrite
|
|
|
|
* - iwave
|
|
|
|
* - nwave
|
|
|
|
* - rate
|
|
|
|
* - NSPB
|
|
|
|
* - TRPeriod
|
|
|
|
* - TxOK
|
|
|
|
* - ndebug debug output or not?
|
|
|
|
* - Transmitting
|
|
|
|
* - Tsec
|
|
|
|
* - ngo
|
|
|
|
* - nmode
|
|
|
|
* - tbuf
|
|
|
|
* - ibuf
|
|
|
|
* - ndsec
|
|
|
|
* output - ?
|
|
|
|
* side effects - Called from audio_init.f90 to start audio decode and
|
|
|
|
* OSS thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
start_threads_(int *ndevin, int *ndevout, short y1[], short y2[],
|
|
|
|
int *nbuflen, int *iwrite, short iwave[],
|
|
|
|
int *nwave, int *nfsample, int *nsamperbuf,
|
|
|
|
int *TRPeriod, int *TxOK, int *ndebug,
|
|
|
|
int *Transmitting, double *Tsec, int *ngo, int *nmode,
|
2006-07-06 09:44:34 -04:00
|
|
|
double tbuf[], int *ibuf, int *ndsec,
|
|
|
|
char *PttPort, char *devin_name, char *devout_name)
|
2006-01-15 15:00:56 -05:00
|
|
|
{
|
|
|
|
pthread_t thread1,thread2;
|
|
|
|
int iret1,iret2;
|
|
|
|
int iarg1 = 1,iarg2 = 2;
|
|
|
|
int32_t rate=*nfsample;
|
|
|
|
int samplesize;
|
|
|
|
int format;
|
|
|
|
int channels;
|
|
|
|
double dnfs;
|
2006-07-06 09:44:34 -04:00
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
|
2006-07-06 16:54:51 -04:00
|
|
|
/* Remove space if present */
|
2006-07-06 09:44:34 -04:00
|
|
|
p = strchr(devin_name, ' ');
|
|
|
|
if(p != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
|
2006-08-31 21:40:03 -04:00
|
|
|
p = strchr(devout_name, ' ');
|
2006-07-06 09:44:34 -04:00
|
|
|
if(p != NULL)
|
2006-08-31 21:40:03 -04:00
|
|
|
*p = '\0';
|
2006-01-15 15:00:56 -05:00
|
|
|
|
2006-08-31 21:40:03 -04:00
|
|
|
data.fd_in = open(devin_name, O_RDONLY, 0);
|
2006-01-15 15:00:56 -05:00
|
|
|
|
2006-07-06 16:54:51 -04:00
|
|
|
if(data.fd_in < 0) {
|
2006-08-31 21:40:03 -04:00
|
|
|
fprintf(stderr, "Cannot open %s for input.\n", devin_name);
|
2006-07-24 15:20:07 -04:00
|
|
|
return (-1);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
2006-08-31 21:40:03 -04:00
|
|
|
if (*devout_name == '\0') {
|
|
|
|
close(data.fd_in);
|
|
|
|
data.fd_in = open(devin_name, O_RDWR, 0);
|
|
|
|
|
|
|
|
if(data.fd_in < 0) {
|
|
|
|
fprintf(stderr, "Cannot open %s for input.\n", devin_name);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.fd_out = data.fd_in;
|
|
|
|
if(ioctl(data.fd_in, SNDCTL_DSP_SETDUPLEX, 0) < 0) {
|
|
|
|
fprintf(stderr, "Cannot use %s for full duplex.\n", devin_name);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
} else {
|
2006-08-31 23:22:02 -04:00
|
|
|
data.fd_out = open(devout_name, O_WRONLY, 0);
|
2006-08-31 21:40:03 -04:00
|
|
|
|
|
|
|
if(data.fd_out < 0) {
|
2006-08-31 23:22:02 -04:00
|
|
|
fprintf(stderr, "Cannot open %s for output.\n", devout_name);
|
2006-08-31 21:40:03 -04:00
|
|
|
return (-1);
|
|
|
|
}
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
data.Tsec = Tsec;
|
|
|
|
data.tbuf = tbuf;
|
|
|
|
data.iwrite = iwrite;
|
|
|
|
data.ibuf = ibuf;
|
|
|
|
data.TxOK = TxOK;
|
|
|
|
data.ndebug = ndebug;
|
|
|
|
data.ndsec = ndsec;
|
|
|
|
data.Transmitting = Transmitting;
|
|
|
|
data.y1 = y1;
|
|
|
|
data.y2 = y2;
|
|
|
|
data.nbuflen = *nbuflen;
|
|
|
|
data.nmode = nmode;
|
|
|
|
data.nwave = nwave;
|
|
|
|
data.iwave = iwave;
|
|
|
|
data.nfs = *nfsample;
|
|
|
|
data.trperiod = TRPeriod;
|
|
|
|
|
|
|
|
dnfs=(double)*nfsample;
|
|
|
|
|
|
|
|
channels = 2;
|
2006-07-06 16:54:51 -04:00
|
|
|
if(ioctl (data.fd_in, SNDCTL_DSP_CHANNELS, &channels) == -1) {
|
2006-01-15 15:00:56 -05:00
|
|
|
fprintf (stderr, "Unable to set 2 channels for input.\n");
|
2006-07-24 15:20:07 -04:00
|
|
|
return (-1);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
2006-07-06 16:54:51 -04:00
|
|
|
if(channels != 2) {
|
2006-01-15 15:00:56 -05:00
|
|
|
fprintf (stderr, "Unable to set 2 channels.\n");
|
2006-07-24 15:20:07 -04:00
|
|
|
return (-1);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
format = AFMT_S16_NE;
|
2006-07-06 16:54:51 -04:00
|
|
|
if(ioctl (data.fd_in, SNDCTL_DSP_SETFMT, &format) == -1) {
|
2006-01-15 15:00:56 -05:00
|
|
|
fprintf (stderr, "Unable to set format for input.\n");
|
2006-07-24 15:20:07 -04:00
|
|
|
return (-1);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
2006-07-06 16:54:51 -04:00
|
|
|
if(ioctl (data.fd_in, SNDCTL_DSP_SPEED, &rate) == -1) {
|
2006-01-15 15:00:56 -05:00
|
|
|
fprintf (stderr, "Unable to set rate for input\n");
|
2006-07-24 15:20:07 -04:00
|
|
|
return (-1);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Audio OSS streams running normally.\n");
|
|
|
|
printf("******************************************************************\n");
|
2006-08-31 21:40:03 -04:00
|
|
|
printf("Opened %s for input.\n", devin_name);
|
|
|
|
if (*devout_name != '\0')
|
|
|
|
printf("Opened %s for output.\n", devout_name);
|
|
|
|
else
|
|
|
|
printf("Opened %s for output.\n", devin_name);
|
2006-01-15 15:00:56 -05:00
|
|
|
printf("Rate set = %d\n", rate);
|
|
|
|
|
|
|
|
// printf("start_threads: creating thread for oss_loop\n");
|
2006-01-15 16:43:30 -05:00
|
|
|
iret1 = pthread_create(&thread1, NULL,
|
|
|
|
(void *(*)(void *))oss_loop, &iarg1);
|
|
|
|
// printf("start_threads: creating thread for decode1_\n");
|
|
|
|
iret2 = pthread_create(&thread2, NULL,
|
|
|
|
(void *(*)(void *))decode1_,&iarg2);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* oss_loop
|
|
|
|
*
|
|
|
|
* inputs - int pointer NOT USED
|
|
|
|
* output - none
|
|
|
|
* side effects -
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
oss_loop(int *iarg)
|
|
|
|
{
|
|
|
|
fd_set readfds, writefds;
|
|
|
|
int nfds = 0;
|
|
|
|
struct timeval timeout = {0, 0};
|
|
|
|
struct timeval tv;
|
|
|
|
int nread;
|
|
|
|
unsigned int i;
|
|
|
|
static int n=0;
|
|
|
|
static int n2=0;
|
|
|
|
static int ia=0;
|
|
|
|
static int ib=0;
|
|
|
|
static int ic=0;
|
|
|
|
static int16_t *in;
|
|
|
|
static int16_t *wptr;
|
|
|
|
static int TxOKz=0;
|
|
|
|
static int ncall=0;
|
|
|
|
static int nsec=0;
|
|
|
|
static double stime;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
FD_ZERO(&readfds );
|
|
|
|
FD_ZERO(&writefds );
|
|
|
|
FD_SET(data.fd_in, &readfds);
|
|
|
|
FD_SET(data.fd_out, &writefds);
|
|
|
|
|
|
|
|
timeout.tv_usec = TIMEOUT;
|
2006-07-06 16:54:51 -04:00
|
|
|
if(select(FD_SETSIZE, &readfds, &writefds, NULL, &timeout) > 0) {
|
|
|
|
if(FD_ISSET(data.fd_in, &readfds)) {
|
2006-01-15 15:00:56 -05:00
|
|
|
nread = read (data.fd_in, rcv_buf, AUDIOBUFSIZE);
|
2006-07-06 16:54:51 -04:00
|
|
|
if(nread <= 0) {
|
2006-01-15 15:00:56 -05:00
|
|
|
fprintf(stderr, "Read error %d\n", nread);
|
2006-04-16 15:52:47 -04:00
|
|
|
return;
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
2006-07-06 16:54:51 -04:00
|
|
|
if(nread == AUDIOBUFSIZE) {
|
2006-01-15 15:00:56 -05:00
|
|
|
/* Get System time */
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
stime = (double) tv.tv_sec + ((double)tv.tv_usec / 1000000.0) +
|
|
|
|
*(data.ndsec) * 0.1;
|
|
|
|
*(data.Tsec) = stime;
|
|
|
|
|
|
|
|
ncall++;
|
|
|
|
|
|
|
|
/* increment buffer pointers only if data available */
|
|
|
|
ia=*(data.iwrite);
|
|
|
|
ib=*(data.ibuf);
|
2006-01-30 11:06:57 -05:00
|
|
|
data.tbuf[ib-1] = stime; /* convert to c index to store */
|
2006-04-13 00:48:28 -04:00
|
|
|
ib++;
|
2006-01-30 11:06:57 -05:00
|
|
|
if(ib>FRAMESPERBUFFER)
|
|
|
|
ib=1;
|
2006-01-15 16:43:30 -05:00
|
|
|
*(data.ibuf) = ib;
|
2006-01-30 11:06:57 -05:00
|
|
|
in = (int16_t *)rcv_buf; /* XXX */
|
2006-01-15 15:00:56 -05:00
|
|
|
for(i=0; i<FRAMESPERBUFFER; i++) {
|
|
|
|
data.y1[ia] = (*in++);
|
|
|
|
data.y2[ia] = (*in++);
|
|
|
|
ia++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ia >= data.nbuflen)
|
|
|
|
ia=0; //Wrap buffer pointer if necessary
|
|
|
|
*(data.iwrite) = ia; /* Save buffer pointer */
|
|
|
|
fivehz_(); /* Call fortran routine */
|
|
|
|
}
|
|
|
|
}
|
2006-07-06 16:54:51 -04:00
|
|
|
if(FD_ISSET(data.fd_in, &writefds)) {
|
2006-01-15 15:00:56 -05:00
|
|
|
/* Get System time */
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
stime = (double) tv.tv_sec + ((double)tv.tv_usec / 1000000.0) +
|
|
|
|
*(data.ndsec) * 0.1;
|
|
|
|
*(data.Tsec) = stime;
|
2006-01-15 17:17:12 -05:00
|
|
|
|
2006-07-06 16:54:51 -04:00
|
|
|
if(*(data.TxOK) && (!TxOKz)) {
|
2006-07-06 16:52:38 -04:00
|
|
|
nsec = (int)stime;
|
2006-07-06 16:54:51 -04:00
|
|
|
n = nsec / *(data.trperiod);
|
|
|
|
ic = (int)(stime - *(data.trperiod) * n) * data.nfs;
|
2006-01-15 15:00:56 -05:00
|
|
|
ic = ic % *(data.nwave);
|
|
|
|
}
|
2006-01-15 17:17:12 -05:00
|
|
|
|
2006-01-15 15:00:56 -05:00
|
|
|
TxOKz = *(data.TxOK);
|
|
|
|
*(data.Transmitting) = *(data.TxOK);
|
2006-01-15 16:43:30 -05:00
|
|
|
wptr = (int16_t *)tx_buf; /* XXX */
|
2006-07-06 16:54:51 -04:00
|
|
|
if(*(data.TxOK)) {
|
|
|
|
for(i=0 ; i<FRAMESPERBUFFER; i++) {
|
2006-01-15 15:00:56 -05:00
|
|
|
n2 = data.iwave[ic];
|
|
|
|
addnoise_(&n2);
|
|
|
|
*wptr++ = n2; /* left */
|
|
|
|
*wptr++ = n2; /* right */
|
|
|
|
ic++;
|
|
|
|
if(ic >= *(data.nwave)) {
|
|
|
|
ic = ic % *(data.nwave); /* Wrap buffer pointer if necessary */
|
|
|
|
if(*(data.nmode) == 2)
|
|
|
|
*(data.TxOK) = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-01-15 17:17:12 -05:00
|
|
|
} else {
|
|
|
|
memset(tx_buf, 0, AUDIOBUFSIZE);
|
2006-01-15 15:00:56 -05:00
|
|
|
}
|
2006-07-06 16:54:51 -04:00
|
|
|
if(write(data.fd_out, tx_buf, AUDIOBUFSIZE) < 0) {
|
2006-01-15 16:43:30 -05:00
|
|
|
fprintf(stderr, "Can't write to soundcard.\n");
|
2006-04-16 15:52:47 -04:00
|
|
|
return;
|
2006-01-15 16:43:30 -05:00
|
|
|
}
|
2006-01-15 15:00:56 -05:00
|
|
|
fivehztx_(); /* Call fortran routine */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|