libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/graph/watch_viewer.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) Juan Pedro Bolivar Puente 2008                          *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef PSYNTH_WATCH_VIEWER_H
00024 #define PSYNTH_WATCH_VIEWER_H
00025 
00026 #include <mutex>
00027 #include <iostream> // FIXME 
00028 #include <psynth/graph/watch.hpp>
00029 #include <psynth/sound/buffer.hpp>
00030 #include <psynth/sound/ring_buffer.hpp>
00031 
00032 namespace psynth
00033 {
00034 namespace graph
00035 {
00036 
00037 template <class BufferType, class RingBufferType>
00038 class watch_viewer : public watch
00039 {
00040 protected:
00041     RingBufferType m_ring;
00042     BufferType m_buffer;
00043     int m_points;
00044     float m_factor;
00045     float m_secs;
00046     bool m_updated;
00047     std::mutex m_mutex;
00048     
00049 public:
00050     watch_viewer (std::size_t points, std::size_t secs) :
00051         m_ring(points),
00052         m_buffer(points, typename BufferType::range::value_type (0), 0),
00053         m_points(points),
00054         m_factor(2),
00055         m_secs(secs),
00056         m_updated(false)
00057         {
00058             //FIXME: This does not work but it should. Fix it some day.
00059             //fill_frames (range (m_ring),
00060             //           typename BufferType::range::value_type (0));
00061         }
00062     
00063     virtual void set_info (const audio_info& info) {
00064         m_factor = (float) info.sample_rate / ((float) m_points / m_secs);
00065         //m_ring.zero();
00066     }
00067 
00068     virtual void update (const typename BufferType::const_range& buf)
00069     {
00070         std::unique_lock<std::mutex> lock (m_mutex, std::try_to_lock_t ());
00071         if (lock.owns_lock ())
00072         {
00073             range (m_ring).write (buf);//sound::sub_sampled_range (buf, m_factor));
00074             m_updated = false;
00075         }
00076     }
00077 
00078     const BufferType& get_buffer ()
00079     {
00080         if (!m_updated)
00081         {
00082             std::unique_lock<std::mutex> lock (m_mutex);
00083             typename RingBufferType::position ptr = range (m_ring).begin_pos ();
00084             range (m_ring).read (ptr, range (m_buffer));
00085             m_updated = true;
00086         }
00087         return m_buffer;
00088     }
00089 };
00090 
00091 typedef watch_viewer<sample_buffer, sample_ring_buffer> watch_view_control;
00092 
00093 class watch_view_audio : public watch_viewer<audio_buffer, audio_ring_buffer>
00094 {
00095 public:
00096     typedef watch_viewer<audio_buffer, audio_ring_buffer> base;
00097     
00098     watch_view_audio (std::size_t points, std::size_t secs) :
00099         base (points, secs) {}
00100     
00101     virtual void set_info (const audio_info& newinfo)
00102     {
00103         audio_info info = newinfo;
00104         info.block_size = m_points;
00105         m_ring.recreate (info.block_size, audio_frame (0), 0);
00106         m_buffer.recreate (info.block_size, audio_frame (0), 0);
00107         //m_factor = (float) info.sample_rate / ((float) m_points / m_secs);
00108     }
00109 };
00110 
00111 } /* namespace graph */
00112 } /* namespace psynth */
00113 
00114 #endif /* PSYNTH_WATCHVIEWER */