Commit e9b71a0d authored by Max Kellermann's avatar Max Kellermann

MusicChunk: move functions to methods

parent efbfe66f
......@@ -429,10 +429,10 @@ decoder_data(struct decoder *decoder,
return dc->command;
}
void *dest = music_chunk_write(chunk, &dc->out_audio_format,
decoder->timestamp -
dc->song->start_ms / 1000.0,
kbit_rate, &nbytes);
void *dest = chunk->Write(dc->out_audio_format,
decoder->timestamp -
dc->song->start_ms / 1000.0,
kbit_rate, &nbytes);
if (dest == NULL) {
/* the chunk is full, flush it */
decoder_flush_chunk(decoder);
......@@ -451,7 +451,7 @@ decoder_data(struct decoder *decoder,
/* expand the music pipe chunk */
full = music_chunk_expand(chunk, &dc->out_audio_format, nbytes);
full = chunk->Expand(dc->out_audio_format, nbytes);
if (full) {
/* the chunk is full, flush it */
decoder_flush_chunk(decoder);
......
......@@ -88,7 +88,7 @@ decoder_flush_chunk(struct decoder *decoder)
assert(decoder != NULL);
assert(decoder->chunk != NULL);
if (music_chunk_is_empty(decoder->chunk))
if (decoder->chunk->IsEmpty())
music_buffer_return(dc->buffer, decoder->chunk);
else
music_pipe_push(dc->pipe, decoder->chunk);
......
......@@ -27,79 +27,61 @@ extern "C" {
#include <assert.h>
void
music_chunk_init(struct music_chunk *chunk)
music_chunk::~music_chunk()
{
chunk->other = NULL;
chunk->length = 0;
chunk->tag = NULL;
chunk->replay_gain_serial = 0;
}
void
music_chunk_free(struct music_chunk *chunk)
{
if (chunk->tag != NULL)
tag_free(chunk->tag);
if (tag != NULL)
tag_free(tag);
}
#ifndef NDEBUG
bool
music_chunk_check_format(const struct music_chunk *chunk,
const struct audio_format *audio_format)
music_chunk::CheckFormat(const struct audio_format &other_format) const
{
assert(chunk != NULL);
assert(audio_format != NULL);
assert(audio_format_valid(audio_format));
assert(audio_format_valid(&other_format));
return chunk->length == 0 ||
audio_format_equals(&chunk->audio_format, audio_format);
return length == 0 ||
audio_format_equals(&audio_format, &other_format);
}
#endif
void *
music_chunk_write(struct music_chunk *chunk,
const struct audio_format *audio_format,
float data_time, uint16_t bit_rate,
size_t *max_length_r)
music_chunk::Write(const struct audio_format &af,
float data_time, uint16_t _bit_rate,
size_t *max_length_r)
{
const size_t frame_size = audio_format_frame_size(audio_format);
size_t num_frames;
assert(music_chunk_check_format(chunk, audio_format));
assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format));
assert(CheckFormat(af));
assert(length == 0 || audio_format_valid(&audio_format));
if (chunk->length == 0) {
if (length == 0) {
/* if the chunk is empty, nobody has set bitRate and
times yet */
chunk->bit_rate = bit_rate;
chunk->times = data_time;
bit_rate = _bit_rate;
times = data_time;
}
num_frames = (sizeof(chunk->data) - chunk->length) / frame_size;
const size_t frame_size = audio_format_frame_size(&af);
size_t num_frames = (sizeof(data) - length) / frame_size;
if (num_frames == 0)
return NULL;
#ifndef NDEBUG
chunk->audio_format = *audio_format;
audio_format = af;
#endif
*max_length_r = num_frames * frame_size;
return chunk->data + chunk->length;
return data + length;
}
bool
music_chunk_expand(struct music_chunk *chunk,
const struct audio_format *audio_format, size_t length)
music_chunk::Expand(const struct audio_format &af, size_t _length)
{
const size_t frame_size = audio_format_frame_size(audio_format);
const size_t frame_size = audio_format_frame_size(&af);
assert(chunk != NULL);
assert(chunk->length + length <= sizeof(chunk->data));
assert(audio_format_equals(&chunk->audio_format, audio_format));
assert(length + _length <= sizeof(data));
assert(audio_format_equals(&audio_format, &af));
chunk->length += length;
length += _length;
return chunk->length + frame_size > sizeof(chunk->data);
return length + frame_size > sizeof(data);
}
......@@ -91,60 +91,63 @@ struct music_chunk {
#ifndef NDEBUG
struct audio_format audio_format;
#endif
};
void
music_chunk_init(struct music_chunk *chunk);
music_chunk()
:other(nullptr),
length(0),
tag(nullptr),
replay_gain_serial(0) {}
void
music_chunk_free(struct music_chunk *chunk);
~music_chunk();
static inline bool
music_chunk_is_empty(const struct music_chunk *chunk)
{
return chunk->length == 0 && chunk->tag == NULL;
}
bool IsEmpty() const {
return length == 0 && tag == nullptr;
}
#ifndef NDEBUG
/**
* Checks if the audio format if the chunk is equal to the specified
* audio_format.
*/
bool
music_chunk_check_format(const struct music_chunk *chunk,
const struct audio_format *audio_format);
/**
* Checks if the audio format if the chunk is equal to the
* specified audio_format.
*/
gcc_pure
bool CheckFormat(const struct audio_format &audio_format) const;
#endif
/**
* Prepares appending to the music chunk. Returns a buffer where you
* may write into. After you are finished, call music_chunk_expand().
*
* @param chunk the music_chunk object
* @param audio_format the audio format for the appended data; must
* stay the same for the life cycle of this chunk
* @param data_time the time within the song
* @param bit_rate the current bit rate of the source file
* @param max_length_r the maximum write length is returned here
* @return a writable buffer, or NULL if the chunk is full
*/
void *
music_chunk_write(struct music_chunk *chunk,
const struct audio_format *audio_format,
float data_time, uint16_t bit_rate,
size_t *max_length_r);
/**
* Prepares appending to the music chunk. Returns a buffer
* where you may write into. After you are finished, call
* music_chunk_expand().
*
* @param chunk the music_chunk object
* @param audio_format the audio format for the appended data;
* must stay the same for the life cycle of this chunk
* @param data_time the time within the song
* @param bit_rate the current bit rate of the source file
* @param max_length_r the maximum write length is returned
* here
* @return a writable buffer, or NULL if the chunk is full
*/
void *Write(const struct audio_format &af,
float data_time, uint16_t bit_rate,
size_t *max_length_r);
/**
* Increases the length of the chunk after the caller has written to
* the buffer returned by music_chunk_write().
*
* @param chunk the music_chunk object
* @param audio_format the audio format for the appended data; must
* stay the same for the life cycle of this chunk
* @param length the number of bytes which were appended
* @return true if the chunk is full
*/
bool
music_chunk_expand(struct music_chunk *chunk,
const struct audio_format *audio_format, size_t length);
/**
* Increases the length of the chunk after the caller has written to
* the buffer returned by music_chunk_write().
*
* @param chunk the music_chunk object
* @param audio_format the audio format for the appended data; must
* stay the same for the life cycle of this chunk
* @param length the number of bytes which were appended
* @return true if the chunk is full
*/
bool Expand(const struct audio_format &af, size_t length);
};
void
music_chunk_init(struct music_chunk *chunk);
void
music_chunk_free(struct music_chunk *chunk);
#endif
......@@ -111,7 +111,7 @@ music_pipe_shift(struct music_pipe *mp)
struct music_chunk *chunk = mp->head;
if (chunk != NULL) {
assert(!music_chunk_is_empty(chunk));
assert(!chunk->IsEmpty());
mp->head = chunk->next;
--mp->size;
......@@ -150,14 +150,14 @@ music_pipe_clear(struct music_pipe *mp, struct music_buffer *buffer)
void
music_pipe_push(struct music_pipe *mp, struct music_chunk *chunk)
{
assert(!music_chunk_is_empty(chunk));
assert(!chunk->IsEmpty());
assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format));
const ScopeLock protect(mp->mutex);
assert(mp->size > 0 || !audio_format_defined(&mp->audio_format));
assert(!audio_format_defined(&mp->audio_format) ||
music_chunk_check_format(chunk, &mp->audio_format));
chunk->CheckFormat(mp->audio_format));
#ifndef NDEBUG
if (!audio_format_defined(&mp->audio_format) && chunk->length > 0)
......
......@@ -282,7 +282,7 @@ audio_output_all_play(struct music_chunk *chunk, GError **error_r)
assert(g_music_buffer != NULL);
assert(g_mp != NULL);
assert(chunk != NULL);
assert(music_chunk_check_format(chunk, &input_audio_format));
assert(chunk->CheckFormat(input_audio_format));
ret = audio_output_all_update();
if (!ret) {
......
......@@ -327,8 +327,8 @@ ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk,
size_t *length_r)
{
assert(chunk != NULL);
assert(!music_chunk_is_empty(chunk));
assert(music_chunk_check_format(chunk, &ao->in_audio_format));
assert(!chunk->IsEmpty());
assert(chunk->CheckFormat(ao->in_audio_format));
const void *data = chunk->data;
size_t length = chunk->length;
......
......@@ -686,7 +686,7 @@ play_chunk(struct player_control *pc,
const struct audio_format *format,
GError **error_r)
{
assert(music_chunk_check_format(chunk, format));
assert(chunk->CheckFormat(*format));
if (chunk->tag != NULL)
update_song_tag(song, chunk->tag);
......@@ -766,7 +766,7 @@ play_next_chunk(struct player *player)
chunk->mix_ratio = nan("");
}
if (music_chunk_is_empty(other_chunk)) {
if (other_chunk->IsEmpty()) {
/* the "other" chunk was a music_chunk
which had only a tag, but no music
data - we cannot cross-fade that;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment