mirror of
https://codeberg.org/armin/monoslicer.git
synced 2026-09-01 20:20:46 +02:00
68 lines
2 KiB
C
68 lines
2 KiB
C
|
|
#pragma once
|
||
|
|
#include <juce_audio_processors/juce_audio_processors.h>
|
||
|
|
#include <juce_dsp/juce_dsp.h>
|
||
|
|
#include <vector>
|
||
|
|
#include <algorithm>
|
||
|
|
#include <cmath>
|
||
|
|
|
||
|
|
class SliceManager
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
struct Slice
|
||
|
|
{
|
||
|
|
int startSample;
|
||
|
|
int endSample;
|
||
|
|
int midiNote; // halftone offset from C3 (0 = C3, 1 = C#3, etc.)
|
||
|
|
};
|
||
|
|
|
||
|
|
SliceManager();
|
||
|
|
|
||
|
|
void setSampleBuffer(juce::AudioBuffer<float>* buffer, double sampleRate);
|
||
|
|
void setSensitivity(float s);
|
||
|
|
void setBassGain(float g);
|
||
|
|
void setTrebleGain(float g);
|
||
|
|
|
||
|
|
void autoSlice();
|
||
|
|
void clearSlices();
|
||
|
|
void addSliceManual(int samplePos);
|
||
|
|
void removeSliceAt(int samplePos, int tolerance = 5);
|
||
|
|
void moveSlice(int fromSample, int toSample);
|
||
|
|
|
||
|
|
void trimStart(int sample);
|
||
|
|
void trimEnd(int sample);
|
||
|
|
|
||
|
|
void undo();
|
||
|
|
void redo();
|
||
|
|
bool canUndo() const;
|
||
|
|
bool canRedo() const;
|
||
|
|
|
||
|
|
const std::vector<Slice>& getSlices() const { return slices; }
|
||
|
|
int getNumSlices() const { return static_cast<int>(slices.size()); }
|
||
|
|
const Slice& getSlice(int index) const { return slices[static_cast<size_t>(index)]; }
|
||
|
|
|
||
|
|
int findSliceIndexForSample(int samplePos) const;
|
||
|
|
|
||
|
|
juce::AudioBuffer<float>* getMutableBuffer() { return sampleBuffer; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
void sortSlices();
|
||
|
|
void assignMidiNotes();
|
||
|
|
void pushHistory();
|
||
|
|
void truncateHistory();
|
||
|
|
|
||
|
|
std::vector<Slice> slices;
|
||
|
|
std::vector<std::vector<Slice>> undoStack;
|
||
|
|
std::vector<std::vector<Slice>> redoStack;
|
||
|
|
static constexpr size_t maxHistory = 50;
|
||
|
|
juce::AudioBuffer<float>* sampleBuffer = nullptr;
|
||
|
|
double currentSampleRate = 44100.0;
|
||
|
|
|
||
|
|
float sensitivity = 0.5f;
|
||
|
|
float bassGain = 1.0f;
|
||
|
|
float trebleGain = 1.0f;
|
||
|
|
|
||
|
|
std::vector<float> computeEnvelope(const float* channelData, int numSamples);
|
||
|
|
std::vector<float> lowpassFilter(const std::vector<float>& input, float cutoffHz);
|
||
|
|
std::vector<float> highpassFilter(const std::vector<float>& input, float cutoffHz);
|
||
|
|
std::vector<float> detectOnsets(const std::vector<float>& envelope, float threshold);
|
||
|
|
};
|