mpd/src/MusicChunk.hxx

151 lines
3.9 KiB
C++
Raw Normal View History

/*
2022-07-14 17:58:12 +02:00
* Copyright 2003-2022 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.
*/
2013-01-04 10:16:16 +01:00
#ifndef MPD_MUSIC_CHUNK_HXX
#define MPD_MUSIC_CHUNK_HXX
#include "MusicChunkPtr.hxx"
#include "Chrono.hxx"
2021-12-06 09:24:12 +01:00
#include "tag/ReplayGainInfo.hxx"
#ifndef NDEBUG
2020-01-18 20:07:09 +01:00
#include "pcm/AudioFormat.hxx"
#endif
#include <cstddef>
#include <cstdint>
#include <memory>
2022-05-20 11:05:48 +02:00
#include <span>
static constexpr size_t CHUNK_SIZE = 4096;
2013-08-03 21:00:50 +02:00
struct AudioFormat;
2013-07-30 20:11:57 +02:00
struct Tag;
struct MusicChunk;
/**
* Meta information for #MusicChunk.
*/
struct MusicChunkInfo {
/** the next chunk in a linked list */
MusicChunkPtr next;
/**
* An optional chunk which should be mixed into this chunk.
* This is used for cross-fading.
*/
MusicChunkPtr other;
/**
* An optional tag associated with this chunk (and the
* following chunks); appears at song boundaries.
*/
std::unique_ptr<Tag> tag;
/**
* The current mix ratio for cross-fading: 1.0 means play 100%
* of this chunk, 0.0 means play 100% of the "other" chunk.
*/
float mix_ratio;
/** number of bytes stored in this chunk */
2016-11-23 17:56:36 +01:00
uint16_t length = 0;
/** current bit rate of the source file */
uint16_t bit_rate;
/** the time stamp within the song */
SignedSongTime time;
/**
* Replay gain information associated with this chunk.
* Only valid if the serial is not 0.
*/
ReplayGainInfo replay_gain_info;
/**
* A serial number for checking if replay gain info has
* changed since the last chunk. The magic value 0 indicates
* that there is no replay gain info available.
*/
unsigned replay_gain_serial;
#ifndef NDEBUG
2013-08-03 21:00:50 +02:00
AudioFormat audio_format;
#endif
MusicChunkInfo() noexcept;
~MusicChunkInfo() noexcept;
MusicChunkInfo(const MusicChunkInfo &) = delete;
MusicChunkInfo &operator=(const MusicChunkInfo &) = delete;
2016-11-23 17:57:29 +01:00
2013-01-04 21:38:46 +01:00
bool IsEmpty() const {
return length == 0 && tag == nullptr;
}
2009-03-07 21:40:27 +01:00
#ifndef NDEBUG
2013-01-04 21:38:46 +01:00
/**
* Checks if the audio format if the chunk is equal to the
* specified audio_format.
*/
2021-10-13 11:28:04 +02:00
[[gnu::pure]]
bool CheckFormat(AudioFormat audio_format) const noexcept;
#endif
};
/**
* A chunk of music data. Its format is defined by the
* MusicPipe::Push() caller.
*/
struct MusicChunk : MusicChunkInfo {
/** the data (probably PCM) */
2022-05-20 11:05:48 +02:00
std::byte data[CHUNK_SIZE - sizeof(MusicChunkInfo)];
2013-01-04 21:38:46 +01:00
/**
* Prepares appending to the music chunk. Returns a buffer
* where you may write into. After you are finished, call
2014-08-12 15:56:11 +02:00
* Expand().
2013-01-04 21:38:46 +01:00
*
2015-03-17 11:21:29 +01:00
* @param af the audio format for the appended data;
2013-01-04 21:38:46 +01:00
* 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
2013-10-19 18:19:03 +02:00
* @return a writable buffer, or nullptr if the chunk is full
2013-01-04 21:38:46 +01:00
*/
2022-05-20 11:05:48 +02:00
std::span<std::byte> Write(AudioFormat af,
SongTime data_time,
uint16_t bit_rate) noexcept;
2013-01-04 21:38:46 +01:00
/**
* Increases the length of the chunk after the caller has written to
2014-08-12 15:56:11 +02:00
* the buffer returned by Write().
2013-01-04 21:38:46 +01:00
*
2015-03-17 11:21:29 +01:00
* @param af the audio format for the appended data; must
2013-01-04 21:38:46 +01:00
* 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(AudioFormat af, size_t length) noexcept;
2013-01-04 21:38:46 +01:00
};
static_assert(sizeof(MusicChunk) == CHUNK_SIZE, "Wrong size");
#endif