changeset 1959:3fef1171cd6c

Basic start of cleanup of cdaudio-ng.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2007 15:38:08 +0300
parents 2db72dd92ee3
children a76855c7942e 600972dea2ac
files src/cdaudio-ng/cdaudio-ng.c
diffstat 1 files changed, 112 insertions(+), 132 deletions(-) [+]
line wrap: on
line diff
--- a/src/cdaudio-ng/cdaudio-ng.c	Tue Oct 02 06:26:10 2007 +0300
+++ b/src/cdaudio-ng/cdaudio-ng.c	Tue Oct 02 15:38:08 2007 +0300
@@ -16,12 +16,14 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses>.
  */
 
+#include "config.h"
 
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
 #include <libgen.h>
+#include <stdarg.h>
 
 #include <cdio/cdio.h>
 #include <cdio/cdtext.h>
@@ -34,14 +36,13 @@
 
 #include <glib.h>
 
+#include <audacious/plugin.h>
 #include <audacious/configdb.h>
 #include <audacious/i18n.h>
 #include <audacious/output.h>
 #include <audacious/playlist.h>
-#include <audacious/plugin.h>
 #include <audacious/ui_plugin_menu.h>
 #include <audacious/util.h>
-#include "config.h"
 
 #include "cdaudio-ng.h"
 #include "configure.h"
@@ -60,7 +61,7 @@
 static gboolean			is_paused = FALSE;
 static int				playing_track = -1;
 static dae_params_t		*pdae_params = NULL;
-static gboolean			debug = FALSE;
+static gboolean			use_debug = FALSE;
 static char				cddb_server[DEF_STRING_LEN];
 static int				cddb_port;
 static InputPlayback	*pglobalinputplayback = NULL;
@@ -114,13 +115,34 @@
 DECLARE_PLUGIN(cdaudio, NULL, NULL, cdaudio_iplist, NULL, NULL, NULL, NULL, NULL);
 
 
-void cdaudio_init()
+void cdaudio_error(const char *fmt, ...)
+{
+	va_list ap;
+	fprintf(stderr, "cdaudio-ng: ");
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+}
+
+
+void CDDEBUG(const char *fmt, ...)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_init()\n");
+	if (use_debug) {
+		va_list ap;
+		fprintf(stderr, "cdaudio-ng: ");
+		va_start(ap, fmt);
+		vfprintf(stderr, fmt, ap);
+		va_end(ap);
+	}
+}
+
+
+static void cdaudio_init()
+{
+	CDDEBUG("cdaudio_init()\n");
 
 	if (!cdio_init()) {
-		fprintf(stderr, "cdaudio-ng: failed to initialize cdio subsystem\n");
+		cdaudio_error("Failed to initialize cdio subsystem.\n");
 		cleanup_on_error();
 		return;
 	}
@@ -151,15 +173,14 @@
 		strcpy(device, "");
 	else
 		strcpy(device, string);
-	if (!bmp_cfg_db_get_bool(db, "CDDA", "debug", &debug))
-		debug = FALSE;
+	if (!bmp_cfg_db_get_bool(db, "CDDA", "debug", &use_debug))
+		use_debug = FALSE;
 
 	bmp_cfg_db_close(db);
 
-	if (debug)
-		printf("cdaudio-ng: configuration: "/*use_dae = %d, */"limitspeed = %d, use_cdtext = %d, use_cddb = %d, cddbserver = \"%s\", cddbport = %d, device = \"%s\", debug = %d\n", /*use_dae, */limitspeed, use_cdtext, use_cddb, cddb_server, cddb_port, device, debug);
+	CDDEBUG(/*use_dae = %d, */"limitspeed = %d, use_cdtext = %d, use_cddb = %d, cddbserver = \"%s\", cddbport = %d, device = \"%s\", debug = %d\n", /*use_dae, */limitspeed, use_cdtext, use_cddb, cddb_server, cddb_port, device, use_debug);
 
-	configure_set_variables(/*&use_dae, */&limitspeed, &use_cdtext, &use_cddb, device, &debug, cddb_server, &cddb_port);
+	configure_set_variables(/*&use_dae, */&limitspeed, &use_cdtext, &use_cddb, device, &use_debug, cddb_server, &cddb_port);
 	configure_create_gui();
 
 	main_menu_item = gtk_image_menu_item_new_with_label(menu_item_text);
@@ -177,10 +198,9 @@
 	uri_set_plugin("cdda://", &inputplugin);
 }
 
-void cdaudio_about()
+static void cdaudio_about()
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_about()\n");
+	CDDEBUG("cdaudio_about()\n");
 
 	static GtkWidget* about_window = NULL;
 
@@ -188,22 +208,20 @@
         gdk_window_raise(about_window->window);
     }
 
-    char about_text[1000];
-	sprintf(about_text, _("Copyright (c) 2007, by Calin Crisan <ccrisan@gmail.com> and The Audacious Team.\n\n"
-						"Many thanks to libcdio developers <http://www.gnu.org/software/libcdio/>\n\tand to libcddb developers <http://libcddb.sourceforge.net/>.\n\n"
-						"Also thank you Tony Vroon for mentoring & guiding me.\n\n"
- 						"This was a Google Summer of Code 2007 project."));
-
-    about_window = audacious_info_dialog(_("About CD Audio Plugin NG"), about_text, _("OK"), FALSE, NULL, NULL);
+    about_window = audacious_info_dialog(_("About CD Audio Plugin NG"),
+    _("Copyright (c) 2007, by Calin Crisan <ccrisan@gmail.com> and The Audacious Team.\n\n"
+    "Many thanks to libcdio developers <http://www.gnu.org/software/libcdio/>\n"
+    "\tand to libcddb developers <http://libcddb.sourceforge.net/>.\n\n"
+    "Also thank you Tony Vroon for mentoring & guiding me.\n\n"
+    "This was a Google Summer of Code 2007 project."), _("OK"), FALSE, NULL, NULL);
 
     g_signal_connect(G_OBJECT(about_window), "destroy",
                      G_CALLBACK(gtk_widget_destroyed), &about_window);
 }
 
-void cdaudio_configure()
+static void cdaudio_configure()
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_configure()\n");
+	CDDEBUG("cdaudio_configure()\n");
 
 	/*
 	if (playing_track != -1)
@@ -213,61 +231,52 @@
 	configure_show_gui();
 }
 
-gint cdaudio_is_our_file(gchar *filename)
+static gint cdaudio_is_our_file(gchar *filename)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_is_our_file(\"%s\")\n", filename);
+	CDDEBUG("cdaudio_is_our_file(\"%s\")\n", filename);
 
 	if ((filename != NULL) && strlen(filename) > 4 && (!strcasecmp(filename + strlen(filename) - 4, ".cda"))) {
 			/* no CD information yet */
 		if (pcdio == NULL) {
-			if (debug)
-				printf("cdaudio-ng: no cd information, scanning\n");
+			CDDEBUG("no CD information, scanning\n");
 			cdaudio_scan_dir(CDDA_DEFAULT);
 		}
 
 			/* reload the cd information if the media has changed */
 		if (cdio_get_media_changed(pcdio) && pcdio != NULL) {
-			if (debug)
-				printf("cdaudio-ng: cd changed, rescanning\n");
+			CDDEBUG("CD changed, rescanning\n");
 			if (cdaudio_scan_dir(CDDA_DEFAULT) == NULL)
 				pcdio = NULL;
 		}
 
 		if (pcdio == NULL) {
-			if (debug)
-				printf("cdaudio-ng: \"%s\" is not our file\n", filename);
+			CDDEBUG("\"%s\" is not our file\n", filename);
 			return FALSE;
 		}
 
 			/* check if the requested track actually exists on the current audio cd */
 		gint trackno = find_trackno_from_filename(filename);
 		if (trackno < firsttrackno || trackno > lasttrackno) {
-			if (debug)
-				printf("cdaudio-ng: \"%s\" is not our file\n", filename);
+			CDDEBUG("\"%s\" is not our file\n", filename);
 			return FALSE;
 		}
-
-		if (debug)
-			printf("cdaudio-ng: \"%s\" is our file\n", filename);
+		
+		CDDEBUG("\"%s\" is our file\n", filename);
 		return TRUE;
 	}
 	else {
-		if (debug)
-			printf("cdaudio-ng: \"%s\" is not our file\n", filename);
+		CDDEBUG("\"%s\" is not our file\n", filename);
 		return FALSE;
 	}
 }
 
 GList *cdaudio_scan_dir(gchar *dirname)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_scan_dir(\"%s\")\n", dirname);
