2 * minmad - a minimal mp3 player using libmad and oss
4 * Copyright (C) 2009-2015 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
18 #include <sys/soundcard.h>
21 #define CTRLKEY(x) ((x) - 96)
22 #define MIN(a, b) ((a) < (b) ? (a) : (b))
23 #define MAX(a, b) ((a) > (b) ? (a) : (b))
25 static struct mad_decoder maddec
;
26 static int afd
; /* oss fd */
28 static char filename
[128];
29 static int mfd
; /* input file descriptor */
30 static long msize
; /* file size */
31 static unsigned char mbuf
[1 << 16];
32 static long mpos
; /* the position of mbuf[] */
33 static long mlen
; /* data in mbuf[] */
34 static long moff
; /* offset into mbuf[] */
35 static long mark
[256]; /* mark positions */
36 static int frame_sz
; /* frame size */
37 static int frame_ms
; /* frame duration in milliseconds */
38 static int played
; /* playing time in milliseconds */
48 static int oss_open(void)
50 afd
= open("/dev/dsp", O_WRONLY
);
54 static void oss_close(void)
56 if (afd
> 0) /* zero fd is used for input */
62 static void oss_conf(int rate
, int ch
, int bits
)
64 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
65 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bits
);
66 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
69 static int cmdread(void)
72 if (read(0, &b
, 1) <= 0)
74 return (unsigned char) b
;
77 static void cmdwait(void)
79 struct pollfd ufds
[1];
81 ufds
[0].events
= POLLIN
;
85 static void cmdinfo(void)
87 int per
= (mpos
+ moff
) * 1000 / msize
;
88 int loc
= (mpos
+ moff
) / frame_sz
* frame_ms
/ 1000;
89 printf("%c %02d.%d%% (%d:%02d:%02d - %04d.%ds) [%s]\r",
90 paused
? (afd
< 0 ? '*' : ' ') : '>',
92 loc
/ 3600, (loc
% 3600) / 60, loc
% 60,
93 played
/ 1000, (played
/ 100) % 10,
98 static int cmdcount(int def
)
100 int result
= count
? count
: def
;
105 static void seek(long pos
)
107 mark
['\''] = mpos
+ moff
;
108 mpos
= MAX(0, MIN(msize
, pos
));
112 static void cmdseekrel(int n
)
114 int diff
= n
* frame_sz
* 1000 / (frame_ms
? frame_ms
: 40);
115 seek(mpos
+ moff
+ diff
);
118 static void cmdseek100(int n
)
121 seek(msize
* n
/ 100);
124 static int cmdexec(void)
127 while ((c
= cmdread()) >= 0) {
130 mark
[c
] = mpos
+ moff
;
141 cmdseekrel(+600 * cmdcount(1));
144 cmdseekrel(-600 * cmdcount(1));
147 cmdseekrel(+60 * cmdcount(1));
150 cmdseekrel(-60 * cmdcount(1));
153 cmdseekrel(+10 * cmdcount(1));
156 cmdseekrel(-10 * cmdcount(1));
159 cmdseek100(cmdcount(0));
187 count
= count
* 10 + c
- '0';
193 static enum mad_flow
madinput(void *data
, struct mad_stream
*stream
)
195 int nread
= stream
->next_frame
? stream
->next_frame
- mbuf
: moff
;
196 int nleft
= mlen
- nread
;
204 memmove(mbuf
, mbuf
+ nread
, nleft
);
205 if (nleft
< sizeof(mbuf
)) {
206 if ((nr
= read(mfd
, mbuf
+ nleft
, sizeof(mbuf
) - nleft
)) <= 0) {
208 return MAD_FLOW_STOP
;
212 mad_stream_buffer(stream
, mbuf
, mlen
);
215 return MAD_FLOW_CONTINUE
;
218 static signed int madscale(mad_fixed_t sample
)
220 sample
+= (1l << (MAD_F_FRACBITS
- 16)); /* round */
221 if (sample
>= MAD_F_ONE
) /* clip */
222 sample
= MAD_F_ONE
- 1;
223 if (sample
< -MAD_F_ONE
)
225 return sample
>> (MAD_F_FRACBITS
+ 1 - 16); /* quantize */
228 static void madupdate(void)
232 moff
= maddec
.sync
->stream
.this_frame
- mbuf
;
233 sz
= maddec
.sync
->stream
.next_frame
-
234 maddec
.sync
->stream
.this_frame
;
235 ms
= mad_timer_count(maddec
.sync
->frame
.header
.duration
,
236 MAD_UNITS_MILLISECONDS
);
237 frame_ms
= frame_ms
? ((frame_ms
<< 5) - frame_ms
+ ms
) >> 5 : ms
;
238 frame_sz
= frame_sz
? ((frame_sz
<< 5) - frame_sz
+ sz
) >> 5 : sz
;
242 static char mixed
[1 << 18];
243 static enum mad_flow
madoutput(void *data
,
244 struct mad_header
const *header
,
248 int c2
= pcm
->channels
> 1 ? 1 : 0;
250 played
+= mad_timer_count(maddec
.sync
->frame
.header
.duration
,
251 MAD_UNITS_MILLISECONDS
);
252 for (i
= 0; i
< pcm
->length
; i
++) {
253 mixed
[i
* 4 + 0] = madscale(pcm
->samples
[c1
][i
]) & 0xff;
254 mixed
[i
* 4 + 1] = (madscale(pcm
->samples
[c1
][i
]) >> 8) & 0xff;
255 mixed
[i
* 4 + 2] = madscale(pcm
->samples
[c2
][i
]) & 0xff;
256 mixed
[i
* 4 + 3] = (madscale(pcm
->samples
[c2
][i
]) >> 8) & 0xff;
258 if (header
->samplerate
!= rate
) {
259 rate
= header
->samplerate
;
260 oss_conf(rate
, 2, 16);
262 write(afd
, mixed
, pcm
->length
* 4);
264 return cmdexec() ? MAD_FLOW_STOP
: MAD_FLOW_CONTINUE
;
267 static enum mad_flow
maderror(void *data
,
268 struct mad_stream
*stream
,
269 struct mad_frame
*frame
)
271 return MAD_FLOW_CONTINUE
;
274 static void maddecode(void)
276 mad_decoder_init(&maddec
, NULL
, madinput
, 0, 0, madoutput
, maderror
, 0);
282 mad_decoder_run(&maddec
, MAD_DECODER_MODE_SYNC
);
285 mad_decoder_finish(&maddec
);
288 static void term_init(struct termios
*termios
)
290 struct termios newtermios
;
291 tcgetattr(0, termios
);
292 newtermios
= *termios
;
293 newtermios
.c_lflag
&= ~ICANON
;
294 newtermios
.c_lflag
&= ~ECHO
;
295 tcsetattr(0, TCSAFLUSH
, &newtermios
);
296 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
299 static void term_done(struct termios
*termios
)
301 tcsetattr(0, 0, termios
);
304 int main(int argc
, char *argv
[])
307 struct termios termios
;
310 snprintf(filename
, 30, "%s", argv
[1]);
311 mfd
= open(argv
[1], O_RDONLY
);
312 if (fstat(mfd
, &stat
) == -1 || stat
.st_size
== 0)
314 msize
= stat
.st_size
;
316 fprintf(stderr
, "minmad: /dev/dsp busy?\n");