Skip to content

Commit

Permalink
Add Bof_GenerateWaveform
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Apr 7, 2024
1 parent d2cae4a commit b9faa39
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
12 changes: 11 additions & 1 deletion lib/include/bofstd/bofaudiostandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* V 1.00 May 26 2020 BHA : Initial release
*/
#pragma once

#include <bofstd/bofstring.h>

BEGIN_BOF_NAMESPACE()
Expand All @@ -37,6 +36,17 @@ enum class BOF_AUDIO_SAMPLE_FORMAT : uint32_t // See vlc_fourcc.h VLC_CODEC_S24L
typedef uint64_t AudioStandardId;
static const AudioStandardId DefaultAudioStandard = BOF_AUDIO_STANDARD_ID(16, 48000, static_cast<uint32_t>(BOF_AUDIO_SAMPLE_FORMAT::BOF_AUDIO_SAMPLE_FORMAT_S24L32));

enum class BOF_WAVE_FORM_TYPE
{
BOF_WAVE_FORM_TYPE_SINUS = 0,
BOF_WAVE_FORM_TYPE_SQUARE,
BOF_WAVE_FORM_TYPE_TRIANGLE,
BOF_WAVE_FORM_TYPE_SAW_TOOTH,
BOF_WAVE_FORM_TYPE_MAX
};

bool Bof_GenerateWaveform(BOF_WAVE_FORM_TYPE _WaveForm_E, float _Amplitude_f, float _Frequency_f, float _SampleRate_f, uint32_t _ChunkSize_U32, float *_pDataY_f, float &_rPhase_f);

class BOFSTD_EXPORT BofAudioStandard
{
public:
Expand Down
81 changes: 73 additions & 8 deletions lib/src/bofaudiostandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,77 @@
*
* V 1.00 May 26 2020 BHA : Initial release
*/
#define _USE_MATH_DEFINES
#include <cmath>
#include <regex>

#include <bofstd/bofaudiostandard.h>
#include <bofstd/bofenum.h>

#include <regex>

BEGIN_BOF_NAMESPACE()
bool Bof_GenerateWaveform(BOF_WAVE_FORM_TYPE _WaveForm_E, float _Amplitude_f, float _Frequency_f,
float _SampleRate_f, uint32_t _ChunkSize_U32, float *_pDataY_f, float &_rPhase_f)
{
bool Rts_B = false;
uint32_t i_U32;
float IncPhase_f, Phase_f, PeriodFactor_f;

//_pDataX_f tested below !!
if ((_WaveForm_E < BOF_WAVE_FORM_TYPE::BOF_WAVE_FORM_TYPE_MAX) && (_Amplitude_f > 0.0f) && (_Frequency_f > 0.0f) && (_SampleRate_f > 0.0f) && (_ChunkSize_U32) && (_pDataY_f))
{
Rts_B = true;
// After _SampleRate_f / _Frequency_f we have covered 2 M_PI, so for Fr 440 Fe 44100 we roll over angle after 100 sample->So for a chunk of 1024 byte we have 10 sinus
IncPhase_f = (2.0f * M_PI * _Frequency_f) / _SampleRate_f;
/* PeriodFactor_f / 10.0f->the ajust factor is 0.628 ???
Triangle 2.0f * M_PI / 2.0f (2PI for one cycle, divided by 2 for two triangles)
Sawtooth 2.0f * M_PI * 0.25f (2PI for one cycle, compressed to 0-1 range, adjusted by 0.25)
*/
PeriodFactor_f = 2.0f * M_PI / 10.0f; // *_Frequency_f / _SampleRate_f;
Phase_f = _rPhase_f;

switch (_WaveForm_E)
{
case BOF_WAVE_FORM_TYPE::BOF_WAVE_FORM_TYPE_SINUS:
for (i_U32 = 0; i_U32 < _ChunkSize_U32; i_U32++)
{
_pDataY_f[i_U32] = _Amplitude_f * sin(Phase_f);
Phase_f += IncPhase_f;
}
break;
case BOF_WAVE_FORM_TYPE::BOF_WAVE_FORM_TYPE_SQUARE:
for (i_U32 = 0; i_U32 < _ChunkSize_U32; i_U32++)
{
_pDataY_f[i_U32] = (sin(Phase_f) >= 0) ? _Amplitude_f : -_Amplitude_f;
Phase_f += IncPhase_f;
}
break;
case BOF_WAVE_FORM_TYPE::BOF_WAVE_FORM_TYPE_TRIANGLE:
for (i_U32 = 0; i_U32 < _ChunkSize_U32; i_U32++)
{
_pDataY_f[i_U32] = (_Amplitude_f * fabs(fmod(Phase_f * PeriodFactor_f, 4.0f) - 2.0f)) - _Amplitude_f;
Phase_f += IncPhase_f;
}
break;
case BOF_WAVE_FORM_TYPE::BOF_WAVE_FORM_TYPE_SAW_TOOTH:
for (i_U32 = 0; i_U32 < _ChunkSize_U32; i_U32++)
{
_pDataY_f[i_U32] = (_Amplitude_f * (2.0f * fmod(Phase_f * PeriodFactor_f * 0.25f, 1.0f)) - _Amplitude_f);
Phase_f += IncPhase_f;
}
break;

default:
Rts_B = false;
break;
}

if (Rts_B)
{
_rPhase_f = fmod(Phase_f, 2.0f * M_PI); // Keep phase within 0 to 2*PI range
}
}
return Rts_B;
}

static BofEnum<BOF_AUDIO_SAMPLE_FORMAT> S_BofAudioStandardEnumConverter(
{
Expand All @@ -47,13 +112,13 @@ BofAudioStandard::BofAudioStandard(uint32_t _NbMonoChannel_U32, uint32_t _Sampli
{
switch (_SampleFormat_E)
{
case BOF_AUDIO_SAMPLE_FORMAT::BOF_AUDIO_SAMPLE_FORMAT_S24L32:
mNbBitPerSample_U32 = 32;
break;
case BOF_AUDIO_SAMPLE_FORMAT::BOF_AUDIO_SAMPLE_FORMAT_S24L32:
mNbBitPerSample_U32 = 32;
break;

default:
mNbBitPerSample_U32 = 0;
break;
default:
mNbBitPerSample_U32 = 0;
break;
}
mNbMonoChannel_U32 = _NbMonoChannel_U32;
mSamplingRateInHz_U32 = _SamplingRateInHz_U32;
Expand Down

0 comments on commit b9faa39

Please sign in to comment.