File: modplugplay.c

package info (click to toggle)
modplugxmms 2.05-9.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,236 kB
  • ctags: 291
  • sloc: sh: 6,989; cpp: 2,081; ansic: 450; makefile: 102
file content (706 lines) | stat: -rw-r--r-- 18,768 bytes parent folder | download | duplicates (2)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706

/*
 * Example Interface for libmodplug without using XMMS
 **
commandline interface to modplugxmms library
gurkan@linuks.mine.nu www.linuks.mine.nu
Copyright (C) 2003 Gürkan Sengün

TODO
unlock /dev/dsp when in 'p'ause mode
and have a look at SNDCTL_DSP_GETxPTR
more functions
sig handling
12:23 < tp76> tarzeau: yes. I prefer `T *foo = malloc (N * sizeof *foo);'.
use stat() to get filesize
run indent -kr

14:08 < tarzeau> so the player isn't stuck when anotehr thing is using /dev/dsp
14:08 < tarzeau> if that works a msg is displayed
14:08 < tarzeau> later the song is tried to be opened
14:08 < tarzeau> then the soundcard settings are set
14:09 < tarzeau> the the module is load using libmodplug
14:09 < tarzeau> module settings being set
14:09 < tarzeau> keyboard handling set
14:09 < tarzeau> the status string set up
14:09 < tarzeau> time stuff and the main loop
14:09 < warp> ok.
14:09 < tarzeau> where keyboard input is handled
14:09 < tarzeau> that's more or less it
14:10 < warp> see, you've just described all the functions you could make.
14:10 < warp> so, start that for loop with a   init_sound_device ()   or 
      something.
14:10 < warp> etc...  make functions for all the things you just described.
14:10 < tarzeau> heh good idea, i'll copy paste this irc talk and do that for 
       the next version like 0.9 with the sighandler for people 
        trying to ctrl-c or ctlr-z

maybe add support for write a .wav file for later play or .ogg
and maybe add support for gzip -d some.mod.gz | modplugplay
add support to allow modplugplay *.IT *.it *.s3m *.xm (n)ext (N)ot next
and -r for random order (make a random played/unplayed (bit)list)
show file size

argument processing
if it's not a file, we check if it's an option if not we ignore it

action keys
-> like key (put in list/directory)
-> dislike key (remove)
-> show info

|| [] >> << >| ()

TODO
or maybe just a $HOME/.modplugplay or /etc/modplugplay
command line option handling
  -l --loop
  -m --mono
  -s --stereo (default)
  -4 --44100 (default)
  -2 --22050
  -1 --11025
  -8 --8bit (default is 16bit)
  -h --help
  -q --quiet
  -v --version (head -c1080 module |strings)
  -i --info    don't play, only show module info (songname,length,size,type)
*/

#include <stdio.h>			/* printf */
#include <string.h>			/* strcpy */
#include <stdlib.h>			/* srand/rand */
#include <unistd.h>
#include <modplug.h>			/* core */
#include <sys/ioctl.h>			/* control device */
#include <fcntl.h>

#ifdef __NetBSD__
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif

#include <sys/time.h>			/* gettimeofday */
#include <time.h>
#include <sys/poll.h>			/* poll for keyboard input */
#include <termios.h>			/* needed to get terminal size */

#define VERSION "1.0"

#define BUF_SIZE 4096
#define DEVICE_NAME "/dev/dsp"

static struct termios stored_settings;
int audio_fd, mixer_fd;
unsigned char audio_buffer[BUF_SIZE];
char order[3];

typedef struct {
	int x, y;
} term_size;

/* inquire actual terminal size */
static int get_term_size(int fd, term_size *t) {
#ifdef TIOCGSIZE
    struct ttysize win;
#elif defined(TIOCGWINSZ)
    struct winsize win;
#endif

#ifdef TIOCGSIZE
    if (ioctl(fd,TIOCGSIZE,&win)) return 0;
    if (t) { 
	t->y=win.ts_lines;
    	t->x=win.ts_cols;
    }
#elif defined TIOCGWINSZ
    if (ioctl(fd,TIOCGWINSZ,&win)) return 0;
    if (t) {
        t->y=win.ws_row;
        t->x=win.ws_col;
    }
#else
    {
	const char *s;
	s=getenv("LINES");   if (s) t->y=strtol(s,NULL,10); else t->y=25;
	s=getenv("COLUMNS"); if (s) t->x=strtol(s,NULL,10); else t->x=80;
    }
#endif
    return 1;
}

void set_keypress(void)
{
    struct termios new_settings;
    tcgetattr(0,&stored_settings);
    new_settings=stored_settings;
    new_settings.c_lflag &= (~ICANON);
    new_settings.c_lflag &= (~ECHO);
    new_settings.c_cc[VTIME] = 0;
    new_settings.c_cc[VMIN] = 1;
    tcsetattr(0,TCSANOW,&new_settings);
    return;
}

void reset_keypress(void)
{
    tcsetattr(0,TCSANOW,&stored_settings);
    return;
}

void ansi_cursor(int visible)
{
    if (visible) {
	printf("\033[?25h");
    } else {
	printf("\033[?25l");    
    }
}

void help(char *s)
{
	printf("Copyright (C) 2003 Gürkan Sengün\n");
        printf("Version %s compiled on %s at %s.\n",VERSION,__DATE__,__TIME__);
	printf("\n");
	printf("%s: too few arguments\n",s);
	printf("Usage: modplugplay" /*[OPTIONS]*/" [FILES]\n");
	printf("\n");

/*	printf("  -v   print version info\n");
	printf("  -h   print help\n");
	printf("  -l   start in looping mode\n");
*/
/*
	printf("  -r   randomize play sequence\n");
	printf("  -c   write to console instead of %s\n",DEVICE_NAME);
	printf("  -i   use stdin for file input\n");
	printf("  -q   be quiet\n");
*/	
	exit(1);
}

int get_byteorder() 
{
#define sz sizeof(int)
    int ival;
    int format;
    char s[sz];
    char t[sz];
    int i, lit, big;

    for (i=0; i<sz; i++) s[i] = i;
    ival = *(int *)s;
    big = lit = 0;

    for (i=0; i<sz; i++) {
        char c = ival&0xff;
        ival >>= 8;
        if (s[i] == c) lit++;
        if (s[sz-i-1] == c) big++;
        t[i] = c;
    }
    if (lit == sz && big == 0) {
        /*printf("little endian\n");*/
	format = AFMT_S16_LE;
	strcpy(order, "LE");
    } else if (big == sz && lit == 0) {
        /*printf("big endian\n");*/
	format = AFMT_S16_BE;
	strcpy(order, "BE");
    } else {
        strcpy(order, "??");
	format = -1;
    }
    return(format);
}

char *getFileData(char *filename, long *size)
{
    FILE *f;
    char *data;

    f = fopen(filename, "rb");
    if (f == NULL) {
      return NULL;
    }
    fseek(f, 0L, SEEK_END);
    (*size) = ftell(f);
    rewind(f);
    data = (char*)malloc(*size);
    fread(data, *size, sizeof(char), f);
    fclose(f);

    return(data);
}

int getpcmvol()
/* get absolute pcm volume, 0 - 100 (for left+right) */
{
    int vol;
    
    if ((mixer_fd=open("/dev/mixer", O_RDONLY | O_NONBLOCK, 0)) == -1) {
	printf("can't open mixer\n");
	exit(1);
    }
    ioctl(mixer_fd,MIXER_READ(SOUND_MIXER_PCM),&vol);
    close(mixer_fd);

    vol=vol & 255;
    if (vol==100) vol=99;
    return(vol);
}

int setrelpcmvol(int newvol)
/* set relative pcm volume, -100 .. 100 */
{
    int currentvol=getpcmvol();
    currentvol+=newvol;
    if (currentvol>100) currentvol=100;
    if (currentvol<0) currentvol=0;
    newvol=currentvol+(currentvol*256);

    if ((mixer_fd=open("/dev/mixer", O_RDONLY | O_NONBLOCK, 0)) == -1) {
	printf("can't open mixer\n");
	exit(1);
    }
    ioctl(mixer_fd,MIXER_WRITE(SOUND_MIXER_PCM),&newvol);
    close(mixer_fd);
}

