kwave  18.07.70
Kwave::SampleFIFO Class Reference

#include <SampleFIFO.h>

Collaboration diagram for Kwave::SampleFIFO:
Collaboration graph

Public Member Functions

 SampleFIFO ()
 
 SampleFIFO (const SampleFIFO &other)
 
virtual ~SampleFIFO ()
 
virtual void flush ()
 
virtual void put (const Kwave::SampleArray &source)
 
virtual unsigned int get (Kwave::SampleArray &buffer)
 
virtual unsigned int length ()
 
virtual void setSize (unsigned int size)
 
virtual void crop ()
 

Private Member Functions

unsigned int unlockedLength ()
 

Private Attributes

QQueue< Kwave::SampleArraym_buffer
 
sample_index_t m_size
 
sample_index_t m_read_offset
 
QMutex m_lock
 

Detailed Description

Definition at line 32 of file SampleFIFO.h.

Constructor & Destructor Documentation

◆ SampleFIFO() [1/2]

Kwave::SampleFIFO::SampleFIFO ( )

Constructor

Definition at line 24 of file SampleFIFO.cpp.

25  :m_buffer(), m_size(0), m_read_offset(0), m_lock(QMutex::Recursive)
26 {
27 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
sample_index_t m_read_offset
Definition: SampleFIFO.h:99
sample_index_t m_size
Definition: SampleFIFO.h:93

◆ SampleFIFO() [2/2]

Kwave::SampleFIFO::SampleFIFO ( const SampleFIFO other)

copy constructor

Definition at line 30 of file SampleFIFO.cpp.

31  :m_buffer(other.m_buffer), m_size(other.m_size),
32  m_read_offset(other.m_read_offset),
33  m_lock(QMutex::Recursive)
34 {
35 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
sample_index_t m_read_offset
Definition: SampleFIFO.h:99
sample_index_t m_size
Definition: SampleFIFO.h:93

◆ ~SampleFIFO()

Kwave::SampleFIFO::~SampleFIFO ( )
virtual

Destructor

Definition at line 38 of file SampleFIFO.cpp.

References flush(), and m_lock.

39 {
40  QMutexLocker _lock(&m_lock);
41  flush();
42 }
virtual void flush()
Definition: SampleFIFO.cpp:45
Here is the call graph for this function:

Member Function Documentation

◆ crop()

void Kwave::SampleFIFO::crop ( )
virtual

discards all superfluous content until the size condition is met.

Definition at line 136 of file SampleFIFO.cpp.

References m_buffer, m_lock, m_read_offset, m_size, and unlockedLength().

Referenced by Kwave::RecordPlugin::flushPrerecordingQueue().

137 {
138  QMutexLocker _lock(&m_lock);
139 
140  if (!m_size) return; // no limit set
141  if (unlockedLength() <= m_size) return; // nothing to do
142 
143  // we have to throw away some samples
144  while ((unlockedLength() - m_buffer.head().size()) > m_size)
145  m_buffer.dequeue();
146  m_read_offset = 0;
147  if (unlockedLength() <= m_size) return; // nothing more to do
148 
149  // put the read offset into the next buffer
150  Q_ASSERT(unlockedLength() > m_size);
152  Q_ASSERT(unlockedLength() - m_read_offset == m_size);
153 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
sample_index_t m_read_offset
Definition: SampleFIFO.h:99
sample_index_t m_size
Definition: SampleFIFO.h:93
unsigned int unlockedLength()
Definition: SampleFIFO.cpp:113
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush()

void Kwave::SampleFIFO::flush ( )
virtual

Reset the FIFO. This destroys the content and sets all pointers to their initial value.

Definition at line 45 of file SampleFIFO.cpp.

References m_buffer, m_lock, and m_read_offset.

Referenced by Kwave::RecordPlugin::flushPrerecordingQueue(), Kwave::Delay::setDelay(), and ~SampleFIFO().

46 {
47  QMutexLocker _lock(&m_lock);
48 
49  m_buffer.clear();
50  m_read_offset = 0;
51 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
sample_index_t m_read_offset
Definition: SampleFIFO.h:99
Here is the caller graph for this function:

◆ get()

unsigned int Kwave::SampleFIFO::get ( Kwave::SampleArray buffer)
virtual

gets and removes samples from the FIFO

Parameters
bufferreference to an array of samples to be filled
Returns
number of received samples

Definition at line 70 of file SampleFIFO.cpp.

References Kwave::SampleArray::constData(), Kwave::SampleArray::data(), length(), m_buffer, m_lock, m_read_offset, MEMCPY, and Kwave::SampleArray::size().

Referenced by Kwave::RecordPlugin::flushPrerecordingQueue(), and Kwave::Delay::goOn().

71 {
72  QMutexLocker _lock(&m_lock);
73 
74  if (m_buffer.isEmpty()) return 0;
75 
76  unsigned int rest = buffer.size();
77  const unsigned int available = length();
78  if (rest > available) rest = available;
79 
80  sample_t *dst = buffer.data();
81  unsigned int read = 0;
82  while (rest && !m_buffer.isEmpty()) {
83  const Kwave::SampleArray head = m_buffer.head();
84  const sample_t *src = head.constData();
85  unsigned int src_len = head.size();
86  Q_ASSERT(src_len > m_read_offset);
87 
88  if (m_read_offset + rest >= src_len) {
89  // use the whole buffer up to it's end
90  size_t len = static_cast<size_t>(src_len - m_read_offset);
91  MEMCPY(dst, src + m_read_offset, len * sizeof(sample_t));
92  rest -= len;
93  read += len;
94  dst += len;
95  m_read_offset = 0;
96 
97  // remove the buffer from the queue
98  m_buffer.dequeue();
99  } else {
100  // use only a portion of the buffer
101  MEMCPY(dst, src + m_read_offset, rest * sizeof(sample_t));
102  read += rest;
103  m_read_offset += rest;
104  Q_ASSERT(m_read_offset < src_len);
105  rest = 0;
106  }
107  }
108 
109  return read;
110 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
sample_index_t m_read_offset
Definition: SampleFIFO.h:99
#define MEMCPY
Definition: memcpy.h:37
virtual unsigned int length()
Definition: SampleFIFO.cpp:122
const sample_t * constData() const
Definition: SampleArray.h:54
unsigned int size() const
sample_t * data()
Definition: SampleArray.h:62
qint32 sample_t
Definition: Sample.h:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ length()

unsigned int Kwave::SampleFIFO::length ( )
virtual

Returns the number of samples that can be read out.

See also
m_written

Definition at line 122 of file SampleFIFO.cpp.

References m_lock, and unlockedLength().

Referenced by Kwave::RecordPlugin::flushPrerecordingQueue(), and get().

123 {
124  QMutexLocker _lock(&m_lock);
125  return unlockedLength();
126 }
unsigned int unlockedLength()
Definition: SampleFIFO.cpp:113
Here is the call graph for this function:
Here is the caller graph for this function:

◆ put()

void Kwave::SampleFIFO::put ( const Kwave::SampleArray source)
virtual

puts samples into the FIFO

Parameters
sourcereference to an array of samples to feed in

Definition at line 54 of file SampleFIFO.cpp.

References Kwave::SampleArray::isEmpty(), m_buffer, m_lock, m_size, and unlockedLength().

Referenced by Kwave::Delay::input(), and Kwave::Delay::setDelay().

55 {
56  if (buffer.isEmpty()) return;
57  QMutexLocker _lock(&m_lock);
58 
59  // always enqueue the new buffer
60  m_buffer.enqueue(buffer);
61 
62  if (!m_size) return; // no limit set
63 
64  // crop away whole unneeded buffers
65  while ((unlockedLength() - m_buffer.head().size()) > m_size)
66  m_buffer.dequeue();
67 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
sample_index_t m_size
Definition: SampleFIFO.h:93
unsigned int unlockedLength()
Definition: SampleFIFO.cpp:113
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSize()

void Kwave::SampleFIFO::setSize ( unsigned int  size)
virtual

sets the maximum size of the content

Definition at line 129 of file SampleFIFO.cpp.

References m_lock, and m_size.

130 {
131  QMutexLocker _lock(&m_lock);
132  m_size = size;
133 }
sample_index_t m_size
Definition: SampleFIFO.h:93

◆ unlockedLength()

unsigned int Kwave::SampleFIFO::unlockedLength ( )
private

internal version of length(), without locking

Definition at line 113 of file SampleFIFO.cpp.

References m_buffer, and Kwave::SampleArray::size().

Referenced by crop(), length(), and put().

114 {
115  unsigned int len = 0;
116  foreach (const Kwave::SampleArray &buf, m_buffer)
117  len += buf.size();
118  return len;
119 }
QQueue< Kwave::SampleArray > m_buffer
Definition: SampleFIFO.h:90
unsigned int size() const
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_buffer

QQueue<Kwave::SampleArray> Kwave::SampleFIFO::m_buffer
private

list of buffers with sample data

Definition at line 90 of file SampleFIFO.h.

Referenced by crop(), flush(), get(), put(), and unlockedLength().

◆ m_lock

QMutex Kwave::SampleFIFO::m_lock
private

mutex for access to the FIFO (recursive)

Definition at line 102 of file SampleFIFO.h.

Referenced by crop(), flush(), get(), length(), put(), setSize(), and ~SampleFIFO().

◆ m_read_offset

sample_index_t Kwave::SampleFIFO::m_read_offset
private

number of samples that have already been read out from the first buffer (head, first one to read out)

Definition at line 99 of file SampleFIFO.h.

Referenced by crop(), flush(), and get().

◆ m_size

sample_index_t Kwave::SampleFIFO::m_size
private

maximum number of samples of the content

Definition at line 93 of file SampleFIFO.h.

Referenced by crop(), put(), and setSize().


The documentation for this class was generated from the following files: