Skip to content

Commit

Permalink
sampler rec and stop Rec Thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Nov 30, 2024
1 parent 47aac15 commit dcc10be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Source/Common/AudioHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Point<float> DecibelsHelpers::mid2 = Point<float>(-18, .4f);
Point<float> DecibelsHelpers::end = Point<float>(6, 1);


DecibelFloatParameter::DecibelFloatParameter(const String& niceName, const String& description, float initValue) :
FloatParameter(niceName, description, initValue, 0, 1)
DecibelFloatParameter::DecibelFloatParameter(const String& niceName, const String& description, float initValue, bool enabled) :
FloatParameter(niceName, description, initValue, 0, 1, enabled)
{
decibels = DecibelsHelpers::valueToDecibels(value);
gain = DecibelsHelpers::valueToGain(value);
Expand Down
2 changes: 1 addition & 1 deletion Source/Common/AudioHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DecibelFloatParameter :
public FloatParameter
{
public:
DecibelFloatParameter(const String& niceName, const String& description, float initValue = .85f);
DecibelFloatParameter(const String& niceName, const String& description, float initValue = .85f, bool enabled = true);
~DecibelFloatParameter();

float gain;
Expand Down
25 changes: 24 additions & 1 deletion Source/Node/nodes/sampler/SamplerNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ SamplerNode::SamplerNode(var params) :
addChildControllableContainer(&controlsCC, false, 1);

fadeTimeMS = recordCC.addIntParameter("Fade Time", "Time to fade between start and end of recording, in milliseconds", 20);
recVolumeThreshold = (FloatParameter*)recordCC.addParameter(new DecibelFloatParameter("Rec Volume Threshold", "Volume threshold to start recording", .3, 0, 1, false));
recVolumeThreshold->canBeDisabledByUser = true;
stopRecVolumeThreshold = (FloatParameter*)recordCC.addParameter(new DecibelFloatParameter("Stop Rec Volume Threshold", "Volume threshold to stop recording", .1, 0, 1, false));
stopRecVolumeThreshold->canBeDisabledByUser = true;

isRecording = recordCC.addBoolParameter("Is Recording", "Is recording a note ?", false);
isRecording->setControllableFeedbackOnly(true);

Expand Down Expand Up @@ -503,7 +508,8 @@ void SamplerNode::handleNoteOn(MidiKeyboardState* source, int midiChannel, int m
}
else
{
startRecording(midiNoteNumber);
if (recVolumeThreshold->enabled) sn->state->setValueWithData(ONSET);
else startRecording(midiNoteNumber);
}
}
else //filled note playing
Expand Down Expand Up @@ -767,6 +773,14 @@ void SamplerNode::processBlockInternal(AudioBuffer<float>& buffer, MidiBuffer& m

recordedSamples += blockSize;
}

if (stopRecVolumeThreshold->enabled)
{
float mag = buffer.getMagnitude(0, blockSize);
float decibels = Decibels::gainToDecibels(mag, -100.f);
float normDecibels = jmap(decibels, -100.f, 6.f, 0.f, 1.f);
if (normDecibels < stopRecVolumeThreshold->floatValue()) stopRecording();
}
}

if (!monitor->boolValue()) buffer.clear();
Expand All @@ -783,7 +797,16 @@ void SamplerNode::processBlockInternal(AudioBuffer<float>& buffer, MidiBuffer& m
NoteState st = s->state->getValueDataAsEnum<NoteState>();
if (st == EMPTY || st == RECORDING || st == PROCESSING) continue;

if (st == ONSET)
{
if (isRecording->boolValue()) continue;
float mag = buffer.getMagnitude(0, blockSize);

float decibels = Decibels::gainToDecibels(mag, -100.f);
float normDecibels = jmap(decibels, -100.f, 6.f, 0.f, 1.f);
if (normDecibels > recVolumeThreshold->floatValue()) startRecording(i);
continue;
}

if (s->adsr.getState() == CurvedADSR::env_idle)
{
Expand Down
5 changes: 3 additions & 2 deletions Source/Node/nodes/sampler/SamplerNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class SamplerNode :
BoolParameter* isRecording;
IntParameter* fadeTimeMS;
IntParameter* autoKeyFadeTimeMS;

FloatParameter* recVolumeThreshold;
FloatParameter* stopRecVolumeThreshold;

ControllableContainer adsrCC;
FloatParameter* attack;
Expand Down Expand Up @@ -90,7 +91,7 @@ class SamplerNode :
MidiKeyboardState keyboardState;


enum NoteState { EMPTY, RECORDING, FILLED, PROCESSING, PLAYING };
enum NoteState { EMPTY, ONSET, RECORDING, FILLED, PROCESSING, PLAYING };
class SamplerNote :
public Thread
{
Expand Down

0 comments on commit dcc10be

Please sign in to comment.