-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.c
128 lines (113 loc) · 2.83 KB
/
main2.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
#include <stdio.h>
#include "SDL2/SDL.h"
#include "player.h"
#include "loaddata.h"
struct player player;
static void mySDL_AudioCallback(void*h, Uint8* stream, int len) {
player_gen(&player, (short *)stream, len >> 1);
}
//#include <emscripten.h>
int main(int argc, char **argv) {
const char *filename;
//const char *melobnk = "melodic.bnk";
//const char *drumbnk = "drum.bnk";
int argi = 1;
while (argi < argc && argv[argi][0] == '-') {
switch (argv[argi++][1]) {
/*
case 'm':
melobnk = argv[argi++];
break;
case 'd':
drumbnk = argv[argi++];
break;
*/
default:
fprintf(stderr, "unknown option: %s\n", argv[argi]);
return 1;
}
}
filename = argi < argc ? argv[argi] : "descent.hmp";
if(SDL_Init(SDL_INIT_AUDIO)<0) {
printf("SDL_Init failed");
return 1;
}
//atexit(SDL_Quit);
//printf("%d sndbuf_empty=%d\n", __LINE__, sndbuf_empty);
SDL_AudioSpec spec;
spec.freq = 48000;
spec.format = AUDIO_S16SYS;
spec.channels = 2;
spec.samples = 4096;
spec.callback = mySDL_AudioCallback;
if(SDL_OpenAudio(&spec, NULL) < 0) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return 1;
}
//short buf[1024];
int freq = spec.freq;
printf("freq %d\n", freq);
printf("start play %s\n", filename);
player_init(&player, freq);
if (player_play_file(&player, filename, 1)) {
fprintf(stderr, "play failed\n");
return 1;
}
SDL_PauseAudio(0);
#if 0
while (1) {
fflush(stdout);
emscripten_sleep(10);
// SDL_Delay(100);
//#ifdef __linux__
//player_gen(&player, buf, sizeof(buf) / sizeof(buf[0]));
//#endif
//SDL_Event event;
//if(!SDL_WaitEventTimeout(&event,0)) break;
//if (event.type == SDL_QUIT) break;
}
#endif
return 0;
}
int play(const char *filename) {
printf("play: %s\n", filename);
player_stop(&player);
if (player_play_file(&player, filename, 1)) {
fprintf(stderr, "play failed\n");
return 1;
}
return 0;
}
const char *getsng(void) {
//void *runner = NULL;
//int sng_size;
//return (const char *)loadurl(runner, "descent.sng", &sng_size);
char *buf;
const char *fn;
int idx = 0;
int bufsize = 64;
int bufpos = 0;
if (!(buf = (char *)malloc(bufsize)))
return NULL;
while ((fn = cf_entry(idx++))) {
char *p = strchr(fn, '.');
if (!p || strcmp(p, ".hmp"))
continue;
int l = strlen(fn);
if (bufpos + l + 2 > bufsize) {
int size = bufsize + (bufsize >> 1) + 16 + l;
char *nbuf = (char *)realloc(buf, size);
if (!nbuf) {
free(buf);
return NULL;
}
buf = nbuf;
bufsize = size;
}
memcpy(buf + bufpos, fn, l);
bufpos += l;
buf[bufpos++] = '\n';
}
buf[bufpos] = 0;
return buf;
}