LMMS
Loading...
Searching...
No Matches
RingBuffer.h
Go to the documentation of this file.
1/*
2 * RingBuffer.h - an effective and flexible implementation of a ringbuffer for LMMS
3 *
4 * Copyright (c) 2014 Vesa Kivimäki
5 * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6 *
7 * This file is part of LMMS - https://lmms.io
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program (see COPYING); if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 */
25
26#ifndef LMMS_RING_BUFFER_H
27#define LMMS_RING_BUFFER_H
28
29#include <cmath>
30#include <QObject>
31#include "LmmsTypes.h"
32#include "lmms_export.h"
33
34
35namespace lmms
36{
37
38class SampleFrame;
39
42class LMMS_EXPORT RingBuffer : public QObject
43{
44 Q_OBJECT
45public:
50
54 RingBuffer( float size );
55 ~RingBuffer() override;
56
57
58
59
61// Provided functions //
63
64// utility functions
65
68 void reset();
69
73 void changeSize( f_cnt_t size );
74
78 void changeSize( float size );
79
83 void setSamplerateAware( bool b );
84
85
86// position adjustment functions
87
90 void advance();
91
95 void movePosition( f_cnt_t amount );
96
100 void movePosition( float amount );
101
102
103// read functions
104
109 void pop( SampleFrame* dst );
110
111// note: ringbuffer position is unaffected by all other read functions beside pop()
112
117 void read( SampleFrame* dst, f_cnt_t offset = 0 );
118
123 void read( SampleFrame* dst, float offset );
124
130 void read( SampleFrame* dst, f_cnt_t offset, f_cnt_t length );
131
137 void read( SampleFrame* dst, float offset, f_cnt_t length );
138
139
140// write functions
141
147 void write( SampleFrame* src, f_cnt_t offset=0, f_cnt_t length=0 );
148
154 void write( SampleFrame* src, float offset, f_cnt_t length=0 );
155
161 void writeAdding( SampleFrame* src, f_cnt_t offset=0, f_cnt_t length=0 );
162
168 void writeAdding( SampleFrame* src, float offset, f_cnt_t length=0 );
169
177 void writeAddingMultiplied( SampleFrame* src, f_cnt_t offset, f_cnt_t length, float level );
178
186 void writeAddingMultiplied( SampleFrame* src, float offset, f_cnt_t length, float level );
187
195 void writeSwappedAddingMultiplied( SampleFrame* src, f_cnt_t offset, f_cnt_t length, float level );
196
204 void writeSwappedAddingMultiplied( SampleFrame* src, float offset, f_cnt_t length, float level );
205
206
207protected slots:
208 void updateSamplerate();
209
210private:
211 inline f_cnt_t msToFrames( float ms )
212 {
213 return static_cast<f_cnt_t>( ceilf( ms * (float)m_samplerate * 0.001f ) );
214 }
215
218 size_t m_size;
220 volatile unsigned int m_position;
221
222};
223
224
225} // namespace lmms
226
227#endif // LMMS_RING_BUFFER_H
void movePosition(f_cnt_t amount)
Moves position forwards/backwards by an amount of frames.
Definition RingBuffer.cpp:109
void setSamplerateAware(bool b)
Sets whether the ringbuffer size is adjusted for samplerate when samplerate changes.
Definition RingBuffer.cpp:90
void writeAdding(SampleFrame *src, f_cnt_t offset=0, f_cnt_t length=0)
Mixes a buffer of sampleframes additively to the ringbuffer at specified position.
Definition RingBuffer.cpp:223
void updateSamplerate()
Definition RingBuffer.cpp:306
sample_rate_t m_samplerate
Definition RingBuffer.h:217
void advance()
Advances the position by one period.
Definition RingBuffer.cpp:103
void reset()
Clears the ringbuffer of any data and resets the position to 0.
Definition RingBuffer.cpp:65
size_t m_size
Definition RingBuffer.h:218
void writeSwappedAddingMultiplied(SampleFrame *src, f_cnt_t offset, f_cnt_t length, float level)
Mixes a buffer of sampleframes additively to the ringbuffer at specified position,...
Definition RingBuffer.cpp:279
const f_cnt_t m_fpp
Definition RingBuffer.h:216
f_cnt_t msToFrames(float ms)
Definition RingBuffer.h:211
RingBuffer(f_cnt_t size)
Constructs a ringbuffer of specified size, will not care about samplerate changes.
Definition RingBuffer.cpp:35
void writeAddingMultiplied(SampleFrame *src, f_cnt_t offset, f_cnt_t length, float level)
Mixes a buffer of sampleframes additively to the ringbuffer at specified position,...
Definition RingBuffer.cpp:250
void write(SampleFrame *src, f_cnt_t offset=0, f_cnt_t length=0)
Writes a buffer of sampleframes to the ringbuffer at specified position.
Definition RingBuffer.cpp:196
SampleFrame * m_buffer
Definition RingBuffer.h:219
void pop(SampleFrame *dst)
Destructively reads a period-sized buffer from the current position, writes it to a specified destina...
Definition RingBuffer.cpp:121
void changeSize(f_cnt_t size)
Changes the size of the ringbuffer. Clears all data.
Definition RingBuffer.cpp:72
volatile unsigned int m_position
Definition RingBuffer.h:220
Definition SampleFrame.h:41
Definition AudioAlsa.cpp:35
std::uint32_t sample_rate_t
Definition LmmsTypes.h:42
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
png_uint_32 length
Definition png.c:2247
b
Definition crypt.c:628
ulg size
Definition extract.c:2350
read(f, &c, 1)