Commit Diff


commit - a728254f63b163e7690e40990d9e039506203eb9
commit + 0ea0da1e5c4ee818758feb8ef792fb882330792e
blob - 63d5bd4337343389bdc473f7b9901fd6bfce8529
blob + 5a598e37a5ea16dd8da1dc46006c6e7595d42ad7
--- Makefile
+++ Makefile
@@ -1,13 +1,13 @@
 PROG=	amused
 SRCS=	amused.c control.c log.c xmalloc.c player.c ctl.c playlist.c \
-	player_flac.c player_mad.c player_opus.c player_oggvorbis.c
+	player_flac.c player_123.c player_opus.c player_oggvorbis.c
 
 .include "amused-version.mk"
 
 CPPFLAGS += -I/usr/local/include -I/usr/local/include/opus
 
 LDADD =	-levent -lm -lsndio -lutil \
-	-L/usr/local/lib -lmad -lvorbisfile -lopusfile -lFLAC
+	-L/usr/local/lib -lmpg123 -lvorbisfile -lopusfile -lFLAC
 DPADD =	${LIBEVENT} ${LIBM} ${LIBSNDIO} ${LIBUTIL}
 
 .if "${AMUSED_RELEASE}" == "Yes"
blob - 6f96113dbc549977403d3de5702ed5127b9e2197
blob + 845b8e9849032c9b0ef20ffec373f4aca806c5ed
--- README.md
+++ README.md
@@ -23,7 +23,7 @@ too)
 it needs the following packages from ports:
 
  - flac
- - libmad
+ - libmpg123
  - libvorbis
  - opusfile
 
