Skip to content

Commit 151bc1a

Browse files
committed
first commit
1 parent 9c95e54 commit 151bc1a

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

bad_apple.mp3

8.36 MB
Binary file not shown.

bad_apple.txt.gz

1.45 MB
Binary file not shown.

player.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#define DATAFILE "bad_apple.txt"
5+
#define MUSICFILE "bad_apple.mp3"
6+
#define MSTODELAY 30000
7+
#define BUFLEN 10000
8+
#define CHARPERLINE 161
9+
#define LINEPERFRAME 61
10+
int main(void) {
11+
FILE *fp;
12+
char buffer[BUFLEN] = { 0 };
13+
if ((fp = fopen(DATAFILE, "r")) == 0) {
14+
return 1;
15+
}
16+
pid_t pid=fork();
17+
if(pid==0)
18+
{
19+
execlp("mpg123", "mpg123", "-q", MUSICFILE, (char *)0);
20+
}
21+
while (1) {
22+
for (int i = 0; i < LINEPERFRAME; i++) {
23+
if (fgets(buffer + i * CHARPERLINE, CHARPERLINE + 1, fp) == NULL) {
24+
fclose(fp);
25+
exit(0);
26+
}
27+
}
28+
printf("%s", buffer);
29+
buffer[0] = 0;
30+
usleep(MSTODELAY);
31+
}
32+
}

player2.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#define DATAFILE "bad_apple.txt"
6+
#define MUSICFILE "bad_apple.mp3"
7+
#define MSTODELAY 30000
8+
#define BUFLEN 10000
9+
#define CHARPERFRAME 9662
10+
int main(void) {
11+
int fd;
12+
char buffer[BUFLEN]={0};
13+
if ((fd = open(DATAFILE,O_RDONLY))==-1)
14+
{
15+
return 1;
16+
}
17+
pid_t pid=fork();
18+
if(pid==0)
19+
{
20+
execlp("mpg123", "mpg123", "-q", MUSICFILE, (char *)0);
21+
}
22+
while (1)
23+
{
24+
if(read(fd,buffer,CHARPERFRAME)!=CHARPERFRAME)
25+
{
26+
close(fd);
27+
return 0;
28+
}
29+
printf("%s",buffer);
30+
usleep(MSTODELAY);
31+
}
32+
}

readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Bad_Apple(ascii) player in C.
2+
3+
Requirements
4+
gzip to uncompress data
5+
mpg123 to play mp3 file
6+
cc to compile
7+
8+
You may need to change MSTODELAY in player(2).c to make the time per picture more accurate.
9+
Frame per second:30.(delay should be a little less than 33333ms)
10+
Best wishes for you.

0 commit comments

Comments
 (0)