summaryrefslogtreecommitdiff
path: root/lib/ft2/ft2audio.c
blob: b5f19fa40501447c0fac16dc9c6836a8a9691cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <stdio.h>
#include "portaudio.h"
#include <string.h>
#include <time.h>

int iaa;
int icc;
double total_time=0.0;

//  Definition of structure pointing to the audio data
typedef struct
{
  int    *iwrite;
  int    *itx;
  int    *TxOK;
  int    *Transmitting;
  int    *nwave;
  int    *nright;
  int     nring;
  int     nfs;
  short  *y1;
  short  *y2;
  short  *iwave;
} paTestData;

//  Input callback routine:
static int
SoundIn( void *inputBuffer, void *outputBuffer,
		       unsigned long framesPerBuffer,
		       const PaStreamCallbackTimeInfo* timeInfo, 
		       PaStreamCallbackFlags statusFlags,
		       void *userData )
{
  paTestData *data = (paTestData*)userData;
  short *in = (short*)inputBuffer;
  unsigned int i;
  static int ia=0;

  if(*data->Transmitting) return 0;
  
  if(statusFlags!=0) printf("Status flags %d\n",(int)statusFlags);

  if((statusFlags&1) == 0) {
    //increment buffer pointers only if data available
    ia=*data->iwrite;
    if(*data->nright==0) {                 //Use left channel for input
      for(i=0; i<framesPerBuffer; i++) {
	data->y1[ia] = (*in++);
	data->y2[ia] = (*in++);
	ia++;
      }
    } else {                               //Use right channel
      for(i=0; i<framesPerBuffer; i++) {
	data->y2[ia] = (*in++);
	data->y1[ia] = (*in++);
	ia++;
      }
    }
  }

  if(ia >= data->nring) ia=0;          //Wrap buffer pointer if necessary
  *data->iwrite = ia;                  //Save buffer pointer
  iaa=ia;
  total_time += (double)framesPerBuffer/12000.0;
  //  printf("iwrite:  %d\n",*data->iwrite);
  return 0;
}

//  Output callback routine:
static int
SoundOut( void *inputBuffer, void *outputBuffer,
		       unsigned long framesPerBuffer,
		       const PaStreamCallbackTimeInfo* timeInfo, 
		       PaStreamCallbackFlags statusFlags,
		       void *userData )
{
  paTestData *data = (paTestData*)userData;
  short *wptr = (short*)outputBuffer;
  unsigned int i,n;
  static short int n2;
  static int ic=0;
  static int TxOKz=0;
  static clock_t tstart=-1;
  static clock_t tend=-1;
  static int nsent=0;

  //  printf("txOK:  %d  %d\n",TxOKz,*data->TxOK);

  if(*data->TxOK && (!TxOKz)) ic=0;   //Reset buffer pointer to start Tx
  *data->Transmitting=*data->TxOK;    //Set the "transmitting" flag

  if(*data->TxOK)  {
    if(!TxOKz) {
      // Start of a transmission
      tstart=clock();
      nsent=0;
      //      printf("Start Tx\n");
    }
    TxOKz=*data->TxOK;
    for(i=0 ; i < framesPerBuffer; i++ )  {
      n2=data->iwave[ic];
      *wptr++ = n2;                   //left
      *wptr++ = n2;                   //right
      ic++;

      if(ic > *data->nwave) {
	*data->TxOK = 0;
	*data->Transmitting = 0;
	*data->iwrite = 0;            //Reset Rx buffer pointer to 0
	ic=0;
	tend=clock();
	double TxT=((double)(tend-tstart))/CLOCKS_PER_SEC;
	//	printf("End Tx, TxT = %f  nSent = %d\n",TxT,nsent);
	break;
      }
    }
    nsent += framesPerBuffer;
  } else {
    memset((void*)outputBuffer, 0, 2*sizeof(short)*framesPerBuffer);
  }
  *data->itx = icc;                    //Save buffer pointer
  icc=ic;
  return 0;
}

/*******************************************************************/
int ft2audio_(int *ndevin, int *ndevout, int *npabuf, int *nright, 
	      short y1[], short y2[], int *nring, int *iwrite, 
	      int *itx, short iwave[], int *nwave, int *nfsample, 
	      int *TxOK, int *Transmitting, int *ngo)

{
  paTestData data;
  PaStream *instream, *outstream;
  PaStreamParameters inputParameters, outputParameters;
  //  PaStreamInfo *streamInfo;

  int nfpb = *npabuf;
  int nSampleRate = *nfsample;
  int ndevice_in = *ndevin;
  int ndevice_out = *ndevout;
  double dSampleRate = (double) *nfsample;
  PaError err_init, err_open_in, err_open_out, err_start_in, err_start_out;
  PaError err = 0;

  data.iwrite = iwrite;
  data.itx = itx;
  data.TxOK = TxOK;
  data.Transmitting = Transmitting;
  data.y1 = y1;
  data.y2 = y2;
  data.nring = *nring;
  data.nright = nright;
  data.nwave = nwave;
  data.iwave = iwave;
  data.nfs = nSampleRate;

  err_init = Pa_Initialize();                      // Initialize PortAudio

  if(err_init) {
    printf("Error initializing PortAudio.\n");
    printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_init),
	   err_init);
    Pa_Terminate();  // I don't think we need this but...
    return(-1);
  }

  //  printf("Opening device %d for input, %d for output...\n",
  //         ndevice_in,ndevice_out);

  inputParameters.device = ndevice_in;
  inputParameters.channelCount = 2;
  inputParameters.sampleFormat = paInt16;
  inputParameters.suggestedLatency = 0.2;
  inputParameters.hostApiSpecificStreamInfo = NULL;

