MusicChunk: move functions to methods
This commit is contained in:
parent
efbfe66f21
commit
e9b71a0d28
@ -429,10 +429,10 @@ decoder_data(struct decoder *decoder,
|
|||||||
return dc->command;
|
return dc->command;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *dest = music_chunk_write(chunk, &dc->out_audio_format,
|
void *dest = chunk->Write(dc->out_audio_format,
|
||||||
decoder->timestamp -
|
decoder->timestamp -
|
||||||
dc->song->start_ms / 1000.0,
|
dc->song->start_ms / 1000.0,
|
||||||
kbit_rate, &nbytes);
|
kbit_rate, &nbytes);
|
||||||
if (dest == NULL) {
|
if (dest == NULL) {
|
||||||
/* the chunk is full, flush it */
|
/* the chunk is full, flush it */
|
||||||
decoder_flush_chunk(decoder);
|
decoder_flush_chunk(decoder);
|
||||||
@ -451,7 +451,7 @@ decoder_data(struct decoder *decoder,
|
|||||||
|
|
||||||
/* expand the music pipe chunk */
|
/* 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) {
|
if (full) {
|
||||||
/* the chunk is full, flush it */
|
/* the chunk is full, flush it */
|
||||||
decoder_flush_chunk(decoder);
|
decoder_flush_chunk(decoder);
|
||||||
|
@ -88,7 +88,7 @@ decoder_flush_chunk(struct decoder *decoder)
|
|||||||
assert(decoder != NULL);
|
assert(decoder != NULL);
|
||||||
assert(decoder->chunk != NULL);
|
assert(decoder->chunk != NULL);
|
||||||
|
|
||||||
if (music_chunk_is_empty(decoder->chunk))
|
if (decoder->chunk->IsEmpty())
|
||||||
music_buffer_return(dc->buffer, decoder->chunk);
|
music_buffer_return(dc->buffer, decoder->chunk);
|
||||||
else
|
else
|
||||||
music_pipe_push(dc->pipe, decoder->chunk);
|
music_pipe_push(dc->pipe, decoder->chunk);
|
||||||
|
@ -27,79 +27,61 @@ extern "C" {
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
void
|
music_chunk::~music_chunk()
|
||||||
music_chunk_init(struct music_chunk *chunk)
|
|
||||||
{
|
{
|
||||||
chunk->other = NULL;
|
if (tag != NULL)
|
||||||
chunk->length = 0;
|
tag_free(tag);
|
||||||
chunk->tag = NULL;
|
|
||||||
chunk->replay_gain_serial = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
music_chunk_free(struct music_chunk *chunk)
|
|
||||||
{
|
|
||||||
if (chunk->tag != NULL)
|
|
||||||
tag_free(chunk->tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
bool
|
bool
|
||||||
music_chunk_check_format(const struct music_chunk *chunk,
|
music_chunk::CheckFormat(const struct audio_format &other_format) const
|
||||||
const struct audio_format *audio_format)
|
|
||||||
{
|
{
|
||||||
assert(chunk != NULL);
|
assert(audio_format_valid(&other_format));
|
||||||
assert(audio_format != NULL);
|
|
||||||
assert(audio_format_valid(audio_format));
|
|
||||||
|
|
||||||
return chunk->length == 0 ||
|
return length == 0 ||
|
||||||
audio_format_equals(&chunk->audio_format, audio_format);
|
audio_format_equals(&audio_format, &other_format);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *
|
void *
|
||||||
music_chunk_write(struct music_chunk *chunk,
|
music_chunk::Write(const struct audio_format &af,
|
||||||
const struct audio_format *audio_format,
|
float data_time, uint16_t _bit_rate,
|
||||||
float data_time, uint16_t bit_rate,
|
size_t *max_length_r)
|
||||||
size_t *max_length_r)
|
|
||||||
{
|
{
|
||||||
const size_t frame_size = audio_format_frame_size(audio_format);
|
assert(CheckFormat(af));
|
||||||
size_t num_frames;
|
assert(length == 0 || audio_format_valid(&audio_format));
|
||||||
|
|
||||||
assert(music_chunk_check_format(chunk, audio_format));
|
if (length == 0) {
|
||||||
assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format));
|
|
||||||
|
|
||||||
if (chunk->length == 0) {
|
|
||||||
/* if the chunk is empty, nobody has set bitRate and
|
/* if the chunk is empty, nobody has set bitRate and
|
||||||
times yet */
|
times yet */
|
||||||
|
|
||||||
chunk->bit_rate = bit_rate;
|
bit_rate = _bit_rate;
|
||||||
chunk->times = data_time;
|
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)
|
if (num_frames == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
chunk->audio_format = *audio_format;
|
audio_format = af;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
*max_length_r = num_frames * frame_size;
|
*max_length_r = num_frames * frame_size;
|
||||||
return chunk->data + chunk->length;
|
return data + length;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
music_chunk_expand(struct music_chunk *chunk,
|
music_chunk::Expand(const struct audio_format &af, size_t _length)
|
||||||
const struct audio_format *audio_format, 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(length + _length <= sizeof(data));
|
||||||
assert(chunk->length + length <= sizeof(chunk->data));
|
assert(audio_format_equals(&audio_format, &af));
|
||||||
assert(audio_format_equals(&chunk->audio_format, audio_format));
|
|
||||||
|
|
||||||
chunk->length += length;
|
length += _length;
|
||||||
|
|
||||||
return chunk->length + frame_size > sizeof(chunk->data);
|
return length + frame_size > sizeof(data);
|
||||||
}
|
}
|
||||||
|
@ -91,6 +91,57 @@ struct music_chunk {
|
|||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
struct audio_format audio_format;
|
struct audio_format audio_format;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
music_chunk()
|
||||||
|
:other(nullptr),
|
||||||
|
length(0),
|
||||||
|
tag(nullptr),
|
||||||
|
replay_gain_serial(0) {}
|
||||||
|
|
||||||
|
~music_chunk();
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
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 *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 Expand(const struct audio_format &af, size_t length);
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -99,52 +150,4 @@ music_chunk_init(struct music_chunk *chunk);
|
|||||||
void
|
void
|
||||||
music_chunk_free(struct music_chunk *chunk);
|
music_chunk_free(struct music_chunk *chunk);
|
||||||
|
|
||||||
static inline bool
|
|
||||||
music_chunk_is_empty(const struct music_chunk *chunk)
|
|
||||||
{
|
|
||||||
return chunk->length == 0 && chunk->tag == NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#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);
|
|
||||||
#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);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -111,7 +111,7 @@ music_pipe_shift(struct music_pipe *mp)
|
|||||||
|
|
||||||
struct music_chunk *chunk = mp->head;
|
struct music_chunk *chunk = mp->head;
|
||||||
if (chunk != NULL) {
|
if (chunk != NULL) {
|
||||||
assert(!music_chunk_is_empty(chunk));
|
assert(!chunk->IsEmpty());
|
||||||
|
|
||||||
mp->head = chunk->next;
|
mp->head = chunk->next;
|
||||||
--mp->size;
|
--mp->size;
|
||||||
@ -150,14 +150,14 @@ music_pipe_clear(struct music_pipe *mp, struct music_buffer *buffer)
|
|||||||
void
|
void
|
||||||
music_pipe_push(struct music_pipe *mp, struct music_chunk *chunk)
|
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));
|
assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format));
|
||||||
|
|
||||||
const ScopeLock protect(mp->mutex);
|
const ScopeLock protect(mp->mutex);
|
||||||
|
|
||||||
assert(mp->size > 0 || !audio_format_defined(&mp->audio_format));
|
assert(mp->size > 0 || !audio_format_defined(&mp->audio_format));
|
||||||
assert(!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
|
#ifndef NDEBUG
|
||||||
if (!audio_format_defined(&mp->audio_format) && chunk->length > 0)
|
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_music_buffer != NULL);
|
||||||
assert(g_mp != NULL);
|
assert(g_mp != NULL);
|
||||||
assert(chunk != NULL);
|
assert(chunk != NULL);
|
||||||
assert(music_chunk_check_format(chunk, &input_audio_format));
|
assert(chunk->CheckFormat(input_audio_format));
|
||||||
|
|
||||||
ret = audio_output_all_update();
|
ret = audio_output_all_update();
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
|
@ -327,8 +327,8 @@ ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk,
|
|||||||
size_t *length_r)
|
size_t *length_r)
|
||||||
{
|
{
|
||||||
assert(chunk != NULL);
|
assert(chunk != NULL);
|
||||||
assert(!music_chunk_is_empty(chunk));
|
assert(!chunk->IsEmpty());
|
||||||
assert(music_chunk_check_format(chunk, &ao->in_audio_format));
|
assert(chunk->CheckFormat(ao->in_audio_format));
|
||||||
|
|
||||||
const void *data = chunk->data;
|
const void *data = chunk->data;
|
||||||
size_t length = chunk->length;
|
size_t length = chunk->length;
|
||||||
|
@ -686,7 +686,7 @@ play_chunk(struct player_control *pc,
|
|||||||
const struct audio_format *format,
|
const struct audio_format *format,
|
||||||
GError **error_r)
|
GError **error_r)
|
||||||
{
|
{
|
||||||
assert(music_chunk_check_format(chunk, format));
|
assert(chunk->CheckFormat(*format));
|
||||||
|
|
||||||
if (chunk->tag != NULL)
|
if (chunk->tag != NULL)
|
||||||
update_song_tag(song, chunk->tag);
|
update_song_tag(song, chunk->tag);
|
||||||
@ -766,7 +766,7 @@ play_next_chunk(struct player *player)
|
|||||||
chunk->mix_ratio = nan("");
|
chunk->mix_ratio = nan("");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (music_chunk_is_empty(other_chunk)) {
|
if (other_chunk->IsEmpty()) {
|
||||||
/* the "other" chunk was a music_chunk
|
/* the "other" chunk was a music_chunk
|
||||||
which had only a tag, but no music
|
which had only a tag, but no music
|
||||||
data - we cannot cross-fade that;
|
data - we cannot cross-fade that;
|
||||||
|
Loading…
Reference in New Issue
Block a user