test_vorbis_encoder.cxx 2.42 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#include "encoder/EncoderList.hxx"
#include "encoder/EncoderPlugin.hxx"
22
#include "encoder/EncoderInterface.hxx"
23
#include "encoder/ToOutputStream.hxx"
24
#include "pcm/AudioFormat.hxx"
25
#include "config/Block.hxx"
26
#include "fs/io/StdioOutputStream.hxx"
27
#include "tag/Tag.hxx"
28
#include "tag/Builder.hxx"
29
#include "util/PrintException.hxx"
30

31
#include <cassert>
32 33
#include <memory>

34 35 36 37 38
#include <stddef.h>

static uint8_t zero[256];

int
Rosen Penev's avatar
Rosen Penev committed
39
main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
40
try {
41 42
	/* create the encoder */

43
	const auto plugin = encoder_plugin_get("vorbis");
44
	assert(plugin != nullptr);
45

46 47
	ConfigBlock block;
	block.AddBlockParam("quality", "5.0", -1);
48

49
	std::unique_ptr<PreparedEncoder> p_encoder(encoder_init(*plugin, block));
50
	assert(p_encoder != nullptr);
51

52
	/* open the encoder */
53

54
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
55
	std::unique_ptr<Encoder> encoder(p_encoder->Open(audio_format));
56
	assert(encoder != nullptr);
57

58
	StdioOutputStream os(stdout);
59

60
	EncoderToOutputStream(os, *encoder);
61

62
	/* write a block of data */
63

64
	encoder->Write(zero, sizeof(zero));
65

66
	EncoderToOutputStream(os, *encoder);
67

68
	/* write a tag */
69

70
	encoder->PreTag();
71

72
	EncoderToOutputStream(os, *encoder);
73

74
	Tag tag;
75

76 77 78 79 80 81
	{
		TagBuilder tag_builder;
		tag_builder.AddItem(TAG_ARTIST, "Foo");
		tag_builder.AddItem(TAG_TITLE, "Bar");
		tag_builder.Commit(tag);
	}
82

83
	encoder->SendTag(tag);
84

85
	EncoderToOutputStream(os, *encoder);
86

87
	/* write another block of data */
88

89
	encoder->Write(zero, sizeof(zero));
90

91
	/* finish */
92

93
	encoder->End();
94
	EncoderToOutputStream(os, *encoder);
95

96
	return EXIT_SUCCESS;
97 98
} catch (...) {
	PrintException(std::current_exception());
99
	return EXIT_FAILURE;
100
}