Debian Codegraph @

garglk/zbleep.cpp

raw symbols SCIP
1 #include <cmath>
2 #include <cstdint>
3 #include <fstream>
4 #include <string>
5 #include <vector>
6
7 #include "optional.hpp"
8
9 #include "garglk.h"
10
11 // C++20: std::numbers::pi
12 // M_PI is POSIX, but C++ can't request a POSIX environment via
13 // _XOPEN_SOURCE. Compilers on Unix systems seem to provide a POSIX
14 // environment regardless of "standard" C++ settings (e.g. "g++
15 // -std=c++14" will still expose POSIX functionality, whereas "gcc
16 // -std=c11" won't).
17 //
18 // But on Windows, if a standard C++ environment is requested, M_PI is
19 // _not_ defined. It requires _USE_MATH_DEFINES to be defined first.
20 //
21 // Rather than trying to coax POSIX out of various platforms, just
22 // define a suitable pi here. Maybe some day Gargoyle won't require
23 // POSIX at all.
24 static constexpr double pi = 3.14159265358979323846;
25
26 Bleeps::Bleeps()
27 {
28 update(1, 0.1, 1175);
29 update(2, 0.1, 440);
30 }
31
32 // 22050Hz, unsigned 8 bits per sample, mono
33 void Bleeps::update(int number, double duration, int frequency)
34 {
35 if (number != 1 && number != 2) {
36 return;
37 }
38
39 number--;
40
41 std::uint32_t samplerate = 22050;
42 std::size_t frames = duration * samplerate;
43
44 if (frames == 0) {
45 m_bleeps[number] = nonstd::nullopt;
46 return;
47 }
48
49 // Construct the WAV header
50 std::uint16_t pcm = 1;
51 std::uint16_t channels = 1;
52 std::uint16_t bytes = 1;
53 std::uint32_t byterate = samplerate * channels * bytes;
54 std::uint16_t blockalign = channels * bytes;
55 std::uint16_t bits_per_sample = 8 * bytes;
56 std::uint32_t datasize = frames * bytes * channels;
57 bool need_padding = datasize % 2 == 1;
58
59 std::vector<std::uint8_t> data;
60 data.reserve(datasize + 44 + (need_padding ? 1 : 0));
61
62 #define le16(n) \
63 static_cast<std::uint8_t>((static_cast<std::uint16_t>(n) >> 0) & 0xff), \
64 static_cast<std::uint8_t>((static_cast<std::uint16_t>(n) >> 8) & 0xff)
65
66 #define le32(n) \
67 static_cast<std::uint8_t>((static_cast<std::uint32_t>(n) >> 0) & 0xff), \
68 static_cast<std::uint8_t>((static_cast<std::uint32_t>(n) >> 8) & 0xff), \
69 static_cast<std::uint8_t>((static_cast<std::uint32_t>(n) >> 16) & 0xff), \
70 static_cast<std::uint8_t>((static_cast<std::uint32_t>(n) >> 24) & 0xff)
71
72 data = {
73 'R', 'I', 'F', 'F',
74 le32(datasize + 36 + (need_padding ? 1 : 0)),
75 'W', 'A', 'V', 'E',
76 'f', 'm', 't', ' ',
77 le32(16),
78 le16(pcm),
79 le16(channels),
80 le32(samplerate),
81 le32(byterate),
82 le16(blockalign),
83 le16(bits_per_sample),
84 'd', 'a', 't', 'a',
85 le32(datasize),
86 };
87
88 #undef le32
89 #undef le16
90
91 for (std::size_t i = 0; i < frames; i++) {
92 auto point = 1 + std::sin(frequency * 2 * pi * i / static_cast<double>(samplerate));
93 data.push_back(127 * point);
94 }
95
96 if (need_padding) {
97 data.push_back(0);
98 }
99
100 m_bleeps[number] = std::move(data);
101 }
102
103 void Bleeps::update(int number, const std::string &path)
104 {
105 if (number != 1 && number != 2) {
106 return;
107 }
108
109 number--;
110
111 std::ifstream f(path, std::ios::binary);
112
113 std::vector<std::uint8_t> data((std::istreambuf_iterator<char>(f)),
114 std::istreambuf_iterator<char>());
115
116 if (!f.fail()) {
117 m_bleeps[number] = std::move(data);
118 }
119 }
120
121 std::vector<std::uint8_t> &Bleeps::at(int number)
122 {
123 try {
124 return m_bleeps.at(number - 1).value();
125 } catch (const nonstd::bad_optional_access &) {
126 throw Empty();
127 }
128 }
129
130 Bleeps gli_bleeps;