-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdl.c
66 lines (52 loc) · 1.67 KB
/
sdl.c
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
#include <SDL2/SDL.h>
#include <math.h>
#include <stdbool.h>
#include "context.h"
#include "timers.h"
#include "sound.h"
#define SAMPLE_RATE 44100
#define CYCLES_PER_SAMPLE (CLOCKSPEED * 1.0 / SAMPLE_RATE)
#define BUFFER_SIZE 4096
#define AMPLITUDE 127
#define FREQUENCY 440.0
int main() {
SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
const int waveform_cycles = 8; // one full waveform at max freq.
context_t ctx;
if (!context_init_minimal(&ctx)) {
printf("unhappy\n");
return 1;
}
if (!sound_init(&ctx.snd)) {
printf("Sound failed\n");
return 1;
}
// This is is what a ROM would do via CPU insns.
ctx.mem.sound.square1.period_lsb = 0x40;
ctx.mem.sound.square1.period_msb = 0x07;
ctx.mem.sound.square1.duty = 0b10;
// This is what the APU would do given a trigger.
ctx.snd.square1.divider = 0x740;
ctx.snd.square1.volume = 0b1111;
printf("Playing something\n");
double cycles = CYCLES_PER_SAMPLE;
uint8_t buffer[BUFFER_SIZE];
while (true) {
if (SDL_GetQueuedAudioSize(ctx.snd.device) >= SAMPLE_RATE/4) {
SDL_Delay(5);
continue;
}
for (unsigned int i = 0; i < sizeof(buffer); i++) {
sound_update(&ctx, cycles - fmod(cycles, 4));
buffer[i] = ctx.snd.square1.value;
cycles = CYCLES_PER_SAMPLE + fmod(cycles, 4);
}
if (SDL_QueueAudio(ctx.snd.device, buffer, sizeof(buffer)) != 0) {
fprintf(stderr, "Error queueing audio: %s\n", SDL_GetError());
break;
}
}
SDL_CloseAudioDevice(ctx.snd.device);
SDL_Quit();
return 0;
}