run_convert.cxx 2.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 26
 * 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).
 *
 */

#include "config.h"
27
#include "AudioParser.hxx"
28
#include "AudioFormat.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "pcm/PcmConvert.hxx"
30
#include "config/ConfigGlobal.hxx"
31
#include "util/ConstBuffer.hxx"
32
#include "util/StaticFifoBuffer.hxx"
33
#include "util/Error.hxx"
34
#include "Log.hxx"
35
#include "stdbin.h"
36

37
#include <assert.h>
38
#include <stddef.h>
39
#include <stdlib.h>
40 41 42
#include <unistd.h>

const char *
43 44
config_get_string(gcc_unused enum ConfigOption option,
		  const char *default_value)
45 46 47 48 49 50
{
	return default_value;
}

int main(int argc, char **argv)
{
51
	AudioFormat in_audio_format, out_audio_format;
52 53

	if (argc != 3) {
54 55
		fprintf(stderr,
			"Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT\n");
56 57 58
		return 1;
	}

59
	Error error;
60
	if (!audio_format_parse(in_audio_format, argv[1],
61
				false, error)) {
62 63
		LogError(error, "Failed to parse audio format");
		return EXIT_FAILURE;
64 65
	}

66 67
	AudioFormat out_audio_format_mask;
	if (!audio_format_parse(out_audio_format_mask, argv[2],
68
				true, error)) {
69 70
		LogError(error, "Failed to parse audio format");
		return EXIT_FAILURE;
71 72
	}

73
	out_audio_format = in_audio_format;
74
	out_audio_format.ApplyMask(out_audio_format_mask);
75

76
	const size_t in_frame_size = in_audio_format.GetFrameSize();
77

78
	PcmConvert state;
79
	if (!state.Open(in_audio_format, out_audio_format_mask, error)) {
80
		LogError(error, "Failed to open PcmConvert");
81 82
		return EXIT_FAILURE;
	}
83

84
	StaticFifoBuffer<uint8_t, 4096> buffer;
85 86

	while (true) {
87 88 89
		{
			const auto dest = buffer.Write();
			assert(!dest.IsEmpty());
90

91 92 93
			ssize_t nbytes = read(0, dest.data, dest.size);
			if (nbytes <= 0)
				break;
94

95 96
			buffer.Append(nbytes);
		}
97

98 99
		auto src = buffer.Read();
		assert(!src.IsEmpty());
100

101 102
		src.size -= src.size % in_frame_size;
		if (src.IsEmpty())
103 104
			continue;

105
		buffer.Consume(src.size);
106

107 108
		auto output = state.Convert({src.data, src.size}, error);
		if (output.IsNull()) {
109
			state.Close();
110 111
			LogError(error, "Failed to convert");
			return EXIT_FAILURE;
112 113
		}

114 115
		gcc_unused ssize_t ignored = write(1, output.data,
						   output.size);
116 117
	}

118 119
	state.Close();

120
	return EXIT_SUCCESS;
121
}