Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_throttle.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_throttle.h>
28 #include <gr_io_signature.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <math.h>
32 #ifdef HAVE_TIME_H
33 #include <time.h>
34 #endif
35
36
37 #ifdef HAVE_NANOSLEEP
38 void
39 gr_nanosleep(struct timespec *ts)
40 {
41   struct timespec       *req = ts;
42   struct timespec       rem;
43   int r = nanosleep(req, &rem);
44   while (r < 0 && errno == EINTR){
45     req = &rem;
46     r = nanosleep(req, &rem);
47   }
48   if (r < 0)
49     perror ("gr_nanosleep");
50 }
51 #endif
52
53 gr_throttle_sptr
54 gr_make_throttle(size_t itemsize, double samples_per_sec)
55 {
56   return gr_throttle_sptr(new gr_throttle(itemsize, samples_per_sec));
57 }
58
59 gr_throttle::gr_throttle(size_t itemsize, double samples_per_sec)
60   : gr_sync_block("throttle",
61                   gr_make_io_signature(1, 1, itemsize),
62                   gr_make_io_signature(1, 1, itemsize)),
63     d_itemsize(itemsize), d_samples_per_sec(samples_per_sec),
64     d_total_samples(0)
65 {
66 #ifdef HAVE_GETTIMEOFDAY
67   gettimeofday(&d_start, 0);
68 #endif  
69 }
70
71 gr_throttle::~gr_throttle()
72 {
73 }
74
75 int
76 gr_throttle::work (int noutput_items,
77                    gr_vector_const_void_star &input_items,
78                    gr_vector_void_star &output_items)
79 {
80   const char *in = (const char *) input_items[0];
81   char *out = (char *) output_items[0];
82
83 #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_NANOSLEEP)
84   //
85   // If our average sample rate exceeds our target sample rate,
86   // delay long enough to reduce to our target rate.
87   //
88   struct timeval now;
89   gettimeofday(&now, 0);
90   long t_usec = now.tv_usec - d_start.tv_usec;
91   long t_sec  = now.tv_sec - d_start.tv_sec;
92   double t = (double)t_sec + (double)t_usec * 1e-6;
93   if (t < 1e-6)         // avoid unlikely divide by zero
94     t = 1e-6;
95
96   double actual_samples_per_sec = d_total_samples / t;
97   if (actual_samples_per_sec > d_samples_per_sec){      // need to delay
98     double delay = d_total_samples / d_samples_per_sec - t;
99     struct timespec ts;
100     ts.tv_sec = (time_t)floor(delay);
101     ts.tv_nsec = (long)((delay - floor(delay)) * 1e9);
102     gr_nanosleep(&ts);
103   }
104 #endif  
105
106   memcpy(out, in, noutput_items * d_itemsize);
107   d_total_samples += noutput_items;
108   return noutput_items;
109 }