1 /*	file: ymplayer.cpp
2 
3 	This file is a part of STYMulator - GNU/Linux YM player
4 
5 	Player & ST-Sound GPL library GNU/Linux port by Grzegorz Tomasz Stanczyk
6 	Project Page: http://atariarea.krap.pl/stymulator
7 
8 	Original ST-Sound GPL library by Arnaud Carre (http://leonard.oxg.free.fr)
9 
10 -----------------------------------------------------------------------------
11  *   STYMulator is free software; you can redistribute it and/or modify    *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   STYMulator is distributed in the hope that it will be useful,         *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with STYMulator; if not, write to the                           *
23  *   Free Software Foundation, Inc.,                                       *
24  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25 ---------------------------------------------------------------------------*/
26 
27 #if defined(__x86_64__) || defined(__alpha__) || defined(__ia64__)\
28 	|| defined(__ppc64__) || defined(__s390x__)
29 #define PLATFORM 1
30 #else
31 #define PLATFORM 0
32 #endif
33 
34 #include "stsoundlib/StSoundLibrary.h"
35 #include "ui.h"
36 #include "sound.h"
37 #include <sys/soundcard.h>
38 #include <sys/ioctl.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 
46 extern int period_size;
47 bool digi;
48 
main(int argc,char ** argv)49 int main(int argc, char **argv)
50 {
51 bool quit = false;
52 bool repeat = false;
53 bool rmode = true;
54 bool stop = false;
55 bool pmode = true;
56 bool paused = false;
57 bool playing;
58 bool ff = false;
59 bool rew = false;
60 
61 const char *platform;
62 
63 	if (PLATFORM)
64 		platform = "64bit";
65 	else
66 		platform = "32bit";
67 
68 	if (argc != 2) {
69 		printf("STYMulator v0.21a (%s) - Copyright (C) 2005-2007 by Grzegorz Tomasz Stanczyk\n", platform);
70 		printf("http://atariarea.krap.pl/stymulator\n");
71 		printf("\nUsage: ymplayer <ym music file>\n\n");
72 		return -1;
73 	}
74 
75         int audio_fd;
76 
77 	int err;
78 	unsigned int buf;
79 
80 	char pcm_name[] = "/dev/dsp";
81 
82 	YMMUSIC *pMusic = ymMusicCreate();
83 
84 	if (ymMusicLoad(pMusic,argv[1])) {
85 		if ((audio_fd = open(pcm_name, O_WRONLY)) < 0) {
86 			printf("device open error: %s\n", strerror(errno));
87 			return 0;
88 		}
89 		if (oss_init(audio_fd) < 0) {
90 			printf("Setting of hwparams failed: %s\n", strerror(errno));
91 			exit(EXIT_FAILURE);
92 		}
93 
94 		buf = period_size;
95 		ymsample *convertBuffer = new ymsample[buf];
96 
97 		ymMusicInfo_t info;
98 		ymMusicGetInfo(pMusic,&info);
99 
100 		digi = strncmp(info.pSongType,"MIX1",4);
101 
102 		ui_init();
103 		draw_frame();
104 		draw_info(info, argv[1]);
105 
106 		while(!quit) {
107 			playing = ymMusicCompute(pMusic,convertBuffer, buf);
108 			if (digi)
109 				draw_time(ymMusicGetPos(pMusic) / 1000);
110 
111 			if (write(audio_fd, convertBuffer, buf*2) < 0) {
112                             perror("audio device not ready for data");
113                         }
114 
115 			switch(getch()) {
116 				case 27: case 'q':	quit = true; break;
117 				case 'z':	pmode = true;	break; //play
118 				case 'x':	paused = true;	break; //pause
119 				case 'c':	pmode = false;	break; //stop
120 				case 'r':	rmode = true;	break;
121 				case 'm':	ff = true; break;
122 				case 'n':	rew = true; break;
123 			}
124 			if (rmode)
125 			{
126 				if (repeat) {
127 					ymMusicSetLoopMode(pMusic,YMTRUE);
128 					mvaddstr(pos_y+7,pos_x+67,"Yes");
129 					rmode = false;
130 					repeat = false;
131 				} else {
132 					ymMusicSetLoopMode(pMusic,YMFALSE);
133 					mvaddstr(pos_y+7,pos_x+67,"No ");
134 					rmode = false;
135 					repeat = true;
136 				}
137 			}
138 			if (!stop && playing) {
139 				if (pmode && !paused)
140 					mvaddstr(pos_y+6,pos_x+67,"Play");	//play
141 				else if (pmode && paused) {
142 					ymMusicPause(pMusic);		//pause
143 					mvaddstr(pos_y+6,pos_x+67,"Pause");
144 					stop = true;
145 					pmode = false;
146 
147 				} else if (!pmode) {		//stop
148 					ymMusicStop(pMusic);
149 					mvaddstr(pos_y+6,pos_x+67,"Stop ");
150 					stop = true;
151 				}
152 			}  else if (pmode && playing) {
153 				ymMusicPlay(pMusic);
154 				mvaddstr(pos_y+6,pos_x+67,"Play ");
155 				stop = false;
156 				paused = false;
157 			}  else if (!playing) {
158 				ymMusicLoad(pMusic,argv[1]); // hmmm :/
159 				pmode = false;
160 			}
161 
162 			if (ymMusicIsSeekable(pMusic) && digi)
163 			{
164 				if (ff) {
165 					ymMusicSeek(pMusic, ymMusicGetPos(pMusic) + 1000);
166 					ff = false;
167 				} else if (rew) {
168 					ymMusicSeek(pMusic, ymMusicGetPos(pMusic) - 1000);
169 					rew = false;
170 				}
171 			}
172 		}
173 
174 		ymMusicStop(pMusic);
175 		close(audio_fd);
176 		delete convertBuffer;
177 		ui_end();
178 
179 	} else {
180 		printf("Error in loading file %s: %s\n", argv[1], ymMusicGetLastError(pMusic));
181 	}
182 
183 	ymMusicDestroy(pMusic);
184 
185 	return 0;
186 }
187