int main(int argc, char* argv[])
{
    long size;
    char *d;
    term_size terminal;
    ModPlugFile *f2;
    int mlen, len;
    struct timeval tvstart;
    struct timeval tv;
    struct timeval tvpause;
    struct timeval tvunpause;
    struct timeval tvptotal;
    char status[161];
    char songname[41];
    char notpaus[4];
    
    int vol=getpcmvol();
    int songsplayed = 0;

    ModPlug_Settings settings;
    ModPlug_GetSettings(&settings);

    int format;
    int channels = 2;
    int speed = 44100;

    int c; // kontest
    char buffer[128];
    int result, nread;
    struct pollfd pollfds;
    int timeout = 1;            /* Timeout in msec. */
    int loop=0; // kontest
    int pause=0;
    int mono=0;
    int bits=0;
    int song;
    int millisecond;
    char *randplayed; /* randomly played songs
			 songs that are n/N'd are done or not? */
    /* what about N if the previous song is not playable? */
    /* maybe mark it played in randplayed */

    // [rev--dly--] [sur--dly--] [bas--rng--]
    int rev=0;    // a
    int revdly=0; // s
    int sur=0;    // d
    int surdly=0; // y
    int bas=0;    // x
    int basrng=0; // c

    if ((format = get_byteorder()) == -1) {
        return 1;
    }

    /* Initialize pollfds; we're looking at input, stdin */
    pollfds.fd = 0;             /* stdin */
    pollfds.events = POLLIN;    /* Wait for input */

    if (argc==1) {
	help(argv[0]);
    }

    if (!get_term_size(STDIN_FILENO,&terminal)) {
	fprintf(stderr,"warning: failed to get terminal size\n");
    }
    
    srand(time(NULL));

for (song=1; song<argc; song++) {

    /* O_NONBLOCK gave me the problem 
    that after about 5 seconds writing audiobuffer to DEVICE_NAME
    was not completed, like 3072 bytes instead of the full buffer 4096
    which made the sound get faster (and wrong)
    */
    if ((audio_fd=open(DEVICE_NAME, O_WRONLY | O_NONBLOCK, 0)) == -1) {
	fprintf(stderr,"%s (%s): ",DEVICE_NAME,argv[song]);
	perror("");
	exit(1);
    }
    close(audio_fd);

    if ((audio_fd=open(DEVICE_NAME, O_WRONLY /*| O_NONBLOCK*/, 0)) == -1) {
	perror(DEVICE_NAME);
	exit(1);
    } else {
	printf("opened %s for playing ",DEVICE_NAME);
    }
    printf("%s ",argv[song]);
    printf("[%d/%d]",song,argc-1);

    if (argv[song][0] == '-') {
      if (!songsplayed && strstr(argv[song],"-h")) {
        printf("\n");
        help(argv[0]);
      } else if (!songsplayed && strstr(argv[song],"-v")) {
        printf("\nCopyright (C) 2003 Gürkan Sengün\n");
        printf("Version %s compiled on %s at %s.\n",VERSION,__DATE__,__TIME__);
	exit(0);
      } else if (strstr(argv[song],"-l")) {
        loop=1;
	continue;
      }
    }

    d = getFileData(argv[song], &size);
    if (d == NULL) continue;
    printf(" [%d]\n",size);

    if (ioctl(audio_fd,SNDCTL_DSP_SETFMT, &format) == -1) {
	perror("SND_CTL_DSP_SETFMT");
	exit(1);
    }

    if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
	perror("SNDCTL_DSP_CHANNELS");
	exit(1);
    }
    /* int mChannels; */   /* Number of channels - 1 for mono or 2 for stereo */
    /* int mBits; */       /* Bits per sample - 8, 16, or 32 */
    /* int mFrequency; */  /* Sampling rate - 11025, 22050, or 44100 */

    if (ioctl(audio_fd,SNDCTL_DSP_SPEED,&speed) == -1) {
	perror("SNDCTL_DSP_SPEED");
	exit(1);
    }

    f2 = ModPlug_Load(d, size);
    if (!f2) {
	printf("could not load %s\n", argv[song]);
	close(audio_fd);
	free(d); /* ? */
    } else {
      songsplayed++;
/*    settings.mFlags=MODPLUG_ENABLE_OVERSAMPLING | \
                    MODPLUG_ENABLE_NOISE_REDUCTION | \
		    MODPLUG_ENABLE_REVERB | \
		    MODPLUG_ENABLE_MEGABASS | \
		    MODPLUG_ENABLE_SURROUND;*/

//    settings.mReverbDepth = 100; /* 0 - 100 */ *   [REV--DLY--]
//    settings.mReverbDelay = 200; /* 40 - 200 ms  00-FF */ 
//    settings.mSurroundDepth = 100; /* 0 - 100 */   [SUR--DLY--]
//    settings.mSurroundDelay = 40; /* 5 - 40 ms */  
//    settings.mBassAmount  = 100; /* 0 - 100 */     [BAS--RNG--]
//    settings.mBassRange   = 100; /* 10 - 100 hz */ 
// [REV--DLY--] [SUR--DLY--] [BAS--RNG--]
// [rev--dly--] [sur--dly--] [bas--rng--]

    settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; /* RESAMP */
    settings.mChannels = 2;
    settings.mBits = 16;
    settings.mFrequency = 44100;
    /* insert more setting changes here */
    ModPlug_SetSettings(&settings);

    set_keypress();
    strcpy(songname, ModPlug_GetName(f2));
    if (strlen(songname)==0) strcpy(songname,argv[song]);
    sprintf(status,"playing %s (%%d.%%d/%d\") (%%d/%%d/%%d%%s)    \b\b\b\b",songname,ModPlug_GetLength(f2)/1000,speed,channels,settings.mBits,order);
    if (loop) sprintf(status,"looping %s (%%d.%%d/%d\") (%%d/%%d/%%d%%s)    \b\b\b\b",songname,ModPlug_GetLength(f2)/1000,speed,channels,settings.mBits,order);

    gettimeofday(&tvstart,NULL);
    tvptotal.tv_sec=tvptotal.tv_usec=0;
    mlen=1;
    
    while(mlen!=0) {
	if (mlen==0) { break; }

	if (!pause) {
	    gettimeofday(&tv,NULL);
	    mlen=ModPlug_Read(f2,audio_buffer,BUF_SIZE);
	    if ((len=write(audio_fd,audio_buffer,mlen)) == -1) {
		perror("audio write");
		exit(1);
    	    }
	    /*printf("%d %d\n",mlen,len);*/
        }
	printf(status,tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec,tv.tv_usec/100000,speed,channels,settings.mBits,order/*,rev,revdly,sur,surdly,bas,basrng*/);
	fflush(stdout);

	if ((mlen==0) && (loop==1)) {
	    /*printf("LOOPING NOW\n");*/
	    ModPlug_Seek(f2,0);
	    gettimeofday(&tvstart,NULL);
	    mlen=ModPlug_Read(f2,audio_buffer,BUF_SIZE);
	    tvptotal.tv_sec=tvptotal.tv_usec=0;
	}

        result = poll(&pollfds, 1, timeout);
        switch (result) {
        case 0:
            /*printf(".");*/
            break;
        case -1:
            perror("select");
            exit(1);

        default:
            if (pollfds.revents && POLLIN) {
	        nread = read(0, buffer, 1); /* s/nread/1/2 */
                if (nread == 0) {
                    printf("keyboard done\n");
                    exit(0);
               } else {
                    buffer[nread] = 0;
                    /* printf("%s", buffer); */

		    if (buffer[0]=='q') { mlen=0; song=argc; ioctl(audio_fd,SNDCTL_DSP_RESET,0); } /* quit */

		    if (buffer[0]=='f') {
			if ((tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec+10) < (ModPlug_GetLength(f2)/1000)) {
			    ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			    ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000+10000);
			    tvstart.tv_sec-=10;
			}
		    } /* forward 10" */

		    if (buffer[0]=='b') {
			if ((tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec-10) > 0) {
			    ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			    ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000-10000);
			    tvstart.tv_sec+=10;
			}
		    } /* backward 10" */
		    
		    /*
		    if (buffer[0]=='i') {
			printf("\n");
		    } */

/*
		    if (buffer[0]=='a') {
			rev++; settings.mReverbDepth=rev;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='A') {
			rev--; settings.mReverbDepth=rev;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='s') {
			revdly++; settings.mReverbDelay=revdly;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='S') {
			revdly--; settings.mReverbDelay=revdly;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='d') {
			sur++; settings.mSurroundDepth=sur;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='D') {
			sur--; settings.mSurroundDepth=sur;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='y') {
			surdly++; settings.mSurroundDelay=surdly;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='Y') {
			surdly--; settings.mSurroundDelay=surdly;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='x') {
			bas++; settings.mBassAmount=bas;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='X') {
			bas--; settings.mBassAmount=bas;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='c') {
			basrng++; settings.mBassRange=basrng;
			ModPlug_SetSettings(&settings);
		    }
		    if (buffer[0]=='C') {
			basrng--; settings.mBassRange=basrng;
			ModPlug_SetSettings(&settings);
		    }
*/
		    
		    if (buffer[0]=='n') {
			if (song<argc) { mlen=0; pause=0; ioctl(audio_fd,SNDCTL_DSP_RESET,0); }
		    }

		    if (buffer[0]=='N') {
			if (song>1) { song-=2; mlen=0; pause=0; ioctl(audio_fd,SNDCTL_DSP_RESET,0); }
		    }
		    
		    if (buffer[0]=='r') {
			song=(int) ((float)(argc-1)*rand()/(RAND_MAX+1.0));
			mlen=0; pause=0;
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			/* printf("\n[%d?]\n",song+1); */
		    }
		    
		    /*if (buffer[0]=='R') {
			song=(int) ((float)(argc-1)*rand()/(RAND_MAX+1.0));
			mlen=0; pause=0;
		    }*/
		    
		    if (buffer[0]=='1') {
			/* 11025 hertz */
			speed=11025;
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			if (ioctl(audio_fd,SNDCTL_DSP_SPEED,&speed) == -1) {
			    perror("SNDCTL_DSP_SPEED");
			    exit(1);
			}
			settings.mFrequency = 11025;
			ModPlug_SetSettings(&settings);
			f2=ModPlug_Load(d,size);
			ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000+10000);
		    }
		    
		    if (buffer[0]=='2') {
			/* 22050 hertz */
			speed=22050;
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			if (ioctl(audio_fd,SNDCTL_DSP_SPEED,&speed) == -1) {
			    perror("SNDCTL_DSP_SPEED");
			    exit(1);
			}
			settings.mFrequency = 22050;
			ModPlug_SetSettings(&settings);
			f2=ModPlug_Load(d,size);
			ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000+10000);
		    }
		    
		    if (buffer[0]=='4') {
			/* 44100 hertz */
			speed=44100;
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			if (ioctl(audio_fd,SNDCTL_DSP_SPEED,&speed) == -1) {
			    perror("SNDCTL_DSP_SPEED");
			    exit(1);
			}
			settings.mFrequency = 44100;
			ModPlug_SetSettings(&settings);
			f2=ModPlug_Load(d,size);
			ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000+10000);
		    }

		    if (buffer[0]=='8') {
			/* 8/16 bit */
			/*format=;*/
			bits^=1;
			if (bits) { format=AFMT_U8; strcpy(order,""); } else {
			    if (get_byteorder()==0)  {
				format=AFMT_S16_LE;
				strcpy(order,"LE");
			    } else if (get_byteorder()==1) {
			    	format=AFMT_S16_BE;
				strcpy(order,"BE");
			    } else {
				printf("could not determine byte order.\n");
				strcpy(order,"??");
				return 1;
			    }
			}
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			if (ioctl(audio_fd,SNDCTL_DSP_SETFMT, &format) == -1) {
			    perror("SND_CTL_DSP_SETFMT");
			    exit(1);
			}
			if (bits) settings.mBits=8; else settings.mBits=16;
			ModPlug_SetSettings(&settings);
			f2=ModPlug_Load(d,size);
			ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000+10000);
		    }

		    if (buffer[0]=='m') {
			/* mono/stereo */
			mono^=1;
			if (mono) channels=1; else channels=2;
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
			    perror("SNDCTL_DSP_CHANNELS");
			    exit(1);
			}
			if (mono) settings.mChannels=1; else settings.mChannels=2;
			ModPlug_SetSettings(&settings);
			f2=ModPlug_Load(d,size);
			ModPlug_Seek(f2,(tv.tv_sec-tvstart.tv_sec-tvptotal.tv_sec)*1000+10000);
		    }
		    
		    if (buffer[0]=='l') {
			loop^=1;
			if (loop) {
			    memcpy(status+4,"loop",4);
			} else {
			    memcpy(status+4,"play",4);
			}
		    } /* loop */
		    
		    if (buffer[0]=='+') {
			setrelpcmvol(4);
		    }

		    if (buffer[0]=='-') {
			setrelpcmvol(-4);
		    }

		    if (buffer[0]=='p') {
			pause^=1;
			ioctl(audio_fd,SNDCTL_DSP_RESET,0);
			if (pause) {
			    gettimeofday(&tvpause,NULL);
			    memcpy(notpaus,status+4,4);
			    memcpy(status+4,"paus",4);
			} else {
			    gettimeofday(&tvunpause,NULL);
			    memcpy(status+4,notpaus,4);
			    tvptotal.tv_sec+=tvunpause.tv_sec-tvpause.tv_sec;
			    tvptotal.tv_usec+=tvunpause.tv_usec-tvpause.tv_usec;
			    /* printf(status,tv.tv_sec-tvstart.tv_sec,tv.tv_usec/100000); */
			}
		    } /* pause */
                }
            }
        }
    }
    printf("\n");

    reset_keypress();
    ModPlug_Unload(f2);
    close(audio_fd);
    free(d);
    } /* valid module */
    
} /* for */

    return 0;
}