summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 90f3e9513d80ed7d410548cd307f2fe66a0928f9 (plain)
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
#include <SDL2/SDL.h>
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <vector>

const float AMPLITUDE = 0.2f;
float sequence[8] = { 130.81f, 220.f, 130.81f, 440.f, 330.f, 440.f, 130.81f, 261.63f };
float modSequence[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
float mulSequence[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };

class Knob {
public:
	Knob(int x, int y, float *valuePtr, float scaling = 1.f)
		: m_x(x), m_y(y), value(valuePtr), valueScaling(scaling) {}

	void Draw(SDL_Renderer*);
	bool InBounds(int x, int y);

	float *value;
	float valueScaling;
private:
	int m_x, m_y;
};

std::vector<Knob> knobs;

class Window {
public:
	Window();
private:
	bool m_isRunning{ false };
};

void Knob::Draw(SDL_Renderer *renderer)
{
	constexpr int Radius = 25;
	SDL_Rect rect = { m_x, m_y, Radius, Radius };
	SDL_RenderDrawRect(renderer, &rect);

	SDL_RenderDrawLine(renderer, m_x, m_y + (*value * valueScaling), m_x + Radius, m_y + (*value * valueScaling));
}

bool Knob::InBounds(int x, int y)
{
	constexpr int Radius = 25;
	SDL_Rect rect = { m_x, m_y, Radius, Radius };
	SDL_Point point = { x, y };

	return SDL_PointInRect(&point, &rect);
}

struct CallbackInfo {
	int samplesDone;
	int samplesPerStep;
	int currentStep;
	int sampleRate;
};

void AudioCallback(void *userData, Uint8 *rawBuffer, int bytes)
{
	float *buffer = (float*)rawBuffer;
	int length = bytes / sizeof(float);
	CallbackInfo *info = (CallbackInfo*)userData;

	for (int i = 0; i < length; i++, info->samplesDone++) {
		float time = (float)info->samplesDone / (float)info->sampleRate;
		float modWave = sinf(2.0f * M_PI * modSequence[info->currentStep] * time);
		float freq = sequence[info->currentStep] + modWave * mulSequence[info->currentStep] ;

		if (info->samplesDone >= info->samplesPerStep) {
			info->samplesDone = 0;
			info->currentStep = (info->currentStep + 1) % 8;
		}

		buffer[i] = (sinf(2.0f * M_PI * freq * time)) * AMPLITUDE;
	}
}

void DrawInterface(SDL_Renderer* renderer, int currentStep)
{
	SDL_Rect rect = { 
		(640 - 8 * (32 + 8)) / 2, 
		(320 - 32) / 2, 
		32, 
		32 
	};

	for (int i = 0; i < 8; i++) {
		SDL_SetRenderDrawColor(renderer, 128, i * (255 / 8), 255, 255);

		if (i == currentStep) {
			SDL_RenderFillRect(renderer, &rect);
		} else {
			SDL_RenderDrawRect(renderer, &rect);
		}

		rect.x += rect.w + 8;
	}

	for (Knob& knob : knobs) {
		knob.Draw(renderer);
	}
}

int main(int argc, char** argv)
{
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
		std::cerr << "Failed to initalize SDL! " << SDL_GetError() << std::endl;
		return 1;
	}

	SDL_Window* window = SDL_CreateWindow("fmseq", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 320, SDL_WINDOW_SHOWN);
	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	SDL_Event event;

	CallbackInfo info;

	SDL_AudioSpec desiredSpec;
	desiredSpec.freq = 44100;
	desiredSpec.format = AUDIO_F32SYS;
	desiredSpec.channels = 1;
	desiredSpec.samples = 2048;
	desiredSpec.callback = AudioCallback;
	desiredSpec.userdata = &info;

	SDL_AudioSpec obtainedSpec;
	if (SDL_OpenAudio(&desiredSpec, &obtainedSpec) < 0) {
		std::cerr << "Failed to initalize audio! " << SDL_GetError() << std::endl;
		return 1;
	}

	if (desiredSpec.format != obtainedSpec.format)
		std::cerr << "Different format: " << obtainedSpec.format << std::endl;

	info.samplesDone = 0;
	info.samplesPerStep = obtainedSpec.freq * 0.4f;
	info.currentStep = 0;
	info.sampleRate = obtainedSpec.freq;

	SDL_PauseAudio(0);

	knobs.emplace_back(10, 120, sequence, 0.1f);
	knobs.emplace_back(40, 120, sequence + 1, 0.1f);
	knobs.emplace_back(70, 120, sequence + 2, 0.1f);
	knobs.emplace_back(110, 120, sequence + 3, 0.1f);
	knobs.emplace_back(140, 120, sequence + 4, 0.1f);
	knobs.emplace_back(170, 120, sequence + 5, 0.1f);
	knobs.emplace_back(210, 120, sequence + 6, 0.1f);
	knobs.emplace_back(240, 120, sequence + 7, 0.1f);

	knobs.emplace_back(10, 180, modSequence);
	knobs.emplace_back(40, 180, modSequence + 1);
	knobs.emplace_back(70, 180, modSequence + 2);
	knobs.emplace_back(110, 180, modSequence + 3);
	knobs.emplace_back(140, 180, modSequence + 4);
	knobs.emplace_back(170, 180, modSequence + 5);
	knobs.emplace_back(210, 180, modSequence + 6);
	knobs.emplace_back(240, 180, modSequence + 7);

	knobs.emplace_back(10, 220, mulSequence, 3.0f);
	knobs.emplace_back(40, 220, mulSequence + 1, 3.0f);
	knobs.emplace_back(70, 220, mulSequence + 2, 3.0f);
	knobs.emplace_back(110, 220, mulSequence + 3, 3.0f);
	knobs.emplace_back(140, 220, mulSequence + 4, 3.0f);
	knobs.emplace_back(170, 220, mulSequence + 5, 3.0f);
	knobs.emplace_back(210, 220, mulSequence + 6, 3.0f);
	knobs.emplace_back(240, 220, mulSequence + 7, 3.0f);

	bool running = true;
	Knob* mouseKnob = nullptr;
	int yStart = 0;
	while (running) {
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
			case SDL_QUIT:
				running = false;
				break;
			case SDL_MOUSEBUTTONDOWN:
				for (Knob& knob : knobs) {
					if (knob.InBounds(event.button.x, event.button.y)) {
						mouseKnob = &knob;
						yStart = event.button.y;
						break;
					}
				}
				break;
			case SDL_MOUSEMOTION:
				if (mouseKnob) {
					*mouseKnob->value -= (yStart - event.button.y) / mouseKnob->valueScaling;
					yStart = event.button.y;
					if (*mouseKnob->value < 0) *mouseKnob->value = 0;
				}
				
				break;
			case SDL_MOUSEBUTTONUP:
				mouseKnob = nullptr;
				yStart = 0;
				break;
			}
		}
		
		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
		SDL_RenderClear(renderer);

		DrawInterface(renderer, info.currentStep);

		SDL_RenderPresent(renderer);
		SDL_UpdateWindowSurface(window);

		SDL_Delay(10);
	}

	SDL_PauseAudio(1);
	SDL_CloseAudio();

	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}