run_convert.cxx 3.57 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * This program is a command line interface to MPD's PCM conversion
 * library (pcm_convert.c).
 *
 */

26
#include "ConfigGlue.hxx"
27 28
#include "pcm/AudioParser.hxx"
#include "pcm/AudioFormat.hxx"
29
#include "pcm/Convert.hxx"
30
#include "fs/Path.hxx"
31
#include "util/ConstBuffer.hxx"
32
#include "util/StaticFifoBuffer.hxx"
33
#include "util/OptionDef.hxx"
34
#include "util/OptionParser.hxx"
35
#include "util/PrintException.hxx"
36 37
#include "Log.hxx"
#include "LogBackend.hxx"
38

39
#include <cassert>
40 41
#include <stdexcept>

42
#include <stddef.h>
43
#include <stdlib.h>
44
#include <stdio.h>
45 46
#include <unistd.h>

47 48
struct CommandLine {
	AudioFormat in_audio_format, out_audio_format;
49

50 51
	Path config_path = nullptr;

52 53 54 55
	bool verbose = false;
};

enum Option {
56
	OPTION_CONFIG,
57 58 59 60
	OPTION_VERBOSE,
};

static constexpr OptionDef option_defs[] = {
61
	{"config", 0, true, "Load a MPD configuration file"},
62
	{"verbose", 'v', false, "Verbose logging"},
63 64 65 66 67 68 69
};

static CommandLine
ParseCommandLine(int argc, char **argv)
{
	CommandLine c;

70 71 72
	OptionParser option_parser(option_defs, argc, argv);
	while (auto o = option_parser.Next()) {
		switch (Option(o.index)) {
73 74 75 76
		case OPTION_CONFIG:
			c.config_path = Path::FromFS(o.value);
			break;

77 78 79 80
		case OPTION_VERBOSE:
			c.verbose = true;
			break;
		}
81 82
	}

83 84 85
	auto args = option_parser.GetRemaining();
	if (args.size != 2)
		throw std::runtime_error("Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT");
86

87 88 89 90 91
	c.in_audio_format = ParseAudioFormat(args[0], false);
	c.out_audio_format = c.in_audio_format.WithMask(ParseAudioFormat(args[1], false));
	return c;
}

92 93 94 95 96 97 98 99 100 101 102
class GlobalInit {
	const ConfigData config;

public:
	explicit GlobalInit(Path config_path)
		:config(AutoLoadConfigFile(config_path))
	{
		pcm_convert_global_init(config);
	}
};

103 104 105 106
int
main(int argc, char **argv)
try {
	const auto c = ParseCommandLine(argc, argv);
107

108
	SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
109
	const GlobalInit init(c.config_path);
110

111
	const size_t in_frame_size = c.in_audio_format.GetFrameSize();
112

113
	PcmConvert state(c.in_audio_format, c.out_audio_format);
114

115
	StaticFifoBuffer<uint8_t, 4096> buffer;
116 117

	while (true) {
118 119
		{
			const auto dest = buffer.Write();
120
			assert(!dest.empty());
121

122 123 124
			ssize_t nbytes = read(0, dest.data, dest.size);
			if (nbytes <= 0)
				break;
125

126 127
			buffer.Append(nbytes);
		}
128

129
		auto src = buffer.Read();
130
		assert(!src.empty());
131

132
		src.size -= src.size % in_frame_size;
133
		if (src.empty())
134 135
			continue;

136
		buffer.Consume(src.size);
137

138
		auto output = state.Convert({src.data, src.size});
139

Rosen Penev's avatar
Rosen Penev committed
140
		[[maybe_unused]] ssize_t ignored = write(1, output.data,
141
						   output.size);
142 143
	}

144 145 146 147 148
	while (true) {
		auto output = state.Flush();
		if (output.IsNull())
			break;

Rosen Penev's avatar
Rosen Penev committed
149
		[[maybe_unused]] ssize_t ignored = write(1, output.data,
150 151 152
						   output.size);
	}

153
	return EXIT_SUCCESS;
154 155
} catch (...) {
	PrintException(std::current_exception());
156
	return EXIT_FAILURE;
157
}