From 06d43e27c412083cf704af48ea40e5c589504240 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= <cgloeckner@freenet.de>
Date: Fri, 22 Jan 2016 09:39:50 +0100
Subject: stdcerr output overhaul

---
 drumgizmo/enginefactory.cc  |  6 ++++--
 drumgizmo/input/midifile.cc | 30 +++++++++++++++++++++++-------
 drumgizmo/output/alsa.cc    | 31 +++++++++++++++++++++++++------
 drumgizmo/output/wavfile.cc | 22 ++++++++++++++++++----
 4 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/drumgizmo/enginefactory.cc b/drumgizmo/enginefactory.cc
index 5fa230b..c9f2ef3 100644
--- a/drumgizmo/enginefactory.cc
+++ b/drumgizmo/enginefactory.cc
@@ -24,6 +24,8 @@
  *  along with DrumGizmo; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
+#include <iostream>
+
 #include "enginefactory.h"
 #include "jackclient.h"
 
@@ -61,7 +63,7 @@ InputEnginePtr createInputEngine(std::string const & name) {
 	
 	// todo: add more engines
 	
-	printf("Unsupported input engine: %s\n", name.c_str());
+	std::cerr << "Unsupported input engine '" << name << "'\n";
 	return nullptr;
 }
 
@@ -84,6 +86,6 @@ OutputEnginePtr createOutputEngine(std::string const & name) {
 	
 	// todo: add more engines
 	
-	printf("Unsupported output engine: %s\n", name.c_str());
+	std::cerr << "Unsupported output engine '" << name << "'\n";
 	return nullptr;
 }
diff --git a/drumgizmo/input/midifile.cc b/drumgizmo/input/midifile.cc
index d1748dc..323a198 100644
--- a/drumgizmo/input/midifile.cc
+++ b/drumgizmo/input/midifile.cc
@@ -24,6 +24,8 @@
  *  along with DrumGizmo; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
+#include <iostream>
+
 #include "midifile.h"
 
 int const NOTE_ON = 0x90;
