change sample buffer to appear as shorts for performance reasons

This commit is contained in:
Dimitri Stolnikov 2012-04-06 18:52:26 +02:00
parent 43c59ad2b9
commit e15b5cf6df
2 changed files with 31 additions and 32 deletions

View File

@ -74,13 +74,18 @@ rtl_source_c::rtl_source_c (const std::string &args)
int ret; int ret;
int dev_index = 0; int dev_index = 0;
_buf = boost::circular_buffer<unsigned char>(1024*1024); _buf = boost::circular_buffer<unsigned short>(1024*1024);
// create a lookup table for gr_complex values // create a lookup table for gr_complex values
for (unsigned int i = 0; i <= 0xffff; i++) 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, _lut.push_back( gr_complex( (float(i >> 8) - 127.0) * 0.00787,
(float(i & 0xff) - 127.0) * 0.00787 ) ); (float(i & 0xff) - 127.0) * 0.00787 ) );
#endif
} }
std::cout << "Opening " << rtlsdr_get_device_name(dev_index) << std::endl; 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) if (ret < 0)
throw std::runtime_error("failed to reset usb buffers."); throw std::runtime_error("failed to reset usb buffers.");
rtlsdr_set_sample_rate( _dev, 2048000 ); // rtlsdr_set_sample_rate( _dev, 2048000 );
_has_i_sample = false;
_thread = gruel::thread(_rtlsdr_wait, this); _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) 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 (len % 2 != 0) {
if (!_buf.full()) { printf("len: %d\n", len); fflush(stdout);
_buf.push_back(buf[i]); }
_buf_cond.notify_one();
} else { boost::mutex::scoped_lock lock( _buf_mutex );
printf("O"); fflush(stdout);
break; 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) 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_const_void_star &input_items,
gr_vector_void_star &output_items ) gr_vector_void_star &output_items )
{ {
gr_complex *out = (gr_complex *) output_items[0]; gr_complex *out = (gr_complex *)output_items[0];
unsigned short index = 0;
int items_left = noutput_items; int items_left = noutput_items;
while ( items_left ) 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 // 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--; items_left--;
} }

View File

@ -95,10 +95,9 @@ private:
rtlsdr_dev_t *_dev; rtlsdr_dev_t *_dev;
gruel::thread _thread; gruel::thread _thread;
boost::circular_buffer<unsigned char> _buf; boost::circular_buffer<unsigned short> _buf;
boost::mutex _buf_mutex; boost::mutex _buf_mutex;
boost::condition_variable _buf_cond; boost::condition_variable _buf_cond;
bool _has_i_sample;
}; };
#endif /* INCLUDED_RTLSDR_SOURCE_C_H */ #endif /* INCLUDED_RTLSDR_SOURCE_C_H */