-
Notifications
You must be signed in to change notification settings - Fork 0
/
apu.cpp
72 lines (46 loc) · 1.23 KB
/
apu.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
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <queue>
#include <cmath>
const int AMPLITUDE = 28000;
const int FREQUENCY = 44100;
class APU {
public:
}
int queue_audio(int deviceId) {
Uint32 length = 44100; // should be 1 second
Uint8* buffer = (Uint8*) malloc(length); // 1 second of audio?
int flag = 1;
for (int i = 0; i < length; i++) {
if (i % 100 == 0) {
flag = -flag;
}
buffer[i] = flag * 10000;
}
int success = SDL_QueueAudio(deviceId, buffer, length);
return success;
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec desiredSpec;
desiredSpec.freq = FREQUENCY;
desiredSpec.format = AUDIO_S16SYS;
desiredSpec.channels = 1;
desiredSpec.samples = 2048;
desiredSpec.callback = NULL;
desiredSpec.userdata = NULL;
SDL_AudioSpec obtainedSpec;
int duration = 1000;
double Hz = 440;
// start play audio
SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &desiredSpec, &obtainedSpec, 0);
int success = queue_audio(deviceId);
printf("%d", success);
SDL_PauseAudioDevice(deviceId, 0);
SDL_Delay(3000);
SDL_CloseAudioDevice(deviceId);
SDL_Quit();
return 0;
return 0;
}