configure.ac: Add subheaders to Input/Sticker/Autodiscovery/Metadata and Misc.
[mpd-mk.git] / src / chunk.c
blob5418b6cdaaa6c92d6568492ac25e4656dba9ae56
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "chunk.h"
22 #include "audio_format.h"
23 #include "tag.h"
25 #include <assert.h>
27 void
28 music_chunk_init(struct music_chunk *chunk)
30 chunk->length = 0;
31 chunk->tag = NULL;
32 chunk->replay_gain_serial = 0;
35 void
36 music_chunk_free(struct music_chunk *chunk)
38 if (chunk->tag != NULL)
39 tag_free(chunk->tag);
42 #ifndef NDEBUG
43 bool
44 music_chunk_check_format(const struct music_chunk *chunk,
45 const struct audio_format *audio_format)
47 assert(chunk != NULL);
48 assert(audio_format != NULL);
49 assert(audio_format_valid(audio_format));
51 return chunk->length == 0 ||
52 audio_format_equals(&chunk->audio_format, audio_format);
54 #endif
56 void *
57 music_chunk_write(struct music_chunk *chunk,
58 const struct audio_format *audio_format,
59 float data_time, uint16_t bit_rate,
60 size_t *max_length_r)
62 const size_t frame_size = audio_format_frame_size(audio_format);
63 size_t num_frames;
65 assert(music_chunk_check_format(chunk, audio_format));
66 assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format));
68 if (chunk->length == 0) {
69 /* if the chunk is empty, nobody has set bitRate and
70 times yet */
72 chunk->bit_rate = bit_rate;
73 chunk->times = data_time;
76 num_frames = (sizeof(chunk->data) - chunk->length) / frame_size;
77 if (num_frames == 0)
78 return NULL;
80 #ifndef NDEBUG
81 chunk->audio_format = *audio_format;
82 #endif
84 *max_length_r = num_frames * frame_size;
85 return chunk->data + chunk->length;
88 bool
89 music_chunk_expand(struct music_chunk *chunk,
90 const struct audio_format *audio_format, size_t length)
92 const size_t frame_size = audio_format_frame_size(audio_format);
94 assert(chunk != NULL);
95 assert(chunk->length + length <= sizeof(chunk->data));
96 assert(audio_format_equals(&chunk->audio_format, audio_format));
98 chunk->length += length;
100 return chunk->length + frame_size > sizeof(chunk->data);