test_vorbis_encoder.cxx 2.53 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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
#include "EncoderList.hxx"
22
#include "EncoderPlugin.hxx"
23
#include "AudioFormat.hxx"
24
#include "ConfigData.hxx"
25
#include "stdbin.h"
26
#include "tag/Tag.hxx"
27
#include "util/Error.hxx"
28 29 30 31 32 33 34

#include <stddef.h>
#include <unistd.h>

static uint8_t zero[256];

static void
35
encoder_to_stdout(Encoder &encoder)
36 37 38 39
{
	size_t length;
	static char buffer[32768];

40
	while ((length = encoder_read(&encoder, buffer, sizeof(buffer))) > 0) {
41
		gcc_unused ssize_t ignored = write(1, buffer, length);
42 43 44 45
	}
}

int
46
main(gcc_unused int argc, gcc_unused char **argv)
47
{
48
	gcc_unused bool success;
49 50 51

	/* create the encoder */

52
	const auto plugin = encoder_plugin_get("vorbis");
53 54
	assert(plugin != NULL);

55
	config_param param;
56
	param.AddBlockParam("quality", "5.0", -1);
57

58
	const auto encoder = encoder_init(*plugin, param, IgnoreError());
59 60 61 62
	assert(encoder != NULL);

	/* open the encoder */

63
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
64
	success = encoder_open(encoder, audio_format, IgnoreError());
65 66
	assert(success);

67
	encoder_to_stdout(*encoder);
68

69 70
	/* write a block of data */

71
	success = encoder_write(encoder, zero, sizeof(zero), IgnoreError());
72 73
	assert(success);

74
	encoder_to_stdout(*encoder);
75 76 77

	/* write a tag */

78
	success = encoder_pre_tag(encoder, IgnoreError());
79 80
	assert(success);

81
	encoder_to_stdout(*encoder);
82

Max Kellermann's avatar
Max Kellermann committed
83 84 85
	Tag tag;
	tag.AddItem(TAG_ARTIST, "Foo");
	tag.AddItem(TAG_TITLE, "Bar");
86

87
	success = encoder_tag(encoder, &tag, IgnoreError());
88 89
	assert(success);

90
	encoder_to_stdout(*encoder);
91 92 93

	/* write another block of data */

94
	success = encoder_write(encoder, zero, sizeof(zero), IgnoreError());
95 96 97 98
	assert(success);

	/* finish */

99
	success = encoder_end(encoder, IgnoreError());
100 101
	assert(success);

102
	encoder_to_stdout(*encoder);
103 104 105 106

	encoder_close(encoder);
	encoder_finish(encoder);
}