music_chunk: added music_chunk_write(), music_chunk_expand()
Moved some code from music_pipe_write() and music_pipe_expand(). Only music_chunk.c should access the music_chunk internals.
This commit is contained in:
42
src/chunk.c
42
src/chunk.c
@@ -17,8 +17,11 @@
|
||||
*/
|
||||
|
||||
#include "chunk.h"
|
||||
#include "audio_format.h"
|
||||
#include "tag.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
void
|
||||
music_chunk_init(struct music_chunk *chunk)
|
||||
{
|
||||
@@ -32,3 +35,42 @@ music_chunk_free(struct music_chunk *chunk)
|
||||
if (chunk->tag != NULL)
|
||||
tag_free(chunk->tag);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
const size_t frame_size = audio_format_frame_size(audio_format);
|
||||
size_t num_frames;
|
||||
|
||||
if (chunk->length == 0) {
|
||||
/* if the chunk is empty, nobody has set bitRate and
|
||||
times yet */
|
||||
|
||||
chunk->bit_rate = bit_rate;
|
||||
chunk->times = data_time;
|
||||
}
|
||||
|
||||
num_frames = (sizeof(chunk->data) - chunk->length) / frame_size;
|
||||
if (num_frames == 0)
|
||||
return NULL;
|
||||
|
||||
*max_length_r = num_frames * frame_size;
|
||||
return chunk->data + chunk->length;
|
||||
}
|
||||
|
||||
bool
|
||||
music_chunk_expand(struct music_chunk *chunk,
|
||||
const struct audio_format *audio_format, size_t length)
|
||||
{
|
||||
const size_t frame_size = audio_format_frame_size(audio_format);
|
||||
|
||||
assert(chunk != NULL);
|
||||
assert(chunk->length + length <= sizeof(chunk->data));
|
||||
|
||||
chunk->length += length;
|
||||
|
||||
return chunk->length + frame_size > sizeof(chunk->data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user