show paused status with '>' sign in printinfo()
[minmad.git] / minmad.c
blob0100620e9b16b1308e904b4880b0ada81bdedbc2
1 /*
2 * minmad - a minimal mp3 player using libmad and oss
4 * Copyright (C) 2009-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
7 */
8 #include <ctype.h>
9 #include <fcntl.h>
10 #include <pty.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/mman.h>
17 #include <sys/poll.h>
18 #include <termios.h>
19 #include <unistd.h>
20 #include <sys/soundcard.h>
21 #include <mad.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))
26 #define JMP3 600
27 #define JMP2 60
28 #define JMP1 10
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;
36 static long len;
37 static long pos;
38 static int frame_sz; /* frame size */
39 static int frame_ms; /* frame duration in milliseconds */
40 static int played; /* playing time in milliseconds */
41 static int rate;
43 static int exited;
44 static int paused;
45 static int count;
47 static int readkey(void)
49 char b;
50 if (read(STDIN_FILENO, &b, 1) <= 0)
51 return -1;
52 return b;
55 static void updatepos(void)
57 int sz, ms;
58 if (decoder.sync) {
59 pos = decoder.sync->stream.this_frame - mem;
60 sz = decoder.sync->stream.next_frame -
61 decoder.sync->stream.this_frame;
62 ms = mad_timer_count(decoder.sync->frame.header.duration,
63 MAD_UNITS_MILLISECONDS);
64 frame_ms = frame_ms ? ((frame_ms << 5) - frame_ms + ms) >> 5 : ms;
65 frame_sz = frame_sz ? ((frame_sz << 5) - frame_sz + sz) >> 5 : sz;
69 static void printinfo(void)
71 int per = pos * 1000.0 / len;
72 int loc = pos / frame_sz * frame_ms / 1000;
73 printf("%c %02d.%d%% (%d:%02d:%02d - %04d.%ds) [%s]\r",
74 paused ? ' ' : '>',
75 per / 10, per % 10,
76 loc / 3600, (loc % 3600) / 60, loc % 60,
77 played / 1000, (played / 100) % 10,
78 filename);
79 fflush(stdout);
82 static int getcount(int def)
84 int result = count ? count : def;
85 count = 0;
86 return result;
89 static void seek(int n)
91 int diff = n * frame_sz * 1000 / (frame_ms ? frame_ms : 40);
92 pos = MAX(0, MIN(len, pos + diff));
95 static void seek_thousands(int n)
97 pos = len * (float) n / 1000;
98 pos -= pos % frame_sz;
101 static int execkey(void)
103 int c;
104 updatepos();
105 while ((c = readkey()) != -1) {
106 switch (c) {
107 case 'J':
108 seek(JMP3 * getcount(1));
109 return 1;
110 case 'K':
111 seek(-JMP3 * getcount(1));
112 return 1;
113 case 'j':
114 seek(JMP2 * getcount(1));
115 return 1;
116 case 'k':
117 seek(-JMP2 * getcount(1));
118 return 1;
119 case 'l':
120 seek(JMP1 * getcount(1));
121 return 1;
122 case 'h':
123 seek(-JMP1 * getcount(1));
124 return 1;
125 case '%':
126 seek_thousands(getcount(0) * 10);
127 return 1;
128 case 'i':
129 printinfo();
130 break;
131 case 'p':
132 case ' ':
133 paused = !paused;
134 return 1;
135 case 'q':
136 exited = 1;
137 return 1;
138 case 27:
139 count = 0;
140 break;
141 default:
142 if (isdigit(c))
143 count = count * 10 + c - '0';
146 return 0;
149 static enum mad_flow input(void *data, struct mad_stream *stream)
151 static unsigned long cpos;
152 if (pos && pos == cpos) {
153 exited = 1;
154 return MAD_FLOW_STOP;
156 cpos = pos;
157 mad_stream_buffer(stream, mem + pos, len - pos);
158 return MAD_FLOW_CONTINUE;
161 static signed int scale(mad_fixed_t sample)
163 /* round */
164 sample += (1L << (MAD_F_FRACBITS - 16));
165 /* clip */
166 if (sample >= MAD_F_ONE)
167 sample = MAD_F_ONE - 1;
168 else if (sample < -MAD_F_ONE)
169 sample = -MAD_F_ONE;
170 /* quantize */
171 return sample >> (MAD_F_FRACBITS + 1 - 16);
174 static void push_sample(char *buf, mad_fixed_t sample)
176 *buf++ = (sample >> 0) & 0xff;
177 *buf++ = (sample >> 8) & 0xff;
180 static void oss_conf(void)
182 int ch = 2;
183 int bits = 16;
184 ioctl(afd, SOUND_PCM_WRITE_RATE, &rate);
185 ioctl(afd, SOUND_PCM_WRITE_CHANNELS, &ch);
186 ioctl(afd, SOUND_PCM_WRITE_BITS, &bits);
189 static char mixed[1 << 20];
190 static enum mad_flow output(void *data,
191 struct mad_header const *header,
192 struct mad_pcm *pcm)
194 int i;
195 int right = pcm->channels > 1 ? 1 : 0;
196 played += mad_timer_count(decoder.sync->frame.header.duration,
197 MAD_UNITS_MILLISECONDS);
198 for (i = 0; i < pcm->length; i++) {
199 push_sample(mixed + i * 4, scale(pcm->samples[0][i]));
200 push_sample(mixed + i * 4 + 2, scale(pcm->samples[right][i]));
202 if (header->samplerate != rate) {
203 rate = header->samplerate;
204 oss_conf();
206 write(afd, mixed, pcm->length * 4);
207 return execkey() ? MAD_FLOW_STOP : MAD_FLOW_CONTINUE;
210 static enum mad_flow error(void *data,
211 struct mad_stream *stream,
212 struct mad_frame *frame)
214 return MAD_FLOW_CONTINUE;
217 static void waitkey(void)
219 struct pollfd ufds[1];
220 ufds[0].fd = STDIN_FILENO;
221 ufds[0].events = POLLIN;
222 poll(ufds, 1, -1);
225 static void decode(void)
227 mad_decoder_init(&decoder, NULL, input, 0, 0, output, error, 0);
228 while (!exited) {
229 if (paused) {
230 waitkey();
231 execkey();
232 } else {
233 mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
236 mad_decoder_finish(&decoder);
239 static void oss_init(void)
241 afd = open("/dev/dsp", O_RDWR);
242 if (afd < 0) {
243 fprintf(stderr, "cannot open /dev/dsp\n");
244 exit(1);
248 static void oss_close(void)
250 close(afd);
253 static void term_setup(void)
255 struct termios newtermios;
256 tcgetattr(STDIN_FILENO, &termios);
257 newtermios = termios;
258 newtermios.c_lflag &= ~ICANON;
259 newtermios.c_lflag &= ~ECHO;
260 tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios);
261 fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
264 static void term_cleanup(void)
266 tcsetattr(STDIN_FILENO, 0, &termios);
269 static void sigcont(int sig)
271 term_setup();
274 int main(int argc, char *argv[])
276 struct stat stat;
277 int fd;
278 if (argc < 2)
279 return 1;
280 fd = open(argv[1], O_RDONLY);
281 strcpy(filename, argv[1]);
282 filename[30] = '\0';
283 if (fstat(fd, &stat) == -1 || stat.st_size == 0)
284 return 1;
285 mem = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
286 len = stat.st_size;
287 if (mem == MAP_FAILED)
288 return 1;
289 term_setup();
290 signal(SIGCONT, sigcont);
291 oss_init();
292 decode();
293 oss_close();
294 term_cleanup();
295 munmap(mem, stat.st_size);
296 close(fd);
297 return 0;