Size: 1053 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// cs/renderer/audio/pure_tone.h
#ifndef CS_RENDERER_AUDIO_PURE_TONE_H
#define CS_RENDERER_AUDIO_PURE_TONE_H

#include <stddef.h>

#include <vector>

#include "cs/renderer/audio/sinusodal.h"

namespace cs::audio {
// Represents one audio frequency, such as A 440 Hz
class PureTone {
 public:
  explicit PureTone(const float frequency)
      : frequency_hz_(frequency) {}

  // Writes the frequency to a new buffer
  std::vector<float> asBuffer(size_t length) const {
    std::vector<float> buffer;
    // Allocate the array first, more efficient
    buffer.assign(length, 0.f);
    for (size_t i = 0; i < length; i++) {
      // Phase is the duration into a sine wave as a
      // fraction of second
      float phase = static_cast<float>(i) /
                    static_cast<float>(length);
      // Copy in values based on a standard sine wave
      buffer[i] =
          cs::audio::sinusodal(frequency_hz_, phase);
    }
    return buffer;
  }

 private:
  float frequency_hz_;
};

}  // namespace cs::audio

#endif  // CS_RENDERER_AUDIO_PURE_TONE_H
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.