From a13bc485ed21147b1aeeb27d72ea1ec47165b155 Mon Sep 17 00:00:00 2001 From: Steve Markgraf Date: Tue, 16 Oct 2012 18:27:56 +0200 Subject: miri: fix crackle due to garbage samples The amount of input samples in the callback can vary due to the nature of the isochronous transfers. Signed-off-by: Steve Markgraf --- lib/miri/miri_source_c.cc | 11 +++++++++-- lib/miri/miri_source_c.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/miri/miri_source_c.cc b/lib/miri/miri_source_c.cc index cd46897..8b2799b 100644 --- a/lib/miri/miri_source_c.cc +++ b/lib/miri/miri_source_c.cc @@ -131,8 +131,9 @@ miri_source_c::miri_source_c (const std::string &args) throw std::runtime_error("Failed to reset usb buffers."); _buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *)); + _buf_lens = (unsigned int *) malloc(_buf_num * sizeof(unsigned int)); - if (_buf) { + if (_buf && _buf_lens) { for(unsigned int i = 0; i < _buf_num; ++i) _buf[i] = (unsigned short *) malloc(BUF_SIZE); } @@ -161,6 +162,8 @@ miri_source_c::~miri_source_c () free(_buf); _buf = NULL; + free(_buf_lens); + _buf_lens = NULL; } } @@ -180,8 +183,12 @@ void miri_source_c::mirisdr_callback(unsigned char *buf, uint32_t len) { boost::mutex::scoped_lock lock( _buf_mutex ); + if (len > BUF_SIZE) + throw std::runtime_error("Buffer too small."); + int buf_tail = (_buf_head + _buf_used) % _buf_num; memcpy(_buf[buf_tail], buf, len); + _buf_lens[buf_tail] = len; if (_buf_used == _buf_num) { std::cerr << "O" << std::flush; @@ -255,7 +262,7 @@ int miri_source_c::work( int noutput_items, float(*(buf + i * 2 + 1)) * (1.0f/4096.0f) ); _buf_offset = remaining * 2; - _samp_avail = (BUF_SIZE / BYTES_PER_SAMPLE) - remaining; + _samp_avail = (_buf_lens[_buf_head] / BYTES_PER_SAMPLE) - remaining; } return noutput_items; diff --git a/lib/miri/miri_source_c.h b/lib/miri/miri_source_c.h index 8e00efa..168c23a 100644 --- a/lib/miri/miri_source_c.h +++ b/lib/miri/miri_source_c.h @@ -116,6 +116,7 @@ private: mirisdr_dev_t *_dev; gruel::thread _thread; unsigned short **_buf; + unsigned int *_buf_lens; unsigned int _buf_num; unsigned int _buf_head; unsigned int _buf_used; -- cgit v1.2.3