blob - /dev/null
blob + 29d62dd0c22f2b5e00e67bfb032cd7d671bc64aa (mode 644)
--- /dev/null
+++ player_123.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2022 Omar Polo <op@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/uio.h>
+
+#include <err.h>
+#include <event.h>
+#include <limits.h>
+#include <sndio.h>
+#include <imsg.h>
+#include <unistd.h>
+
+#include <mpg123.h>
+
+#include "amused.h"
+#include "log.h"
+
+static int
+setup(mpg123_handle *mh)
+{
+	long	rate;
+	int	chan, enc;
+
+	if (mpg123_getformat(mh, &rate, &chan, &enc) != MPG123_OK) {
+		log_warnx("mpg123_getformat failed");
+		return 0;
+	}
+
+	if (player_setup(mpg123_encsize(enc) * 8, rate, chan) == -1)
+		err(1, "player_setrate");
+
+	return 1;
+}
+
+void
+play_mp3(int fd)
+{
+	static char	 buf[4096];
+	size_t		 len;
+	mpg123_handle	*mh;
+	int		 err;
+
+	if ((mh = mpg123_new(NULL, NULL)) == NULL)
+		fatal("mpg123_new");
+
+	if (mpg123_open_fd(mh, fd) != MPG123_OK) {
+		log_warnx("mpg123_open_fd failed");
+		close(fd);
+		return;
+	}
+
+	if (!setup(mh))
+		goto done;
+
+	for (;;) {
+		if (player_shouldstop())
+			break;
+
+		err = mpg123_read(mh, buf, sizeof(buf), &len);
+		switch (err) {
+		case MPG123_DONE:
+			goto done;
+		case MPG123_NEW_FORMAT:
+			if (!setup(mh))
+				goto done;
+			break;
+		case MPG123_OK:
+			sio_write(hdl, buf, len);
+			break;
+		default:
+			log_warnx("error %d decoding mp3", err);
+			goto done;
+		}
+	}
+
+done:
+	mpg123_delete(mh);
+	close(fd);
+}
blob - ec306437e1e982b5390524f70a201a30a2957cf5 (mode 644)
blob + /dev/null
--- player_mad.c
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- * Copyright (c) 2022 Omar Polo <op@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/queue.h>
-#include <sys/uio.h>
-
-#include <err.h>
-#include <event.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <sndio.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <imsg.h>
-#include <unistd.h>
-
-#include <string.h>
-
-#include <mad.h>
-
-#include "amused.h"
-#include "log.h"
-
-struct mad_stream mad_stream;
-struct mad_frame mad_frame;
-struct mad_synth mad_synth;
-
-struct buffer {
-	const void *start;
-	size_t length;
-	int sample_rate;
-	int channels;
-};
-
-static enum mad_flow
-input(void *d, struct mad_stream *stream)
-{
-	struct buffer *buffer = d;
-
-	if (buffer->length == 0)
-		return MAD_FLOW_STOP;
-
-	mad_stream_buffer(stream, buffer->start, buffer->length);
-	buffer->length = 0;
-	buffer->sample_rate = 0;
-	buffer->channels = 0;
-	return MAD_FLOW_CONTINUE;
-}
-
-static enum mad_flow
-output(void *data, const struct mad_header *header, struct mad_pcm *pcm)
-{
-	static uint32_t buf[BUFSIZ];
-	size_t len;
-	struct buffer *buffer = data;
-	int nsamples, i;
-	uint32_t sample;
-	const mad_fixed_t *leftch, *rightch;
-
-	if (player_shouldstop())
-		return MAD_FLOW_STOP;
-
-	nsamples = pcm->length;
-	leftch = pcm->samples[0];
-	rightch = pcm->samples[1];
-
-	if (buffer->sample_rate != pcm->samplerate ||
-	    buffer->channels != pcm->channels) {
-		buffer->sample_rate = pcm->samplerate;
-		buffer->channels = pcm->channels;
-		if (player_setup(32, pcm->samplerate, pcm->channels) == -1)
-			err(1, "player_setrate");
-	}
-
-	for (i = 0, len = 0; i < nsamples; ++i) {
-		if (len+2 >= sizeof(buf)) {
-			sio_write(hdl, buf, len * sizeof(sample));
-			len = 0;
-		}
-
-		sample = *leftch++;
-		buf[len++] = sample;
-
-		if (pcm->channels == 2) {
-			sample = *rightch++;
-			buf[len++] = sample;
-		}
-	}
-
-	if (len != 0)
-		sio_write(hdl, buf, len * sizeof(sample));
-
-	return MAD_FLOW_CONTINUE;
-}
-
-static enum mad_flow
-error(void *d, struct mad_stream *stream, struct mad_frame *frame)
-{
-	struct buffer *buffer = d;
-
-	/*
-	 * most of the decoding errors are actually ID3 tags.  Since
-	 * they're common, this has a lower priority to avoid spamming
-	 * syslog.
-	 */
-	log_debug("decoding error 0x%04x (%s) at byte offset %zu",
-	    stream->error, mad_stream_errorstr(stream),
-	    stream->this_frame - (const unsigned char *)buffer->start);
-
-	return MAD_FLOW_CONTINUE;
-}
-
-static int
-decode(void *m, size_t len)
-{
-	struct buffer buffer;
-	struct mad_decoder decoder;
-	int result;
-
-	/* initialize our private message structure; */
-	buffer.start = m;
-	buffer.length = len;
-
-	/* configure input, output and error functions */
-	mad_decoder_init(&decoder, &buffer, input, 0 /* header */,
-	    0 /* filter */, output, error, 0 /* message */);
-
-	/* start decoding */
-	result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
-
-	/* release the decoder */
-	mad_decoder_finish(&decoder);
-
-	return result;
-}
-
-void
-play_mp3(int fd)
-{
-	struct stat stat;
-	void *m;
-
-	if (fstat(fd, &stat) == -1) {
-		log_warn("fstat");
-		goto end;
-	}
-
-	m = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-	if (m == MAP_FAILED) {
-		log_warn("map failed");
-		goto end;
-	}
-
-	decode(m, stat.st_size);
-	munmap(m, stat.st_size);
-
-end:
-	close(fd);
-}