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

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

32 33
#include <memory>

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

static uint8_t zero[256];

int
40
main(gcc_unused int argc, gcc_unused char **argv)
41
try {
42 43
	/* create the encoder */

44
	const auto plugin = encoder_plugin_get("vorbis");
45 46
	assert(plugin != NULL);

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

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

53
	/* open the encoder */
54

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

59
	StdioOutputStream os(stdout);
60

61
	EncoderToOutputStream(os, *encoder);
62

63
	/* write a block of data */
64

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

67
	EncoderToOutputStream(os, *encoder);
68

69
	/* write a tag */
70

71
	encoder->PreTag();
72

73
	EncoderToOutputStream(os, *encoder);
74

75
	Tag tag;
76

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

84
	encoder->SendTag(tag);
85

86
	EncoderToOutputStream(os, *encoder);
87

88
	/* write another block of data */
89

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

92
	/* finish */
93

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

97 98 99 100
	return EXIT_SUCCESS;
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
101
}