store playing time in milliseconds
[minmad.git] / minmad.c
blob602408eeeeeda96e891286b3caa8a2e6b12f976b
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("%02d.%d%% (%d:%02d:%02d - %04d.%ds) [%30s]\r",
74 per / 10, per % 10,
75 loc / 3600, (loc % 3600) / 60, loc % 60,
76 played / 1000, (played / 100) % 10,
77 filename);
78 fflush(stdout);
81 static int getcount(int def)
83 int result = count ? count : def;
84 count = 0;
85 return result;
88 static void seek(int n)
90 int diff = n * frame_sz * 1000 / (frame_ms ? frame_ms : 40);
91 pos = MAX(0, MIN(len, pos + diff));
94 static void seek_thousands(int n)
96 pos = len * (float) n / 1000;
97 pos -= pos % frame_sz;
100 static int execkey(void)
102 int c;
103 updatepos();
104 while ((c = readkey()) != -1) {
105 switch (c) {
106 case 'J':
107 seek(JMP3 * getcount(1));
108 return 1;
109 case 'K':
110 seek(-JMP3 * getcount(1));
111 return 1;
112 case 'j':
113 seek(JMP2 * getcount(1));
114 return 1;
115 case 'k':
116 seek(-JMP2 * getcount(1));
117 return 1;
118 case 'l':
119 seek(JMP1 * getcount(1));
120 return 1;
121 case 'h':
122 seek(-JMP1 * getcount(1));
123 return 1;
124 case '%':
125 seek_thousands(getcount(0) * 10);
126 return 1;
127 case 'i':
128 printinfo();
129 break;
130 case 'p':
131 case ' ':
132 paused = !paused;
133 return 1;
134 case 'q':
135 exited = 1;
136 return 1;
137 case 27:
138 count = 0;
139 break;
140 default:
141 if (isdigit(c))
142 count = count * 10 + c - '0';
145 return 0;
148 static enum mad_flow input(void *data, struct mad_stream *stream)
150 static unsigned long cpos;
151 if (pos && pos == cpos) {
152 exited = 1;
153 return MAD_FLOW_STOP;
155 cpos = pos;
156 mad_stream_buffer(stream, mem + pos, len - pos);
157 return MAD_FLOW_CONTINUE;
160 static signed int scale(mad_fixed_t sample)
162 /* round */
163 sample += (1L << (MAD_F_FRACBITS - 16));
164 /* clip */
165 if (sample >= MAD_F_ONE)
166 sample = MAD_F_ONE - 1;
167 else if (sample < -MAD_F_ONE)
168 sample = -MAD_F_ONE;
169 /* quantize */
170 return sample >> (MAD_F_FRACBITS + 1 - 16);
173 static void push_sample(char *buf, mad_fixed_t sample)
175 *buf++ = (sample >> 0) & 0xff;
176 *buf++ = (sample >> 8) & 0xff;
179 static void oss_conf(void)
181 int ch = 2;
182 int bits = 16;
183 ioctl(afd, SOUND_PCM_WRITE_RATE, &rate);
184 ioctl(afd, SOUND_PCM_WRITE_CHANNELS, &ch);
185 ioctl(afd, SOUND_PCM_WRITE_BITS, &bits);
188 static char mixed[1 << 20];
189 static enum mad_flow output(void *data,
190 struct mad_header const *header,
191 struct mad_pcm *pcm)
193 int i;
194 int right = pcm->channels > 1 ? 1 : 0;
195 played += mad_timer_count(decoder.sync->frame.header.duration,
196 MAD_UNITS_MILLISECONDS);
197 for (i = 0; i < pcm->length; i++) {
198 push_sample(mixed + i * 4, scale(pcm->samples[0][i]));
199 push_sample(mixed + i * 4 + 2, scale(pcm->samples[right][i]));
201 if (header->samplerate != rate) {
202 rate = header->samplerate;
203 oss_conf();
205 write(afd, mixed, pcm->length * 4);
206 return execkey() ? MAD_FLOW_STOP : MAD_FLOW_CONTINUE;
209 static enum mad_flow error(void *data,
210 struct mad_stream *stream,
211 struct mad_frame *frame)
213 return MAD_FLOW_CONTINUE;
216 static void waitkey(void)
218 struct pollfd ufds[1];
219 ufds[0].fd = STDIN_FILENO;
220 ufds[0].events = POLLIN;
221 poll(ufds, 1, -1);
224 static void decode(void)
226 mad_decoder_init(&decoder, NULL, input, 0, 0, output, error, 0);
227 while (!exited) {
228 if (paused) {
229 waitkey();
230 execkey();
231 } else {
232 mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
235 mad_decoder_finish(&decoder);
238 static void oss_init(void)
240 afd = open("/dev/dsp", O_RDWR);
241 if (afd < 0) {
242 fprintf(stderr, "cannot open /dev/dsp\n");
243 exit(1);
247 static void oss_close(void)
249 close(afd);
252 static void term_setup(void)
254 struct termios newtermios;
255 tcgetattr(STDIN_FILENO, &termios);
256 newtermios = termios;
257 newtermios.c_lflag &= ~ICANON;
258 newtermios.c_lflag &= ~ECHO;
259 tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios);
260 fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
263 static void term_cleanup(void)
265 tcsetattr(STDIN_FILENO, 0, &termios);
268 static void sigcont(int sig)
270 term_setup();
273 int main(int argc, char *argv[])
275 struct stat stat;
276 int fd;
277 if (argc < 2)
278 return 1;
279 fd = open(argv[1], O_RDONLY);
280 strcpy(filename, argv[1]);
281 filename[30] = '\0';
282 if (fstat(fd, &stat) == -1 || stat.st_size == 0)
283 return 1;
284 mem = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
285 len = stat.st_size;
286 if (mem == MAP_FAILED)
287 return 1;
288 term_setup();
289 signal(SIGCONT, sigcont);
290 oss_init();
291 decode();
292 oss_close();
293 term_cleanup();
294 munmap(mem, stat.st_size);
295 close(fd);
296 return 0;