1 /*** 2 This file is part of snapcast 3 Copyright (C) 2014-2020 Johannes Pohl 4 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 3 of the License, or 8 (at your option) any later version. 9 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. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 ***/ 18 19 #ifndef PCM_CHUNK_H 20 #define PCM_CHUNK_H 21 22 #include "common/sample_format.hpp" 23 #include "message.hpp" 24 #include "wire_chunk.hpp" 25 #include <chrono> 26 27 28 namespace msg 29 { 30 31 /** 32 * Piece of PCM data with SampleFormat information 33 * Has information about "when" recorded (start) and duration 34 * frames can be read with "readFrames", which will also change the start time 35 */ 36 class PcmChunk : public WireChunk 37 { 38 public: PcmChunk(const SampleFormat & sampleFormat,uint32_t ms)39 PcmChunk(const SampleFormat& sampleFormat, uint32_t ms) 40 : WireChunk((sampleFormat.rate() * ms / 1000) * sampleFormat.frameSize()), format(sampleFormat), idx_(0) 41 { 42 } 43 PcmChunk()44 PcmChunk() : WireChunk(), idx_(0) 45 { 46 } 47 48 ~PcmChunk() override = default; 49 50 #if 0 51 template <class Rep, class Period> 52 int readFrames(void* outputBuffer, const std::chrono::duration<Rep, Period>& duration) 53 { 54 auto us = std::chrono::microseconds(duration).count(); 55 auto frames = (us * 48000) / std::micro::den; 56 // return readFrames(outputBuffer, (us * 48000) / std::micro::den); 57 return frames; 58 } 59 #endif 60 61 // std::unique_ptr<PcmChunk> consume(uint32_t frameCount) 62 // { 63 // auto result = std::make_unique<PcmChunk>(format, 0); 64 // if (frameCount * format.frameSize() > payloadSize) 65 // frameCount = payloadSize / format.frameSize(); 66 // result->payload = payload; 67 // result->payloadSize = frameCount * format.frameSize(); 68 // payloadSize -= result->payloadSize; 69 // payload = (char*)realloc(payload + result->payloadSize, payloadSize); 70 // // payload += result->payloadSize; 71 // return result; 72 // } 73 readFrames(void * outputBuffer,uint32_t frameCount)74 int readFrames(void* outputBuffer, uint32_t frameCount) 75 { 76 // logd << "read: " << frameCount << ", total: " << (wireChunk->length / format.frameSize()) << ", idx: " << idx;// << std::endl; 77 int result = frameCount; 78 if (idx_ + frameCount > (payloadSize / format.frameSize())) 79 result = (payloadSize / format.frameSize()) - idx_; 80 81 // logd << ", from: " << format.frameSize()*idx << ", to: " << format.frameSize()*idx + format.frameSize()*result; 82 if (outputBuffer != nullptr) 83 memcpy((char*)outputBuffer, (char*)(payload) + format.frameSize() * idx_, format.frameSize() * result); 84 85 idx_ += result; 86 // logd << ", new idx: " << idx << ", result: " << result << ", wireChunk->length: " << wireChunk->length << ", format.frameSize(): " << 87 // format.frameSize() << "\n"; 88 return result; 89 } 90 seek(int frames)91 int seek(int frames) 92 { 93 if ((frames < 0) && (-frames > static_cast<int>(idx_))) 94 frames = -static_cast<int>(idx_); 95 96 idx_ += frames; 97 if (idx_ > getFrameCount()) 98 idx_ = getFrameCount(); 99 100 return idx_; 101 } 102 start() const103 chronos::time_point_clk start() const override 104 { 105 return chronos::time_point_clk(chronos::sec(timestamp.sec) + chronos::usec(timestamp.usec) + 106 chronos::usec(static_cast<chronos::usec::rep>(1000000. * ((double)idx_ / (double)format.rate())))); 107 } 108 end() const109 inline chronos::time_point_clk end() const 110 { 111 return start() + durationLeft<chronos::usec>(); 112 } 113 114 template <typename T> duration() const115 inline T duration() const 116 { 117 return std::chrono::duration_cast<T>(chronos::nsec(static_cast<chronos::nsec::rep>(1000000 * getFrameCount() / format.msRate()))); 118 } 119 120 // void append(const PcmChunk& chunk) 121 // { 122 // auto newSize = payloadSize + chunk.payloadSize; 123 // payload = (char*)realloc(payload, newSize); 124 // memcpy(payload + payloadSize, chunk.payload, chunk.payloadSize); 125 // payloadSize = newSize; 126 // } 127 durationMs() const128 double durationMs() const 129 { 130 return static_cast<double>(getFrameCount()) / format.msRate(); 131 } 132 133 template <typename T> durationLeft() const134 inline T durationLeft() const 135 { 136 return std::chrono::duration_cast<T>(chronos::nsec(static_cast<chronos::nsec::rep>(1000000 * (getFrameCount() - idx_) / format.msRate()))); 137 } 138 isEndOfChunk() const139 inline bool isEndOfChunk() const 140 { 141 return idx_ >= getFrameCount(); 142 } 143 getFrameCount() const144 inline uint32_t getFrameCount() const 145 { 146 return (payloadSize / format.frameSize()); 147 } 148 getSampleCount() const149 inline uint32_t getSampleCount() const 150 { 151 return (payloadSize / format.sampleSize()); 152 } 153 154 SampleFormat format; 155 156 private: 157 uint32_t idx_ = 0; 158 }; 159 } // namespace msg 160 161 #endif 162