-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.c
253 lines (212 loc) · 6.84 KB
/
player.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "player.h"
#include <assert.h>
#include <errno.h>
#include <raylib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "music_theory.h"
#include "song.h"
void appendSongUnitArray(SongUnitArray *array, SongUnit unit) {
if (array->capacity == array->size) {
array->units =
realloc(array->units, array->capacity * 2 * sizeof(SongUnit));
assert(array->units != NULL && "Buy more RAM.");
array->capacity *= 2;
}
array->units[array->size] = unit;
array->size++;
}
void initSongUnitArray(SongUnitArray *array) {
array->units = calloc(SONG_ARRAY_SIZE, sizeof(SongUnit));
assert(array->units != NULL && "Buy more RAM.");
array->capacity = SONG_ARRAY_SIZE;
array->size = 0;
array->iterator = 0;
}
void deleteSongUnitArray(SongUnitArray *array) { free(array->units); }
// Returns -1 in failure.
int parseSongName(Player *player, char **lines, int lineNum) {
for (int i = 0; i < lineNum; i++) {
int len = strlen(lines[i]);
if (len > 0) {
player->songName = calloc(len + 1, sizeof(char));
strcpy(player->songName, lines[i]);
return i + 1;
}
}
return -1;
}
// Same.
int parseSongInfo(Player *player, char **lines, int lineNum, int i) {
for (; i < lineNum; i++) {
int len = strlen(lines[i]);
if (len > 0) {
if (sscanf(lines[i], "%f %d %f", &player->tune, &player->key,
&player->align) != 3) {
printf("Malformed file: info line does not parse correctly.\n");
return -1;
}
return i + 1;
}
}
return -1;
}
// Same.
int parseSongScale(Player *player, char **lines, int lineNum, int i) {
for (; i < lineNum; i++) {
int len = strlen(lines[i]);
if (len > 0) {
char *scaleSizeStr = strtok(lines[i], " ");
player->scale.size = atoi(scaleSizeStr);
if (player->scale.size == 0) {
printf("Malformed file: forbidden scale size at line %d: %s", i,
scaleSizeStr);
return -1;
}
player->scale.semitoneIndexes = calloc(player->scale.size, sizeof(int));
for (int j = 0; j < player->scale.size; j++) {
char *tok = strtok(NULL, " ");
if (tok == NULL) {
printf("Malformed file: not enough scale values (line %d).\n", i);
return -1;
}
player->scale.semitoneIndexes[j] = atoi(tok);
}
return i + 1;
}
}
return -1;
}
// Same.
int parseSongLines(Player *player, char **lines, int lineNum, int i) {
Channels channel = CHANNEL_PULSE;
// Add a single pause in the beggining to compensate the first frames.
SongUnit spareUnit = (SongUnit){1 * player->align, 0, 0, 0};
appendSongUnitArray(&player->pulseNotes, spareUnit);
appendSongUnitArray(&player->sinNotes, spareUnit);
int octave = 4;
float volume = 1;
for (; i < lineNum; i++) {
if (sscanf(lines[i], "pulse %d %f", &octave, &volume) == 2) {
channel = CHANNEL_PULSE;
} else if (sscanf(lines[i], "sin %d %f", &octave, &volume) == 2) {
channel = CHANNEL_SIN;
} else if (strlen(lines[i]) > 0) {
float time;
char *timestr = strtok(lines[i], " ");
time = atof(timestr);
char *tok = strtok(NULL, " ");
while (tok != NULL) {
SongUnit unit = (SongUnit){time * player->align, atoi(tok), octave, volume};
if (unit.note > player->scale.size) {
printf("Note bigger than scale at line %d: %d\n", i, unit.note);
return (errno = 1);
}
switch (channel) {
case CHANNEL_PULSE:
appendSongUnitArray(&player->pulseNotes, unit);
break;
case CHANNEL_SIN:
appendSongUnitArray(&player->sinNotes, unit);
break;
}
tok = strtok(NULL, " ");
}
}
}
return i + 1;
}
int parse(Player *player, char **lines, int lineNum) {
if (lineNum < 4) {
printf("Malformed file: less than 4 lines\n");
return (errno = 1);
}
int i = 0;
if ((i = parseSongName(player, lines, lineNum)) == -1) return (errno = 1);
if ((i = parseSongInfo(player, lines, lineNum, i)) == -1) return (errno = 1);
if ((i = parseSongScale(player, lines, lineNum, i)) == -1) return (errno = 1);
initSongUnitArray(&player->pulseNotes);
initSongUnitArray(&player->sinNotes);
parseSongLines(player, lines, lineNum, i);
return 0;
}
int initPlayer(Player *player, const char *file) {
FILE *fp = fopen(file, "r");
if (fp == NULL) return errno;
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
char *buffer = calloc(size, 1);
if (buffer == NULL) {
fclose(fp);
return errno;
}
fread(buffer, size, size, fp);
if (buffer[size - 1] != '\n') {
printf("Error with file format: does not end with an empty new line.\n");
fclose(fp);
return (errno = 1);
}
int lineNum = 0;
for (int i = 0; i < size; i++) {
if (buffer[i] == '\n') lineNum++;
}
char **lines = calloc(lineNum, 8);
int lineIdx = 0;
char *last = &(buffer[0]);
for (int i = 0; i < size; i++) {
if (buffer[i] == '\n') {
buffer[i] = '\0';
lines[lineIdx] = last;
last = &(buffer[i + 1]);
lineIdx++;
}
}
parse(player, lines, lineNum);
free(buffer);
fclose(fp);
return errno;
}
void deletePlayer(Player *player) {
free(player->songName);
free(player->scale.semitoneIndexes);
deleteSongUnitArray(&player->pulseNotes);
deleteSongUnitArray(&player->sinNotes);
}
void updateChannelWithArray(Player *player, SongUnitArray *array,
Channels channel, float deltaTime) {
array->units[array->iterator].time -= deltaTime;
if (array->units[array->iterator].time < 0) {
array->units[array->iterator + 1].time +=
array->units[array->iterator].time;
array->iterator++;
}
SongUnit unit = array->units[array->iterator];
toggleChannels(channel, unit.note != 0);
float frequency = num2frequency(player->tune, player->key, unit.octave,
unit.note, player->scale);
setChannelsFrequency(channel, frequency);
setChannelsVolume(channel, unit.volume);
}
bool update(Player *player) {
if (player->pulseNotes.iterator == player->pulseNotes.size &&
player->sinNotes.iterator == player->sinNotes.size)
return false;
float deltaTime = GetFrameTime();
if (player->pulseNotes.iterator < player->pulseNotes.size)
updateChannelWithArray(player, &player->pulseNotes, CHANNEL_PULSE,
deltaTime);
if (player->sinNotes.iterator < player->sinNotes.size)
updateChannelWithArray(player, &player->sinNotes, CHANNEL_SIN, deltaTime);
BeginDrawing();
ClearBackground(BLACK);
DrawFPS(0, 0);
DrawText(player->songName, 0, 30, 30, WHITE);
DrawText(TextFormat("Tune: %f", player->tune), 0, 60, 30, WHITE);
DrawText(TextFormat("Key: %d", player->key), 0, 90, 30, WHITE);
DrawText(TextFormat("Alignment: %f", player->align), 0, 150, 30, WHITE);
EndDrawing();
return true;
}