-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemory_Frequency_Generator.cpp
More file actions
241 lines (205 loc) · 7.81 KB
/
Memory_Frequency_Generator.cpp
File metadata and controls
241 lines (205 loc) · 7.81 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
Memory Frequency Generator by Anthro Teacher (AnthroHeart)
Version 1.5
Created: 4/26/2024
*/
#include <iostream>
#include <cmath>
#include <vector>
#include <thread>
#include <atomic>
#include <complex>
#include <fstream>
#include <cstdint>
#include <algorithm>
#include <cctype>
#include <regex>
const double PI = 3.14159265358979323846;
const std::string VERSION = "v1.5";
void writeWAVFileChunk(const std::string &filename, const std::vector<double> &waveformChunk, double sampleRate, int numChannels, int bitsPerSample)
{
std::ofstream file(filename, std::ios::binary);
if (!file)
{
std::cerr << "Error: Unable to open file for writing." << std::endl;
return;
}
// Write WAV header
file.write("RIFF", 4);
uint32_t fileSize = 44 + waveformChunk.size() * (bitsPerSample / 8);
file.write(reinterpret_cast<const char *>(&fileSize), 4);
file.write("WAVE", 4);
file.write("fmt ", 4);
uint32_t fmtChunkSize = 16;
file.write(reinterpret_cast<const char *>(&fmtChunkSize), 4);
uint16_t audioFormat = 3; // IEEE Float
file.write(reinterpret_cast<const char *>(&audioFormat), 2);
uint16_t numChannelsUint16 = static_cast<uint16_t>(numChannels);
file.write(reinterpret_cast<const char *>(&numChannelsUint16), 2);
uint32_t sampleRateUint32 = static_cast<uint32_t>(sampleRate);
file.write(reinterpret_cast<const char *>(&sampleRateUint32), 4);
uint32_t byteRate = sampleRateUint32 * numChannelsUint16 * (bitsPerSample / 8);
file.write(reinterpret_cast<const char *>(&byteRate), 4);
uint16_t blockAlign = numChannelsUint16 * (bitsPerSample / 8);
file.write(reinterpret_cast<const char *>(&blockAlign), 2);
uint16_t bitsPerSampleUint16 = static_cast<uint16_t>(bitsPerSample);
file.write(reinterpret_cast<const char *>(&bitsPerSampleUint16), 2);
file.write("data", 4);
uint32_t dataChunkSize = waveformChunk.size() * (bitsPerSample / 8);
file.write(reinterpret_cast<const char *>(&dataChunkSize), 4);
// Write waveform data
for (double sample : waveformChunk)
{
float sampleFloat = static_cast<float>(sample);
file.write(reinterpret_cast<const char *>(&sampleFloat), sizeof(float));
}
}
void generateSquareWave(std::vector<double> &waveform, double frequency, double sampleRate, int numSamples)
{
std::vector<double> squareWave(numSamples);
double timeStep = 1.0 / sampleRate;
double period = 1.0 / frequency;
double dutyCycle = 0.5; // 50% duty cycle (square wave)
for (int i = 0; i < numSamples; i++)
{
double time = i * timeStep;
double value = std::fmod(time, period) < dutyCycle * period ? 1.0 : -1.0;
waveform[i] = value * 0.9; // scale to [-0.9, 0.9]
}
}
void generateTriangleWave(std::vector<double> &waveform, double frequency, double sampleRate, int numSamples)
{
double timeStep = 1.0 / sampleRate;
double period = 1.0 / frequency;
double dbToLinear = std::pow(10.0, -8.627 / 20.0); // convert dB to linear scale
// calculate the mean value of the triangle wave
double sum = 0.0;
for (int i = 0; i < numSamples; i++)
{
double time = i * timeStep;
double value = std::fmod(time, period);
value = 2.0 * (value / period) - 1.0; // scale to [-1, 1]
value = 4.0 * std::abs(value) - 1.0; // triangle wave
sum += dbToLinear * 0.9 * value;
}
double meanValue = sum / numSamples;
// generate the triangle wave with the mean value subtracted
for (int i = 0; i < numSamples; i++)
{
double time = i * timeStep;
double value = std::fmod(time, period);
value = 2.0 * (value / period) - 1.0; // scale to [-1, 1]
value = 4.0 * std::abs(value) - 1.0; // triangle wave
waveform[i] = dbToLinear * 0.9 * value - meanValue;
}
}
// Generate a sine wave
void generateSineWave(std::vector<double> &waveform, double frequency, double sampleRate, int numSamples)
{
double angularFrequency = 2 * PI * frequency;
for (int i = 0; i < numSamples; ++i)
{
double t = i / sampleRate;
double value = std::sin(angularFrequency * t) * 0.9;
waveform[i] = value;
}
}
int computeSampleRate(double frequency)
{
int multiplier = 1;
int sampleRate = frequency;
while (sampleRate <= 96000 && sampleRate == static_cast<int>(frequency * multiplier))
{
multiplier *= 2;
sampleRate = frequency * multiplier;
}
if (sampleRate > 96000)
{
sampleRate /= 2;
}
return sampleRate;
}
void generateWaveform(std::vector<double> &waveform, double frequency, double sampleRate, int bufferSize, std::atomic<bool> &quit, std::string waveformType)
{
int index = 0;
// Convert waveform type to uppercase
std::transform(waveformType.begin(), waveformType.end(), waveformType.begin(), ::toupper);
while (!quit)
{
if (waveformType == "SQUARE")
{
generateSquareWave(waveform, frequency, sampleRate, bufferSize);
}
else if (waveformType == "TRIANGLE")
{
generateTriangleWave(waveform, frequency, sampleRate, bufferSize);
}
else
{
generateSineWave(waveform, frequency, sampleRate, bufferSize);
}
index = (index + bufferSize) % waveform.size();
}
}
int main()
{
std::cout << "Memory Frequency Generator " << VERSION << std::endl;
std::cout << "by Anthro Teacher (AnthroHeart)" << std::endl
<< std::endl;
std::string waveformType;
std::string strFrequency;
double frequency;
int bufferSize = 1024 * 1024; // Buffer size for generating waveform
int numGenerators = 1; // Default number of generators
std::cout << "Waveform (Sine, Square, Triangle) [Default: Sine]: ";
std::getline(std::cin, waveformType);
if (waveformType == "") {
waveformType = "Sine";
}
while (strFrequency == "") {
std::cout << "Frequency (Hz): ";
std::cin >> strFrequency;
std::cout << std::flush;
}
// Remove all but period and numbers from frequency
std::regex rgx("[^0-9.]");
std::string frequencyString = std::regex_replace(strFrequency, rgx, "");
frequency = std::stod(frequencyString);
std::cout << "Number of Generators (1-255) [Default 1]: ";
std::cin >> numGenerators;
//if numGenerators is not a number, set to 1
if (!std::cin) {
numGenerators = 1;
} else {
numGenerators = std::min(std::max(numGenerators, 1), 255);
}
double sampleRate = computeSampleRate(frequency);
std::vector<std::vector<double>> waveforms(numGenerators, std::vector<double>(bufferSize));
std::vector<std::thread> threads;
std::atomic<bool> quit(false);
std::cout << "Loading Waveforms.." << std::endl;
for (int i = 0; i < numGenerators; ++i)
{
threads.emplace_back(generateWaveform, std::ref(waveforms[i]), frequency, sampleRate, bufferSize, std::ref(quit), waveformType);
}
std::cout << "Waveforms are being repeated in memory. Press 'q' and Enter to quit." << std::endl;
char input;
while (std::cin >> input)
{
if (input == 'q' || input == 'Q')
{
quit = true;
break;
}
}
for (auto &thread : threads)
{
thread.join();
}
std::cout << "Waveform generation stopped." << std::endl;
// Write the waveform from the first generator to the WAV file
std::string fileName = strFrequency + "Hz_" + waveformType + ".wav";
writeWAVFileChunk(fileName, waveforms[0], sampleRate, 1, 32);
std::cout << "Waveform written to " << fileName << std::endl;
return 0;
}