+	CDDEBUG("cdaudio_scan_dir(\"%s\")\n", dirname);
 
 		/* if the given dirname does not belong to us, we return NULL */
 	if (strstr(dirname, CDDA_DEFAULT) == NULL) {
-		if (debug)
-			printf("cdaudio-ng: \"%s\" directory does not belong to us\n", dirname);
+		CDDEBUG("\"%s\" directory does not belong to us\n", dirname);
 		return NULL;
 	}
 
@@ -275,7 +284,7 @@
 	if (device != NULL && strlen(device) > 0) {
 		pcdio = cdio_open(device, DRIVER_UNKNOWN);
 		if (pcdio == NULL) {
-			fprintf(stderr, "cdaudio-ng: failed to open cd device \"%s\"\n", device);
+			cdaudio_error("Failed to open CD device \"%s\".\n", device);
 			return NULL;
 		}
 	}
@@ -285,15 +294,14 @@
 		if (ppcd_drives != NULL && *ppcd_drives != NULL) { /* we have at least one audio capable cd drive */
 			pcdio = cdio_open(*ppcd_drives, DRIVER_UNKNOWN);
 			if (pcdio == NULL) {
-				fprintf(stderr, "cdaudio-ng: failed to open cd\n");
+				cdaudio_error("Failed to open CD.\n");
 				cleanup_on_error();
 				return NULL;
 			}
-			if (debug)
-				printf("cdaudio-ng: found cd drive \"%s\" with audio capable media\n", *ppcd_drives);
+			CDDEBUG("found cd drive \"%s\" with audio capable media\n", *ppcd_drives);
 		}
 		else {
-			fprintf(stderr, "cdaudio-ng: unable find or access a cdda capable drive\n");
+			cdaudio_error("Unable to find or access a CDDA capable drive.\n");
 			cleanup_on_error();
 			return NULL;
 		}
@@ -303,10 +311,9 @@
 
 		/* limit read speed */
 	if (limitspeed > 0 && use_dae) {
-		if (debug)
-			printf("cdaudio-ng: setting drive speed limit to %dx\n", limitspeed);
+		CDDEBUG("setting drive speed limit to %dx\n", limitspeed);
 		if (cdio_set_speed(pcdio, limitspeed) != DRIVER_OP_SUCCESS)
-			fprintf(stderr, "cdaudio-ng: failed to set drive speed to %dx\n", limitspeed);
+			cdaudio_error("Failed to set drive speed to %dx.\n", limitspeed);
 	}
 
 		/* get general track initialization */
@@ -314,12 +321,11 @@
 	firsttrackno = cdio_get_first_track_num(pcdrom_drive->p_cdio);
 	lasttrackno = cdio_get_last_track_num(pcdrom_drive->p_cdio);
 	if (firsttrackno == CDIO_INVALID_TRACK || lasttrackno == CDIO_INVALID_TRACK) {
-		fprintf(stderr, "cdaudio-ng: failed to retrieve first/last track number\n");
+		cdaudio_error("Failed to retrieve first/last track number.\n");
 		cleanup_on_error();
 		return NULL;
 	}
-	if (debug)
-		printf("cdaudio-ng: first track is %d and last track is %d\n", firsttrackno, lasttrackno);
+	CDDEBUG("first track is %d and last track is %d\n", firsttrackno, lasttrackno);
 
 	g_free(trackinfo);
 	trackinfo = (trackinfo_t *) g_new(trackinfo_t, (lasttrackno + 1));
@@ -338,7 +344,7 @@
 		strcpy(trackinfo[trackno].genre, "");
 
 		if (trackinfo[trackno].startlsn == CDIO_INVALID_LSN || trackinfo[trackno].endlsn == CDIO_INVALID_LSN) {
-			fprintf(stderr, "cdaudio-ng: failed to retrieve stard/end lsn for track %d\n", trackno);
+			cdaudio_error("Failed to retrieve stard/end lsn for track %d.\n", trackno);
 			cleanup_on_error();
 			return NULL;
 		}
