Imported Upstream version 3.2.2
[debian/gnuradio] / gr-audio-portaudio / src / audio_portaudio_sink.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef INCLUDED_AUDIO_PORTAUDIO_SINK_H
23 #define INCLUDED_AUDIO_PORTAUDIO_SINK_H
24
25 #include <gr_sync_block.h>
26 #include <gr_buffer.h>
27 #include <gnuradio/omnithread.h>
28 #include <string>
29 #include <portaudio.h>
30 #include <stdexcept>
31 #include <gri_logger.h>
32
33 class audio_portaudio_sink;
34 typedef boost::shared_ptr<audio_portaudio_sink> audio_portaudio_sink_sptr;
35
36 /*!
37  * \brief PORTAUDIO audio sink.
38  *
39  * \param sampling_rate sampling rate in Hz
40  * \param device_name PORTAUDIO device name, e.g., "pa:"
41  * \param ok_to_block   true if it's ok for us to block
42  */
43 audio_portaudio_sink_sptr
44 audio_portaudio_make_sink (int sampling_rate,
45                            const std::string device_name = "",
46                            bool ok_to_block = true);
47
48 PaStreamCallback portaudio_sink_callback;
49
50
51 /*!
52  * \ Audio sink using PORTAUDIO
53  *
54  * Input samples must be in the range [-1,1].
55  */
56 class audio_portaudio_sink : public gr_sync_block {
57   friend audio_portaudio_sink_sptr
58   audio_portaudio_make_sink (int sampling_rate,
59                              const std::string device_name,
60                              bool ok_to_block);
61
62   friend PaStreamCallback portaudio_sink_callback;
63
64
65   unsigned int          d_sampling_rate;
66   std::string           d_device_name;
67   bool                  d_ok_to_block;
68   bool                  d_verbose;
69
70   unsigned int          d_portaudio_buffer_size_frames; // number of frames in a portaudio buffer
71
72   PaStream              *d_stream;
73   PaStreamParameters    d_output_parameters;
74
75   gr_buffer_sptr        d_writer;               // buffer used between work and callback
76   gr_buffer_reader_sptr d_reader;
77   omni_semaphore        d_ringbuffer_ready;     // binary semaphore
78
79
80   // random stats
81   int                   d_nunderuns;            // count of underruns
82   gri_logger_sptr       d_log;                  // handle to non-blocking logging instance
83
84   void output_error_msg (const char *msg, int err);
85   void bail (const char *msg, int err) throw (std::runtime_error);
86   void create_ringbuffer();
87
88
89  protected:
90   audio_portaudio_sink (int sampling_rate, const std::string device_name,
91                         bool ok_to_block);
92
93  public:
94   ~audio_portaudio_sink ();
95   
96   bool check_topology (int ninputs, int noutputs);
97
98   int work (int noutput_items,
99             gr_vector_const_void_star &input_items,
100             gr_vector_void_star &output_items);
101 };
102
103 #endif /* INCLUDED_AUDIO_PORTAUDIO_SINK_H */