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.
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))
31 static unsigned char *mem
;
32 static unsigned long len
;
33 static struct mad_decoder decoder
;
34 static unsigned long pos
;
35 static int frame_sz
; /* frame size */
36 static int frame_ms
; /* frame duration in milliseconds */
38 static mad_timer_t played
;
39 static unsigned int rate
;
41 static struct termios termios
;
45 static int readkey(void)
48 if (read(STDIN_FILENO
, &b
, 1) <= 0)
53 static void updatepos(void)
57 pos
= decoder
.sync
->stream
.this_frame
- mem
;
58 sz
= decoder
.sync
->stream
.next_frame
-
59 decoder
.sync
->stream
.this_frame
;
60 ms
= mad_timer_count(decoder
.sync
->frame
.header
.duration
,
61 MAD_UNITS_MILLISECONDS
);
62 frame_ms
= frame_ms
? ((frame_ms
<< 3) - frame_ms
+ ms
) >> 3 : ms
;
63 frame_sz
= frame_sz
? ((frame_sz
<< 3) - frame_sz
+ sz
) >> 3 : sz
;
67 static void printinfo(void)
69 int per
= pos
* 1000.0 / len
;
70 int dur
= mad_timer_count(played
, MAD_UNITS_DECISECONDS
);
71 int loc
= pos
* frame_ms
/ frame_sz
/ 1000;
72 printf("minmad: %3d:%02d %3d.%d%% %8d.%ds\r",
74 per
/ 10, per
% 10, dur
/ 10, dur
% 10);
78 static int getcount(int def
)
80 int result
= count
? count
: def
;
85 static void seek(int n
)
87 int diff
= n
* frame_sz
* 1000 / (frame_ms
? frame_ms
: 40);
88 pos
= MAX(0, MIN(len
, pos
+ diff
));
91 static void seek_thousands(int n
)
93 pos
= len
* (float) n
/ 1000;
94 pos
-= pos
% frame_sz
;
97 static int execkey(void)
101 while ((c
= readkey()) != -1) {
104 seek(JMP3
* getcount(1));
107 seek(-JMP3
* getcount(1));
110 seek(JMP2
* getcount(1));
113 seek(-JMP2
* getcount(1));
116 seek(JMP1
* getcount(1));
119 seek(-JMP1
* getcount(1));
122 seek_thousands(getcount(0) * 10);
139 count
= count
* 10 + c
- '0';
145 static enum mad_flow
input(void *data
, struct mad_stream
*stream
)
147 static unsigned long cpos
;
148 if (pos
&& pos
== cpos
) {
150 return MAD_FLOW_STOP
;
153 mad_stream_buffer(stream
, mem
+ pos
, len
- pos
);
154 return MAD_FLOW_CONTINUE
;
157 static signed int scale(mad_fixed_t sample
)
160 sample
+= (1L << (MAD_F_FRACBITS
- 16));
162 if (sample
>= MAD_F_ONE
)
163 sample
= MAD_F_ONE
- 1;
164 else if (sample
< -MAD_F_ONE
)
167 return sample
>> (MAD_F_FRACBITS
+ 1 - 16);
170 static void push_sample(char *buf
, mad_fixed_t sample
)
172 *buf
++ = (sample
>> 0) & 0xff;
173 *buf
++ = (sample
>> 8) & 0xff;
176 static void oss_conf(void)
180 ioctl(afd
, SOUND_PCM_WRITE_RATE
, &rate
);
181 ioctl(afd
, SOUND_PCM_WRITE_CHANNELS
, &ch
);
182 ioctl(afd
, SOUND_PCM_WRITE_BITS
, &bits
);
185 static char mixed
[1 << 20];
186 static enum mad_flow
output(void *data
,
187 struct mad_header
const *header
,
191 int right
= pcm
->channels
> 1 ? 1 : 0;
192 mad_timer_add(&played
, decoder
.sync
->frame
.header
.duration
);
193 for (i
= 0; i
< pcm
->length
; i
++) {
194 push_sample(mixed
+ i
* 4, scale(pcm
->samples
[0][i
]));
195 push_sample(mixed
+ i
* 4 + 2, scale(pcm
->samples
[right
][i
]));
197 if (header
->samplerate
!= rate
) {
198 rate
= header
->samplerate
;
201 write(afd
, mixed
, pcm
->length
* 4);
202 return execkey() ? MAD_FLOW_STOP
: MAD_FLOW_CONTINUE
;
205 static enum mad_flow
error(void *data
,
206 struct mad_stream
*stream
,
207 struct mad_frame
*frame
)
209 return MAD_FLOW_CONTINUE
;
212 static void waitkey(void)
214 struct pollfd ufds
[1];
215 ufds
[0].fd
= STDIN_FILENO
;
216 ufds
[0].events
= POLLIN
;
220 static void decode(void)
222 mad_decoder_init(&decoder
, NULL
, input
, 0, 0, output
, error
, 0);
228 mad_decoder_run(&decoder
, MAD_DECODER_MODE_SYNC
);
231 mad_decoder_finish(&decoder
);
234 static void oss_init(void)
236 afd
= open("/dev/dsp", O_RDWR
);
238 fprintf(stderr
, "cannot open /dev/dsp\n");
243 static void oss_close(void)
248 static void term_setup(void)
250 struct termios newtermios
;
251 tcgetattr(STDIN_FILENO
, &termios
);
252 newtermios
= termios
;
253 newtermios
.c_lflag
&= ~ICANON
;
254 newtermios
.c_lflag
&= ~ECHO
;
255 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
256 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
259 static void term_cleanup(void)
261 tcsetattr(STDIN_FILENO
, 0, &termios
);
264 static void sigcont(int sig
)
269 int main(int argc
, char *argv
[])
275 fd
= open(argv
[1], O_RDONLY
);
276 if (fstat(fd
, &stat
) == -1 || stat
.st_size
== 0)
278 mem
= mmap(0, stat
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
280 if (mem
== MAP_FAILED
)
283 signal(SIGCONT
, sigcont
);
288 munmap(mem
, stat
.st_size
);