// Test if this configuration actually works, so we do not run into an
// ugly assertion
  err_open_in = Pa_IsFormatSupported(&inputParameters, NULL, dSampleRate);

  if (err_open_in == 0) {
    err_open_in = Pa_OpenStream(
		       &instream,              //address of stream
		       &inputParameters,
		       NULL,
		       dSampleRate,            //Sample rate
		       nfpb,                   //Frames per buffer
		       paNoFlag,
		       (PaStreamCallback *)SoundIn,  //Callback routine
		       (void *)&data);  //address of data structure

    if(err_open_in) {   // We should have no error here usually
      printf("Error opening input audio stream:\n");
      printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_open_in),
	     err_open_in);
      err = 1;
    } else {
      //      printf("Successfully opened audio input.\n");
    }
  } else {
    printf("Error opening input audio stream.\n");
    printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_open_in),
	   err_open_in);
    err = 1;
  }

  outputParameters.device = ndevice_out;
  outputParameters.channelCount = 2;
  outputParameters.sampleFormat = paInt16;
  outputParameters.suggestedLatency = 0.2;
  outputParameters.hostApiSpecificStreamInfo = NULL;

// Test if this configuration actually works, so we do not run into an
// ugly assertion.
  err_open_out = Pa_IsFormatSupported(NULL, &outputParameters, dSampleRate);

  if (err_open_out == 0) {
    err_open_out = Pa_OpenStream(
		       &outstream,             //address of stream
		       NULL,
		       &outputParameters,
		       dSampleRate,            //Sample rate
		       nfpb,                   //Frames per buffer
		       paNoFlag,
		       (PaStreamCallback *)SoundOut,  //Callback routine
		       (void *)&data);         //address of data structure

    if(err_open_out) {     // We should have no error here usually
      printf("Error opening output audio stream!\n");
      printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_open_out),
	     err_open_out);
      err += 2;
    } else {
      //      printf("Successfully opened audio output.\n");
    }
  } else {
    printf("Error opening output audio stream.\n");
    printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_open_out),
	   err_open_out);
    err += 2;
  }

  // if there was no error in opening both streams start them
  if (err == 0) {
    err_start_in = Pa_StartStream(instream);             //Start input stream
    if(err_start_in) {
      printf("Error starting input audio stream!\n");
      printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_start_in),
	     err_start_in);
      err += 4;
    }

    err_start_out = Pa_StartStream(outstream);          //Start output stream
    if(err_start_out) {
      printf("Error starting output audio stream!\n");
      printf("\tErrortext: %s\n\tNumber: %d\n",Pa_GetErrorText(err_start_out),
	     err_start_out);
      err += 8;
    } 
  }

  if (err == 0) printf("Audio streams running normally.\n******************************************************************\n");

  while( Pa_IsStreamActive(instream) && (*ngo != 0) && (err == 0) )  {
    int ic1=0;
    int ic2=0;
    if(_kbhit()) ic1 = _getch();
    if(_kbhit()) ic2 = _getch();
    // if(ic1!=0 || ic2!=0) printf("%d   %d   %d\n",iaa,ic1,ic2);
    update_(&total_time,&ic1,&ic2);
    Pa_Sleep(100);
  }

  Pa_AbortStream(instream);              // Abort stream
  Pa_CloseStream(instream);             // Close stream, we're done.
  Pa_AbortStream(outstream);              // Abort stream
  Pa_CloseStream(outstream);             // Close stream, we're done.

  Pa_Terminate();

  return(err);
}


int padevsub_(int *idevin, int *idevout)
{
  int numdev,ndefin,ndefout;
  int nchin[101], nchout[101];
  int      i, devIdx;
  int      numDevices;
  const PaDeviceInfo *pdi;
  PaError  err;

  Pa_Initialize();
  numDevices = Pa_GetDeviceCount();
  numdev = numDevices;

  if( numDevices < 0 )  {
    err = numDevices;
    Pa_Terminate();
    return err;
  }

  if ((devIdx = Pa_GetDefaultInputDevice()) > 0) {
    ndefin = devIdx;
  } else {
    ndefin = 0;
  }

  if ((devIdx = Pa_GetDefaultOutputDevice()) > 0) {
    ndefout = devIdx;
  } else {
    ndefout = 0;
  }

  printf("\nAudio     Input    Output     Device Name\n");
  printf("Device  Channels  Channels\n");
  printf("------------------------------------------------------------------\n");

  for( i=0; i < numDevices; i++ )  {
    pdi = Pa_GetDeviceInfo(i);
//    if(i == Pa_GetDefaultInputDevice()) ndefin = i;
//    if(i == Pa_GetDefaultOutputDevice()) ndefout = i;
    nchin[i]=pdi->maxInputChannels;
    nchout[i]=pdi->maxOutputChannels;
    printf("  %2d       %2d        %2d       %s\n",i,nchin[i],nchout[i],
	   pdi->name);
  }

  printf("\nUser requested devices:   Input = %2d   Output = %2d\n",
  	 *idevin,*idevout);
  printf("Default devices:          Input = %2d   Output = %2d\n",
  	 ndefin,ndefout);
  if((*idevin<0) || (*idevin>=numdev)) *idevin=ndefin;
  if((*idevout<0) || (*idevout>=numdev)) *idevout=ndefout;
  if((*idevin==0) && (*idevout==0))  {
    *idevin=ndefin;
    *idevout=ndefout;
  }
  printf("Will open devices:        Input = %2d   Output = %2d\n",
  	 *idevin,*idevout);

  Pa_Terminate();

  return 0;
}