2 * minmad - a minimal mp3 player using libmad and oss
4 * Copyright (C) 2009-2013 Ali Gholami Rudi
6 * This program is released under the modified BSD license.
20 #include <sys/soundcard.h>
23 #define CTRLKEY(x) ((x) - 96)
24 #define MIN(a, b) ((a) < (b) ? (a) : (b))
25 #define MAX(a, b) ((a) > (b) ? (a) : (b))
30 static struct mad_decoder decoder
;
31 static struct termios termios
;
32 static int afd
; /* oss fd */
34 static char filename
[1 << 12];
35 static unsigned char *mem
;
38 static int frame_sz
; /* frame size */
39 static int frame_ms
; /* frame duration in milliseconds */
40 static int played
; /* playing time in milliseconds */
47 static int oss_open(void)
49 afd
= open("/dev/dsp", O_WRONLY
);
53 static void oss_close(void)
55 if (afd
> 0) /* zero fd is used for input */
61 static void oss_conf(void)
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 readkey(void)
73 if (read(0, &b
, 1) <= 0)
78 static void updatepos(void)
82 pos
= decoder
.sync
->stream
.this_frame
- mem
;
83 sz
= decoder
.sync
->stream
.next_frame
-
84 decoder
.sync
->stream
.this_frame
;
85 ms
= mad_timer_count(decoder
.sync
->frame
.header
.duration
,
86 MAD_UNITS_MILLISECONDS
);
87 frame_ms
= frame_ms
? ((frame_ms
<< 5) - frame_ms
+ ms
) >> 5 : ms
;
88 frame_sz
= frame_sz
? ((frame_sz
<< 5) - frame_sz
+ sz
) >> 5 : sz
;
92 static void printinfo(void)
94 int per
= pos
* 1000.0 / len
;
95 int loc
= pos
/ frame_sz
* frame_ms
/ 1000;
96 printf("%c %02d.%d%% (%d:%02d:%02d - %04d.%ds) [%s]\r",
97 paused
? (afd
< 0 ? '*' : ' ') : '>',
99 loc
/ 3600, (loc
% 3600) / 60, loc
% 60,
100 played
/ 1000, (played
/ 100) % 10,
105 static int getcount(int def
)
107 int result
= count
? count
: def
;
112 static void seek(int n
)
114 int diff
= n
* frame_sz
* 1000 / (frame_ms
? frame_ms
: 40);
115 pos
= MAX(0, MIN(len
, pos
+ diff
));
118 static void seek_thousands(int n
)
120 pos
= len
* (float) n
/ 1000;
121 pos
-= pos
% frame_sz
;
124 static int execkey(void)
128 while ((c
= readkey()) != -1) {
131 seek(JMP3
* getcount(1));
134 seek(-JMP3
* getcount(1));
137 seek(JMP2
* getcount(1));
140 seek(-JMP2
* getcount(1));
143 seek(JMP1
* getcount(1));
146 seek(-JMP1
* getcount(1));
149 seek_thousands(getcount(0) * 10);
171 count
= count
* 10 + c
- '0';
177 static enum mad_flow
input(void *data
, struct mad_stream
*stream
)
179 static unsigned long cpos
;
180 if (pos
&& pos
== cpos
) {
182 return MAD_FLOW_STOP
;
185 mad_stream_buffer(stream
, mem
+ pos
, len
- pos
);
186 return MAD_FLOW_CONTINUE
;
189 static signed int scale(mad_fixed_t sample
)
192 sample
+= (1L << (MAD_F_FRACBITS
- 16));
194 if (sample
>= MAD_F_ONE
)
195 sample
= MAD_F_ONE
- 1;
196 else if (sample
< -MAD_F_ONE
)
199 return sample
>> (MAD_F_FRACBITS
+ 1 - 16);
202 static void push_sample(char *buf
, mad_fixed_t sample
)
204 *buf
++ = (sample
>> 0) & 0xff;
205 *buf
++ = (sample
>> 8) & 0xff;
208 static char mixed
[1 << 20];
209 static enum mad_flow
output(void *data
,
210 struct mad_header
const *header
,
214 int right
= pcm
->channels
> 1 ? 1 : 0;
215 played
+= mad_timer_count(decoder
.sync
->frame
.header
.duration
,
216 MAD_UNITS_MILLISECONDS
);
217 for (i
= 0; i
< pcm
->length
; i
++) {
218 push_sample(mixed
+ i
* 4, scale(pcm
->samples
[0][i
]));
219 push_sample(mixed
+ i
* 4 + 2, scale(pcm
->samples
[right
][i
]));
221 if (header
->samplerate
!= rate
) {
222 rate
= header
->samplerate
;
225 write(afd
, mixed
, pcm
->length
* 4);
226 return execkey() ? MAD_FLOW_STOP
: MAD_FLOW_CONTINUE
;
229 static enum mad_flow
error(void *data
,
230 struct mad_stream
*stream
,
231 struct mad_frame
*frame
)
233 return MAD_FLOW_CONTINUE
;
236 static void waitkey(void)
238 struct pollfd ufds
[1];
240 ufds
[0].events
= POLLIN
;
244 static void decode(void)
246 mad_decoder_init(&decoder
, NULL
, input
, 0, 0, output
, error
, 0);
252 mad_decoder_run(&decoder
, MAD_DECODER_MODE_SYNC
);
255 mad_decoder_finish(&decoder
);
258 static void term_setup(void)
260 struct termios newtermios
;
261 tcgetattr(0, &termios
);
262 newtermios
= termios
;
263 newtermios
.c_lflag
&= ~ICANON
;
264 newtermios
.c_lflag
&= ~ECHO
;
265 tcsetattr(0, TCSAFLUSH
, &newtermios
);
266 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
269 static void term_cleanup(void)
271 tcsetattr(0, 0, &termios
);
274 static void sigcont(int sig
)
279 int main(int argc
, char *argv
[])
285 fd
= open(argv
[1], O_RDONLY
);
286 strcpy(filename
, argv
[1]);
288 if (fstat(fd
, &stat
) == -1 || stat
.st_size
== 0)
290 mem
= mmap(0, stat
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
292 if (mem
== MAP_FAILED
)
295 signal(SIGCONT
, sigcont
);
297 fprintf(stderr
, "minmad: /dev/dsp busy?\n");
303 munmap(mem
, stat
.st_size
);