Nsound  0.9.4
AudioPlaybackRt.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: AudioPlaybackRt.h 912 2015-07-26 00:50:29Z weegreenblobbie $
4 //
5 // Nsound is a C++ library and Python module for audio synthesis featuring
6 // dynamic digital filters. Nsound lets you easily shape waveforms and write
7 // to disk or plot them. Nsound aims to be as powerful as Csound but easy to
8 // use.
9 //
10 // Copyright (c) 2011-Present Nick Hilton
11 //
12 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
13 //
14 //-----------------------------------------------------------------------------
15 
16 //-----------------------------------------------------------------------------
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU Library General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 //
32 //-----------------------------------------------------------------------------
33 #ifndef _NSOUND_AUDIO_PLAYBACK_RT_H_
34 #define _NSOUND_AUDIO_PLAYBACK_RT_H_
35 
36 #include <Nsound/Nsound.h>
37 
38 #include <Nsound/AudioStream.h>
39 
40 #ifdef NSOUND_LIBPORTAUDIO
41  #include <portaudio.h>
42 #else
45  struct PaStreamParameters {};
46  struct PaStream {};
47 #endif
48 
49 #ifdef NSOUND_CPP11
50  #include <atomic>
51 #endif
52 
53 namespace Nsound
54 {
55 
56 // forward declare
57 class AudioStream;
58 class Buffer;
59 class Sine;
60 
62 {
63  // BUM = Buffer Underrun Mode
67 };
68 
70 {
84 
85  std::vector<uint32> n_history;
86 
87  std::string __str__() const;
88 };
89 
91 {
92  public:
93 
94  static void use_jack(bool flag) { use_jack_ = flag; }
95 
97  float64 sample_rate = 44100.0,
98  uint32 channels = 1,
99  uint32 n_buffers = 3,
100  float64 buffer_size_sec = -1.0); // negative means use default low latency time.
101 
103 
106 
108 
110  std::string getInfo();
111 
113  void play(const AudioStream & a);
114 
116  void play(const Buffer & b);
117 
119  void play(float64 sample);
120 
122  void play(float64 left, float64 right);
123 
125  void stop();
126 
127  std::string debug_print();
128 
130 
131  void _swig_shadow() {}
132 
133  private:
134 
135  // disable these
137  AudioPlaybackRt & operator=(const AudioPlaybackRt & rhs){return *this;}
138 
139  int
140  _callback(
141  const void * input,
142  void * output,
143  const uint32 frame_count,
144  const PaStreamCallbackTimeInfo * time_info,
145  PaStreamCallbackFlags status_flags);
146 
147  static
148  int
149  _pa_callback(
150  const void * input,
151  void * output,
152  unsigned long frame_count,
153  const PaStreamCallbackTimeInfo * time_info,
154  PaStreamCallbackFlags status_flags,
155  void * ap_obj );
156 
157  void _start();
158 
159  typedef std::vector< int16 > Int16Vector;
160  typedef std::vector< Int16Vector * > Pool;
161 
162  //-------------------------------------------------------------------------
163  // Data members
164 
167 
169 
174 
177 
178  std::vector<uint32> n_history_;
179 
180  Sine * sine_; // used to generate noise or tones on buffer underrun
181 
184 
185  Pool::iterator pool_begin_;
186  Pool::iterator pool_end_;
187 
188  #ifdef NSOUND_CPP11
189  std::atomic_uint n_ready_;
190  #else
192  #endif
193 
194  Pool::iterator rd_ptr_;
195  Pool::iterator wr_ptr_;
196 
198 
199  struct Driver
200  {
202  :
203  out_params_(NULL),
204  stream_(NULL),
207  driver_id_(0){}
208 
211 
214 
215  std::string host_api_str_;
218  };
219 
221 
223 
224  static bool use_jack_;
225 
226 }; // AudioPlaybackRt
227 
228 
229 // Must delcare friend functions here to give them proper namespace scope.
230 void operator>>(const AudioStream & lhs, AudioPlaybackRt & rhs);
231 void operator>>(const Buffer & lhs, AudioPlaybackRt & rhs);
232 void operator>>(float64 sample, AudioPlaybackRt & rhs);
233 
234 } // namespace Nsound
235 
236 #endif
237 // :mode=c++: jEdit modeline
unsigned int uint32
Definition: Nsound.h:153
float64 getActualLatencySeconds() const
BufferUnderrunMode underrun_mode_
void stop()
Stops playback.
Pool::iterator pool_begin_
std::string getInfo()
Returns information about the backend driver.
std::string __str__() const
double float64
Definition: Nsound.h:146
AudioPlaybackRt & operator=(const AudioPlaybackRt &rhs)
std::vector< uint32 > n_history
void operator>>(const AudioStream &lhs, AudioPlayback &rhs)
AudioPlaybackRt(const AudioPlaybackRt &copy)
uint32 getSamplesPerBuffer() const
int _callback(const void *input, void *output, const uint32 frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags)
AudioPlaybackRtDebug get_debug_info() const
void setBufferUnderrunMode(BufferUnderrunMode bum)
PaStreamParameters * out_params_
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioPlaybackRt(float64 sample_rate=44100.0, uint32 channels=1, uint32 n_buffers=3, float64 buffer_size_sec=-1.0)
static int _pa_callback(const void *input, void *output, unsigned long frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags, void *ap_obj)
std::vector< int16 > Int16Vector
std::vector< uint32 > n_history_
static void use_jack(bool flag)
DOXME.
Definition: Sine.h:43
std::vector< Int16Vector * > Pool
void play(const AudioStream &a)
Writes samples to the internal circular buffer to be played.