Skip to content

Commit

Permalink
adsr, as it is..
Browse files Browse the repository at this point in the history
  • Loading branch information
jarikomppa committed Feb 4, 2021
1 parent e19772a commit 3346bbc
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
28 changes: 6 additions & 22 deletions demos/piano/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,6 @@ void unplonk(float rel)
gPlonked[i].mHandle = 0;
}

void replonk(float vol = 0x50)
{
int i = 0;
while (i < 128 && gPlonked[i].mHandle != 0) i++;
if (i == 128) return;

vol = (vol + 10) / (float)(0x7f + 10);
vol *= vol;
for (i = 0; i < 128; i++)
{
if (gPlonked[i].mHandle != 0)
{
gSoloud.fadeVolume(gPlonked[i].mHandle, vol, 0.1);
}
}
}

void say(const char *text)
{
Expand All @@ -174,11 +158,6 @@ void midicallback(double deltatime, std::vector< unsigned char > *message, void
{
unplonk((float)pow(0.943875f, 0x3c - (*message)[1]));
}
// aftertouch
if (((*message)[0] & 0xf0) == 0xd0)
{
replonk((float)(*message)[1]);
}
}

int DemoEntry(int argc, char *argv[])
Expand Down Expand Up @@ -255,6 +234,11 @@ void waveform_window()
gWave.setWaveform(gWaveSelect);
}

ImGui::DragFloat("Attack", &gWave.mADSR.mA, 0.01f);
ImGui::DragFloat("Decay", &gWave.mADSR.mD, 0.01f);
ImGui::DragFloat("Sustain", &gWave.mADSR.mS, 0.01f);
ImGui::DragFloat("Release", &gRelease, 0.01f);

ImGui::End();
}

Expand All @@ -263,7 +247,7 @@ void info_window()
float* buf = gSoloud.getWave();
float* fft = gSoloud.calcFFT();

ONCE(ImGui::SetNextWindowPos(ImVec2(500, 20)));
ONCE(ImGui::SetNextWindowPos(ImVec2(520, 20)));
ImGui::Begin("Output");
ImGui::PlotLines("##Wave", buf, 256, 0, "Wave", -1, 1, ImVec2(264, 80));
ImGui::PlotHistogram("##FFT", fft, 256 / 2, 0, "FFT", 0, 10, ImVec2(264, 80), 8);
Expand Down
77 changes: 77 additions & 0 deletions demos/piano/soloud_adsr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
SoLoud audio engine
Copyright (c) 2013-2021 Jari Komppa
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/

#ifndef ADSR_H
#define ADSR_H

#include "soloud.h"

namespace SoLoud
{
class ADSR
{
public:
float mA, mD, mS, mR;

ADSR()
{
mA = 0.0f;
mD = 0.0f;
mS = 1.0f;
mR = 0.0f;
}

ADSR(float aA, float aD, float aS, float aR)
{
mA = aA;
mD = aD;
mS = aS;
mR = aR;
}

float val(float aT, float aRelTime)
{
if (aT < mA)
{
return aT / mA;
}
aT -= mA;
if (aT < mD)
{
return 1.0f - ((aT / mD)) * (1.0f - mS);
}
aT -= mD;
if (aT < aRelTime)
return mS;
aT -= aRelTime;
if (aT >= mR)
{
return 0.0f;
}
return (1.0f - aT / mR) * mS;
}
};
};

#endif
5 changes: 4 additions & 1 deletion demos/piano/soloud_basicwave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ namespace SoLoud
mParent = aParent;
mOffset = 0;
mFreq = aParent->mFreq;
mT = 0;
}

unsigned int BasicwaveInstance::getAudio(float *aBuffer, unsigned int aSamplesToRead, unsigned int aBufferSize)
{
unsigned int i;
int waveform = mParent->mWaveform;
float d = 1.0f / mSamplerate;
for (i = 0; i < aSamplesToRead; i++)
{
aBuffer[i] = SoLoud::Misc::generateWaveform(waveform, fmod(mFreq * (float)mOffset, 1.0f));
aBuffer[i] = SoLoud::Misc::generateWaveform(waveform, fmod(mFreq * (float)mOffset, 1.0f)) * mParent->mADSR.val(mT, 10000000000000.0f);
mOffset++;
mT += d;
}
return aSamplesToRead;
}
Expand Down
3 changes: 3 additions & 0 deletions demos/piano/soloud_basicwave.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ freely, subject to the following restrictions:
#define BASICWAVE_H

#include "soloud.h"
#include "soloud_adsr.h"

namespace SoLoud
{
Expand All @@ -36,6 +37,7 @@ namespace SoLoud
Basicwave *mParent;
float mFreq;
int mOffset;
float mT;
public:
BasicwaveInstance(Basicwave *aParent);
virtual unsigned int getAudio(float *aBuffer, unsigned int aSamplesToRead, unsigned int aBufferSize);
Expand All @@ -45,6 +47,7 @@ namespace SoLoud
class Basicwave : public AudioSource
{
public:
ADSR mADSR;
float mFreq;
int mWaveform;
Basicwave();
Expand Down

0 comments on commit 3346bbc

Please sign in to comment.