2 e3317c86 2023-05-02 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 f36fd90a 2022-07-09 op #include "config.h"
19 0ea0da1e 2022-02-23 op #include <sys/mman.h>
20 0ea0da1e 2022-02-23 op #include <sys/stat.h>
22 0ea0da1e 2022-02-23 op #include <limits.h>
23 4b42d3a3 2022-07-13 op #include <stdio.h>
24 0ea0da1e 2022-02-23 op #include <unistd.h>
26 0ea0da1e 2022-02-23 op #include <mpg123.h>
28 0ea0da1e 2022-02-23 op #include "amused.h"
29 0ea0da1e 2022-02-23 op #include "log.h"
32 0ea0da1e 2022-02-23 op setup(mpg123_handle *mh)
35 0ea0da1e 2022-02-23 op int chan, enc;
37 0ea0da1e 2022-02-23 op if (mpg123_getformat(mh, &rate, &chan, &enc) != MPG123_OK) {
38 0ea0da1e 2022-02-23 op log_warnx("mpg123_getformat failed");
42 0ea0da1e 2022-02-23 op if (player_setup(mpg123_encsize(enc) * 8, rate, chan) == -1)
43 1fb06c31 2022-06-10 op err(1, "player_setup");
49 17ef54d6 2022-06-22 op play_mp3(int fd, const char **errstr)
51 0ea0da1e 2022-02-23 op static char buf[4096];
53 0ea0da1e 2022-02-23 op mpg123_handle *mh;
54 791d3db3 2022-07-09 op int64_t seek = -1;
55 0da0ad46 2022-03-09 op int err, ret = -1;
57 0ea0da1e 2022-02-23 op if ((mh = mpg123_new(NULL, NULL)) == NULL)
58 0ea0da1e 2022-02-23 op fatal("mpg123_new");
60 0ea0da1e 2022-02-23 op if (mpg123_open_fd(mh, fd) != MPG123_OK) {
61 17ef54d6 2022-06-22 op *errstr = "mpg123_open_fd failed";
66 0ea0da1e 2022-02-23 op if (!setup(mh))
69 8e006e85 2022-07-13 op if (mpg123_scan(mh) != MPG123_OK)
72 ff06024f 2022-07-08 op player_setduration(mpg123_length(mh));
75 791d3db3 2022-07-09 op if (seek != -1) {
76 791d3db3 2022-07-09 op seek = mpg123_seek(mh, seek, SEEK_SET);
77 791d3db3 2022-07-09 op if (seek < 0) {
81 791d3db3 2022-07-09 op player_setpos(seek);
84 0ea0da1e 2022-02-23 op err = mpg123_read(mh, buf, sizeof(buf), &len);
85 0ea0da1e 2022-02-23 op switch (err) {
86 0ea0da1e 2022-02-23 op case MPG123_DONE:
89 0ea0da1e 2022-02-23 op case MPG123_NEW_FORMAT:
90 0ea0da1e 2022-02-23 op if (!setup(mh))
93 0ea0da1e 2022-02-23 op case MPG123_OK:
94 791d3db3 2022-07-09 op if (!play(buf, len, &seek)) {
100 3a3e6aed 2022-07-09 op log_warnx("skipping mp3 decoding error");
106 0ea0da1e 2022-02-23 op mpg123_delete(mh);