@@ -51,27 +53,28 @@ bool MidifileInputEngine::isMidiEngine() {
 
 bool MidifileInputEngine::init(Instruments& instruments) {
 	if (file == "") {
-		fprintf(stderr, "Missing midifile argument 'file'\n");
+		std::cerr << "[MidifileInputEngine] Missing midi filename\n";
 		return false;
 	}
 	if (midimap == "") {
-		fprintf(stderr, "Missing midimapfile argument 'midimap'.\n");
+		std::cerr << "[MidifileInputEngine] Missing midimap filename\n";
 		return false;
 	}
 	smf = smf_load(file.c_str());
 	if (smf == nullptr) {
-		fprintf(stderr, "Could not open midifile '%s'.\n", file.c_str());
+		std::cerr << "[MidifileInputEngine] Failed to load midifile '"
+			<< file << "'\n";
 		return false;
 	}
 	MidiMapParser p{midimap};
 	if (p.parse()) {
-		fprintf(stderr, "Could not parse midimapfile '%s'.\n", midimap.c_str());
+		std::cerr << "[MidifileInputEngine] Failed to parse midimap '"
+			<< midimap << "'\n";
 		return false;
 	}
 	midi_mapper.midimap = p.midimap;
 	for (auto i = 0u; i < instruments.size(); ++i) {
 		auto name = instruments[i]->name();
-		printf("%d : %s\n", i, name.c_str());
 		midi_mapper.instrmap[name] = i;
 	}
 	return true;
@@ -79,15 +82,28 @@ bool MidifileInputEngine::init(Instruments& instruments) {
 
 void MidifileInputEngine::setParm(std::string parm, std::string value) {
 	if(parm == "file") {
+		// apply midi input filename
 		file = value;
+		
 	} else if(parm == "speed") {
-		speed = std::stof(value);
+		// try to apply speed
+		 try {
+			speed = std::stof(value);
+		} catch (...) {
+			std::cerr << "[MidifileInputEngine] Invalid speed "
+				<< value << "\n";
+		}
 	} else if (parm == "midimap") {
+		// apply midimap filename
 		midimap = value;
+		
 	} else if (parm == "loop") {
+		// apply looping
 		loop = true;
+		
 	} else {
-		printf("Unsupported midifile parameter '%s'\n", parm.c_str());
+		std::cerr << "[MidifileInputEngine] Unsupported parameter '"
+			<< parm << "'\n";
 	}
 }
 
diff --git a/drumgizmo/output/alsa.cc b/drumgizmo/output/alsa.cc
index f1a6806..14a9c6d 100644
--- a/drumgizmo/output/alsa.cc
+++ b/drumgizmo/output/alsa.cc
@@ -24,6 +24,8 @@
  *  along with DrumGizmo; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
+#include <iostream>
+
 #include "alsa.h"
 
 int const BUFFER_SIZE = 40960;
@@ -55,7 +57,7 @@ AlsaOutputEngine::AlsaOutputEngine()
 }
 
 AlsaOutputEngine::~AlsaOutputEngine() {
-	// note: cannot release `params` (seg fault but why?)
+	// note: do NOT release `params`, it was allocated by `alloca()`
 	
 	if (handle != nullptr) {
 		snd_pcm_close(handle);
@@ -69,7 +71,8 @@ bool AlsaOutputEngine::init(Channels channels) {
 		AlsaInitError::test(value, "snd_pcm_open");
 		num_channels = channels.size();
 		if (handle == nullptr) {
-			printf("No handle!\n");
+			std::cerr << "[AlsaOutputEngine] Failed to acquire "
+				<< "hardware handle\n";
 			return false;
 		}
 		// Allocate and init a hardware parameters object
@@ -91,8 +94,8 @@ bool AlsaOutputEngine::init(Channels channels) {
 		AlsaInitError::test(value, "snd_pcm_hw_params");
 		
 	} catch (AlsaInitError const & error) {
-		printf("%s failed: %s\n", error.msg.c_str(), snd_strerror(error.code));
-		fflush(stdout);
+		std::cerr << "[AlsaOutputEngine] " << error.msg << " failed: "
+			<< snd_strerror(error.code) << std::endl;
 		return false;
 	}
 	
@@ -104,11 +107,27 @@ bool AlsaOutputEngine::init(Channels channels) {
 
 void AlsaOutputEngine::setParm(std::string parm, std::string value) {
 	if (parm == "dev") {
+		// apply hardware device name
 		dev = value;
+		
 	} else if (parm == "frames") {
-		frames = std::stoi(value);
+		// try to apply hardware buffer size
+		try {
+			frames = std::stoi(value);
+		} catch (...) {
+			std::cerr << "[AlsaOutputEngine] Invalid buffer size "
+				<< value << "\n";
+		}
 	} else if (parm == "srate") {
-		srate = std::stoi(value);
+		try {
+			srate = std::stoi(value);
+		} catch (...) {
+			std::cerr << "[AlsaOutputEngine] Invalid samplerate "
+				<< value << "\n";
+		}
+	} else {
+		std::cerr << "[AlsaOutputEngine] Unsupported parameter '"
+			<< parm << "'\n";
 	}
 }
 
diff --git a/drumgizmo/output/wavfile.cc b/drumgizmo/output/wavfile.cc
index ae8b3f1..d8b8d99 100644
--- a/drumgizmo/output/wavfile.cc
+++ b/drumgizmo/output/wavfile.cc
@@ -24,6 +24,8 @@
  *  along with DrumGizmo; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
+#include <iostream>
+
 #include "wavfile.h"
 
 WavfileOutputEngine::WavfileOutputEngine()
@@ -54,7 +56,8 @@ bool WavfileOutputEngine::init(Channels data) {
 		auto fname = file + data[i].name + "-" + std::to_string(i) + ".wav";
 		channels[i] = sf_open(fname.c_str(), SFM_WRITE, &info);
 		if (channels[i] == nullptr) {
-			printf("Write error...\n");
+			std::cerr << "[WaffileOutputEngine] Failed to initialize "
+				<< "channel #" << i << "\n";
 			return false;
 		}
 	}
@@ -63,11 +66,21 @@ bool WavfileOutputEngine::init(Channels data) {
 
 void WavfileOutputEngine::setParm(std::string parm, std::string value) {
 	if (parm == "file") {
+		// apply output filename prefix
 		file = value;
+		
 	} else if (parm == "srate") {
-		info.samplerate = std::stoi(value);
+		// try to apply samplerate
+		try {
+			info.samplerate = std::stoi(value);
+		} catch (...) {
+			std::cerr << "[WavfileOutputEngine] Invalid samplerate "
+				<< value << "\n";
+		}
+		
 	} else {
-		printf("Unsupported wavfile parameter '%s'\n", parm.c_str());
+		std::cerr << "[WavfileOutputEngine] Unsupported parameter '"
+			<< parm << "'\n";
 	}
 }
 
@@ -83,7 +96,8 @@ void WavfileOutputEngine::pre(size_t nsamples) {
 
 void WavfileOutputEngine::run(int ch, sample_t* samples, size_t nsamples) {
 	if (static_cast<unsigned int>(ch) >= channels.size()) {
-		printf("Invalid channel %d (%d channels available)", ch, static_cast<int>(channels.size()));
+		std::cerr << "[WavfileOutputEngine] cannot access channel #"
+			<< ch << " (" << channels.size() << " channels available)\n";
 		return;
 	}
 	
-- 
cgit v1.2.3