MCPcopy Create free account
hub / github.com/SmingHub/Sming / generate

Method generate

samples/Basic_Audio/app/application.cpp:50–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 {

Callers 1

initialiseI2SFunction · 0.45

Calls 2

resetMethod · 0.45
getValueMethod · 0.45

Tested by

no test coverage detected