Imported Upstream version 3.2.2
[debian/gnuradio] / gr-audio-jack / src / audio_jack_sink.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,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_JACK_SINK_H
23 #define INCLUDED_AUDIO_JACK_SINK_H
24
25 #include <gr_sync_block.h>
26 #include <string>
27 #include <jack/jack.h>
28 #include <jack/ringbuffer.h>
29 #include <stdexcept>
30
31 class audio_jack_sink;
32 typedef boost::shared_ptr<audio_jack_sink> audio_jack_sink_sptr;
33
34 /*!
35  * \brief make an JACK audio sink.
36  *
37  * \param sampling_rate sampling rate in Hz
38  * \param device_name JACK device name, e.g., "gr_sink"
39  * \param ok_to_block
40  */
41 audio_jack_sink_sptr
42 audio_jack_make_sink (int sampling_rate,
43                       const std::string device_name = "",
44                       bool ok_to_block = true);
45
46 int jack_sink_process (jack_nframes_t nframes, void *arg);
47
48 /*!
49  * \brief audio sink using JACK
50  *
51  * The sink has one input stream of floats.
52  *
53  * Input samples must be in the range [-1,1].
54  */
55 class audio_jack_sink : public gr_sync_block {
56   friend audio_jack_sink_sptr
57   audio_jack_make_sink (int sampling_rate, const std::string device_name, bool ok_to_block);
58
59   friend int jack_sink_process (jack_nframes_t nframes, void *arg);
60
61   // typedef for pointer to class work method
62   typedef int (audio_jack_sink::*work_t)(int noutput_items,
63                                          gr_vector_const_void_star &input_items,
64                                          gr_vector_void_star &output_items);
65
66   unsigned int          d_sampling_rate;
67   std::string           d_device_name;
68   bool                  d_ok_to_block;
69
70   jack_client_t         *d_jack_client;
71   jack_port_t           *d_jack_output_port;
72   jack_ringbuffer_t     *d_ringbuffer;
73   jack_nframes_t        d_jack_buffer_size;
74   pthread_cond_t        d_ringbuffer_ready;
75   pthread_mutex_t       d_jack_process_lock;
76
77   // random stats
78   int                   d_nunderuns;            // count of underruns
79
80   void output_error_msg (const char *msg, int err);
81   void bail (const char *msg, int err) throw (std::runtime_error);
82
83
84  protected:
85   audio_jack_sink (int sampling_rate, const std::string device_name, bool ok_to_block);
86
87  public:
88   ~audio_jack_sink ();
89   
90   bool check_topology (int ninputs, int noutputs);
91
92   int work (int noutput_items,
93             gr_vector_const_void_star &input_items,
94             gr_vector_void_star &output_items);
95 };
96
97 #endif /* INCLUDED_AUDIO_JACK_SINK_H */