-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
211 lines (166 loc) · 5.63 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <gme.h>
#include <emscripten/webaudio.h>
//#include <math.h> //for sin
Music_Emu* emu;
void* nsfdata;
long nsfdataSize=0;
int track;
//int limitlogs=0;
int buf_size;
short* buf;
void handle_error( const char* str );
//static void play_siren( long count, short* out )
// {
// static double a, a2;
// while ( count-- )
// *out++ = 0x2000 * sin( a += .1 + .05*sin( a2+=.00005 ) );
// }
//EMSCRIPTEN_KEEPALIVE
//int setupNSF(void* inputdata, long inputdataSize, int inputBufSize, int tracknum)
//{
// printf("setupNSF\n");
//
// nsfdata = inputdata;
//
// nsfdataSize = inputdataSize;
//
// buf_size = inputBufSize;
//
// int sample_rate = 44100;
//
// /* Open music file in new emulator */
// handle_error( gme_open_data( nsfdata, nsfdataSize, &emu, sample_rate ) );
//
// printf("nsfdataSize: %li \n", nsfdataSize);
//
// /* Start track */
// handle_error( gme_start_track( emu, tracknum ) );
//
//
// buf = malloc(sizeof(*buf) * buf_size); // https://stackoverflow.com/questions/4240331/c-initializing-a-global-array-in-a-function
// //printf("array buf entry number 3: %d\n", buf[3]);
//
// printf("voice count: %d\n", gme_voice_count( emu ));
//
// for (int i=0;i<10;++i){
// gme_play( emu, buf_size*2, buf ); // I think doing this decreases the frequency of it crackling on startup
// }
// handle_error( gme_start_track( emu, tracknum ) );
//
// printf("setupNSF done\n");
// return 0;
//}
EMSCRIPTEN_KEEPALIVE
short* playNSF() { // run in script processor node's on audio process event
/* Sample buffer */
gme_play( emu, buf_size*2, buf ); // I don't know why buf_size is multiplied by 2 here, but it only works when it's multiplied by 2. I figured this out from studying Chip Player JS.
//if (limitlogs<1) {
// short tempbuf [buf_size];
// size_t n = sizeof(tempbuf)/sizeof(tempbuf[0]);
// printf("total size of buf: %zu\n", sizeof(tempbuf));
// printf("number of elements in buf: %zu\n", n);
// printf("size of buf_size: %d\n", buf_size);
// printf("size of buf_size times 2: %d\n", buf_size*2);
// printf("size of one element in buf: %lu\n", sizeof(buf[1]));
// ++limitlogs;
//}
//play_siren(buf_size, buf);
/*if (limitlogs < 50) {
handle_error( gme_play( emu, buf_size, buf ) );
printf("array buf entry number 0: %d\n", buf[0]);
printf("array buf entry number 1: %d\n", buf[1]);
printf("array buf entry number 2: %d\n", buf[2]);
printf("array buf entry number 3: %d\n", buf[3]);
printf("array buf entry number 4: %d\n", buf[4]);
printf("array buf entry number 5: %d\n", buf[5]);
++limitlogs;
} else {
gme_play( emu, buf_size, buf );
}*/
// the c code cannot return an array to javascript, use runtime method getValue to access the buf array.
return buf; //buf is a pointer, c arrays are pointers.
}
EMSCRIPTEN_KEEPALIVE
int getTime() {
return gme_tell(emu);
}
EMSCRIPTEN_KEEPALIVE
int getIfTrackEnded() {
return gme_track_ended(emu);
}
EMSCRIPTEN_KEEPALIVE
int setupNSFstereo(void* inputdata, long inputdataSize, int inputBufSize, int tracknum)
{
printf("setupNSF\n");
nsfdata = inputdata;
nsfdataSize = inputdataSize;
buf_size = inputBufSize;
track = tracknum;
int sample_rate = 44100;
/* Open music file in new emulator */
handle_error( gme_open_data( nsfdata, nsfdataSize, &emu, sample_rate ) );
//gme_mute_voices( emu, -1 ); // mute all voices
gme_ignore_silence( emu, 1 ); // very important for recording separate tracks. 1 is true
printf("nsfdataSize: %li \n", nsfdataSize);
/* Start track */
handle_error( gme_start_track( emu, tracknum ) );
buf = malloc(sizeof(*buf) * buf_size); // https://stackoverflow.com/questions/4240331/c-initializing-a-global-array-in-a-function
//printf("array buf entry number 3: %d\n", buf[3]);
int totalvoices=gme_voice_count( emu );
printf("voice count: %d\n", totalvoices);
for (int i=0;i<totalvoices;++i) {
printf("voice name %d: %s\n", i, gme_voice_name( emu, i ));
}
gme_info_t* info;
gme_track_info( emu, &info, tracknum );
printf("info: song: %s, length: %d, intro_length: %d, loop_length: %d, play_length: %d, fade_length: %d\n", info->song, info->length, info->intro_length, info->loop_length, info->play_length, info->fade_length);
handle_error( gme_start_track( emu, tracknum ) );
printf("setupNSF done\n");
return info->length;
}
EMSCRIPTEN_KEEPALIVE
int getTotalVoices() {
return gme_voice_count( emu ); // making a tiny function like this is easier than figuring out how to return length and total voices in the setupNSFstereo function.
}
EMSCRIPTEN_KEEPALIVE
const char* getVoiceName(int voicenum) {
return gme_voice_name(emu, voicenum);
}
//EMSCRIPTEN_KEEPALIVE
//int setIgnoreSilence(intbool) {
// gme_ignore_silence( emu, intbool );
// return 0;
//}
EMSCRIPTEN_KEEPALIVE
int setVoiceForRecording(int voicenum)
{
printf("setVoiceForRecording with voicenum: %d, name: %s\n", voicenum, gme_voice_name( emu, voicenum ));
if (gme_tell(emu)>0) {
//gme_start_track(emu, track); // restart track from beginning. doesn't work
printf("Time is greater than 0. Seeking to start...\n");
gme_seek( emu, 0 );
printf("Seek to start finished, new time: %d\n", gme_tell(emu));
}
gme_mute_voices( emu, -1 ); // mute all voices
gme_mute_voice( emu, voicenum, 0 ); // unmute
printf("setVoiceForRecording done\n");
return 0;
}
EMSCRIPTEN_KEEPALIVE
int unmuteVoice(int voicenum)
{
printf("unmuteVoice with voicenum: %d, name: %s\n", voicenum, gme_voice_name( emu, voicenum ));
gme_mute_voice( emu, voicenum, 0 ); // unmute
printf("unmuteVoice done\n");
return 0;
}
void handle_error( const char* str )
{
if ( str )
{
printf( "Error: %s\n", str ); //getchar();
//exit( EXIT_FAILURE );
}
}