-
Notifications
You must be signed in to change notification settings - Fork 5
/
unix.c
192 lines (180 loc) · 4.04 KB
/
unix.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
/*
* BB: The portable demo
*
* (C) 1997 by AA-group (e-mail: [email protected])
*
* 3rd August 1997
* version: 1.2 [final3]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public Licences as by published
* by the Free Software Foundation; either version 2; or (at your option)
* any later version
*
* This program is distributed in the hope that it will entertaining,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Publis License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/types.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "bb.h"
static int soundin;
static int soundout;
static int freq = 22050, stereo = 1, _16bit = 1;
static int freqs[14] =
{
5512, 6615, 8000, 9600, 11025, 16000, 18900,
22050, 27428, 32000, 33075, 37800, 44100, 48000
};
int bbsound = 0;
int soundcounter;
static int isplaying=0;
void stop()
{
if (bbsound) {
write(soundout, "T", 1);
isplaying=0;
}
}
int load_song(char *name)
{
finish_stuff=0;
if (bbsound) {
stop();
isplaying=1;
bbsound=1;
switch(name[2]) {
case '.':
write(soundout, "0", 1);
break;
case '2':
write(soundout, "1", 1);
break;
case '3':
write(soundout, "2", 1);
}
}
return 0;
}
static int start_sound(void)
{
pid_t pid;
int mypipe[2];
int mypipe2[2];
/* Create the pipe. */
if (pipe(mypipe) || pipe(mypipe2)) {
fprintf(stderr, "Pipe failed.\n");
return EXIT_FAILURE;
}
pid = fork();
if (pid == (pid_t) 0) { /* This is the child process. */
char str[256];
sprintf(str, "bb_snd_server %i %i %i bb.s3m bb2.s3m bb3.s3m", freq, stereo, _16bit);
close(mypipe[0]);
close(mypipe2[1]);
close(1);
dup(mypipe[1]);
close(0);
dup(mypipe2[0]);
fflush(stdout);
if(system(str)) {
sprintf(str, "./bb_snd_server %i %i %i bb.s3m bb2.s3m bb3.s3m", freq, stereo, _16bit);
system(str);
}
write(mypipe[1], "!", 1);
exit(0);
}
else if (pid > (pid_t) 0) { /* This is the parent process. */
close(mypipe[1]);
close(mypipe2[0]);
soundin = mypipe[0];
soundout = mypipe2[1];
bbsound = 1;
return EXIT_SUCCESS;
}
else { /* The fork failed. */
fprintf(stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
}
void play()
{
if (bbsound) {
write(soundout, "S", 1);
}
}
void wait_sound()
{
char ch;
read(soundin, &ch, 1);
if (ch == '!') {
bbsound = 0;
}
}
void update_sound()
{
fd_set readfds;
char ch;
struct timeval tv;
if (!bbsound)
return;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(soundin, &readfds);
while (select(soundin + 1, &readfds, NULL, NULL, &tv)) {
soundcounter++;
read(soundin, &ch, 1);
if (ch == '!') {
bbsound = 0;
break;
}
}
}
int main(int argc, char *argv[])
{
int retval;
int p=0;
char s[255];
bbinit(argc,argv);
aa_puts(context, 0, p++, AA_SPECIAL, "Music?[Y/n]");
aa_flush(context);
if (tolower(aa_getkey(context, 1)) != 'n') {
aa_puts(context, 0, p, AA_SPECIAL, "Sample rate");
do {
sprintf(s, "%i", freq);
aa_edit(context, 13, p++, 5, s, 6);
} while (sscanf(s, "%i", &freq) == 0 || freq < 8000 || freq > 100000);
{int i,minv=freqs[0];
for(i=0;i<14;i++)
if(abs(freq-minv)>abs(freq-freqs[i])) minv=freqs[i];
freq=minv;
}
aa_puts(context, 0, p++, AA_SPECIAL, "Stereo?[Y/n]");
aa_flush(context);
if (tolower(aa_getkey(context, 1)) == 'n')
stereo = 0;
aa_puts(context, 0, p++, AA_SPECIAL, "16 bit?[Y/n]");
aa_flush(context);
if (tolower(aa_getkey(context, 1)) == 'n')
_16bit = 0;
start_sound();
wait_sound();
}
retval = bb();
if (bbsound) {
write(soundout, "!", 1);
close(soundout);
close(soundin);
}
return retval;
}