2 * minmad - a minimal mp3 player using libmad and oss
4 * Copyright (C) 2009-2016 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 */
39 static int rate
; /* current oss sample rate */
40 static int topause
; /* planned pause (compared with played) */
49 static int oss_open(void)
51 afd
= open("/dev/dsp", O_WRONLY
);
55 static void oss_close(void)
57 if (afd
> 0) /* zero fd is used for input */
63 static void oss_conf(int rate
, int ch
, int bits
)
65 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
66 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bits
);
67 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
70 static int cmdread(void)
73 if (read(0, &b
, 1) <= 0)
75 return (unsigned char) b
;
78 static void cmdwait(void)
80 struct pollfd ufds
[1];
82 ufds
[0].events
= POLLIN
;
86 static long muldiv64(long num
, long mul
, long div
)
88 return (long long) num
* mul
/ div
;
91 static void cmdinfo(void)
93 int per
= muldiv64(mpos
+ moff
, 1000, msize
);
94 int loc
= muldiv64(mpos
+ moff
, frame_ms
, frame_sz
* 1000);
95 printf("%c %02d.%d%% (%d:%02d:%02d - %04d.%ds) [%s]\r",
96 paused
? (afd
< 0 ? '*' : ' ') : '>',
98 loc
/ 3600, (loc
% 3600) / 60, loc
% 60,
99 played
/ 1000, (played
/ 100) % 10,
104 static int cmdcount(int def
)
106 int result
= count
? count
: def
;
111 static void seek(long pos
)
113 mark
['\''] = mpos
+ moff
;
114 mpos
= MAX(0, MIN(msize
, pos
));
118 static void cmdseekrel(int n
)
120 int diff
= muldiv64(n
, frame_sz
* 1000, frame_ms
? frame_ms
: 40);
121 seek(mpos
+ moff
+ diff
);
124 static void cmdseek100(int n
)
127 seek(muldiv64(msize
, n
, 100));
130 static int cmdpause(int pause
)
132 if (!pause
&& paused
) {
137 if (pause
&& !paused
) {
144 static int cmdexec(void)
147 if (topause
> 0 && topause
<= played
) {
151 while ((c
= cmdread()) >= 0) {
154 mark
[c
] = mpos
+ moff
;
165 cmdseekrel(+600 * cmdcount(1));
168 cmdseekrel(-600 * cmdcount(1));
171 cmdseekrel(+60 * cmdcount(1));
174 cmdseekrel(-60 * cmdcount(1));
177 cmdseekrel(+10 * cmdcount(1));
180 cmdseekrel(-10 * cmdcount(1));
183 cmdseek100(cmdcount(0));
196 if (cmdpause(!paused
))
200 topause
= count
? played
+ cmdcount(0) * 60000 : 0;
210 count
= count
* 10 + c
- '0';
216 static enum mad_flow
madinput(void *data
, struct mad_stream
*stream
)
218 int nread
= stream
->next_frame
? stream
->next_frame
- mbuf
: moff
;
219 int nleft
= mlen
- nread
;
227 memmove(mbuf
, mbuf
+ nread
, nleft
);
228 if (nleft
< sizeof(mbuf
)) {
229 if ((nr
= read(mfd
, mbuf
+ nleft
, sizeof(mbuf
) - nleft
)) <= 0) {
231 return MAD_FLOW_STOP
;
235 mad_stream_buffer(stream
, mbuf
, mlen
);
238 return MAD_FLOW_CONTINUE
;
241 static signed int madscale(mad_fixed_t sample
)
243 sample
+= (1l << (MAD_F_FRACBITS
- 16)); /* round */
244 if (sample
>= MAD_F_ONE
) /* clip */
245 sample
= MAD_F_ONE
- 1;
246 if (sample
< -MAD_F_ONE
)
248 return sample
>> (MAD_F_FRACBITS
+ 1 - 16); /* quantize */
251 static void madupdate(void)
255 moff
= maddec
.sync
->stream
.this_frame
- mbuf
;
256 sz
= maddec
.sync
->stream
.next_frame
-
257 maddec
.sync
->stream
.this_frame
;
258 ms
= mad_timer_count(maddec
.sync
->frame
.header
.duration
,
259 MAD_UNITS_MILLISECONDS
);
260 frame_ms
= frame_ms
? ((frame_ms
<< 5) - frame_ms
+ ms
) >> 5 : ms
;
261 frame_sz
= frame_sz
? ((frame_sz
<< 5) - frame_sz
+ sz
) >> 5 : sz
;
265 static char mixed
[1 << 18];
266 static enum mad_flow
madoutput(void *data
,
267 struct mad_header
const *header
,
271 int c2
= pcm
->channels
> 1 ? 1 : 0;
273 played
+= mad_timer_count(maddec
.sync
->frame
.header
.duration
,
274 MAD_UNITS_MILLISECONDS
);
275 for (i
= 0; i
< pcm
->length
; i
++) {
276 mixed
[i
* 4 + 0] = madscale(pcm
->samples
[c1
][i
]) & 0xff;
277 mixed
[i
* 4 + 1] = (madscale(pcm
->samples
[c1
][i
]) >> 8) & 0xff;
278 mixed
[i
* 4 + 2] = madscale(pcm
->samples
[c2
][i
]) & 0xff;
279 mixed
[i
* 4 + 3] = (madscale(pcm
->samples
[c2
][i
]) >> 8) & 0xff;
281 if (header
->samplerate
!= rate
) {
282 rate
= header
->samplerate
;
283 oss_conf(rate
, 2, 16);
285 write(afd
, mixed
, pcm
->length
* 4);
287 return cmdexec() ? MAD_FLOW_STOP
: MAD_FLOW_CONTINUE
;
290 static enum mad_flow
maderror(void *data
,
291 struct mad_stream
*stream
,
292 struct mad_frame
*frame
)
294 return MAD_FLOW_CONTINUE
;
297 static void maddecode(void)
299 mad_decoder_init(&maddec
, NULL
, madinput
, 0, 0, madoutput
, maderror
, 0);
305 mad_decoder_run(&maddec
, MAD_DECODER_MODE_SYNC
);
308 mad_decoder_finish(&maddec
);
311 static void term_init(struct termios
*termios
)
313 struct termios newtermios
;
314 tcgetattr(0, termios
);
315 newtermios
= *termios
;
316 newtermios
.c_lflag
&= ~ICANON
;
317 newtermios
.c_lflag
&= ~ECHO
;
318 tcsetattr(0, TCSAFLUSH
, &newtermios
);
319 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
322 static void term_done(struct termios
*termios
)
324 tcsetattr(0, 0, termios
);
327 int main(int argc
, char *argv
[])
330 struct termios termios
;
331 char *path
= argc
>= 2 ? argv
[1] : NULL
;
334 if (strchr(path
, '/'))
335 path
= strrchr(path
, '/') + 1;
336 snprintf(filename
, 30, "%s", path
);
337 mfd
= open(argv
[1], O_RDONLY
);
338 if (fstat(mfd
, &stat
) == -1 || stat
.st_size
== 0)
340 msize
= stat
.st_size
;
342 fprintf(stderr
, "minmad: /dev/dsp busy?\n");