double AudioTransportSource::getLengthInSeconds() const { if (sampleRate > 0.0) return getTotalLength() / sampleRate; return 0.0; }
bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout) { if (!source || source->getTotalLength() <= 0) return false; if (nextPlayPos + info.numSamples < 0) return true; if (! isLooping() && nextPlayPos > getTotalLength()) return true; const uint32 endTime = Time::getMillisecondCounter() + timeout; uint32 now = Time::getMillisecondCounter(); while (now < endTime) { { const ScopedLock sl (bufferStartPosLock); const int validStart = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos); const int validEnd = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos); if (validStart <= 0 && validStart < validEnd && validEnd >= info.numSamples) return true; } if (! bufferReadyEvent.wait (static_cast<int> (endTime - now))) return false; now = Time::getMillisecondCounter(); } return false; }
Reliapack::Reliapack(char *buf, int length, struct sockaddr_in paddr) { int totalLen = getTotalLength(buf); //printf("received new packet of length %d\n", totalLen); //if this is already a complete packet /* if(totalLen == length - HEADER_LENGTH) { packets = new char * [1]; lengths = new int[1]; *packets = buf; *lengths = totalLength; numPackets = 1; }else {*/ numPackets = (totalLen + MAX_DATA_LENGTH - 1) / MAX_DATA_LENGTH; numPackets = numPackets ? numPackets : 1; packets = new char * [numPackets]; lengths = new int[numPackets]; for(int i=0; i<numPackets; i++) { packets[i] = NULL; lengths[i] = 0; } addPacket(buf, length); //} sequenceNum = getSeqNum(buf); memcpy(&address, &paddr, sizeof(struct sockaddr_in)); ftime(&lastEventTime); //reset the last event time }
int64 InputStream::getNumBytesRemaining() { int64 len = getTotalLength(); if (len >= 0) len -= getPosition(); return len; }
bool TalkyMessage::deSerialise(char* &message, int &remainingBytesReceived) { try { memcpy(Company, message, 2); memcpy(Protocol, message+2, 2); memcpy(&Version, message+4, 2); memcpy(&Timestamp, message+6, 4); memcpy(&Type, message+10, 2); unsigned short tempLength; memcpy(&tempLength, message+12, 2); initPayload(tempLength); if (getTotalLength() > remainingBytesReceived) return false; memcpy(Payload, message+14, PayloadLength); if (message[getTotalLength()-1] != OFXTALKY_ENDCHAR) { throw("TalkyMessage.cpp: End charachter of message is wrong when deserialising"); return false; } } catch (string e) { throw("TalkyMessage.cpp: Failed to deserialise due to exception, likely memory overflow"); return false; } message += getTotalLength(); remainingBytesReceived -= getTotalLength(); return true; }
bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout) { if (!source || source->getTotalLength() <= 0) return false; if (nextPlayPos + info.numSamples < 0) return true; if (! isLooping() && nextPlayPos > getTotalLength()) return true; uint32 now = Time::getMillisecondCounter(); const uint32 startTime = now; uint32 elapsed = (now >= startTime ? now - startTime : (std::numeric_limits<uint32>::max() - startTime) + now); while (elapsed <= timeout) { { const ScopedLock sl (bufferStartPosLock); const int validStart = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos); const int validEnd = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos); if (validStart <= 0 && validStart < validEnd && validEnd >= info.numSamples) return true; } if (elapsed < timeout && (! bufferReadyEvent.wait (static_cast<int> (timeout - elapsed)))) return false; now = Time::getMillisecondCounter(); elapsed = (now >= startTime ? now - startTime : (std::numeric_limits<uint32>::max() - startTime) + now); } return false; }
Path Path::mergeKeysWithTrack(const Track& track) const { if (!track.isInsertPoint() || track.getAddressingMode() == Track::AM_POINT) return *this; Real totalLength=getTotalLength(); Real lineicPos = 0; Real pathLineicPos = 0; Path outputPath; outputPath.addPoint(getPoint(0)); for (unsigned int i = 1; i < mPoints.size(); ) { Real nextLineicPos = pathLineicPos + (mPoints[i] - mPoints[i-1]).length(); std::map<Real,Real>::const_iterator it = track._getKeyValueAfter(lineicPos, lineicPos/totalLength, i-1); Real nextTrackPos = it->first; if (track.getAddressingMode() == Track::AM_RELATIVE_LINEIC) nextTrackPos *= totalLength; // Adds the closest point to the curve, being either from the path or the track if (nextLineicPos<=nextTrackPos || lineicPos>=nextTrackPos) { outputPath.addPoint(mPoints[i]); i++; lineicPos = nextLineicPos; pathLineicPos = nextLineicPos; } else { outputPath.addPoint(getPosition(i-1, (nextTrackPos-pathLineicPos)/(nextLineicPos-pathLineicPos))); lineicPos = nextTrackPos; } } return outputPath; }
bool TalkyMessage::serialise(char* &message, int &remainingAvailableBytes) { int len = getTotalLength(); if (len > remainingAvailableBytes) return false; memcpy(message, Company, 2); memcpy(message+2, Protocol, 2); memcpy(message+4, &Version, 2); memcpy(message+6, &Timestamp, 4); memcpy(message+10, &Type, 2); memcpy(message+12, &PayloadLength, 2); memcpy(message+14, Payload, PayloadLength); message[len-1] = OFXTALKY_ENDCHAR; message += len; remainingAvailableBytes -= len; return true; }
void LaneLineTile::writeToStreamAsSvg(std::ostream &out, const BoundingBox &bb) const { std::pair<double, double> last = std::make_pair(0.0, widthBaseThick); for (auto it = widthBase_.begin(); it != widthBase_.end(); ++it) { writeLine(out, last.first, it->first, 0.0, last.second); last = *it; } writeLine(out, last.first, getTotalLength(), 0.0, last.second); double current = 0.0; for (std::size_t i = 0; i < marks_.size(); ++i) { double length = std::get<2>(marks_[i]); if (current + length > getTotalLength() && (std::get<0>(marks_[i]) != MT_NONE || std::get<1>(marks_[i]) != MT_NONE)) { std::cerr << "WARNING: Overfull tile." << std::endl; break; } switch (std::get<0>(marks_[i])) { case MT_NONE: break; case MT_THIN: writeLine(out, current, current + length, -distThin, widthThin); break; case MT_THICK: writeLine(out, current, current + length, -distThick, widthThick); break; case MT_THICKER: writeLine(out, current, current + length, -distThicker, widthThicker); break; case MT_THIN_DOUBLE: writeLine(out, current, current + length, -1.0 * distThinDouble, widthThinDouble); writeLine(out, current, current + length, -2.0 * distThinDouble, widthThinDouble); break; case MT_THICK_DOUBLE: writeLine(out, current, current + length, -1.0 * distThickDouble, widthThickDouble); writeLine(out, current, current + length, -2.0 * distThickDouble, widthThickDouble); break; case MT_SEPARATOR: { double tmp = 0.5 * (distBase - widthBaseThick); writeLine(out, current, current + length, -0.5 * (tmp + widthBaseThick), tmp * 1.1); writeLine(out, current, current + length, +0.5 * (tmp + widthBaseThick), tmp * 1.1); writeLine(out, current, current + length, 0.0, widthBaseThick, "white"); break; } default: throwing_assert(false); } switch (std::get<1>(marks_[i])) { case MT_NONE: break; case MT_THIN: writeLine(out, current, current + length, +distThin, widthThin); break; case MT_THICK: writeLine(out, current, current + length, +distThick, widthThick); break; case MT_THICKER: writeLine(out, current, current + length, +distThicker, widthThicker); break; case MT_THIN_DOUBLE: writeLine(out, current, current + length, +1.0 * distThinDouble, widthThinDouble); writeLine(out, current, current + length, +2.0 * distThinDouble, widthThinDouble); break; case MT_THICK_DOUBLE: writeLine(out, current, current + length, +1.0 * distThickDouble, widthThickDouble); writeLine(out, current, current + length, +2.0 * distThickDouble, widthThickDouble); break; case MT_SEPARATOR: break; default: throwing_assert(false); } current += length; } }
int Reliapack::getPackTotalLength() { return getTotalLength(packets[0]); }
double AudioTransportSource::getLengthInSeconds() const { return getTotalLength() / sampleRate; }
void LoopMachine::getNextAudioBlockFixedBpm(const AudioSourceChannelInfo& bufferToFill) { auto& transport = audioEngine.getTransport(); bool mainTransportPlaying = transport.isPlaying(); if (fixedBpmTransport.isPlaying() != mainTransportPlaying) { if (mainTransportPlaying) fixedBpmTransport.play(); else fixedBpmTransport.stop(); } fixedBpmTransport.updateTransport(bufferToFill.numSamples); bufferToFill.clearActiveBufferRegion(); if (fixedBpmTransport.isPlaying()) { float frameStartTicks = fixedBpmTransport.getFrameStartTicks(); float frameEndTicks = fixedBpmTransport.getFrameEndTicks(); float nextTick = (float) ((int)frameStartTicks + 1); float fadeLengthTicks = fixedBpmTransport.millisToTicks(FADE_TIME_MS); float fadeStartTicks = nextTick - fadeLengthTicks; float fadeEndTicks = nextTick; if (frameStartTicks < fadeStartTicks && frameEndTicks >= fadeStartTicks) drainRingBuffer(); // std::cout << "MPD: CPP: LoopMachine::getNextAudioBlock: reality check! " << ((int)nextTick/4) << std::endl; for (int groupIx = 0; groupIx < groupIxToLoopInfo.size(); groupIx++) { int state = audioState[groupIx]; int prevState = prevAudioState[groupIx]; if (state == LOOP_INACTIVE && prevState == LOOP_INACTIVE) { // we were doing nothing last period, and we're still doing nothing: do nothing } else if (state == LOOP_INACTIVE && prevState != LOOP_INACTIVE) { // for this loop group, we are fading out: going from an active loop to silence. processFadeOut(groupIx, prevState, frameStartTicks, frameEndTicks, fadeStartTicks, fadeEndTicks, bufferToFill); } else if (!wasPlaying || (state != LOOP_INACTIVE && prevState == LOOP_INACTIVE)) { // for this loop group, we are fading in: going from silence to signal. setReaderPos(groupIx, state, fadeStartTicks, frameStartTicks); processFadeIn(groupIx, state, frameStartTicks, frameEndTicks, fadeStartTicks, fadeEndTicks, bufferToFill); } else if (prevState != state) { // for this loop group, the loop being played has switched: do a crossfade processFadeOut(groupIx, prevState, frameStartTicks, frameEndTicks, fadeStartTicks, fadeEndTicks, bufferToFill); setReaderPos(groupIx, state, fadeStartTicks, frameStartTicks); processFadeIn(groupIx, state, frameStartTicks, frameEndTicks, fadeStartTicks, fadeEndTicks, bufferToFill); } else { // we're playing the same thing as in the last period. if (!wasPlaying) { auto src = (*groupIxToLoopInfo[groupIx])[state]; src->reader->setNextReadPosition(0); } processBlock(groupIx, state, 0, bufferToFill.numSamples, bufferToFill); } } if (frameStartTicks < fadeEndTicks && frameEndTicks >= fadeEndTicks) { bool changes = false; for (int groupIx = 0; groupIx < groupIxToLoopInfo.size(); groupIx++) { int state = audioState[groupIx]; if (state != LOOP_INACTIVE) { auto type = (*groupIxToLoopInfo[groupIx])[state]->type; auto src = (*groupIxToLoopInfo[groupIx])[state]->reader; if (type == LoopType::ONE_SHOT && audioState[groupIx] != LOOP_INACTIVE && src != nullptr && src->getNextReadPosition() >= src->getTotalLength()) { // (groupIx, prevState) is done playing. // now we need to plop a message in the ring buffer audioState[groupIx] = LOOP_INACTIVE; userState[groupIx] = LOOP_INACTIVE; int ix = ++endReserveIx & RINGBUF_SIZE_M1; // == ++reserveIx % RINGBUF_SIZE endringbuf[ix][0] = groupIx; endringbuf[ix][1] = state; endCommitIx++; changes = true; src->setNextReadPosition(0); // std::cout << "MPD: handling messaages audio thread" << std::endl; } } } if (changes) sendChangeMessage(); std::memcpy(prevAudioState, audioState, sizeof(audioState)); wasPlaying = true; } // bufferToFill.buffer->applyGain(0, 0, bufferToFill.numSamples, 0.5); // bufferToFill.buffer->applyGain(1, 0, bufferToFill.numSamples, 0.5); // wasPlaying = true; } if (wasPlaying && !fixedBpmTransport.isPlaying()) wasPlaying = false; }