| 48 | unsigned readPos = 0; |
| 49 | |
| 50 | bool generate(float sampleRate, float frequency) |
| 51 | { |
| 52 | samples.reset(); |
| 53 | readPos = 0; |
| 54 | |
| 55 | sampleCount = round(sampleRate / frequency); |
| 56 | // Modify frequency to fit in exact number of samples |
| 57 | frequency = sampleRate / sampleCount; |
| 58 | |
| 59 | Serial << _F("Generating sine wave table @ ") << frequency << _F(" Hz, ") << sampleCount << _F(" samples") |
| 60 | << endl; |
| 61 | |
| 62 | samples = std::make_unique<uint16_t[]>(sampleCount); |
| 63 | if(!samples) { |
| 64 | debug_e("Memory allocation failed"); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | SignalGenerator gen(eST_Sine, frequency); |
| 69 | for(unsigned i = 0; i < sampleCount; ++i) { |
| 70 | float t = i / (sampleCount * frequency); |
| 71 | float value = gen.getValue(t); |
| 72 | samples[i] = round(value * 32767); |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | uint16_t read() |
| 79 | { |
no test coverage detected