@@ -352,10 +358,9 @@
 	if (use_cddb) {
 		pcddb_conn = cddb_new();
 		if (pcddb_conn == NULL)
-			fprintf(stderr, "cdaudio-ng: failed to create the cddb connection\n");
+			cdaudio_error("Failed to create the cddb connection.\n");
 		else {
-			if (debug)
-				printf("cdaudio-ng: getting cddb info\n");
+			CDDEBUG("getting CDDB info\n");
 
 			cddb_set_server_name(pcddb_conn, cddb_server);
 			cddb_set_server_port(pcddb_conn, cddb_port);
@@ -374,13 +379,12 @@
 
 			int matches;
 			if ((matches = cddb_query(pcddb_conn, pcddb_disc)) == -1) {
-				fprintf(stderr, "cdaudio-ng: failed to query the cddb server: %s\n", cddb_error_str(cddb_errno(pcddb_conn)));
+				cdaudio_error("Failed to query the CDDB server: %s\n", cddb_error_str(cddb_errno(pcddb_conn)));
 				cddb_disc_destroy(pcddb_disc);
 				pcddb_disc = NULL;
 			}
 			else {
-				if (debug)
-					printf("cdaudio-ng: discid = %X, category = \"%s\"\n", cddb_disc_get_discid(pcddb_disc), cddb_disc_get_category_str(pcddb_disc));
+				CDDEBUG("discid = %X, category = \"%s\"\n", cddb_disc_get_discid(pcddb_disc), cddb_disc_get_category_str(pcddb_disc));
 
 				cddb_read(pcddb_conn, pcddb_disc);
 				if (cddb_errno(pcddb_conn) != CDDB_ERR_OK) {
@@ -389,8 +393,7 @@
 					pcddb_disc = NULL;
 				}
 				else {
-					if (debug)
-						printf("cdaudio-ng: we have got the cddb info\n");
+					CDDEBUG("we have got the cddb info\n");
 
 					strcpy(trackinfo[0].performer, cddb_disc_get_artist(pcddb_disc));
 					strcpy(trackinfo[0].name, cddb_disc_get_title(pcddb_disc));
@@ -402,12 +405,10 @@
 
 		/* adding trackinfo[0] information (the disc) */
 	if (use_cdtext) {
-		if (debug)
-			printf("cdaudio-ng: getting cd-text information for disc\n");
+		CDDEBUG("getting cd-text information for disc\n");
 		cdtext_t *pcdtext = cdio_get_cdtext(pcdrom_drive->p_cdio, 0);
 		if (pcdtext == NULL || pcdtext->field[CDTEXT_TITLE] == NULL) {
-			if (debug)
-				printf("cdaudio-ng: no cd-text available for disc\n");
+			CDDEBUG("no cd-text available for disc\n");
 		}
 		else {
 			strcpy(trackinfo[0].performer, pcdtext->field[CDTEXT_PERFORMER] != NULL ? pcdtext->field[CDTEXT_PERFORMER] : "");
@@ -422,12 +423,10 @@
 		list = g_list_append(list, g_strdup_printf("track%02u.cda", trackno));
 		cdtext_t *pcdtext = NULL;
 		if (use_cdtext) {
-			if (debug)
-				printf("cdaudio-ng: getting cd-text information for track %d\n", trackno);
+			CDDEBUG("getting cd-text information for track %d\n", trackno);
 			pcdtext = cdio_get_cdtext(pcdrom_drive->p_cdio, trackno);
 			if (pcdtext == NULL || pcdtext->field[CDTEXT_PERFORMER] == NULL) {
-				if (debug)
-					printf("cdaudio-ng: no cd-text available for track %d\n", trackno);
+				CDDEBUG("no cd-text available for track %d\n", trackno);
 				pcdtext = NULL;
 			}
 		}
@@ -455,11 +454,11 @@
 
 	}
 
-	if (debug) {
-		printf("cdaudio-ng: disc has : performer = \"%s\", name = \"%s\", genre = \"%s\"\n",
+	if (use_debug) {
+		CDDEBUG("disc has : performer = \"%s\", name = \"%s\", genre = \"%s\"\n",
 			   trackinfo[0].performer, trackinfo[0].name, trackinfo[0].genre);
 		for (trackno = firsttrackno; trackno <= lasttrackno; trackno++) {
-			printf("cdaudio-ng: track %d has : performer = \"%s\", name = \"%s\", genre = \"%s\", startlsn = %d, endlsn = %d\n",
+			CDDEBUG("track %d has : performer = \"%s\", name = \"%s\", genre = \"%s\", startlsn = %d, endlsn = %d\n",
 				trackno, trackinfo[trackno].performer, trackinfo[trackno].name, trackinfo[trackno].genre, trackinfo[trackno].startlsn, trackinfo[trackno].endlsn);
 		}
 	}
@@ -476,34 +475,30 @@
 {
 	Tuple *tuple;
 	gchar *title;
-
-	if (debug)
-		printf("cdaudio-ng: cdaudio_play_file(\"%s\")\n", pinputplayback->filename);
+	
+	CDDEBUG("cdaudio_play_file(\"%s\")\n", pinputplayback->filename);
 
 	pglobalinputplayback = pinputplayback;
 
 	if (trackinfo == NULL) {
-		if (debug)
-			printf("cdaudio-ng: no cd information, scanning\n");
+		CDDEBUG("no CD information, scanning\n");
 		cdaudio_scan_dir(CDDA_DEFAULT);
 	}
 
 	if (cdio_get_media_changed(pcdio)) {
-		if (debug)
-			printf("cdaudio-ng: cd changed, rescanning\n");
+		CDDEBUG("CD changed, rescanning\n");
 		cdaudio_scan_dir(CDDA_DEFAULT);
 	}
 
 	if (trackinfo == NULL) {
-		if (debug)
-			printf("cdaudio-ng: no cd information can be retrieved, aborting\n");
+		CDDEBUG("no CD information can be retrieved, aborting\n");
 		pinputplayback->playing = FALSE;
 		return;
 	}
 
 	int trackno = find_trackno_from_filename(pinputplayback->filename);
 	if (trackno < firsttrackno || trackno > lasttrackno) {
-		fprintf(stderr, "cdaudio-ng: trackno %d is out of range [%d..%d]\n", trackno, firsttrackno, lasttrackno);
+		cdaudio_error("Track #%d is out of range [%d..%d]\n", trackno, firsttrackno, lasttrackno);
 		cleanup_on_error();
 		return;
 	}
@@ -520,23 +515,21 @@
 	tuple_free(tuple);
 
 	if (use_dae) {
-		if (debug)
-			printf("cdaudio-ng: using digital audio extraction\n");
+		CDDEBUG("using digital audio extraction\n");
 
 		if (pdae_params != NULL) {
-			fprintf(stderr, "cdaudio-ng: dae playback seems to be already started\n");
+			cdaudio_error("DAE playback seems to be already started.\n");
 			return;
 		}
 
 		if (pinputplayback->output->open_audio(FMT_S16_LE, 44100, 2) == 0) {
-			fprintf(stderr, "cdaudio-ng: failed open audio output\n");
+			cdaudio_error("Failed to open audio output.\n");
 			cleanup_on_error();
 			return;
 		}
 
 		/*
-		if (debug)
-			printf("cdaudio-ng: starting dae thread...\n");
+		CDDEBUG("starting dae thread...\n");
 		*/
 		pdae_params = (dae_params_t *) g_new(dae_params_t, 1);
 		pdae_params->startlsn = trackinfo[trackno].startlsn;
@@ -549,14 +542,13 @@
 		dae_play_loop(pdae_params);
 	}
 	else {
-		if (debug)
-			printf("cdaudio-ng: not using digital audio extraction\n");
+		CDDEBUG("not using digital audio extraction\n");
 
 		msf_t startmsf, endmsf;
 		cdio_lsn_to_msf(trackinfo[trackno].startlsn, &startmsf);
 		cdio_lsn_to_msf(trackinfo[trackno].endlsn, &endmsf);
 		if (cdio_audio_play_msf(pcdio, &startmsf, &endmsf) != DRIVER_OP_SUCCESS) {
-			fprintf(stderr, "cdaudio-ng: failed to play analog audio cd\n");
+			cdaudio_error("Failed to play analog audio CD.\n");
 			cleanup_on_error();
 			return;
 		}
@@ -565,8 +557,7 @@
 
 void cdaudio_stop(InputPlayback *pinputplayback)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_stop(\"%s\")\n", pinputplayback != NULL ? pinputplayback->filename : "N/A");
+	CDDEBUG("cdaudio_stop(\"%s\")\n", pinputplayback != NULL ? pinputplayback->filename : "N/A");
 
 	pglobalinputplayback = NULL;
 
@@ -587,7 +578,7 @@
 	}
 	else {
 		if (cdio_audio_stop(pcdio) != DRIVER_OP_SUCCESS) {
-			fprintf(stderr, "cdaudio-ng: failed to stop analog cd\n");
+			cdaudio_error("Failed to stop analog CD.\n");
 			return;
 		}
 	}
@@ -595,14 +586,13 @@
 
 void cdaudio_pause(InputPlayback *pinputplayback, gshort paused)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_pause(\"%s\", %d)\n", pinputplayback->filename, paused);
+	CDDEBUG("cdaudio_pause(\"%s\", %d)\n", pinputplayback->filename, paused);
 
 	if (!is_paused) {
 		is_paused = TRUE;
 		if (!use_dae)
 			if (cdio_audio_pause(pcdio) != DRIVER_OP_SUCCESS) {
-				fprintf(stderr, "cdaudio-ng: failed to pause analog cd\n");
+				cdaudio_error("Failed to pause analog CD!\n");
 				cleanup_on_error();
 				return;
 			}
@@ -611,7 +601,7 @@
 		is_paused = FALSE;
 		if (!use_dae)
 			if (cdio_audio_resume(pcdio) != DRIVER_OP_SUCCESS) {
-				fprintf(stderr, "cdaudio-ng: failed to resume analog cd\n");
+				cdaudio_error("Failed to resume analog CD!\n");
 				cleanup_on_error();
 				return;
 			}
@@ -620,8 +610,7 @@
 
 void cdaudio_seek(InputPlayback *pinputplayback, gint time)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_seek(\"%s\", %d)\n", pinputplayback->filename, time);
+	CDDEBUG("cdaudio_seek(\"%s\", %d)\n", pinputplayback->filename, time);
 
 	if (playing_track == -1)
 		return;
@@ -638,7 +627,7 @@
 		cdio_lsn_to_msf(trackinfo[playing_track].endlsn, &endmsf);
 
 		if (cdio_audio_play_msf(pcdio, &startmsf, &endmsf) != DRIVER_OP_SUCCESS) {
-			fprintf(stderr, "cdaudio-ng: failed to play analog cd\n");
+			cdaudio_error("Failed to play analog CD\n");
 			cleanup_on_error();
 			return;
 		}
@@ -655,7 +644,7 @@
 	if (!use_dae) {
 		cdio_subchannel_t subchannel;
 		if (cdio_audio_read_subchannel(pcdio, &subchannel) != DRIVER_OP_SUCCESS) {
-			fprintf(stderr, "cdaudio-ng: failed to read analog cd subchannel\n");
+			cdaudio_error("Failed to read analog CD subchannel.\n");
 			cleanup_on_error();
 			return 0;
 		}
@@ -689,7 +678,7 @@
 	else {
 		cdio_audio_volume_t volume;
 		if (cdio_audio_get_volume(pcdio, &volume) != DRIVER_OP_SUCCESS) {
-			fprintf(stderr, "cdaudio-ng: failed to retrieve analog cd volume\n");
+			cdaudio_error("Failed to retrieve analog CD volume.\n");
 			cleanup_on_error();
 			*l = *r = 0;
 			return FALSE;
@@ -703,8 +692,7 @@
 
 gint cdaudio_set_volume(gint l, gint r)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_set_volume(%d, %d)\n", l, r);
+	CDDEBUG("cdaudio_set_volume(%d, %d)\n", l, r);
 
 	if (use_dae) {
 		return FALSE;
@@ -723,8 +711,7 @@
 
 void cdaudio_cleanup()
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_cleanup()\n");
+	CDDEBUG("cdaudio_cleanup()\n");
 
 	libcddb_shutdown();
 
@@ -750,14 +737,13 @@
 	bmp_cfg_db_set_string(db, "CDDA", "cddbserver", cddb_server);
 	bmp_cfg_db_set_int(db, "CDDA", "cddbport", cddb_port);
 	bmp_cfg_db_set_string(db, "CDDA", "device", device);
-	bmp_cfg_db_set_bool(db, "CDDA", "debug", debug);
+	bmp_cfg_db_set_bool(db, "CDDA", "debug", use_debug);
 	bmp_cfg_db_close(db);
 }
 
 void cdaudio_get_song_info(gchar *filename, gchar **title, gint *length)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_get_song_info(\"%s\")\n", filename);
+	CDDEBUG("cdaudio_get_song_info(\"%s\")\n", filename);
 
 	gint trackno = find_trackno_from_filename(filename);
 	Tuple *tuple = create_tuple_from_trackinfo(filename);
@@ -772,8 +758,7 @@
 
 Tuple *cdaudio_get_song_tuple(gchar *filename)
 {
-	if (debug)
-		printf("cdaudio-ng: cdaudio_get_song_tuple(\"%s\")\n", filename);
+	CDDEBUG("cdaudio_get_song_tuple(\"%s\")\n", filename);
 
 	return create_tuple_from_trackinfo(filename);
 }
@@ -848,8 +833,7 @@
 {
 	guchar *buffer = g_new(guchar, CDDA_DAE_FRAMES * CDIO_CD_FRAMESIZE_RAW);
 
-	if (debug)
-		printf("cdaudio-ng: dae started\n");
+	CDDEBUG("dae started\n");
 	cdio_lseek(pcdio, pdae_params->startlsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET);
 
 	gboolean output_paused = FALSE;
@@ -861,8 +845,7 @@
 			/* handle pause status */
 		if (is_paused) {
 			if (!output_paused) {
-				if (debug)
-					printf("cdaudio-ng: playback was not paused, pausing\n");
+				CDDEBUG("playback was not paused, pausing\n");
 				pdae_params->pplayback->output->pause(TRUE);
 				output_paused = TRUE;
 			}
@@ -871,8 +854,7 @@
 		}
 		else {
 			if (output_paused) {
-				if (debug)
-					printf("cdaudio-ng: playback was paused, resuming\n");
+				CDDEBUG("playback was paused, resuming\n");
 				pdae_params->pplayback->output->pause(FALSE);
 				output_paused = FALSE;
 			}
@@ -880,9 +862,8 @@
 
 			/* check if we have to seek */
 		if (pdae_params->seektime != -1) {
-			if (debug)
-				printf("cdaudio-ng: requested seek to %d ms\n", pdae_params->seektime);
-			gint newlsn = pdae_params->startlsn + pdae_params->seektime * 75 / 1000;
+			CDDEBUG("requested seek to %d ms\n", pdae_params->seektime);
+			gint newlsn = pdae_params->startlsn + ((pdae_params->seektime * 75) / 1000);
 			cdio_lseek(pcdio, newlsn * CDIO_CD_FRAMESIZE_RAW, SEEK_SET);
 			pdae_params->pplayback->output->flush(pdae_params->seektime);
 			pdae_params->currlsn = newlsn;
@@ -898,11 +879,11 @@
 		}
 
 		if (cdio_read_audio_sectors(pcdio, buffer, pdae_params->currlsn, lsncount) != DRIVER_OP_SUCCESS) {
-			fprintf(stderr, "cdaudio-ng: failed to read audio sector\n");
+			CDDEBUG("failed to read audio sector\n");
 			read_error_counter++;
 			if (read_error_counter >= 2) {
 				read_error_counter = 0;
-				fprintf(stderr, "cdaudio-ng: this cd can no longer be played, stopping\n");
+				cdaudio_error("This CD can no longer be played, stopping.\n");
 				break;
 			}
 		}
@@ -925,8 +906,7 @@
 		}
 		pdae_params->currlsn += lsncount;
 	}
-	if (debug)
-		printf("cdaudio-ng: dae ended\n");
+	CDDEBUG("dae ended\n");
 
 	pdae_params->pplayback->playing = FALSE;
 	pdae_params->pplayback->output->close_audio();
@@ -936,12 +916,12 @@
 	g_free(buffer);
 }
 
-gint calculate_track_length(gint startlsn, gint endlsn)
+static gint calculate_track_length(gint startlsn, gint endlsn)
 {
 	return ((endlsn - startlsn + 1) * 1000) / 75;
 }
 
-gint find_trackno_from_filename(gchar *filename)
+static gint find_trackno_from_filename(gchar *filename)
 {
 	gchar tracknostr[3];
 	if ((filename == NULL) || strlen(filename) <= 6)
@@ -952,7 +932,7 @@
 	return strtol(tracknostr, NULL, 10);
 }
 
-void cleanup_on_error()
+static void cleanup_on_error()
 {
 	if (playing_track != -1) {
 		playing_track = -1;