kwave  18.07.70
SampleBuffer.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  SampleBuffer.cpp - simple buffer for sample arrays
3  -------------------
4  begin : Sun Oct 17 2010
5  copyright : (C) 2010 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QFuture>
21 #include <QtConcurrentRun>
22 
23 #include "libkwave/Utils.h"
25 
26 //***************************************************************************
27 //***************************************************************************
29  :Kwave::SampleSink(parent),
30  m_data(), m_offset(0), m_buffered(0), m_sema(1)
31 {
32 }
33 
34 //***************************************************************************
36 {
37  m_sema.acquire();
38 }
39 
40 //***************************************************************************
42 {
43  return m_data.isEmpty();
44 }
45 
46 //***************************************************************************
48 {
49  return m_data;
50 }
51 
52 //***************************************************************************
54 {
55  return m_data;
56 }
57 
58 //***************************************************************************
59 unsigned int Kwave::SampleBuffer::available() const
60 {
61  unsigned int size = m_data.size();
62  return (size > m_offset) ? (size - m_offset) : 0;
63 }
64 
65 //***************************************************************************
66 const sample_t *Kwave::SampleBuffer::get(unsigned int len)
67 {
68  const unsigned int size = m_data.size();
69  if (m_offset > size) return Q_NULLPTR; // already reached EOF
70 
71  // limit read length to the size of the buffer
72  if (len > size) len = size;
73 
74  // get a pointer to the raw data
75  const sample_t *raw = m_data.constData() + m_offset;
76 
77  // advance the offset
78  m_offset += len;
79 
80  return raw;
81 }
82 
83 //***************************************************************************
85 {
86  const unsigned int block_size = Kwave::StreamObject::blockSize();
87 
88  if (m_buffered >= block_size) finished();
89 
90  // initial fill of the buffer?
91  bool ok = m_data.resize(block_size);
92  Q_ASSERT(ok);
93  Q_UNUSED(ok);
94 
95  m_data[m_buffered++] = sample;
96  if (m_buffered >= block_size) finished();
97 }
98 
99 
100 //***************************************************************************
102 {
103  bool ok = true;
104 
105  if (m_buffered && (m_data.size() != m_buffered))
106  ok &= m_data.resize(m_buffered);
107 
108  enqueue(m_data);
109  m_buffered = 0;
111  Q_ASSERT(ok);
112 }
113 
114 //***************************************************************************
116 {
117  // if we have buffered data, flush that first
118  if (m_buffered) {
119  bool ok = m_data.resize(m_buffered);
120  Q_ASSERT(ok);
121  Q_UNUSED(ok);
122  m_buffered = 0;
123  enqueue(m_data);
124  }
125 
126  // take over the input array directly
127  m_data = data;
128  m_offset = 0;
129  m_buffered = data.size();
130 }
131 
132 //***************************************************************************
134 {
135  m_sema.acquire();
136  QtConcurrent::run(this, &Kwave::SampleBuffer::emitData, data);
137 }
138 
139 //***************************************************************************
141 {
142  // NOTE: this signal could be connected to a slot that blocks for a while
143  emit output(data);
144  m_sema.release();
145 }
146 
147 //***************************************************************************
148 //***************************************************************************
virtual Kwave::SampleArray & data()
virtual const sample_t * get(unsigned int len)
Definition: App.h:33
void emitData(Kwave::SampleArray data)
virtual bool isEmpty() const
virtual void input(Kwave::SampleArray data)
virtual const Kwave::SampleArray & constData() const
void put(sample_t sample)
void enqueue(Kwave::SampleArray data)
void output(Kwave::SampleArray data)
bool isEmpty() const
Definition: SampleArray.h:118
virtual void finished()
unsigned int m_buffered
Definition: SampleBuffer.h:112
Kwave::SampleArray m_data
Definition: SampleBuffer.h:106
const sample_t * constData() const
Definition: SampleArray.h:54
virtual unsigned int blockSize() const
unsigned int size() const
virtual unsigned int available() const
SampleBuffer(QObject *parent=Q_NULLPTR)
bool resize(unsigned int size) Q_REQUIRED_RESULT
qint32 sample_t
Definition: Sample.h:37
unsigned int m_offset
Definition: SampleBuffer.h:109