diff --git a/lib/rtl_source_c.cc b/lib/rtl_source_c.cc index 6bdaed2..caa42b6 100644 --- a/lib/rtl_source_c.cc +++ b/lib/rtl_source_c.cc @@ -74,13 +74,18 @@ rtl_source_c::rtl_source_c (const std::string &args) int ret; int dev_index = 0; - _buf = boost::circular_buffer(1024*1024); + _buf = boost::circular_buffer(1024*1024); // create a lookup table for gr_complex values for (unsigned int i = 0; i <= 0xffff; i++) { +#if 1 // little endian + _lut.push_back( gr_complex( (float(i & 0xff) - 127.0) * 0.00787, + (float(i >> 8) - 127.0) * 0.00787 ) ); +#else // big endian _lut.push_back( gr_complex( (float(i >> 8) - 127.0) * 0.00787, (float(i & 0xff) - 127.0) * 0.00787 ) ); +#endif } std::cout << "Opening " << rtlsdr_get_device_name(dev_index) << std::endl; @@ -94,9 +99,7 @@ rtl_source_c::rtl_source_c (const std::string &args) if (ret < 0) throw std::runtime_error("failed to reset usb buffers."); - rtlsdr_set_sample_rate( _dev, 2048000 ); - - _has_i_sample = false; +// rtlsdr_set_sample_rate( _dev, 2048000 ); _thread = gruel::thread(_rtlsdr_wait, this); } @@ -122,17 +125,23 @@ void rtl_source_c::_rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) void rtl_source_c::rtlsdr_callback(unsigned char *buf, uint32_t len) { - boost::mutex::scoped_lock lock( _buf_mutex ); + unsigned short * sbuf = (unsigned short *)buf; - for (int i = 0; i < len; i++) { - if (!_buf.full()) { - _buf.push_back(buf[i]); - _buf_cond.notify_one(); - } else { - printf("O"); fflush(stdout); - break; - } + if (len % 2 != 0) { + printf("len: %d\n", len); fflush(stdout); + } + + boost::mutex::scoped_lock lock( _buf_mutex ); + + for (int i = 0; i < len/2; i++) { + if (!_buf.full()) { + _buf.push_back(sbuf[i]); + _buf_cond.notify_one(); + } else { + printf("O"); fflush(stdout); + break; } + } } void rtl_source_c::_rtlsdr_wait(rtl_source_c *obj) @@ -153,9 +162,7 @@ int rtl_source_c::work( int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items ) { - gr_complex *out = (gr_complex *) output_items[0]; - - unsigned short index = 0; + gr_complex *out = (gr_complex *)output_items[0]; int items_left = noutput_items; while ( items_left ) @@ -168,21 +175,14 @@ int rtl_source_c::work( int noutput_items, } } - if (!_has_i_sample) { - index = _buf.front() << 8; - _buf.pop_front(); - _has_i_sample = true; - } else { - index |= _buf.front(); - _buf.pop_front(); - _has_i_sample = false; - } - - if (_has_i_sample) - continue; - // convert samples to gr_complex type by using the lookup table - *out++ = _lut[index]; + *out++ = _lut[ _buf.front() ]; + + { + boost::mutex::scoped_lock lock( _buf_mutex ); + + _buf.pop_front(); + } items_left--; } diff --git a/lib/rtl_source_c.h b/lib/rtl_source_c.h index bd3dedd..8e6bbe7 100644 --- a/lib/rtl_source_c.h +++ b/lib/rtl_source_c.h @@ -95,10 +95,9 @@ private: rtlsdr_dev_t *_dev; gruel::thread _thread; - boost::circular_buffer _buf; + boost::circular_buffer _buf; boost::mutex _buf_mutex; boost::condition_variable _buf_cond; - bool _has_i_sample; }; #endif /* INCLUDED_RTLSDR_SOURCE_C_H */