encoder/wave: convert to C++

This commit is contained in:
Max Kellermann 2013-07-30 08:48:53 +02:00
parent dac740ee17
commit da1f4b3ede
4 changed files with 62 additions and 28 deletions

View File

@ -644,7 +644,9 @@ libencoder_plugins_a_SOURCES = \
src/EncoderList.cxx src/EncoderList.hxx src/EncoderList.cxx src/EncoderList.hxx
if ENABLE_WAVE_ENCODER if ENABLE_WAVE_ENCODER
libencoder_plugins_a_SOURCES += src/encoder/wave_encoder.c libencoder_plugins_a_SOURCES += \
src/encoder/WaveEncoderPlugin.cxx \
src/encoder/WaveEncoderPlugin.hxx
endif endif
if ENABLE_VORBIS_ENCODER if ENABLE_VORBIS_ENCODER

View File

@ -21,6 +21,7 @@
#include "EncoderList.hxx" #include "EncoderList.hxx"
#include "encoder_plugin.h" #include "encoder_plugin.h"
#include "encoder/NullEncoderPlugin.hxx" #include "encoder/NullEncoderPlugin.hxx"
#include "encoder/WaveEncoderPlugin.hxx"
#include "encoder/VorbisEncoderPlugin.hxx" #include "encoder/VorbisEncoderPlugin.hxx"
#include "encoder/OpusEncoderPlugin.hxx" #include "encoder/OpusEncoderPlugin.hxx"
#include "encoder/FlacEncoderPlugin.hxx" #include "encoder/FlacEncoderPlugin.hxx"
@ -29,7 +30,6 @@
extern const struct encoder_plugin lame_encoder_plugin; extern const struct encoder_plugin lame_encoder_plugin;
extern const struct encoder_plugin twolame_encoder_plugin; extern const struct encoder_plugin twolame_encoder_plugin;
extern const struct encoder_plugin wave_encoder_plugin;
const struct encoder_plugin *const encoder_plugins[] = { const struct encoder_plugin *const encoder_plugins[] = {
&null_encoder_plugin, &null_encoder_plugin,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,21 +18,28 @@
*/ */
#include "config.h" #include "config.h"
#include "WaveEncoderPlugin.hxx"
#include "encoder_api.h" #include "encoder_api.h"
#include "encoder_plugin.h" #include "encoder_plugin.h"
#include "util/fifo_buffer.h" #include "util/fifo_buffer.h"
extern "C" {
#include "util/growing_fifo.h" #include "util/growing_fifo.h"
}
#include <glib.h> #include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
struct wave_encoder { struct WaveEncoder {
struct encoder encoder; struct encoder encoder;
unsigned bits; unsigned bits;
struct fifo_buffer *buffer; struct fifo_buffer *buffer;
WaveEncoder() {
encoder_struct_init(&encoder, &wave_encoder_plugin);
}
}; };
struct wave_header { struct wave_header {
@ -84,18 +91,14 @@ static struct encoder *
wave_encoder_init(gcc_unused const struct config_param *param, wave_encoder_init(gcc_unused const struct config_param *param,
gcc_unused GError **error) gcc_unused GError **error)
{ {
struct wave_encoder *encoder; WaveEncoder *encoder = new WaveEncoder();
encoder = g_new(struct wave_encoder, 1);
encoder_struct_init(&encoder->encoder, &wave_encoder_plugin);
return &encoder->encoder; return &encoder->encoder;
} }
static void static void
wave_encoder_finish(struct encoder *_encoder) wave_encoder_finish(struct encoder *_encoder)
{ {
struct wave_encoder *encoder = (struct wave_encoder *)_encoder; WaveEncoder *encoder = (WaveEncoder *)_encoder;
g_free(encoder); g_free(encoder);
} }
@ -105,7 +108,7 @@ wave_encoder_open(struct encoder *_encoder,
gcc_unused struct audio_format *audio_format, gcc_unused struct audio_format *audio_format,
gcc_unused GError **error) gcc_unused GError **error)
{ {
struct wave_encoder *encoder = (struct wave_encoder *)_encoder; WaveEncoder *encoder = (WaveEncoder *)_encoder;
assert(audio_format_valid(audio_format)); assert(audio_format_valid(audio_format));
@ -133,7 +136,7 @@ wave_encoder_open(struct encoder *_encoder,
} }
encoder->buffer = growing_fifo_new(); encoder->buffer = growing_fifo_new();
struct wave_header *header = wave_header *header = (wave_header *)
growing_fifo_write(&encoder->buffer, sizeof(*header)); growing_fifo_write(&encoder->buffer, sizeof(*header));
/* create PCM wave header in initial buffer */ /* create PCM wave header in initial buffer */
@ -150,7 +153,7 @@ wave_encoder_open(struct encoder *_encoder,
static void static void
wave_encoder_close(struct encoder *_encoder) wave_encoder_close(struct encoder *_encoder)
{ {
struct wave_encoder *encoder = (struct wave_encoder *)_encoder; WaveEncoder *encoder = (WaveEncoder *)_encoder;
fifo_buffer_free(encoder->buffer); fifo_buffer_free(encoder->buffer);
} }
@ -200,9 +203,9 @@ wave_encoder_write(struct encoder *_encoder,
const void *src, size_t length, const void *src, size_t length,
gcc_unused GError **error) gcc_unused GError **error)
{ {
struct wave_encoder *encoder = (struct wave_encoder *)_encoder; WaveEncoder *encoder = (WaveEncoder *)_encoder;
void *dst = growing_fifo_write(&encoder->buffer, length); uint8_t *dst = (uint8_t *)growing_fifo_write(&encoder->buffer, length);
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN) #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
switch (encoder->bits) { switch (encoder->bits) {
@ -212,7 +215,7 @@ wave_encoder_write(struct encoder *_encoder,
memcpy(dst, src, length); memcpy(dst, src, length);
break; break;
case 24: case 24:
length = pcm24_to_wave(dst, src, length); length = pcm24_to_wave(dst, (const uint32_t *)src, length);
break; break;
} }
#elif (G_BYTE_ORDER == G_BIG_ENDIAN) #elif (G_BYTE_ORDER == G_BIG_ENDIAN)
@ -221,13 +224,13 @@ wave_encoder_write(struct encoder *_encoder,
memcpy(dst, src, length); memcpy(dst, src, length);
break; break;
case 16: case 16:
length = pcm16_to_wave(dst, src, length); length = pcm16_to_wave(dst, (const uint16_t *)src, length);
break; break;
case 24: case 24:
length = pcm24_to_wave(dst, src, length); length = pcm24_to_wave(dst, (const uint32_t *)src, length);
break; break;
case 32: case 32:
length = pcm32_to_wave(dst, src, length); length = pcm32_to_wave(dst, (const uint32_t *)src, length);
break; break;
} }
#else #else
@ -241,7 +244,7 @@ wave_encoder_write(struct encoder *_encoder,
static size_t static size_t
wave_encoder_read(struct encoder *_encoder, void *dest, size_t length) wave_encoder_read(struct encoder *_encoder, void *dest, size_t length)
{ {
struct wave_encoder *encoder = (struct wave_encoder *)_encoder; WaveEncoder *encoder = (WaveEncoder *)_encoder;
size_t max_length; size_t max_length;
const void *src = fifo_buffer_read(encoder->buffer, &max_length); const void *src = fifo_buffer_read(encoder->buffer, &max_length);
@ -263,12 +266,16 @@ wave_encoder_get_mime_type(gcc_unused struct encoder *_encoder)
} }
const struct encoder_plugin wave_encoder_plugin = { const struct encoder_plugin wave_encoder_plugin = {
.name = "wave", "wave",
.init = wave_encoder_init, wave_encoder_init,
.finish = wave_encoder_finish, wave_encoder_finish,
.open = wave_encoder_open, wave_encoder_open,
.close = wave_encoder_close, wave_encoder_close,
.write = wave_encoder_write, nullptr,
.read = wave_encoder_read, nullptr,
.get_mime_type = wave_encoder_get_mime_type, nullptr,
nullptr,
wave_encoder_write,
wave_encoder_read,
wave_encoder_get_mime_type,
}; };

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_ENCODER_WAVE_HXX
#define MPD_ENCODER_WAVE_HXX
extern const struct encoder_plugin wave_encoder_plugin;
#endif