SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
samplesourcefifo.cpp
Go to the documentation of this file.
1 // Copyright (C) 2016 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <algorithm>
19 #include <assert.h>
20 #include "samplesourcefifo.h"
21 
23  QObject(parent),
24  m_size(size),
25  m_init(false)
26 {
27  m_data.resize(2*m_size);
28  init();
29 }
30 
32  QObject(other.parent()),
33  m_size(other.m_size),
34  m_data(other.m_data)
35 {
36  init();
37 }
38 
40 {}
41 
43 {
44  qDebug("SampleSourceFifo::resize: %d", size);
45 
46  m_size = size;
47  m_data.resize(2*m_size);
48  init();
49 }
50 
52 {
53  static Sample zero = {0,0};
54  std::fill(m_data.begin(), m_data.end(), zero);
55  m_ir = 0;
56  m_iw = m_size/2;
57  m_init = true;
58 }
59 
60 void SampleSourceFifo::readAdvance(SampleVector::iterator& readUntil, unsigned int nbSamples)
61 {
62 // QMutexLocker mutexLocker(&m_mutex);
63  assert(nbSamples <= m_size/2);
64  emit dataWrite(nbSamples);
65 
66  m_ir = (m_ir + nbSamples) % m_size;
67  readUntil = m_data.begin() + m_size + m_ir;
68  emit dataRead(nbSamples);
69 }
70 
71 void SampleSourceFifo::write(const Sample& sample)
72 {
73  m_data[m_iw] = sample;
74  m_data[m_iw+m_size] = sample;
75 
76  {
77 // QMutexLocker mutexLocker(&m_mutex);
78  m_iw = (m_iw+1) % m_size;
79  }
80 }
81 
82 void SampleSourceFifo::getReadIterator(SampleVector::iterator& readUntil)
83 {
84  readUntil = m_data.begin() + m_size + m_ir;
85 }
86 
87 void SampleSourceFifo::getWriteIterator(SampleVector::iterator& writeAt)
88 {
89  writeAt = m_data.begin() + m_iw;
90 }
91 
92 void SampleSourceFifo::bumpIndex(SampleVector::iterator& writeAt)
93 {
95 
96  {
97 // QMutexLocker mutexLocker(&m_mutex);
98  m_iw = (m_iw+1) % m_size;
99  }
100 
101  writeAt = m_data.begin() + m_iw;
102 }
103 
104 int SampleSourceFifo::getIteratorOffset(const SampleVector::iterator& iterator)
105 {
106  return iterator - m_data.begin();
107 }
108 
109 void SampleSourceFifo::setIteratorFromOffset(SampleVector::iterator& iterator, int offset)
110 {
111  iterator = m_data.begin() + offset;
112 }
uint32_t size() const
int getIteratorOffset(const SampleVector::iterator &iterator)
SampleVector m_data
void dataRead(int nbSamples)
SampleSourceFifo(uint32_t size, QObject *parent=nullptr)
unsigned int uint32_t
Definition: rtptypes_win.h:46
void bumpIndex(SampleVector::iterator &writeAt)
copy current item to second buffer and bump write index - write phase 2
void readAdvance(SampleVector::iterator &readUntil, unsigned int nbSamples)
void dataWrite(int nbSamples)
void write(const Sample &sample)
write directly - phase 1 + phase 2
void resize(uint32_t size)
void setIteratorFromOffset(SampleVector::iterator &iterator, int offset)
void getWriteIterator(SampleVector::iterator &writeAt)
get iterator to current item for update - write phase 1
void getReadIterator(SampleVector::iterator &readUntil)
get iterator past the last sample of a read advance operation (i.e. current read iterator) ...