1 /*
2  * music_client.c  music client ��
3  *
4  * Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
5  *               1998-                           <masaki-c@is.aist-nara.ac.jp>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21 */
22 /* $Id: music_client.c,v 1.18 2004/10/31 04:18:06 chikama Exp $ */
23 
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <sys/un.h>
30 #include <glib.h>
31 
32 #include "portab.h"
33 #include "music.h"
34 #include "music_client.h"
35 #include "counter.h"
36 #include "pcmlib.h"
37 #include "nact.h"
38 
39 /* ���줾��� sub system �����Ѳ�ǽ���ɤ��� */
40 static boolean cdrom_available;
41 static boolean midi_available;
42 static boolean audio_available;
43 
connect_to_server()44 static int connect_to_server() {
45 	int fd;
46 	struct sockaddr_un saddr;
47 
48 	saddr.sun_family = AF_UNIX;
49 	strcpy(saddr.sun_path, nact->tmpdir);
50 	strcat(saddr.sun_path, XSYS35MUSSOCK);
51 	if (-1 != (fd = socket(AF_UNIX, SOCK_STREAM, 0))) {
52                 if (-1 != connect(fd, (struct sockaddr *)&saddr, sizeof(saddr))) {
53                         return fd;
54 		} else {
55 			perror("connect");
56 		}
57 	} else {
58 		perror("socket");
59 	}
60 	close(fd);
61 
62 	return -1;
63 }
64 
cl_read_packet(int fd,ServerPktHeader * pkt_hdr)65 static void *cl_read_packet(int fd, ServerPktHeader *pkt_hdr) {
66 	void *data = NULL;
67 
68 	if (sizeof(ServerPktHeader) == read(fd, pkt_hdr, sizeof (ServerPktHeader))) {
69                 if (pkt_hdr->data_length) {
70                         data = g_malloc0(pkt_hdr->data_length);
71                         read(fd, data, pkt_hdr->data_length);
72                 }
73         }
74 
75         return data;
76 }
77 
cl_read_ack(int fd)78 static void cl_read_ack(int fd) {
79         void *data;
80 	ServerPktHeader pkt_hdr;
81 
82 	data = cl_read_packet(fd, &pkt_hdr);
83 	if (data) {
84 		g_free(data);
85 	}
86 }
87 
cl_send_packet(int fd,int command,void * data,int data_length)88 static void cl_send_packet(int fd, int command, void * data, int data_length) {
89 	ClientPktHeader pkt_hdr;
90 
91 	pkt_hdr.version = XSYS35_PROTOCOL_VERSION;
92 	pkt_hdr.command = command;
93 	pkt_hdr.data_length = data_length;
94 
95 	write(fd, &pkt_hdr, sizeof(ClientPktHeader));
96 
97         if (data_length && data) {
98                 write(fd, data, data_length);
99 	}
100 }
101 
cl_send_guint32(int cmd,guint32 val)102 static void cl_send_guint32(int cmd, guint32 val) {
103 	int fd;
104 
105 	if (-1 == (fd = connect_to_server())) {
106 		return;
107 	}
108 
109 	cl_send_packet(fd, cmd, &val, sizeof(guint32));
110 	cl_read_ack(fd);
111 	close(fd);
112 }
113 
cl_send_boolean(int cmd,boolean val)114 static void cl_send_boolean(int cmd, boolean val) {
115 	int fd;
116 
117 	if (-1 == (fd = connect_to_server())) {
118 		return;
119 	}
120 
121 	cl_send_packet(fd, cmd, &val, sizeof(boolean));
122 	cl_read_ack(fd);
123 	close(fd);
124 }
125 
cl_send_cmd(int cmd)126 static void cl_send_cmd(int cmd) {
127 	int fd;
128 
129 	if (-1 == (fd = connect_to_server())) {
130 		return;
131 	}
132 
133 	cl_send_packet(fd, cmd, NULL, 0);
134 	cl_read_ack(fd);
135 	close(fd);
136 }
137 
cl_get_boolean(int cmd)138 static boolean cl_get_boolean(int cmd) {
139         ServerPktHeader pkt_hdr;
140 	boolean ret = FALSE;
141         void *data;
142         int fd;
143 
144         if (-1 == (fd = connect_to_server())) {
145                 return ret;
146 	}
147 
148         cl_send_packet(fd, cmd, NULL, 0);
149         data = cl_read_packet(fd, &pkt_hdr);
150         if (data) {
151                 ret = *((boolean *) data);
152                 g_free(data);
153         }
154         cl_read_ack(fd);
155         close(fd);
156 
157         return ret;
158 }
159 
cl_get_guint32(int cmd)160 static int cl_get_guint32(int cmd) {
161         ServerPktHeader pkt_hdr;
162         void *data;
163         int fd, ret = 0;
164 
165         if (-1 == (fd = connect_to_server())) {
166                 return ret;
167 	}
168 
169         cl_send_packet(fd, cmd, NULL, 0);
170         data = cl_read_packet(fd, &pkt_hdr);
171         if (data) {
172                 ret = *((int *) data);
173                 g_free(data);
174         }
175         cl_read_ack(fd);
176         close(fd);
177 
178         return ret;
179 }
180 
musclient_init()181 int musclient_init() {
182 	ServerPktHeader pkt_hdr;
183 	int fd;
184 	void *data;
185 	int i;
186 
187 	cdrom_available = FALSE;
188 	midi_available  = FALSE;
189 	audio_available = FALSE;
190 
191 	usleep(100*1000); // initial wait
192 
193 	for (i = 0; i < 10; i++) { // retry 10 times
194 		if (-1 != (fd = connect_to_server())) {
195 			break;
196 		}
197 		usleep(1000*1000); // The cdrom retry takes many clock.
198 	}
199 
200 	cl_send_packet(fd, MUS_GET_VALIDSUBSYSTEM, NULL, 0);
201 	data = cl_read_packet(fd, &pkt_hdr);
202 	if (data) {
203 		cdrom_available = ((ValidSubsystem *)data)->cdrom;
204 		midi_available  = ((ValidSubsystem *)data)->midi;
205 		audio_available = ((ValidSubsystem *)data)->pcm;
206 		g_free(data);
207 	}
208 	cl_read_ack(fd);
209 	close(fd);
210 
211 	return OK;
212 }
213 
musclient_exit()214 int musclient_exit() {
215 	cl_send_cmd(MUS_EXIT);
216 	return OK;
217 }
218 
219 /*
220  * cdrom �α��ճ���
221  *   track: �ȥ�å��ֹ� (���ȥ�å��� 1)
222  *   loop : �����֤���� (0�ξ���̵��)
223  */
mus_cdrom_start(int track,int loop)224 int mus_cdrom_start(int track, int loop) {
225 	int v[2];
226 	int fd;
227 
228 	if (!cdrom_available) return NG;
229 
230         if (-1 == (fd = connect_to_server())) {
231 		puts("fail to connect");
232                 return NG;
233 	}
234 
235 	v[0] = track;
236 	v[1] = loop;
237 	cl_send_packet(fd, MUS_CDROM_START, v, 2 * sizeof(int));
238 	cl_read_ack(fd);
239 	close(fd);
240 
241 	return OK;
242 }
243 
244 /*
245  * cdrom �α������
246  */
mus_cdrom_stop()247 int mus_cdrom_stop() {
248 	if (!cdrom_available) return NG;
249 
250 	cl_send_cmd(MUS_CDROM_STOP);
251 	return OK;
252 }
253 
254 /*
255  * cdrom �α��վ��֤μ���
256  *   info: ���ջ���(track/min/sec/frame)�ξ��֤��Ǽ������
257  *         ��ߤ��Ƥ������ 999/999/999/999 ���֤�
258  */
mus_cdrom_get_playposition(cd_time * tm)259 int mus_cdrom_get_playposition(cd_time *tm) {
260 	ServerPktHeader pkt_hdr;
261 	int fd;
262 	void *data;
263 
264 	if (!cdrom_available) return NG;
265 
266 	if (-1 == (fd = connect_to_server())) {
267 		tm->t = tm->m = tm->s = tm->f = 999;
268 		puts("fail to connect");
269 		return NG;
270 	}
271 
272 	cl_send_packet(fd, MUS_CDROM_GETPOSITION, NULL, 0);
273 	data = cl_read_packet(fd, &pkt_hdr);
274 	if (data) {
275 		*tm = *((cd_time *)data);
276 		g_free(data);
277 	}
278 	cl_read_ack(fd);
279 	close(fd);
280 	return OK;
281 }
282 
283 /*
284  * cdrom �κ���ȥ�å����μ���
285  *
286  */
mus_cdrom_get_maxtrack()287 int mus_cdrom_get_maxtrack() {
288 	int trk;
289 
290 	if (!cdrom_available) return 0;
291 
292 	trk = cl_get_guint32(MUS_CDROM_GETMAXTRACK);
293 
294 	return trk;
295 }
296 
297 /*
298  * CDROM ��ͭ��/̵�� �ե饰�μ���
299  *   return: FALASE -> ̵��
300  *           TRUE   -> ͭ��
301  */
mus_cdrom_get_state()302 boolean mus_cdrom_get_state() {
303 	return cdrom_available;
304 }
305 
306 /*
307  * midi �α��ճ���
308  *   no  : �ե������ֹ�( no >= 1)
309  *   loop: �����֤���� (0�ξ���̵��)
310  */
mus_midi_start(int no,int loop)311 int mus_midi_start(int no, int loop) {
312 	int v[2];
313 	int fd;
314 
315 	if (!midi_available) return NG;
316 
317         if (-1 == (fd = connect_to_server())) {
318 		puts("fail to connect");
319                 return NG;
320 	}
321 
322 	v[0] = no;
323 	v[1] = loop;
324 	cl_send_packet(fd, MUS_MIDI_START, v, 2 * sizeof(int));
325 	cl_read_ack(fd);
326 	close(fd);
327 	return OK;
328 }
329 
330 /*
331  * midi �α������
332  */
mus_midi_stop(void)333 int mus_midi_stop(void) {
334 	if (!midi_available) return NG;
335 
336 	cl_send_cmd(MUS_MIDI_STOP);
337 	return OK;
338 }
339 
340 /*
341  * midi �ΰ�����
342  */
mus_midi_pause(void)343 int mus_midi_pause(void) {
344 	if (!midi_available) return NG;
345 
346 	cl_send_cmd(MUS_MIDI_PAUSE);
347 	return OK;
348 }
349 
350 /*
351  * midi �ΰ����߲��
352  */
mus_midi_unpause(void)353 int mus_midi_unpause(void) {
354 	if (!midi_available) return NG;
355 
356 	cl_send_cmd(MUS_MIDI_UNPAUSE);
357 	return OK;
358 }
359 
360 /*
361  * midi �α��վ��֤μ���
362  *  state: ���ջ��֤��ֹ�ξ��֤��Ǽ������
363  *         ��ߤ��Ƥ������ 0 ������
364  */
mus_midi_get_playposition(midiplaystate * state)365 int mus_midi_get_playposition(midiplaystate *state) {
366 	ServerPktHeader pkt_hdr;
367 	int fd;
368 	void *data;
369 
370 	if (!midi_available) return NG;
371 
372 	if (-1 == (fd = connect_to_server())) {
373 		state->loc_ms = state->play_no = 0;
374 		puts("fail to connect");
375 		return NG;
376 	}
377 
378 	cl_send_packet(fd, MUS_MIDI_GETPOSITION, NULL, 0);
379 	data = cl_read_packet(fd, &pkt_hdr);
380 	if (data) {
381 		*state = *(midiplaystate *)data;
382 		g_free(data);
383 	}
384 	cl_read_ack(fd);
385 	close(fd);
386 	return OK;
387 }
388 
389 /*
390  * midi �α��� flag/variable �ξ��֤����ꤹ��
391  *   mode : 0 -> flag mode
392  *          1 -> variable mode
393  *   index: flag/variable �ֹ�
394  *   val  : ��������
395  */
mus_midi_set_flag(int mode,int index,int val)396 int mus_midi_set_flag(int mode, int index, int val) {
397 	int v[3];
398 	int fd;
399 
400 	if (!midi_available) return NG;
401 
402         if (-1 == (fd = connect_to_server())) {
403 		puts("fail to connect");
404                 return NG;
405 	}
406 
407 	v[0] = mode;
408 	v[1] = index;
409 	v[2] = val;
410 	cl_send_packet(fd, MUS_MIDI_SETFLAG, v, 3 * sizeof(int));
411 	cl_read_ack(fd);
412 	close(fd);
413 	return OK;
414 }
415 
416 /*
417  * midi �α��� flag/variable �ξ��֤��������
418  *   mode : 0 -> flag mode
419  *          1 -> variable mode
420  *   index: flag/variable �ֹ�
421  *
422  *   return : flag/variable ����
423  */
mus_midi_get_flag(int mode,int index)424 int mus_midi_get_flag(int mode, int index) {
425 	ServerPktHeader pkt_hdr;
426 	int v[2];
427 	int fd, val = 0;
428 	void *data;
429 
430 	if (!midi_available) return NG;
431 
432         if (-1 == (fd = connect_to_server())) {
433 		puts("fail to connect");
434                 return NG;
435 	}
436 
437 	v[0] = mode;
438 	v[1] = index;
439 	cl_send_packet(fd, MUS_MIDI_GETFLAG, v, 2 * sizeof(int));
440 	data = cl_read_packet(fd, &pkt_hdr);
441 	if (data) {
442 		val = *((int *)data);
443 		g_free(data);
444 	}
445 	cl_read_ack(fd);
446 	close(fd);
447 
448 	return val;
449 }
450 
451 /*
452  * MIDI ��ͭ��/̵�� �ե饰�μ���
453  *   return: FALASE -> ̵��
454  *           TRUE   -> ͭ��
455  */
mus_midi_get_state()456 boolean mus_midi_get_state() {
457 	return midi_available;
458 }
459 
460 /*
461  * WAV �α��ճ��� (command S?)
462  *   no  : �ե������ֹ�( no >= 1)
463  *   loop: �����֤���� (0�ξ���̵��)
464  */
mus_pcm_start(int no,int loop)465 int mus_pcm_start(int no, int loop) {
466 	int v[2];
467 	int fd;
468 
469 	if (!audio_available) return NG;
470 
471 	if (-1 == (fd = connect_to_server())) {
472 		puts("fail to connect");
473 		return NG;
474 	}
475 
476 	/* load file */
477 	v[0] = 0;
478 	v[1] = no;
479 	cl_send_packet(fd, MUS_PCM_LOAD_NO, v, 2 * sizeof(int));
480 	cl_read_ack(fd);
481 	close(fd);
482 
483 	if (-1 == (fd = connect_to_server())) {
484 		puts("fail to connect");
485 		return NG;
486 	}
487 	/* play start */
488 	v[0] = 0;
489 	v[1] = loop;
490 	cl_send_packet(fd, MUS_PCM_START, v, 2 * sizeof(int));
491 	cl_read_ack(fd);
492 	close(fd);
493 
494 	return OK;
495 }
496 
497 /*
498  * WAV ���� mix ���Ʊ���
499  *   noL : ���ѤΥե������ֹ�(noL >= 1)
500  *   noR : ���ѤΥե������ֹ�(noR >= 1)
501  *   loop: �����֤���(0�ξ���̵�¥롼��)
502  */
mus_pcm_mix(int noL,int noR,int loop)503 int mus_pcm_mix(int noL, int noR, int loop) {
504 	int v[2];
505 	int fd, len;
506 	WAVFILE *wfile;
507 
508 	if (!audio_available) return NG;
509 
510 	if (-1 == (fd = connect_to_server())) {
511 		puts("fail to connect");
512 		return NG;
513 	}
514 
515 	/* mix 2 wave files */
516 	wfile = pcmlib_mixlr(noL, noR);
517 	if (wfile == NULL) {
518 		puts("mixlr fail");
519 		return NG;
520 	}
521 
522 	/* load from mem */
523 	{
524 		ClientPktHeader pkt_hdr;
525 		len = sizeof(WAVFILE) + wfile->bytes;
526 
527 		v[0] = 0;
528 		pkt_hdr.version = XSYS35_PROTOCOL_VERSION;
529 		pkt_hdr.command = MUS_PCM_LOAD_MEM;
530 		pkt_hdr.data_length = sizeof(int) + len;
531 
532 		write(fd, &pkt_hdr, sizeof(ClientPktHeader));
533 		write(fd, v, sizeof(int));
534 		write(fd, wfile, sizeof(WAVFILE));
535 		write(fd, wfile->data, wfile->bytes);
536 	}
537 	cl_read_ack(fd);
538 	close(fd);
539 
540 	if (-1 == (fd = connect_to_server())) {
541 		puts("fail to connect");
542 		return NG;
543 	}
544 	/* play start */
545 	v[0] = 0;
546 	v[1] = loop;
547 	cl_send_packet(fd, MUS_PCM_START, v, 2 * sizeof(int));
548 	cl_read_ack(fd);
549 
550 	close(fd);
551 
552 	pcmlib_free(wfile);
553 
554 	return OK;
555 }
556 
557 /*
558  * WAV �α������ (command S?)
559  *   msec: �ߤޤ�ޤǤλ���(msec), 0�ξ��Ϥ����˻ߤޤ�
560  */
mus_pcm_stop(int msec)561 int mus_pcm_stop(int msec) {
562 	int v[2];
563 	int fd;
564 
565 	if (!audio_available) return NG;
566 
567 	if (-1 == (fd = connect_to_server())) {
568 		puts("fail to connect");
569 		return NG;
570 	}
571 
572 	v[0] = 0;
573 	v[1] = msec;
574 	cl_send_packet(fd, MUS_PCM_STOP, v, 2 * sizeof(int));
575 	cl_read_ack(fd);
576 
577 	close(fd);
578 
579 	return OK;
580 }
581 
582 /*
583  * WAV �ե����������˺ܤ���
584  *   no  : �ե������ֹ�( no >= 1)
585  */
mus_pcm_load(int no)586 int mus_pcm_load(int no) {
587 	int v[2];
588 	int fd;
589 
590 	if (!audio_available) return NG;
591 
592 	if (-1 == (fd = connect_to_server())) {
593 		puts("fail to connect");
594 		return NG;
595 	}
596 
597 	/* load file */
598 	v[0] = 0;
599 	v[1] = no;
600 	cl_send_packet(fd, MUS_PCM_LOAD_NO, v, 2 * sizeof(int));
601 	cl_read_ack(fd);
602 
603 	close(fd);
604 
605 	return OK;
606 }
607 
608 /*
609  * WAV �α��վ��֤μ���
610  *   pos: ���ջ��֤��Ǽ������(msec)
611  *        ��ߤ��Ƥ������ 0 ������
612  *        loop���Ƥ�����Ϲ�׻���
613  */
mus_pcm_get_playposition(int * pos)614 int mus_pcm_get_playposition(int *pos) {
615 	ServerPktHeader pkt_hdr;
616 	int v[2];
617 	int fd;
618 	void *data;
619 
620 	if (!audio_available) return NG;
621 
622 	if (-1 == (fd = connect_to_server())) {
623 		puts("fail to connect");
624 		return 0;
625 	}
626 
627 	v[0] = 0;
628 	cl_send_packet(fd, MUS_PCM_GETPOSITION, v, sizeof(int));
629 
630 	data = cl_read_packet(fd, &pkt_hdr);
631 	if (data) {
632 		*pos = *(int *)(data);
633 		g_free(data);
634 	}
635 	cl_read_ack(fd);
636 	close(fd);
637 
638 	return OK;
639 }
640 
641 
642 /*
643  * �ե����ɳ���
644  *   device: �ե����ɤ���ǥХ���(MIX_MAXTER/MIX_PCM/....)
645  *   time  : �ǽ��ܥ�塼��ޤǤ�ã�������(msec)
646  *   volume: �ǽ��ܥ�塼��
647  *   stop:   �ե����ɽ�λ���˱��դ��ȥåפ��뤫�ɤ�����
648  *           0: ���ʤ�
649  *           1: ����
650  */
mus_mixer_fadeout_start(int device,int time,int volume,int stop)651 int mus_mixer_fadeout_start(int device, int time, int volume, int stop) {
652 	int v[5];
653 	int fd;
654 
655 	if (!audio_available) return NG;
656 
657 	if (-1 == (fd = connect_to_server())) {
658 		puts("fail to connect");
659 		return NG;
660 	}
661 
662 	v[0] = device;
663 	v[1] = 0;
664 	v[2] = time;
665 	v[3] = volume;
666 	v[4] = stop;
667 	cl_send_packet(fd, MUS_FADE_START, v, 5 * sizeof(int));
668 	cl_read_ack(fd);
669 
670 	close(fd);
671 
672 	return OK;
673 }
674 
675 /*
676  * ����ΥǥХ��������ߥե������椫�ɤ�����Ĵ�٤�
677  *   device: ����ǥХ���
678  *
679  *   return: TRUE  -> �ե�������
680  *           FALSE -> �ե�������Ǥʤ�
681  */
mus_mixer_fadeout_get_state(int device)682 boolean mus_mixer_fadeout_get_state(int device) {
683 	ServerPktHeader pkt_hdr;
684 	int v[2];
685 	int fd;
686 	boolean bool = FALSE;
687 	void *data;
688 
689 	if (!audio_available) return FALSE;
690 
691 	if (-1 == (fd = connect_to_server())) {
692 		puts("fail to connect");
693 		return FALSE;
694 	}
695 
696 	v[0] = device;
697 	v[1] = 0;
698 	cl_send_packet(fd, MUS_FADE_GETSTATE, v, 2 * sizeof(int));
699 	data = cl_read_packet(fd, &pkt_hdr);
700 	if (data) {
701 		bool = *((boolean *)data);
702 		g_free(data);
703 	}
704 	cl_read_ack(fd);
705 	close(fd);
706 
707 	return bool;
708 }
709 
710 /*
711  * ����ΥǥХ����Υե����ɤ�����ǻߤ��
712  *   device: ����ǥХ���
713  */
mus_mixer_fadeout_stop(int device)714 int mus_mixer_fadeout_stop(int device) {
715 	int v[2];
716 	int fd;
717 
718 	if (!audio_available) return NG;
719 
720 	if (-1 == (fd = connect_to_server())) {
721 		puts("fail to connect");
722 		return NG;
723 	}
724 
725 	v[0] = device;
726 	v[1] = 0;
727 	cl_send_packet(fd, MUS_FADE_STOP, v, 2 * sizeof(int));
728 	cl_read_ack(fd);
729 
730 	close(fd);
731 
732 	return OK;
733 }
734 
735 /*
736  * ����ΥǥХ����Υߥ�������٥���������
737  *   device: ����ǥХ���
738  *
739  *   return: �ߥ�������٥�(0 - 100) (������������ꤵ�줿��)
740  */
mus_mixer_get_level(int device)741 int mus_mixer_get_level(int device) {
742 	ServerPktHeader pkt_hdr;
743 	int v, lv = 0;
744 	void *data;
745 	int fd;
746 
747 	if (!audio_available) return 0;
748 
749 	if (-1 == (fd = connect_to_server())) {
750 		puts("fail to connect");
751 		return 0;
752 	}
753 
754 	v = device;
755 	cl_send_packet(fd, MUS_MIXER_GETLEVEL, &v, sizeof(int));
756 
757 	data = cl_read_packet(fd, &pkt_hdr);
758 	if (data) {
759 		lv = *((int *)data);
760 		g_free(data);
761 	}
762 	cl_read_ack(fd);
763 	close(fd);
764 	return lv;
765 }
766 
767 /*
768  * ����Υե����ޥåȤǺ�����ǽ���ɤ���Ĵ�٤�
769  *   bit : 8 or 16 bit
770  *   rate: frequency
771  *   ch  : Mono or Stereo
772  *   able: ��ǽ���ɤ����ξ��֤���������
773  */
mus_pcm_check_ability(int bit,int rate,int ch,boolean * able)774 int mus_pcm_check_ability(int bit, int rate, int ch, boolean *able) {
775 	if (!audio_available) {
776 		printf("not available\n");
777 		*able = FALSE;
778 		return NG;
779 	}
780 
781 	*able = TRUE;
782 	return OK;
783 }
784 
785 /*
786  * ����Υ����ͥ�� wave file �����
787  *   ch : channel (0-127)
788  *   num: �ե������ֹ� (1-65535)
789  */
mus_wav_load(int ch,int num)790 int mus_wav_load(int ch, int num) {
791 	int v[2];
792 	int fd;
793 
794 	if (!audio_available) return NG;
795 
796 	if (ch < 0 || ch > 128) return NG;
797 
798 	if (-1 == (fd = connect_to_server())) {
799 		puts("fail to connect");
800 		return NG;
801 	}
802 
803 	/* load file */
804 	v[0] = ch + 1;
805 	v[1] = num;
806 	cl_send_packet(fd, MUS_PCM_LOAD_NO, v, 2 * sizeof(int));
807 	cl_read_ack(fd);
808 	close(fd);
809 	return OK;
810 }
811 
812 /*
813  * ����Υ����ͥ뤫�� wave file ���˴�
814  *   ch : channel
815  */
mus_wav_unload(int ch)816 int mus_wav_unload(int ch) {
817 	if (!audio_available) return NG;
818 
819 	if (ch < 0 || ch > 128) return NG;
820 
821 	cl_send_guint32(MUS_PCM_UNLOAD, (guint32)(ch + 1));
822 	return OK;
823 }
824 
825 /*
826  * WAV �α��ճ��� (wavXXXX)
827  *   ch  : ������������ͥ� (0-127)
828            (���餫���� mus_wav_load��load���Ƥ���)
829  *   loop: �����֤����       (0�ξ���̵��, ����ʳ��ϣ���Τ�)
830  */
mus_wav_play(int ch,int loop)831 int mus_wav_play(int ch, int loop) {
832 	int v[2];
833 	int fd;
834 
835 	if (!audio_available) return NG;
836 
837 	if (ch < 0 || ch > 128) return NG;
838 
839 	if (-1 == (fd = connect_to_server())) {
840 		puts("fail to connect");
841 		return NG;
842 	}
843 
844 	v[0] = ch + 1;
845 	v[1] = loop;
846 	cl_send_packet(fd, MUS_PCM_START, v, 2 * sizeof(int));
847 	cl_read_ack(fd);
848 	close(fd);
849 	return OK;
850 }
851 
852 /*
853  * ����Υ����ͥ��WAV�α������ (wavXXX)
854  *   ch: channel
855  */
mus_wav_stop(int ch)856 int mus_wav_stop(int ch) {
857 	if (!audio_available) return NG;
858 
859 	if (ch < 0 || ch > 128) return NG;
860 
861 	cl_send_guint32(MUS_PCM_STOP, (guint32)(ch +1));
862 	return OK;
863 }
864 
865 /*
866  * ����Υ����ͥ�α��վ��֤μ���
867  *   ch: channel (0-127)
868  *
869  *   return: ���ջ���(msec) 65535ms ��˰��
870  */
mus_wav_get_playposition(int ch)871 int mus_wav_get_playposition(int ch) {
872 	ServerPktHeader pkt_hdr;
873 	int v[2];
874 	int fd;
875 	void *data;
876 	int ret = 0;
877 
878 	if (!audio_available) return NG;
879 
880 	if (ch < 0 || ch > 128) return NG;
881 
882 	if (-1 == (fd = connect_to_server())) {
883 		puts("fail to connect");
884 		return 0;
885 	}
886 
887 	v[0] = ch + 1;
888 	cl_send_packet(fd, MUS_PCM_GETPOSITION, v, sizeof(int));
889 
890 	data = cl_read_packet(fd, &pkt_hdr);
891 	if (data) {
892 		ret = *(int *)(data);
893 		g_free(data);
894 	}
895 	cl_read_ack(fd);
896 	close(fd);
897 
898 	if (ret > 65565) ret = 65535;
899 
900 	return ret;
901 }
902 
903 
904 /*
905  * ����Υ����ͥ��WAV�Υե�����
906  *   ch: channel(0-127)
907  *   time  : �ǽ��ܥ�塼��ޤǤ�ã�������(msec)
908  *   volume: �ǽ��ܥ�塼��
909  *   stop  : �ե����ɽ�λ���˱��դ��ȥåפ��뤫�ɤ�����
910  *             0: ���ʤ�
911  *             1: ����
912  */
mus_wav_fadeout_start(int ch,int time,int volume,int stop)913 int mus_wav_fadeout_start(int ch, int time, int volume, int stop) {
914 	int v[5];
915 	int fd;
916 
917 	if (!audio_available) return NG;
918 
919 	if (ch < 0 || ch > 128) return NG;
920 
921 	if (-1 == (fd = connect_to_server())) {
922 		puts("fail to connect");
923 		return NG;
924 	}
925 
926 	v[0] = MIX_PCM;
927 	v[1] = ch + 1;
928 	v[2] = time;
929 	v[3] = volume;
930 	v[4] = stop;
931 	cl_send_packet(fd, MUS_FADE_START, v, 5 * sizeof(int));
932 	cl_read_ack(fd);
933 
934 	close(fd);
935 
936 	return OK;
937 }
938 
939 /*
940  * ����Υ����ͥ�Υե����ɤ�����ǻߤ��
941  *   ch: channel (0-127)
942  */
mus_wav_fadeout_stop(int ch)943 int mus_wav_fadeout_stop(int ch) {
944 	int v[2];
945 	int fd;
946 
947 	if (!audio_available) return NG;
948 
949 	if (ch < 0 || ch > 128) return NG;
950 
951 	if (-1 == (fd = connect_to_server())) {
952 		puts("fail to connect");
953 		return NG;
954 	}
955 
956 	v[0] = MIX_PCM;
957 	v[1] = ch + 1;
958 	cl_send_packet(fd, MUS_FADE_STOP, v, 2 * sizeof(int));
959 	cl_read_ack(fd);
960 
961 	close(fd);
962 
963 	return OK;
964 }
965 
966 /*
967  * ����Υ����ͥ뤬���ߥե������椫�ɤ�����Ĵ�٤�
968  *   ch: channel
969  *
970  *   return: TRUE  -> �ե�������
971  *           FALSE -> �ե�������Ǥʤ�
972  */
mus_wav_fadeout_get_state(int ch)973 boolean mus_wav_fadeout_get_state(int ch) {
974 	ServerPktHeader pkt_hdr;
975 	int v[2];
976 	int fd;
977 	boolean bool = FALSE;
978 	void *data;
979 
980 	if (!audio_available) return FALSE;
981 
982 	if (ch < 0 || ch > 128) return FALSE;
983 
984 	if (-1 == (fd = connect_to_server())) {
985 		puts("fail to connect");
986 		return FALSE;
987 	}
988 
989 	v[0] = MIX_PCM;
990 	v[1] = ch + 1;
991 	cl_send_packet(fd, MUS_FADE_GETSTATE, v, 2 * sizeof(int));
992 	data = cl_read_packet(fd, &pkt_hdr);
993 
994 	if (data) {
995 		bool = *((boolean *)data);
996 		g_free(data);
997 	}
998 	cl_read_ack(fd);
999 	close(fd);
1000 
1001 	return bool;
1002 }
1003 
1004 
1005 /*
1006  * ����Υ����ͥ�κ�������λ����ޤ��Ԥ�
1007  *   ch: channel (0-127)
1008  */
mus_wav_waitend(int ch)1009 int mus_wav_waitend(int ch) {
1010 	int v[2];
1011 	int fd;
1012 
1013 	if (!audio_available) return NG;
1014 
1015 	if (ch < 0 || ch > 128) return NG;
1016 
1017 	if (-1 == (fd = connect_to_server())) {
1018 		puts("fail to connect");
1019 		return NG;
1020 	}
1021 
1022 	v[0] = ch + 1;
1023 	cl_send_packet(fd, MUS_PCM_WAITEND, v, sizeof(int));
1024 	cl_read_ack(fd);
1025 	close(fd);
1026 
1027 	return OK;
1028 }
1029 
1030 /*
1031  * ����Υ����ͥ�ǻ����Ԥ�
1032  *     �������Ƥ��ʤ��ʤ餹������롣���ޥ�ɤ�ȯ�Ԥ��줿�ִ֤˱������
1033  *     ����С����դ����äƤ������ַв᤹��ޤ��Ԥġ�
1034  *   ch  : channel (0-127)
1035  *   time: �Ԥ�����(msec)
1036  */
mus_wav_waittime(int ch,int time)1037 int mus_wav_waittime(int ch, int time) {
1038 	ServerPktHeader pkt_hdr;
1039 	int v[1];
1040 	int fd;
1041 	void *data;
1042 	int cnt, ret = 0;
1043 
1044 	if (!audio_available) return NG;
1045 
1046 	if (ch < 0 || ch > 128) return NG;
1047 
1048 	cnt = get_high_counter(SYSTEMCOUNTER_MSEC);
1049 
1050 	if (-1 == (fd = connect_to_server())) {
1051 		puts("fail to connect");
1052 		return NG;
1053 	}
1054 
1055 	v[0] = ch + 1;
1056 	cl_send_packet(fd, MUS_PCM_GETPOSITION, v, sizeof(int));
1057 
1058 	data = cl_read_packet(fd, &pkt_hdr);
1059 	if (data) {
1060 		ret = *(int *)(data);
1061 		g_free(data);
1062 	}
1063 	cl_read_ack(fd);
1064 	close(fd);
1065 
1066 	if (ret != 0) {
1067 		int cntn = get_high_counter(SYSTEMCOUNTER_MSEC);
1068 		int sleeptime = time - (cntn - cnt);
1069 		if (sleeptime > 0) {
1070 			usleep(sleeptime * 1000);
1071 		}
1072 	}
1073 	return OK;
1074 }
1075 
1076 /*
1077  * PCM ��ͭ��/̵�� �ե饰�μ���
1078  *   return: FALASE -> ̵��
1079  *           TRUE   -> ͭ��
1080  */
mus_pcm_get_state()1081 boolean mus_pcm_get_state() {
1082 	return audio_available;
1083 }
1084 
1085 /*
1086  * ����Υ����ͥ��WAV�ǡ����α��ջ��֤μ���
1087  *   ch: channel
1088  *
1089  *   return: ����(msec) 65535ms ��˰��
1090  */
mus_wav_wavtime(int ch)1091 int mus_wav_wavtime(int ch) {
1092 	ServerPktHeader pkt_hdr;
1093 	int v[2];
1094 	int fd;
1095 	int ret = 0;
1096 	void *data;
1097 
1098 	if (!audio_available) return 0;
1099 
1100 	if (ch < 0 || ch > 128) return NG;
1101 
1102 	if (-1 == (fd = connect_to_server())) {
1103 		puts("fail to connect");
1104 		return FALSE;
1105 	}
1106 
1107 	v[0] = ch + 1;
1108 	cl_send_packet(fd, MUS_PCM_GETWAVETIME, v, sizeof(int));
1109 	data = cl_read_packet(fd, &pkt_hdr);
1110 
1111 	if (data) {
1112 		ret = *((boolean *)data);
1113 		g_free(data);
1114 	}
1115 	cl_read_ack(fd);
1116 	close(fd);
1117 
1118 	if (ret > 65565) ret = 65535;
1119 	return ret;
1120 
1121 }
1122 
1123 /*
1124  * ����� channel �� WAVFILE ���å�
1125  *   ch:    channel
1126  *   wfile: WAVFILE
1127  */
mus_wav_sendfile(int ch,WAVFILE * wfile)1128 int mus_wav_sendfile(int ch, WAVFILE *wfile) {
1129 	int v[2];
1130 	int fd, len;
1131 	ClientPktHeader pkt_hdr;
1132 
1133 	if (!audio_available) return NG;
1134 
1135 	if (ch < 0 || ch > 128) return NG;
1136 
1137 	if (-1 == (fd = connect_to_server())) {
1138 		puts("fail to connect");
1139 		return NG;
1140 	}
1141 
1142 	len = sizeof(WAVFILE) + wfile->bytes;
1143 
1144 	v[0] = ch + 1;
1145 	pkt_hdr.version = XSYS35_PROTOCOL_VERSION;
1146 	pkt_hdr.command = MUS_PCM_LOAD_MEM;
1147 	pkt_hdr.data_length = sizeof(int) + len;
1148 
1149 	write(fd, &pkt_hdr, sizeof(ClientPktHeader));
1150 	write(fd, v, sizeof(int));
1151 	write(fd, wfile, sizeof(WAVFILE));
1152 	write(fd, wfile->data, wfile->bytes);
1153 
1154 	cl_read_ack(fd);
1155 	close(fd);
1156 	return OK;
1157 }
1158 
1159 /*
1160  * ����Υ����ͥ�� wave file ��LRȿž���ƥ���
1161  *   ch : channel (0-127)
1162  *   num: �ե������ֹ� (1-65535)
1163  */
mus_wav_load_lrsw(int ch,int num)1164 int mus_wav_load_lrsw(int ch, int num) {
1165 	int v[2];
1166 	int fd;
1167 
1168 	if (!audio_available) return NG;
1169 
1170 	if (ch < 0 || ch > 128) return NG;
1171 
1172 	if (-1 == (fd = connect_to_server())) {
1173 		puts("fail to connect");
1174 		return NG;
1175 	}
1176 
1177 	/* load file */
1178 	v[0] = ch + 1;
1179 	v[1] = num;
1180 	cl_send_packet(fd, MUS_PCM_LOAD_LRSW, v, 2 * sizeof(int));
1181 	cl_read_ack(fd);
1182 	close(fd);
1183 	return OK;
1184 }
1185 
mus_bgm_play(int no,int time,int vol)1186 int mus_bgm_play(int no, int time, int vol) {
1187 	int v[3];
1188 	int fd;
1189 
1190 	if (!audio_available) return NG;
1191 
1192 	if (-1 == (fd = connect_to_server())) {
1193 		puts("fail to connect");
1194 		return NG;
1195 	}
1196 
1197 	v[0] = no;
1198 	v[1] = time;
1199 	v[2] = vol;
1200 	cl_send_packet(fd, MUS_BGM_PLAY, v, 3 * sizeof(int));
1201 	cl_read_ack(fd);
1202 	close(fd);
1203 	return OK;
1204 }
1205 
mus_bgm_stop(int no,int time)1206 int mus_bgm_stop(int no, int time) {
1207 	int v[2];
1208 	int fd;
1209 
1210 	if (!audio_available) return NG;
1211 
1212 	if (-1 == (fd = connect_to_server())) {
1213 		puts("fail to connect");
1214 		return NG;
1215 	}
1216 
1217 	v[0] = no;
1218 	v[1] = time * 10;
1219 	cl_send_packet(fd, MUS_BGM_STOP, v, 2 * sizeof(int));
1220 	cl_read_ack(fd);
1221 	close(fd);
1222 	return OK;
1223 }
1224 
mus_bgm_fade(int no,int time,int vol)1225 int mus_bgm_fade(int no, int time, int vol) {
1226 	int v[3];
1227 	int fd;
1228 
1229 	if (!audio_available) return NG;
1230 
1231 	if (-1 == (fd = connect_to_server())) {
1232 		puts("fail to connect");
1233 		return NG;
1234 	}
1235 
1236 	v[0] = no;
1237 	v[1] = time * 10;
1238 	v[2] = vol;
1239 	cl_send_packet(fd, MUS_BGM_FADE, v, 3 * sizeof(int));
1240 	cl_read_ack(fd);
1241 	close(fd);
1242 	return OK;
1243 }
1244 
mus_bgm_getpos(int no)1245 int mus_bgm_getpos(int no) {
1246 	ServerPktHeader pkt_hdr;
1247 	int v[2];
1248 	int fd;
1249 	void *data;
1250 	int ret = 0;
1251 
1252 	if (!audio_available) return NG;
1253 
1254 	if (-1 == (fd = connect_to_server())) {
1255 		puts("fail to connect");
1256 		return 0;
1257 	}
1258 
1259 	v[0] = no;
1260 	cl_send_packet(fd, MUS_BGM_GETPOS, v, sizeof(int));
1261 
1262 	data = cl_read_packet(fd, &pkt_hdr);
1263 	if (data) {
1264 		ret = *(int *)(data);
1265 		g_free(data);
1266 	}
1267 	cl_read_ack(fd);
1268 	close(fd);
1269 
1270 	if (ret > 65565) ret = 65535;
1271 
1272 	return ret;
1273 }
1274 
mus_bgm_wait(int no,int timeout)1275 int mus_bgm_wait(int no, int timeout) {
1276 	ServerPktHeader pkt_hdr;
1277 	int v[2], ret = 0;
1278 	int fd, curtime, maxtime;
1279 	void *data;
1280 
1281 	if (!audio_available) return NG;
1282 
1283 	curtime = get_high_counter(SYSTEMCOUNTER_MSEC);
1284 	maxtime = curtime + timeout * 10;
1285 
1286 	while (curtime < maxtime) {
1287 		if (-1 == (fd = connect_to_server())) {
1288 			puts("fail to connect");
1289 			break;
1290 		}
1291 		v[0] = no;
1292 		cl_send_packet(fd, MUS_BGM_GETPOS, v, sizeof(int));
1293 		data = cl_read_packet(fd, &pkt_hdr);
1294 		if (data) {
1295 			ret = *(int *)(data);
1296 			g_free(data);
1297 		}
1298 		cl_read_ack(fd);
1299 		close(fd);
1300 		if (ret == 0) break;
1301 		usleep(10*1000);
1302 		curtime = get_high_counter(SYSTEMCOUNTER_MSEC);
1303 	}
1304 
1305 	return OK;
1306 }
1307 
mus_bgm_waitpos(int no,int index)1308 int mus_bgm_waitpos(int no, int index) {
1309 	int v[2];
1310 	int fd;
1311 
1312 	if (!audio_available) return NG;
1313 
1314 	if (-1 == (fd = connect_to_server())) {
1315 		puts("fail to connect");
1316 		return NG;
1317 	}
1318 
1319 	v[0] = no;
1320 	v[1] = index;
1321 	cl_send_packet(fd, MUS_BGM_WAITPOS, v, 2 * sizeof(int));
1322 	cl_read_ack(fd);
1323 	close(fd);
1324 
1325 	return OK;
1326 }
1327 
mus_bgm_stopall(int time)1328 int mus_bgm_stopall(int time) {
1329 	if (!audio_available) return NG;
1330 
1331 	cl_send_guint32(MUS_BGM_STOPALL, (guint32)time * 10);
1332 	return OK;
1333 }
1334 
mus_bgm_getlength(int no)1335 int mus_bgm_getlength(int no) {
1336 	ServerPktHeader pkt_hdr;
1337 	int v[2];
1338 	int fd;
1339 	void *data;
1340 	int ret = 0;
1341 
1342 	if (!audio_available) return NG;
1343 
1344 	if (-1 == (fd = connect_to_server())) {
1345 		puts("fail to connect");
1346 		return 0;
1347 	}
1348 
1349 	v[0] = no;
1350 	cl_send_packet(fd, MUS_BGM_GETLEN, v, sizeof(int));
1351 
1352 	data = cl_read_packet(fd, &pkt_hdr);
1353 	if (data) {
1354 		ret = *(int *)(data);
1355 		g_free(data);
1356 	}
1357 	cl_read_ack(fd);
1358 	close(fd);
1359 
1360 	if (ret > 65565) ret = 65535;
1361 
1362 	return ret;
1363 }
1364 
mus_vol_set_valance(int * vols,int num)1365 int mus_vol_set_valance(int *vols, int num) {
1366 	int fd;
1367 
1368 	if (!audio_available) return NG;
1369 
1370 	if (-1 == (fd = connect_to_server())) {
1371 		puts("fail to connect");
1372 		return NG;
1373 	}
1374 
1375 	cl_send_packet(fd, MUS_MIXER_SETVOLVAL, vols, num * sizeof(int));
1376 	cl_read_ack(fd);
1377 	close(fd);
1378 	return OK;
1379 }
1380