-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
93 lines (77 loc) · 2.38 KB
/
main.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
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
#include <cstdint>
#include <SDL2/SDL.h>
void audio_callback(void* userdata, uint8_t* stream, int len)
{
uint64_t* samples_played = (uint64_t*)userdata;
float* fstream = (float*)(stream);
static const float volume = 0.2;
static const float frequency = 200.0;
for(int sid = 0; sid < (len / 8); ++sid)
{
double time = (*samples_played + sid) / 44100.0;
fstream[2 * sid + 0] = volume * sin(frequency * 2.0 * M_PI * time); /* L */
fstream[2 * sid + 1] = volume * sin(frequency * 2.0 * M_PI * time); /* R */
}
*samples_played += (len / 8);
}
int main(int argc, char* argv[])
{
uint64_t samples_played = 0;
if(SDL_Init(SDL_INIT_AUDIO) < 0)
{
fprintf(stderr, "Error initializing SDL. SDL_Error: %s\n", SDL_GetError());
return -1;
}
SDL_AudioSpec audio_spec_want, audio_spec;
SDL_memset(&audio_spec_want, 0, sizeof(audio_spec_want));
audio_spec_want.freq = 44100;
audio_spec_want.format = AUDIO_F32;
audio_spec_want.channels = 2;
audio_spec_want.samples = 512;
audio_spec_want.callback = audio_callback;
audio_spec_want.userdata = (void*)&samples_played;
SDL_AudioDeviceID audio_device_id = SDL_OpenAudioDevice(
NULL, 0,
&audio_spec_want, &audio_spec,
SDL_AUDIO_ALLOW_FORMAT_CHANGE
);
if(!audio_device_id)
{
fprintf(stderr, "Error creating SDL audio device. SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
int window_width = 600;
int window_height = 600;
SDL_Window* window;
{
window = SDL_CreateWindow(
"SDL Tone Generator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
window_width, window_height,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);
if(!window)
{
fprintf(stderr, "Error creating SDL window. SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
}
SDL_PauseAudioDevice(audio_device_id, 0);
bool running = true;
while(running)
{
// Process input
SDL_Event sdl_event;
while(SDL_PollEvent(&sdl_event) != 0)
{
if(sdl_event.type == SDL_QUIT)
running = false;
}
}
SDL_DestroyWindow(window);
SDL_CloseAudioDevice(audio_device_id);
SDL_Quit();
return 0;
}