snapcast/test/message.cpp
(no author) acae4edbe7 added test project
git-svn-id: svn://elaine/murooma/trunk@227 d8a302eb-03bc-478d-80e4-98257eca68ef
2014-09-02 20:35:28 +00:00

73 lines
1.5 KiB
C++

#include "message.h"
#include <string.h>
#include <iostream>
#include "common/log.h"
Chunk::Chunk(const SampleFormat& sampleFormat, size_t ms) : format(sampleFormat), idx(0)
{
payloadSize = format.rate*format.frameSize*ms / 1000;
payload = (char*)malloc(payloadSize);
}
Chunk::~Chunk()
{
}
bool Chunk::isEndOfChunk() const
{
return idx >= getFrameCount();
}
double Chunk::getFrameCount() const
{
return (payloadSize / 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 > (payloadSize / format.frameSize))
result = (payloadSize / format.frameSize) - idx;
//logd << ", from: " << format.frameSize*idx << ", to: " << format.frameSize*idx + format.frameSize*result;
if (outputBuffer != NULL)
memcpy((char*)outputBuffer, (char*)(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;
}