gr-baz Package
baz_burster.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 /*
00024  * gr-baz by Balint Seeber (http://spench.net/contact)
00025  * Information, documentation & samples: http://wiki.spench.net/wiki/gr-baz
00026  */
00027 
00028 #ifndef INCLUDED_BAZ_BURSTER_H
00029 #define INCLUDED_BAZ_BURSTER_H
00030 
00031 //#include <sys/time.h>
00032 #include <boost/thread/thread.hpp>
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <map>
00037 
00038 #include <gr_sync_block.h>
00039 #include <gr_msg_queue.h>
00040 #include <gruel/pmt.h>
00041 
00042 #include "baz_burster_config.h"
00043 
00044 class BAZ_API baz_burster;
00045 
00046 /*
00047  * We use boost::shared_ptr's instead of raw pointers for all access
00048  * to gr_blocks (and many other data structures).  The shared_ptr gets
00049  * us transparent reference counting, which greatly simplifies storage
00050  * management issues.  This is especially helpful in our hybrid
00051  * C++ / Python system.
00052  *
00053  * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
00054  *
00055  * As a convention, the _sptr suffix indicates a boost::shared_ptr
00056  */
00057 typedef boost::shared_ptr<baz_burster> baz_burster_sptr;
00058 
00059 /*!
00060  * \brief Return a shared_ptr to a new instance of baz_burster.
00061  *
00062  * To avoid accidental use of raw pointers, baz_burster's
00063  * constructor is private.  baz_make_burster is the public
00064  * interface for creating new instances.
00065  */
00066 BAZ_API baz_burster_sptr baz_make_burster (const baz_burster_config& config);
00067 
00068 /*!
00069  * \brief burster a stream of floats.
00070  * \ingroup block
00071  *
00072  * This uses the preferred technique: subclassing gr_sync_block.
00073  */
00074 class BAZ_API baz_burster : public gr_block
00075 {
00076 private:
00077         friend BAZ_API baz_burster_sptr baz_make_burster (const baz_burster_config& config);
00078 
00079         baz_burster (const baz_burster_config& config);         // private constructor
00080 
00081         baz_burster_config d_config;
00082         
00083         typedef struct burst_time_t
00084         {
00085                 uint64_t seconds;
00086                 double fractional_seconds;
00087                 uint64_t sample_offset;
00088                 int sample_rate;
00089         } burst_time;
00090         
00091         inline static burst_time burst_time_absorb_whole_samples(const burst_time& t)
00092         {
00093                 burst_time _t(t);
00094                 uint64_t s = t.sample_offset / t.sample_rate;
00095                 _t.seconds += s;
00096                 _t.sample_offset -= (s * t.sample_rate);
00097                 return _t;
00098         }
00099         
00100         inline static burst_time burst_time_absorb_samples(const burst_time& t)
00101         {
00102                 burst_time _t(t);
00103                 double d = (double)t.sample_offset / (double)t.sample_rate;
00104                 _t.seconds += (int)d;
00105                 _t.fractional_seconds += (d - (int)d);
00106                 _t.sample_offset = 0;
00107                 return _t;
00108         }
00109         
00110         inline static burst_time burst_time_difference(const burst_time& t1, const burst_time& t2, bool relative_to_second = true)
00111         {
00112                 burst_time t;
00113                 
00114                 if ((t1.sample_rate == t2.sample_rate)/* && (t1.seconds == t2.seconds) && (t1.fractional_seconds == t2.fractional_seconds)*/)
00115                 {
00116                         t.sample_rate = t1.sample_rate;
00117                         t.seconds = 0;
00118                         t.fractional_seconds = 0;
00119                         t.sample_offset =
00120                                 (t1.sample_rate * (t1.seconds - t2.seconds)) +
00121                                 (uint64_t)((double)t1.sample_rate * (t1.fractional_seconds - t2.fractional_seconds)) +
00122                                 (t1.sample_offset - t2.sample_offset);
00123                         return t;
00124                 }
00125                 
00126                 if ((t1.sample_rate != t2.sample_rate) && ((t1.sample_offset > 0) || (t2.sample_offset > 0)))
00127                 {
00128                         return burst_time_difference(burst_time_absorb_samples(t1), burst_time_absorb_samples(t2));
00129                 }
00130                 
00131                 t.seconds = 0;
00132                 t.fractional_seconds = 0;
00133                 
00134                 if (relative_to_second)
00135                 {
00136                         t.sample_rate = t2.sample_rate;
00137                 }
00138                 else
00139                 {
00140                         t.sample_rate = t1.sample_rate;
00141                 }
00142                 
00143                 t.sample_offset = (t.sample_rate * (t1.seconds - t2.seconds)) + (uint64_t)((double)t.sample_rate * (t1.fractional_seconds - t2.fractional_seconds));
00144                 
00145                 return t;
00146         }
00147         
00148         bool send_pending_msg(void);
00149         void set_burst_length(int length);
00150 //////////////////// ZERO
00151         union {
00152                 char d_dummy_zero_first;
00153                 //uint64_t d_last_time_seconds;
00154                 burst_time d_stream_time;
00155         };
00156         //double d_last_time_fractional_seconds;
00157         burst_time d_last_burst_time;
00158         int d_burst_message_sample_index;
00159         //bool d_msg_ready;
00160         bool d_in_burst;
00161         int d_burst_count;      // Incremented when new one is started (not when it is transmitted)
00162         bool d_last_burst_system_time_valid;
00163         int d_time_tags;
00164         uint64_t d_current_burst_length;
00165         char* d_message_buffer;
00166         int d_message_buffer_length;
00167         int d_system_time_ticks_per_second;
00168         // Next must come last
00169         char d_dummy_zero_last;
00170 ////////////////////
00171         //uint64_t d_last_burst_sample_time;
00172         //double d_last_burst_time;
00173         boost::system_time d_system_time;
00174         boost::system_time d_last_burst_system_time;
00175         //uint64_t d_samples_since_last_time_tag;
00176         //gr_message_sptr d_current_msg;
00177         gr_message_sptr d_pending_msg;
00178         std::vector<gr_tag_t> d_incoming_time_tags;
00179 public:
00180         ~baz_burster ();        // public destructor
00181 
00182         //
00183 
00184         void forecast(int noutput_items, gr_vector_int &ninput_items_required);
00185         int general_work (int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items);
00186 };
00187 
00188 #endif /* INCLUDED_BAZ_BURSTER_H */