Updated license from GPL version 2 or later to GPL version 3 or later.
[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 #if !defined(HAVE_NANOSLEEP) && defined(HAVE_SSLEEP)
36 #include <windows.h>
37 #endif
38
39
40 #ifdef HAVE_NANOSLEEP
41 void
42 gr_nanosleep(struct timespec *ts)
43 {
44   struct timespec       *req = ts;
45   struct timespec       rem;
46   int r = nanosleep(req, &rem);
47   while (r < 0 && errno == EINTR){
48     req = &rem;
49     r = nanosleep(req, &rem);
50   }
51   if (r < 0)
52     perror ("gr_nanosleep");
53 }
54 #endif
55
56 gr_throttle_sptr
57 gr_make_throttle(size_t itemsize, double samples_per_sec)
58 {
59   return gr_throttle_sptr(new gr_throttle(itemsize, samples_per_sec));
60 }
61
62 gr_throttle::gr_throttle(size_t itemsize, double samples_per_sec)
63   : gr_sync_block("throttle",
64                   gr_make_io_signature(1, 1, itemsize),
65                   gr_make_io_signature(1, 1, itemsize)),
66     d_itemsize(itemsize), d_samples_per_sec(samples_per_sec),
67     d_total_samples(0)
68 {
69 #ifdef HAVE_GETTIMEOFDAY
70   gettimeofday(&d_start, 0);
71 #endif  
72 }
73
74 gr_throttle::~gr_throttle()
75 {
76 }
77
78 int
79 gr_throttle::work (int noutput_items,
80                    gr_vector_const_void_star &input_items,
81                    gr_vector_void_star &output_items)
82 {
83   const char *in = (const char *) input_items[0];
84   char *out = (char *) output_items[0];
85
86 #if defined(HAVE_GETTIMEOFDAY)
87   //
88   // If our average sample rate exceeds our target sample rate,
89   // delay long enough to reduce to our target rate.
90   //
91   struct timeval now;
92   gettimeofday(&now, 0);
93   long t_usec = now.tv_usec - d_start.tv_usec;
94   long t_sec  = now.tv_sec - d_start.tv_sec;
95   double t = (double)t_sec + (double)t_usec * 1e-6;
96   if (t < 1e-6)         // avoid unlikely divide by zero
97     t = 1e-6;
98
99   double actual_samples_per_sec = d_total_samples / t;
100   if (actual_samples_per_sec > d_samples_per_sec){      // need to delay
101     double delay = d_total_samples / d_samples_per_sec - t;
102 #ifdef HAVE_NANOSLEEP
103     struct timespec ts;
104     ts.tv_sec = (time_t)floor(delay);
105     ts.tv_nsec = (long)((delay - floor(delay)) * 1e9);
106     gr_nanosleep(&ts);
107 #elif HAVE_SSLEEP
108     Sleep( (DWORD)(delay*1000) );
109 #endif
110   }
111 #endif  
112
113   memcpy(out, in, noutput_items * d_itemsize);
114   d_total_samples += noutput_items;
115   return noutput_items;
116 }