Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marsystems/AudioSink.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2013 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 
00020 #ifndef MARSYAS_AUDIOSINK_H
00021 #define MARSYAS_AUDIOSINK_H
00022 
00023 #include <marsyas/realtime/realvec_queue.h>
00024 #include <marsyas/system/MarSystem.h>
00025 
00026 #include "RtAudio.h"
00027 
00028 #include <mutex>
00029 #include <condition_variable>
00030 #include <atomic>
00031 
00032 
00033 class RtAudio;
00034 class Thread;
00035 
00036 
00037 
00038 namespace Marsyas
00039 {
00040 
00061 class AudioSink:public MarSystem
00062 {
00063 private:
00064 
00065   struct OutputData
00066   {
00067     OutputData(): underrun(false) {}
00068     OutputData(const OutputData &): underrun(false) {}
00069 
00070     std::mutex mutex;
00071     std::condition_variable condition;
00072 
00073     realvec_queue buffer;
00074 
00075     std::atomic<mrs_natural> watermark;
00076     bool underrun;
00077 
00078     unsigned int channel_count;
00079     unsigned int sample_rate;
00080 
00081   } shared;
00082 
00083   mrs_real old_source_sample_rate_;
00084   mrs_natural old_dest_block_size_;
00085 
00086   realvec resampler_output_;
00087   MarSystem* resampler_;
00088 
00089   RtAudio*  audio_;
00090 
00091   bool isInitialized_;
00092   bool stopped_;
00093   bool is_resampling_;
00094 
00095   void addControls();
00096   void myUpdate(MarControlPtr sender);
00097 
00098   void initRtAudio(
00099     unsigned int* sample_rate,
00100     unsigned int* block_size,
00101     unsigned int channel_count,
00102     bool realtime
00103   );
00104 
00105   void start();
00106   void stop();
00107 
00108   void localActivate(bool state);
00109 
00110   void configureResampler(mrs_real in_sample_rate, mrs_natural in_block_size,
00111                           mrs_real out_sample_rate, mrs_natural *out_block_size,
00112                           mrs_natural channel_count);
00113 
00114   void updateResamplerBlockSize(mrs_natural in_block_size,
00115                                 mrs_natural *out_block_size,
00116                                 mrs_natural channel_count);
00117 
00118   void clearBuffer();
00119   bool reformatBuffer(mrs_natural sourceBlockSize,
00120                       mrs_natural destBlockSize,
00121                       mrs_natural channel_count,
00122                       bool realtime, bool resize);
00123 
00124 
00125   static int playCallback(void *outputBuffer, void *inputBuffer,
00126                           unsigned int nBufferFrames, double streamTime, unsigned int status, void *userData);
00127   void playCallback_test();
00128 
00129 
00130 public:
00131   AudioSink(std::string name);
00132   ~AudioSink();
00133   AudioSink(const AudioSink& a);
00134 
00135   MarSystem* clone() const;
00136 
00137   void myProcess(realvec& in, realvec& out);
00138 };
00139 
00140 }//namespace Marsyas
00141 
00142 #endif