Advertisement
qarlosalberto

delay_impl.cc

Sep 23rd, 2014
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. /* -*- c++ -*- */
  2. /*
  3.  * Copyright 2014 <+YOU OR YOUR COMPANY+>.
  4.  *
  5.  * This is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 3, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This software is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this software; see the file COPYING.  If not, write to
  17.  * the Free Software Foundation, Inc., 51 Franklin Street,
  18.  * Boston, MA 02110-1301, USA.
  19.  */
  20.  
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24.  
  25. #include <gnuradio/io_signature.h>
  26. #include "delay_impl.h"
  27.  
  28. #include <string.h>
  29.  
  30. namespace gr {
  31.   namespace howto {
  32.  
  33.     delay::sptr
  34.     delay::make(size_t itemsize, int delay)
  35.     {
  36.       return gnuradio::get_initial_sptr
  37.         (new delay_impl(itemsize, delay));
  38.     }
  39.  
  40.  
  41.  
  42.     /*
  43.      * The private constructor
  44.      */
  45.     delay_impl::delay_impl(size_t itemsize, int delay)
  46.         : block("delay",
  47.             io_signature::make(2, 2, itemsize),
  48.             io_signature::make(1, 1, itemsize)),
  49.             d_itemsize(itemsize)
  50.     {
  51.         if(delay < 0) {
  52.             throw std::runtime_error("delay: Cannot initialize block with a delay < 0.");
  53.         }
  54.         del=delay;
  55.         set_dly(delay);
  56.         d_delta = 0;
  57.     }
  58.  
  59.  
  60.  
  61.     delay_impl::~delay_impl()
  62.     {
  63.     }
  64.  
  65.     void
  66.         delay_impl::forecast(int noutput_items,
  67.         gr_vector_int &ninput_items_required)
  68.     {
  69.         // make sure all inputs have noutput_items available
  70.         unsigned ninputs = ninput_items_required.size();
  71.         for(unsigned i = 0; i < ninputs; i++)
  72.             ninput_items_required[i] = noutput_items;
  73.     }
  74.  
  75.  
  76.  
  77.  
  78.     void
  79.     delay_impl::set_dly(int d)
  80.     {
  81.         // only set a new delta if there is a change in the delay; this
  82.         // protects from quickly-repeated calls to this function that
  83.         // would end with d_delta=0.
  84.         if(d != dly()) {
  85.             gr::thread::scoped_lock l(d_mutex_delay);
  86.             int old = dly();
  87.             set_history(d+1);
  88.             declare_sample_delay(history()-1);
  89.             d_delta += dly() - old;
  90.         }
  91.     }
  92.  
  93.     int
  94.     delay_impl::get_del()
  95.     {
  96.         return del;
  97.     }
  98.  
  99.     void
  100.     delay_impl::set_del(int d)
  101.     {
  102.         del=d;
  103.     }
  104.  
  105.     int
  106.         delay_impl::general_work(int noutput_items,
  107.         gr_vector_int &ninput_items,
  108.         gr_vector_const_void_star &input_items,
  109.         gr_vector_void_star &output_items)
  110.     {
  111.         //assert(input_items.size() == output_items.size());
  112.         const char *iptr;
  113.         char *optr;
  114.         int cons, ret;
  115.         // No change in delay; just memcpy ins to outs
  116.  
  117.         const float *in_delay = (const float *) input_items[1];
  118.  
  119.         if(get_del()!=(int)in_delay[0]){
  120.             set_del(in_delay[0]);
  121.             set_dly((int)in_delay[0]);
  122.         }
  123.  
  124.         gr::thread::scoped_lock l(d_mutex_delay);
  125.  
  126.         if(d_delta == 0) {
  127.             for(size_t i = 0; i < 1; i++) {
  128.                 iptr = (const char *)input_items[i];
  129.                 optr = (char *)output_items[i];
  130.                 std::memcpy(optr, iptr, noutput_items*d_itemsize);
  131.             }
  132.             cons = noutput_items;
  133.             ret = noutput_items;
  134.         }
  135.         // Skip over d_delta items on the input
  136.         else if(d_delta < 0) {
  137.             int n_to_copy, n_adj;
  138.             int delta = -d_delta;
  139.             n_to_copy = std::max(0, noutput_items-delta);
  140.             n_adj = std::min(delta, noutput_items);
  141.             for(size_t i = 0; i < 1; i++) {
  142.                 iptr = (const char *) input_items[i];
  143.                 optr = (char *) output_items[i];
  144.                 std::memcpy(optr, iptr+delta*d_itemsize, n_to_copy*d_itemsize);
  145.             }
  146.             cons = noutput_items;
  147.             ret = n_to_copy;
  148.             delta -= n_adj;
  149.             d_delta = -delta;
  150.         }
  151.         //produce but not consume (inserts zeros)
  152.         else { // d_delta > 0
  153.             int n_from_input, n_padding;
  154.             n_from_input = std::max(0, noutput_items-d_delta);
  155.             n_padding = std::min(d_delta, noutput_items);
  156.             for(size_t i = 0; i < 1; i++) {
  157.                 iptr = (const char *) input_items[i];
  158.                 optr = (char *) output_items[i];
  159.                 std::memset(optr, 0, n_padding*d_itemsize);
  160.                 std::memcpy(optr, iptr, n_from_input*d_itemsize);
  161.             }
  162.             cons = n_from_input;
  163.             ret = noutput_items;
  164.             d_delta -= n_padding;
  165.         }
  166.  
  167.  
  168.                 //td::cout << "Sale.\n";
  169.  
  170.  
  171.         consume_each(cons);
  172.         return ret;
  173.     }
  174.   } /* namespace howto */
  175. } /* namespace gr */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement