-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbw64_create_sine.cpp
36 lines (31 loc) · 1.03 KB
/
bw64_create_sine.cpp
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
#include <cmath>
#include <iostream>
#include <bw64/bw64.hpp>
const float DURATION = 10.f; // seconds
const float FREQUENCY = 440.f; // Hz
const float GAIN = 0.5f; // Hz
const uint32_t BLOCK_SIZE = 512;
const float PI = 3.14159265358979323846f;
using namespace bw64;
int main(int argc, char const* argv[]) {
if (argc != 2) {
std::cout << "usage: " << argv[0] << " [BW64_OUTPUT_FILE]" << std::endl;
exit(1);
}
auto bw64File = writeFile(argv[1]);
uint32_t frameDuration =
static_cast<uint32_t>(DURATION * bw64File->sampleRate());
float sineState = 0.f;
float normF =
static_cast<float>(2.f * PI * FREQUENCY / bw64File->sampleRate());
while (bw64File->framesWritten() < frameDuration) {
std::vector<float> buffer;
for (uint32_t i = 0; i < BLOCK_SIZE; ++i) {
sineState = static_cast<float>(std::fmod(sineState + normF, 2.f * PI));
auto value = static_cast<float>(std::sin(sineState) * GAIN);
buffer.push_back(value);
}
bw64File->write(&buffer.front(), BLOCK_SIZE);
}
return 0;
}