2 0ea0da1e 2022-02-23 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 0ea0da1e 2022-02-23 op * Permission to use, copy, modify, and distribute this software for any
5 0ea0da1e 2022-02-23 op * purpose with or without fee is hereby granted, provided that the above
6 0ea0da1e 2022-02-23 op * copyright notice and this permission notice appear in all copies.
8 0ea0da1e 2022-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0ea0da1e 2022-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0ea0da1e 2022-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0ea0da1e 2022-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0ea0da1e 2022-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0ea0da1e 2022-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0ea0da1e 2022-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0ea0da1e 2022-02-23 op #include <sys/mman.h>
18 0ea0da1e 2022-02-23 op #include <sys/stat.h>
19 0ea0da1e 2022-02-23 op #include <sys/types.h>
20 0ea0da1e 2022-02-23 op #include <sys/queue.h>
21 0ea0da1e 2022-02-23 op #include <sys/uio.h>
23 0ea0da1e 2022-02-23 op #include <err.h>
24 0ea0da1e 2022-02-23 op #include <event.h>
25 0ea0da1e 2022-02-23 op #include <limits.h>
26 0ea0da1e 2022-02-23 op #include <sndio.h>
27 0ea0da1e 2022-02-23 op #include <imsg.h>
28 0ea0da1e 2022-02-23 op #include <unistd.h>
30 0ea0da1e 2022-02-23 op #include <mpg123.h>
32 0ea0da1e 2022-02-23 op #include "amused.h"
33 0ea0da1e 2022-02-23 op #include "log.h"
36 0ea0da1e 2022-02-23 op setup(mpg123_handle *mh)
39 0ea0da1e 2022-02-23 op int chan, enc;
41 0ea0da1e 2022-02-23 op if (mpg123_getformat(mh, &rate, &chan, &enc) != MPG123_OK) {
42 0ea0da1e 2022-02-23 op log_warnx("mpg123_getformat failed");
46 0ea0da1e 2022-02-23 op if (player_setup(mpg123_encsize(enc) * 8, rate, chan) == -1)
47 0ea0da1e 2022-02-23 op err(1, "player_setrate");
53 0ea0da1e 2022-02-23 op play_mp3(int fd)
55 0ea0da1e 2022-02-23 op static char buf[4096];
57 0ea0da1e 2022-02-23 op mpg123_handle *mh;
60 0ea0da1e 2022-02-23 op if ((mh = mpg123_new(NULL, NULL)) == NULL)
61 0ea0da1e 2022-02-23 op fatal("mpg123_new");
63 0ea0da1e 2022-02-23 op if (mpg123_open_fd(mh, fd) != MPG123_OK) {
64 0ea0da1e 2022-02-23 op log_warnx("mpg123_open_fd failed");
69 0ea0da1e 2022-02-23 op if (!setup(mh))
73 0ea0da1e 2022-02-23 op if (player_shouldstop())
76 0ea0da1e 2022-02-23 op err = mpg123_read(mh, buf, sizeof(buf), &len);
77 0ea0da1e 2022-02-23 op switch (err) {
78 0ea0da1e 2022-02-23 op case MPG123_DONE:
80 0ea0da1e 2022-02-23 op case MPG123_NEW_FORMAT:
81 0ea0da1e 2022-02-23 op if (!setup(mh))
84 0ea0da1e 2022-02-23 op case MPG123_OK:
85 0ea0da1e 2022-02-23 op sio_write(hdl, buf, len);
88 0ea0da1e 2022-02-23 op log_warnx("error %d decoding mp3", err);
94 0ea0da1e 2022-02-23 op mpg123_delete(mh);