Akifumi Takahashi / DSinGenerator
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DSin.cpp Source File

DSin.cpp

00001 #include "DSin.h"
00002 
00003 DSin::DSin(
00004     uint16_t const arg_resolution_ofsin
00005 ):
00006     ampl_max(10),
00007     freq_max(5000),
00008     resolution_ofsin(arg_resolution_ofsin)
00009 {
00010     init();
00011     setParam(0, 4000);
00012 }
00013 
00014 DSin::DSin(
00015     float const arg_ampl,
00016     uint16_t const arg_freq,
00017     uint16_t const arg_resolution_ofsin
00018 ): 
00019     ampl_max(10),
00020     freq_max(5000),
00021     resolution_ofsin(arg_resolution_ofsin)
00022 {
00023     init();
00024     setParam(arg_ampl, arg_freq);
00025 }
00026 
00027 void DSin::setParam(
00028     float const arg_ampl,
00029     uint16_t const arg_freq
00030 )
00031 {
00032     setAmplitude(arg_ampl);
00033     setFrequency(arg_freq);
00034 }
00035 
00036 void DSin::setAmplitude(
00037     float const arg_ampl
00038 )
00039 {
00040     m_ampl = arg_ampl;
00041     for(int i = 0; i < resolution_ofsin; i++){
00042         m_discretized_sin_p16m16[i] = static_cast<int32_t>(4095.0 / ampl_max * m_ampl * m_discretized_sin[i] );
00043     }
00044 }
00045 
00046 void DSin::setFrequency(
00047     uint16_t const arg_freq
00048 )
00049 {
00050     m_freq = arg_freq;
00051     m_pwth = 1000000 / m_freq / resolution_ofsin;
00052 }
00053 
00054 void DSin::init()
00055 {
00056     m_discretized_sin = new float[resolution_ofsin];
00057     m_discretized_sin_p16m16 = new int32_t[resolution_ofsin];
00058     
00059     for(int i = 0; i < resolution_ofsin; i++){
00060         m_discretized_sin[i] = sin( 2.0 * M_PI * static_cast<float>(i) / static_cast<float>(resolution_ofsin));
00061     }
00062 }
00063 
00064 float DSin::getValue()
00065 {
00066     static int itr = 0;
00067     return m_ampl * m_discretized_sin[itr++ % resolution_ofsin];
00068 }
00069 
00070 int32_t DSin::getValue_p16m16()
00071 {
00072     static int itr = 0;
00073     return m_discretized_sin_p16m16[itr++ % resolution_ofsin];
00074 }
00075 
00076 void DSin::getValueofSamplePoints(float arg_dsin[])
00077 {
00078     memcpy(arg_dsin, m_discretized_sin, sizeof(float) * resolution_ofsin);
00079 }