mirror of
https://github.com/LMMS/lmms.git
synced 2026-01-29 08:43:18 -05:00
* Remove the struct StereoSample Remove the struct `StereoSample`. Let `AudioEngine::getPeakValues` return a `sampleFrame` instead. Adjust the calls in `Mixer` and `Oscilloscope`. * Simplify AudioEngine::getPeakValues * Remove surroundSampleFrame Some code assumes that `surroundSampleFrame` is interchangeable with `sampleFrame`. Thus, if the line `#define LMMS_DISABLE_SURROUND` is commented out in `lmms_basics.h` then the code does not compile anymore because `surroundSampleFrame` now is defined to be an array with four values instead of two. There also does not seem to be any support for surround sound (four channels instead of two) in the application. The faders and mixers do not seem to support more that two channels and the instruments and effects all expect a `sampleFrame`, i.e. stereo channels. It therefore makes sense to remove the "feature" because it also hinders the improvement of `sampleFrame`, e.g. by making it a class with some convenience methods that act on `sampleFrame` instances. All occurrences of `surroundSampleFrame` are replaced with `sampleFrame`. The version of `BufferManager::clear` that takes a `surroundSampleFrame` is removed completely. The define `SURROUND_CHANNELS` is removed. All its occurrences are replaced with `DEFAULT_CHANNELS`. Most of the audio devices classes, i.e. classes that inherit from `AudioDevice`, now clamp the configuration parameter between two values of `DEFAULT_CHANNELS`. This can be improved/streamlined later. `BYTES_PER_SURROUND_FRAME` has been removed as it was not used anywhere anyway. * Make sampleFrame a class Make `sampleFrame` a class with several convenience methods. As a first step and demonstration adjust the follow methods to make use of the new functionality: * `AudioEngine::getPeakValues`: Much more concise now. * `lmms::MixHelpers::sanitize`: Better structure, better readable, less dereferencing and juggling with indices. * `AddOp`, `AddMultipliedOp`, `multiply`: Make use of operators. Might become superfluous in the future. * More operators and methods for sampleFrame Add some more operators and methods to `sampleFrame`: * Constructor which initializes both channels from a single sample value * Assignment operator from a single sample value * Addition/multiplication operators * Scalar product Adjust some more plugins to the new functionality of `sampleFrame`. * Adjust DelayEffect to methods in sampleFrame * Use composition instead of inheritance Using inheritance was the quickest way to enable adding methods to `sampleFrame` without having to reimpement much of `std::array`s interface. This is changed with this commit. The array is now a member of `sampleFrame` and the interface is extended with the necessary methods `data` and the index operator. An `average` method was added so that no iterators need to be implemented (see changes in `SampleWaveform.cpp`). * Apply suggestions from code review Apply Veratil's suggestions from the code review Co-authored-by: Kevin Zander <veratil@gmail.com> * Fix warnings: zeroing non-trivial type Fix several warnings of the following form: Warnung: »void* memset(void*, int, size_t)« Säubern eines Objekts von nichttrivialem Typ »class lmms::sampleFrame«; use assignment or value-initialization instead [-Wclass-memaccess] * Remove unnecessary reinterpret_casts Remove some unnecessary reinterpret_casts with regards to `sampleFrame` buffers. `PlayHandle::m_playHandleBuffer` already is a `sampleFrame*` and does not need a reinterpret_cast anymore. In `LadspaEffect::processAudioBuffer` the `QVarLengthArray` is now directly initialized as an array of `sampleFrame` instances. I guess in both places the `sampleFrame` previously was a `surroundSampleFrame` which has been removed. * Clean up zeroSampleFrames code * Fix warnings in RemotePlugin Fix some warnings related to calls to `memcpy` in conjunction with`sampleFrame` which is now a class. Add the helper functions `copyToSampleFrames` and `copyFromSampleFrames` and use them. The first function copies data from a `float` buffer into a `sampleFrame` buffer and the second copies vice versa. * Rename "sampleFrame" to "SampleFrame" Uppercase the name of `sampleFrame` so that it uses UpperCamelCase convention. * Move SampleFrame into its own file Move the class `SampleFrame` into its own class and remove it from `lmms_basics.h`. Add forward includes to all headers where possible or include the `SampleFrame` header if it's not just referenced but used. Add include to all cpp files where necessary. It's a bit surprising that the `SampleFrame` header does not need to be included much more often in the implementation/cpp files. This is an indicator that it seems to be included via an include chain that at one point includes one of the headers where an include instead of a forward declaration had to be added in this commit. * Return reference for += and *= Return a reference for the compound assignment operators `+=` and `-=`. * Explicit float constructor Make the constructor that takes a `float` explicit. Remove the assignment operator that takes a `float`. Clients must use the explicit `float` constructor and assign the result. Adjust the code in "BitInvader" accordingly. * Use std::fill in zeroSampleFrames * Use zeroSampleFrames in sanitize * Replace max with absMax Replace `SampleFrame::max` with `SampleFrame::absMax`. Use `absMax` in `DelayEffect::processAudioBuffer`. This should also fix a buggy implementation of the peak computation. Add the function `getAbsPeakValues`. It computes the absolute peak values for a buffer. Remove `AudioEngine::getPeakValues`. It's not really the business of the audio engine. Let `Mixer` and `Oscilloscope` use `getAbsPeakValues`. * Replace scalarProduct Replace the rather mathematical method `scalarProduct` with `sumOfSquaredAmplitudes`. It was always called on itself anyway. * Remove comment/TODO * Simplify sanitize Simplify the `sanitize` function by getting rid of the `bool found` and by zeroing the buffer as soon as a problem is found. * Put pointer symbols next to type * Code review adjustments * Remove "#pragme once" * Adjust name of include guard * Remove superfluous includes (leftovers from previous code changes) --------- Co-authored-by: Kevin Zander <veratil@gmail.com>
228 lines
8.9 KiB
C++
228 lines
8.9 KiB
C++
/*
|
|
* RingBuffer.h - an effective and flexible implementation of a ringbuffer for LMMS
|
|
*
|
|
* Copyright (c) 2014 Vesa Kivimäki
|
|
* Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
|
*
|
|
* This file is part of LMMS - https://lmms.io
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2 of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public
|
|
* License along with this program (see COPYING); if not, write to the
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
* Boston, MA 02110-1301 USA.
|
|
*
|
|
*/
|
|
|
|
#ifndef LMMS_RING_BUFFER_H
|
|
#define LMMS_RING_BUFFER_H
|
|
|
|
#include <cmath>
|
|
#include <QObject>
|
|
#include "lmms_basics.h"
|
|
#include "lmms_export.h"
|
|
|
|
|
|
namespace lmms
|
|
{
|
|
|
|
class SampleFrame;
|
|
|
|
/** \brief A basic LMMS ring buffer for single-thread use. For thread and realtime safe alternative see LocklessRingBuffer.
|
|
*/
|
|
class LMMS_EXPORT RingBuffer : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
/** \brief Constructs a ringbuffer of specified size, will not care about samplerate changes
|
|
* \param size The size of the buffer in frames. The actual size will be size + period size
|
|
*/
|
|
RingBuffer( f_cnt_t size );
|
|
|
|
/** \brief Constructs a ringbuffer of specified samplerate-dependent size, which will be updated when samplerate changes
|
|
* \param size The size of the buffer in milliseconds. The actual size will be size + period size
|
|
*/
|
|
RingBuffer( float size );
|
|
~RingBuffer() override;
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
// Provided functions //
|
|
////////////////////////////////////
|
|
|
|
// utility functions
|
|
|
|
/** \brief Clears the ringbuffer of any data and resets the position to 0
|
|
*/
|
|
void reset();
|
|
|
|
/** \brief Changes the size of the ringbuffer. Clears all data.
|
|
* \param size New size in frames
|
|
*/
|
|
void changeSize( f_cnt_t size );
|
|
|
|
/** \brief Changes the size of the ringbuffer. Clears all data.
|
|
* \param size New size in milliseconds
|
|
*/
|
|
void changeSize( float size );
|
|
|
|
/** \brief Sets whether the ringbuffer size is adjusted for samplerate when samplerate changes
|
|
* \param b True if samplerate should affect buffer size
|
|
*/
|
|
void setSamplerateAware( bool b );
|
|
|
|
|
|
// position adjustment functions
|
|
|
|
/** \brief Advances the position by one period
|
|
*/
|
|
void advance();
|
|
|
|
/** \brief Moves position forwards/backwards by an amount of frames
|
|
* \param amount Number of frames to move, may be negative
|
|
*/
|
|
void movePosition( f_cnt_t amount );
|
|
|
|
/** \brief Moves position forwards/backwards by an amount of milliseconds
|
|
* \param amount Number of milliseconds to move, may be negative
|
|
*/
|
|
void movePosition( float amount );
|
|
|
|
|
|
// read functions
|
|
|
|
/** \brief Destructively reads a period-sized buffer from the current position, writes it
|
|
* to a specified destination, and advances the position by one period
|
|
* \param dst Destination pointer
|
|
*/
|
|
void pop( SampleFrame* dst );
|
|
|
|
// note: ringbuffer position is unaffected by all other read functions beside pop()
|
|
|
|
/** \brief Reads a period-sized buffer from the ringbuffer and writes it to a specified destination
|
|
* \param dst Destination pointer
|
|
* \param offset Offset in frames against current position, may be negative
|
|
*/
|
|
void read( SampleFrame* dst, f_cnt_t offset = 0 );
|
|
|
|
/** \brief Reads a period-sized buffer from the ringbuffer and writes it to a specified destination
|
|
* \param dst Destination pointer
|
|
* \param offset Offset in milliseconds against current position, may be negative
|
|
*/
|
|
void read( SampleFrame* dst, float offset );
|
|
|
|
/** \brief Reads a buffer of specified size from the ringbuffer and writes it to a specified destination
|
|
* \param dst Destination pointer
|
|
* \param offset Offset in frames against current position, may be negative
|
|
* \param length Length in frames of the buffer to read - must not be higher than the size of the ringbuffer!
|
|
*/
|
|
void read( SampleFrame* dst, f_cnt_t offset, f_cnt_t length );
|
|
|
|
/** \brief Reads a buffer of specified size from the ringbuffer and writes it to a specified destination
|
|
* \param dst Destination pointer
|
|
* \param offset Offset in milliseconds against current position, may be negative
|
|
* \param length Length in frames of the buffer to read - must not be higher than the size of the ringbuffer!
|
|
*/
|
|
void read( SampleFrame* dst, float offset, f_cnt_t length );
|
|
|
|
|
|
// write functions
|
|
|
|
/** \brief Writes a buffer of sampleframes to the ringbuffer at specified position
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in frames against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used - must not be higher than the size of the ringbuffer!
|
|
*/
|
|
void write( SampleFrame* src, f_cnt_t offset=0, f_cnt_t length=0 );
|
|
|
|
/** \brief Writes a buffer of sampleframes to the ringbuffer at specified position
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in milliseconds against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used - must not be higher than the size of the ringbuffer!
|
|
*/
|
|
void write( SampleFrame* src, float offset, f_cnt_t length=0 );
|
|
|
|
/** \brief Mixes a buffer of sampleframes additively to the ringbuffer at specified position
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in frames against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used - must not be higher than the size of the ringbuffer!
|
|
*/
|
|
void writeAdding( SampleFrame* src, f_cnt_t offset=0, f_cnt_t length=0 );
|
|
|
|
/** \brief Mixes a buffer of sampleframes additively to the ringbuffer at specified position
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in milliseconds against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used - must not be higher than the size of the ringbuffer!
|
|
*/
|
|
void writeAdding( SampleFrame* src, float offset, f_cnt_t length=0 );
|
|
|
|
/** \brief Mixes a buffer of sampleframes additively to the ringbuffer at specified position, with
|
|
* a specified multiplier applied to the frames
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in frames against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used - must not be higher than the size of the ringbuffer!
|
|
* \param level Multiplier applied to the frames before they're written to the ringbuffer
|
|
*/
|
|
void writeAddingMultiplied( SampleFrame* src, f_cnt_t offset, f_cnt_t length, float level );
|
|
|
|
/** \brief Mixes a buffer of sampleframes additively to the ringbuffer at specified position, with
|
|
* a specified multiplier applied to the frames
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in milliseconds against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used
|
|
* \param level Multiplier applied to the frames before they're written to the ringbuffer
|
|
*/
|
|
void writeAddingMultiplied( SampleFrame* src, float offset, f_cnt_t length, float level );
|
|
|
|
/** \brief Mixes a buffer of sampleframes additively to the ringbuffer at specified position, with
|
|
* a specified multiplier applied to the frames, with swapped channels
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in frames against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used - must not be higher than the size of the ringbuffer!
|
|
* \param level Multiplier applied to the frames before they're written to the ringbuffer
|
|
*/
|
|
void writeSwappedAddingMultiplied( SampleFrame* src, f_cnt_t offset, f_cnt_t length, float level );
|
|
|
|
/** \brief Mixes a buffer of sampleframes additively to the ringbuffer at specified position, with
|
|
* a specified multiplier applied to the frames, with swapped channels
|
|
* \param src Pointer to the source buffer
|
|
* \param offset Offset in milliseconds against current position, may *NOT* be negative
|
|
* \param length Length of the source buffer, if zero, period size is used
|
|
* \param level Multiplier applied to the frames before they're written to the ringbuffer
|
|
*/
|
|
void writeSwappedAddingMultiplied( SampleFrame* src, float offset, f_cnt_t length, float level );
|
|
|
|
|
|
protected slots:
|
|
void updateSamplerate();
|
|
|
|
private:
|
|
inline f_cnt_t msToFrames( float ms )
|
|
{
|
|
return static_cast<f_cnt_t>( ceilf( ms * (float)m_samplerate * 0.001f ) );
|
|
}
|
|
|
|
const fpp_t m_fpp;
|
|
sample_rate_t m_samplerate;
|
|
size_t m_size;
|
|
SampleFrame* m_buffer;
|
|
volatile unsigned int m_position;
|
|
|
|
};
|
|
|
|
|
|
} // namespace lmms
|
|
|
|
#endif // LMMS_RING_BUFFER_H
|