mirror of
https://github.com/badaix/snapcast.git
synced 2025-08-03 16:48:52 +02:00
d
git-svn-id: svn://elaine/murooma/trunk@237 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
344892b8f1
commit
989f986962
2 changed files with 0 additions and 186 deletions
|
@ -1,79 +0,0 @@
|
|||
#include "chunk.h"
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include "common/log.h"
|
||||
|
||||
|
||||
Chunk::Chunk(const SampleFormat& sampleFormat, WireChunk* _wireChunk) : wireChunk(_wireChunk), format(sampleFormat), idx(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Chunk::Chunk(const SampleFormat& sampleFormat, size_t ms) : format(sampleFormat), idx(0)
|
||||
{
|
||||
wireChunk = makeChunk(payload, format.rate*format.frameSize*ms / 1000);
|
||||
}
|
||||
|
||||
|
||||
Chunk::~Chunk()
|
||||
{
|
||||
free(wireChunk->payload);
|
||||
delete wireChunk;
|
||||
}
|
||||
|
||||
|
||||
bool Chunk::isEndOfChunk() const
|
||||
{
|
||||
return idx >= getFrameCount();
|
||||
}
|
||||
|
||||
|
||||
double Chunk::getFrameCount() const
|
||||
{
|
||||
return (wireChunk->length / format.frameSize);
|
||||
}
|
||||
|
||||
|
||||
|
||||
double Chunk::getDuration() const
|
||||
{
|
||||
return getFrameCount() / format.msRate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
double Chunk::getTimeLeft() const
|
||||
{
|
||||
return (getFrameCount() - idx) / format.msRate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Chunk::seek(int frames)
|
||||
{
|
||||
idx += frames;
|
||||
if (idx > getFrameCount())
|
||||
idx = getFrameCount();
|
||||
if (idx < 0)
|
||||
idx = 0;
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
||||
int Chunk::read(void* outputBuffer, size_t frameCount)
|
||||
{
|
||||
//logd << "read: " << frameCount << ", total: " << (wireChunk->length / format.frameSize) << ", idx: " << idx;// << std::endl;
|
||||
int result = frameCount;
|
||||
if (idx + frameCount > (wireChunk->length / format.frameSize))
|
||||
result = (wireChunk->length / format.frameSize) - idx;
|
||||
|
||||
//logd << ", from: " << format.frameSize*idx << ", to: " << format.frameSize*idx + format.frameSize*result;
|
||||
if (outputBuffer != NULL)
|
||||
memcpy((char*)outputBuffer, (char*)(wireChunk->payload) + format.frameSize*idx, format.frameSize*result);
|
||||
|
||||
idx += result;
|
||||
//logd << ", new idx: " << idx << ", result: " << result << ", wireChunk->length: " << wireChunk->length << ", format.frameSize: " << format.frameSize << "\n";//std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
#ifndef CHUNK_H
|
||||
#define CHUNK_H
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include "common/sampleFormat.h"
|
||||
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> time_point_ms;
|
||||
|
||||
|
||||
struct WireChunk
|
||||
{
|
||||
uint16_t type;
|
||||
int32_t tv_sec;
|
||||
int32_t tv_usec;
|
||||
uint32_t length;
|
||||
char* payload;
|
||||
};
|
||||
|
||||
enum chunk_type
|
||||
{
|
||||
header = 0,
|
||||
payload = 1
|
||||
};
|
||||
|
||||
class Chunk
|
||||
{
|
||||
public:
|
||||
Chunk(const SampleFormat& sampleFormat, WireChunk* _wireChunk);
|
||||
Chunk(const SampleFormat& sampleFormat, size_t ms);
|
||||
~Chunk();
|
||||
|
||||
static inline size_t getHeaderSize()
|
||||
{
|
||||
return sizeof(WireChunk::type) + sizeof(WireChunk::tv_sec) + sizeof(WireChunk::tv_usec) + sizeof(WireChunk::length);
|
||||
}
|
||||
|
||||
static WireChunk* makeChunk(chunk_type type, size_t size)
|
||||
{
|
||||
WireChunk* wireChunk = new WireChunk;
|
||||
wireChunk->type = type;
|
||||
wireChunk->length = size;
|
||||
wireChunk->payload = (char*)malloc(wireChunk->length);
|
||||
return wireChunk;
|
||||
}
|
||||
|
||||
int read(void* outputBuffer, size_t frameCount);
|
||||
bool isEndOfChunk() const;
|
||||
|
||||
inline time_point_ms timePoint() const
|
||||
{
|
||||
time_point_ms tp;
|
||||
std::chrono::milliseconds::rep relativeIdxTp = ((double)idx / ((double)format.rate/1000.));
|
||||
return
|
||||
tp +
|
||||
std::chrono::seconds(wireChunk->tv_sec) +
|
||||
std::chrono::milliseconds(wireChunk->tv_usec / 1000) +
|
||||
std::chrono::milliseconds(relativeIdxTp);
|
||||
}
|
||||
|
||||
inline chunk_type getType()
|
||||
{
|
||||
return (chunk_type)wireChunk->type;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T getAge() const
|
||||
{
|
||||
return getAge<T>(timePoint());
|
||||
}
|
||||
|
||||
inline long getAge() const
|
||||
{
|
||||
return getAge<std::chrono::milliseconds>().count();
|
||||
}
|
||||
|
||||
inline static long getAge(const time_point_ms& time_point)
|
||||
{
|
||||
return getAge<std::chrono::milliseconds>(time_point).count();
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
static inline T getAge(const std::chrono::time_point<U>& time_point)
|
||||
{
|
||||
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - time_point);
|
||||
}
|
||||
|
||||
int seek(int frames);
|
||||
double getDuration() const;
|
||||
double getDurationUs() const;
|
||||
double getTimeLeft() const;
|
||||
double getFrameCount() const;
|
||||
|
||||
WireChunk* wireChunk;
|
||||
SampleFormat format;
|
||||
|
||||
private:
|
||||
// SampleFormat format_;
|
||||
uint32_t idx;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue