-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.c
More file actions
188 lines (156 loc) · 4.36 KB
/
main.c
File metadata and controls
188 lines (156 loc) · 4.36 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
#include <pspuser.h>
#include <pspdebug.h>
#include <pspaudiolib.h>
#include <pspaudio.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
PSP_MODULE_INFO("audio", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
#define printf pspDebugScreenPrintf
/* Exit callback */
int exitCallback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
/* Callback thread */
int callbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", (void*) exitCallback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int setupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0);
if (thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
/* Main code */
const float PI = 3.1415926535897932f;
const int sampleRate = 44100;
float frequency = 440.0f;
float currentTime = 0;
int function = 0;
typedef struct {
short l, r;
} sample_t;
float currentFunction(const float time) {
double x;
float t = modf((time / (2 * PI)), &x);
switch(function) {
case 0: // SINE
return sinf(time);
case 1: // SQUARE
if (t < 0.5f) {
return -0.2f;
} else {
return 0.2f;
}
case 2: // TRIANGLE
if (t < 0.5f) {
return (t * 2.0f) - 0.5f;
} else {
return 0.5f - (t - 0.5f) * 2.0f;
}
default:
return 0.0f;
}
}
/* This function gets called by pspaudiolib every time the
audio buffer needs to be filled. The sample format is
16-bit, stereo. */
void audioCallback(void* buf, unsigned int length, void *userdata) {
const float sampleLength = 1.0f / sampleRate;
const float scaleFactor = SHRT_MAX - 1.0f;
static float freq0 = 440.0f;
sample_t* ubuf = (sample_t*) buf;
int i;
if (frequency != freq0) {
currentTime *= (freq0 / frequency);
}
for (i = 0; i < length; i++) {
short s = (short) (scaleFactor * currentFunction(2.0f * PI * frequency * currentTime));
ubuf[i].l = s;
ubuf[i].r = s;
currentTime += sampleLength;
}
if (currentTime * frequency > 1.0f) {
double d;
currentTime = modf(currentTime * frequency, &d) / frequency;
}
freq0 = frequency;
}
/* Read the analog stick and adjust the frequency */
void controlFrequency(void) {
static int oldButtons = 0;
const int zones[6] = {30, 70, 100, 112, 125, 130};
const float response[6] = {0.0f, 0.1f, 0.5f, 1.0f, 4.0f, 8.0f};
const float minFreq = 32.0f;
const float maxFreq = 7040.0f;
SceCtrlData pad;
float direction;
int changedButtons;
int i, v;
sceCtrlReadBufferPositive(&pad, 1);
v = pad.Ly - 128;
if (v < 0) {
direction = 1.0f;
v = -v;
} else {
direction = -1.0f;
}
for (i = 0; i < 6; i++) {
if (v < zones[i]) {
frequency += (response[i] * direction);
break;
}
}
if (frequency < minFreq) {
frequency = minFreq;
} else if (frequency > maxFreq) {
frequency = maxFreq;
}
changedButtons = pad.Buttons & (~oldButtons);
if (changedButtons & PSP_CTRL_CROSS) {
function++;
if (function > 2) {
function = 0;
}
}
oldButtons = pad.Buttons;
}
int main(void) {
pspDebugScreenInit();
setupCallbacks();
pspAudioInit();
pspAudioSetChannelCallback(0, audioCallback, NULL);
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
printf("Press up and down to select frequency\nPress X to change function\n");
while(1) {
sceDisplayWaitVblankStart();
pspDebugScreenSetXY(0,2);
printf("freq = %.2f \n", frequency);
switch(function) {
case 0:
printf("SINE WAVE. \n");
break;
case 1:
printf("SQUARE WAVE. \n");
break;
case 2:
printf("TRIANGLE WAVE. \n");
break;
}
controlFrequency();
}
return 0;
}