summaryrefslogtreecommitdiff
path: root/gr-audio/lib/portaudio/audio_portaudio_sink.cc
blob: 515cd04d9d1e784f81da14ca2d0af0a97b8734d9 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*- c++ -*- */
/*
 * Copyright 2006-2011 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in he hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gr_audio_registry.h"
#include <audio_portaudio_sink.h>
#include <gr_io_signature.h>
#include <gr_prefs.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <stdexcept>
#include <gri_portaudio.h>
#include <string.h>

AUDIO_REGISTER_SINK(REG_PRIO_MED, portaudio)(
    int sampling_rate, const std::string &device_name, bool ok_to_block
){
    return audio_sink::sptr(new audio_portaudio_sink(sampling_rate, device_name, ok_to_block));
}

//#define	LOGGING   0		// define to 0 or 1

#define SAMPLE_FORMAT 		paFloat32
typedef float sample_t;

// Number of portaudio buffers in the ringbuffer
static const unsigned int N_BUFFERS = 4;

static std::string 
default_device_name ()
{
  return gr_prefs::singleton()->get_string("audio_portaudio", "default_output_device", "");
}

void
audio_portaudio_sink::create_ringbuffer(void)
{
  int bufsize_samples = d_portaudio_buffer_size_frames * d_output_parameters.channelCount;
  
  if (d_verbose)
    fprintf(stderr,"ring buffer size  = %d frames\n",
	    N_BUFFERS*bufsize_samples/d_output_parameters.channelCount);

  // FYI, the buffer indicies are in units of samples.
  d_writer = gr_make_buffer(N_BUFFERS * bufsize_samples, sizeof(sample_t));
  d_reader = gr_buffer_add_reader(d_writer, 0);
}

/*
 * This routine will be called by the PortAudio engine when audio is needed.
 * It may called at interrupt level on some machines so don't do anything
 * that could mess up the system like calling malloc() or free().
 *
 * Our job is to write framesPerBuffer frames into outputBuffer.
 */
int
portaudio_sink_callback (const void *inputBuffer,
			 void *outputBuffer,
			 unsigned long framesPerBuffer,
			 const PaStreamCallbackTimeInfo* timeInfo,
			 PaStreamCallbackFlags statusFlags,
			 void *arg)
{
  audio_portaudio_sink *self = (audio_portaudio_sink *)arg;
  int nreqd_samples =
    framesPerBuffer * self->d_output_parameters.channelCount;

  int navail_samples = self->d_reader->items_available();
  
  if (nreqd_samples <= navail_samples) {  // We've got enough data...
    {
      gruel::scoped_lock guard(self->d_ringbuffer_mutex);

      memcpy(outputBuffer,
	     self->d_reader->read_pointer(),
	     nreqd_samples * sizeof(sample_t));
      self->d_reader->update_read_pointer(nreqd_samples);

      self->d_ringbuffer_ready = true;
    }

    // Tell the sink thread there is new room in the ringbuffer.
    self->d_ringbuffer_cond.notify_one();
    return paContinue;
  }

  else {			// underrun
    self->d_nunderuns++;
    ssize_t r = ::write(2, "aU", 2);	// FIXME change to non-blocking call
    if(r == -1) {
      perror("audio_portaudio_source::portaudio_source_callback write error to stderr.");
    }

    // FIXME we should transfer what we've got and pad the rest
    memset(outputBuffer, 0, nreqd_samples * sizeof(sample_t));

    self->d_ringbuffer_ready = true;
    self->d_ringbuffer_cond.notify_one();  // Tell the sink to get going!

    return paContinue;
  }
}


// ----------------------------------------------------------------

audio_portaudio_sink::audio_portaudio_sink(int sampling_rate,
					   const std::string device_name,
					   bool ok_to_block)
  : audio_sink ("audio_portaudio_sink",
		   gr_make_io_signature(0, 0, 0),
		   gr_make_io_signature(0, 0, 0)),
    d_sampling_rate(sampling_rate),
    d_device_name(device_name.empty() ? default_device_name() : device_name),
    d_ok_to_block(ok_to_block),
    d_verbose(gr_prefs::singleton()->get_bool("audio_portaudio", "verbose", false)),
    d_portaudio_buffer_size_frames(0),
    d_stream(0),
    d_ringbuffer_mutex(),
    d_ringbuffer_cond(),
    d_ringbuffer_ready(false),
    d_nunderuns(0)
{
  memset(&d_output_parameters, 0, sizeof(d_output_parameters));
  //if (LOGGING)
  //  d_log = gri_logger::singleton();

  PaError 	      err;
  int     	      i, numDevices;
  PaDeviceIndex	      device = 0;
  const PaDeviceInfo *deviceInfo = NULL;

  err = Pa_Initialize();
  if (err != paNoError) {
    bail ("Initialize failed", err);
  }

  if (d_verbose)
    gri_print_devices();

  numDevices = Pa_GetDeviceCount();
  if (numDevices < 0)
    bail("Pa Device count failed", 0);
  if (numDevices == 0)
    bail("no devices available", 0);

  if (d_device_name.empty()) 
  {
    // FIXME Get smarter about picking something
    fprintf(stderr,"\nUsing Default Device\n");
    device = Pa_GetDefaultOutputDevice();
    deviceInfo = Pa_GetDeviceInfo(device);
    fprintf(stderr,"%s is the chosen device using %s as the host\n",
	    deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name);
  }
  else
  {
    bool found = false;
    fprintf(stderr,"\nTest Devices\n");
    for (i=0;i<numDevices;i++) {
      deviceInfo = Pa_GetDeviceInfo( i );
      fprintf(stderr,"Testing device name: %s",deviceInfo->name);
      if (deviceInfo->maxOutputChannels <= 0) {
	fprintf(stderr,"\n");
	continue;
      }
      if (strstr(deviceInfo->name, d_device_name.c_str())){
	fprintf(stderr,"  Chosen!\n");
	device = i;
	fprintf(stderr,"%s using %s as the host\n",d_device_name.c_str(),
		Pa_GetHostApiInfo(deviceInfo->hostApi)->name), fflush(stderr);
	found = true;
	deviceInfo = Pa_GetDeviceInfo(device);
	i = numDevices; 	// force loop exit
      }
      else
	fprintf(stderr,"\n"),fflush(stderr);
    }

    if (!found){
      bail("Failed to find specified device name", 0);
      exit(1);
    }
  }


  d_output_parameters.device = device;
  d_output_parameters.channelCount     = deviceInfo->maxOutputChannels;
  d_output_parameters.sampleFormat     = SAMPLE_FORMAT;
  d_output_parameters.suggestedLatency = deviceInfo->defaultLowOutputLatency;
  d_output_parameters.hostApiSpecificStreamInfo = NULL;

  // We fill in the real channelCount in check_topology when we know
  // how many inputs are connected to us.

  // Now that we know the maximum number of channels (allegedly)
  // supported by the h/w, we can compute a reasonable input
  // signature.  The portaudio specs say that they'll accept any
  // number of channels from 1 to max.
  set_input_signature(gr_make_io_signature(1, deviceInfo->maxOutputChannels,
					   sizeof (sample_t)));
}


bool
audio_portaudio_sink::check_topology (int ninputs, int noutputs)
{
  PaError err;

  if (Pa_IsStreamActive(d_stream))
  {
      Pa_CloseStream(d_stream);
      d_stream = 0;
      d_reader.reset();		// boost::shared_ptr for d_reader = 0
      d_writer.reset();		// boost::shared_ptr for d_write = 0
  }

  d_output_parameters.channelCount = ninputs;	// # of channels we're really using

#if 1
  d_portaudio_buffer_size_frames = (int)(0.0213333333  * d_sampling_rate + 0.5);  // Force 1024 frame buffers at 48000
  fprintf(stderr, "Latency = %8.5f, requested sampling_rate = %g\n", // Force latency to 21.3333333.. ms
	  0.0213333333, (double)d_sampling_rate);
#endif
  err = Pa_OpenStream(&d_stream,
		      NULL,			// No input
		      &d_output_parameters,
		      d_sampling_rate,
		      d_portaudio_buffer_size_frames,
		      paClipOff,
		      &portaudio_sink_callback,
		      (void*)this);

  if (err != paNoError) {
    output_error_msg ("OpenStream failed", err);
    return false;
  }

#if 0  
  const PaStreamInfo *psi = Pa_GetStreamInfo(d_stream);

  d_portaudio_buffer_size_frames = (int)(d_output_parameters.suggestedLatency  * psi->sampleRate);
  fprintf(stderr, "Latency = %7.4f, psi->sampleRate = %g\n",
	  d_output_parameters.suggestedLatency, psi->sampleRate);
#endif

  fprintf(stderr, "d_portaudio_buffer_size_frames = %d\n", d_portaudio_buffer_size_frames);

  assert(d_portaudio_buffer_size_frames != 0);

  create_ringbuffer();

  err = Pa_StartStream(d_stream);
  if (err != paNoError) {
    output_error_msg ("StartStream failed", err);
    return false;
  }

  return true;
}

audio_portaudio_sink::~audio_portaudio_sink ()
{
  Pa_StopStream(d_stream);	// wait for output to drain
  Pa_CloseStream(d_stream);
  Pa_Terminate();
}

/*
 * This version consumes everything sent to it, blocking if required.
 * I think this will allow us better control of the total buffering/latency
 * in the audio path.
 */
int
audio_portaudio_sink::work (int noutput_items,
			    gr_vector_const_void_star &input_items,
			    gr_vector_void_star &output_items)
{
  const float **in = (const float **) &input_items[0];
  const unsigned nchan = d_output_parameters.channelCount; // # of channels == samples/frame

  int k;

  for (k = 0; k < noutput_items; ){
    int nframes = d_writer->space_available() / nchan;	// How much space in ringbuffer
    if (nframes == 0){			// no room...
      if (d_ok_to_block){
	{
	  gruel::scoped_lock guard(d_ringbuffer_mutex);
	  while (!d_ringbuffer_ready)
	    d_ringbuffer_cond.wait(guard);
	}

	continue;
      }
      else {
	// There's no room and we're not allowed to block.
	// (A USRP is most likely controlling the pacing through the pipeline.)
	// We drop the samples on the ground, and say we processed them all ;)
	//
	// FIXME, there's probably room for a bit more finesse here.
	return noutput_items;
      }
    }

    // We can write the smaller of the request and the room we've got
    {
      gruel::scoped_lock guard(d_ringbuffer_mutex);

      int nf = std::min(noutput_items - k, nframes);
      float *p = (float *) d_writer->write_pointer();
      
      for (int i = 0; i < nf; i++)
	for (unsigned int c = 0; c < nchan; c++)
	  *p++ = in[c][k + i];
      
      d_writer->update_write_pointer(nf * nchan);
      k += nf;

      d_ringbuffer_ready = false;
    }
  }

  return k;  // tell how many we actually did
}

void
audio_portaudio_sink::output_error_msg (const char *msg, int err)
{
  fprintf (stderr, "audio_portaudio_sink[%s]: %s: %s\n",
	   d_device_name.c_str (), msg, Pa_GetErrorText(err));
}

void
audio_portaudio_sink::bail (const char *msg, int err) throw (std::runtime_error)
{
  output_error_msg (msg, err);
  throw std::runtime_error ("audio_portaudio_sink");
}