1 /* splayFP.c
2  * An example on how to use libmikmod to play a module from a FILE*
3  *
4  * (C) 2004, Raphael Assenat (raph@raphnet.net)
5  *
6  * This example is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRENTY; without event the implied warrenty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  */
10 
11 #include <stdio.h>
12 #include <mikmod.h>
13 
14 #if defined(_WIN32)
15 #define MikMod_Sleep(ns) Sleep(ns / 1000)
16 #elif defined(_MIKMOD_AMIGA)
17 void amiga_sysinit (void);
18 void amiga_usleep (unsigned long timeout);
19 #define MikMod_Sleep(ns) amiga_usleep(ns)
20 #else
21 #include <unistd.h>  /* for usleep() */
22 #define MikMod_Sleep(ns) usleep(ns)
23 #endif
24 
main(int argc,char ** argv)25 int main(int argc, char **argv)
26 {
27 	MODULE *module;
28 	FILE *fptr;
29 
30 	if (argc < 2) {
31 		fprintf(stderr, "Usage: ./splayFP file\n");
32 		return 1;
33 	}
34 
35 #ifdef _MIKMOD_AMIGA
36 	amiga_sysinit ();
37 #endif
38 
39 	/* initialize MikMod threads */
40 	MikMod_InitThreads ();
41 
42 	/* register all the drivers */
43 	MikMod_RegisterAllDrivers();
44 
45 	/* register all the module loaders */
46 	MikMod_RegisterAllLoaders();
47 
48 	/* init the library */
49 	md_mode |= DMODE_SOFT_MUSIC;
50 	if (MikMod_Init("")) {
51 		fprintf(stderr, "Could not initialize sound, reason: %s\n",
52 				MikMod_strerror(MikMod_errno));
53 		return 2;
54 	}
55 
56 	/* open the file */
57 	fptr = fopen(argv[1], "rb");
58 	if (fptr == NULL) {
59 		perror("fopen");
60 		MikMod_Exit();
61 		return 1;
62 	}
63 
64 	/* load module */
65 	module = Player_LoadFP(fptr, 64, 0);
66 	if (module) {
67 		/* start module */
68 		printf("Playing %s\n", module->songname);
69 		Player_Start(module);
70 
71 		while (Player_Active()) {
72 			MikMod_Sleep(10000);
73 			MikMod_Update();
74 		}
75 
76 		Player_Stop();
77 		Player_Free(module);
78 	} else
79 		fprintf(stderr, "Could not load module, reason: %s\n",
80 				MikMod_strerror(MikMod_errno));
81 
82 	fclose(fptr);
83 	MikMod_Exit();
84 
85 	return 0;
86 }
87 
88