diff --git a/lib/include/bofstd/bofaudiostandard.h b/lib/include/bofstd/bofaudiostandard.h index d56f670..018a208 100644 --- a/lib/include/bofstd/bofaudiostandard.h +++ b/lib/include/bofstd/bofaudiostandard.h @@ -20,7 +20,6 @@ * V 1.00 May 26 2020 BHA : Initial release */ #pragma once - #include BEGIN_BOF_NAMESPACE() @@ -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(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: diff --git a/lib/src/bofaudiostandard.cpp b/lib/src/bofaudiostandard.cpp index f9a4b24..c4087df 100644 --- a/lib/src/bofaudiostandard.cpp +++ b/lib/src/bofaudiostandard.cpp @@ -19,12 +19,77 @@ * * V 1.00 May 26 2020 BHA : Initial release */ +#define _USE_MATH_DEFINES +#include +#include + #include #include -#include - 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 S_BofAudioStandardEnumConverter( { @@ -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;