Loading...
Searching...
No Matches
juce_AudioPlayHead.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API AudioPlayHead
{
protected:
//==============================================================================
AudioPlayHead() = default;
public:
virtual ~AudioPlayHead() = default;
//==============================================================================
enum FrameRateType
{
fps23976 = 0,
fps24 = 1,
fps25 = 2,
fps2997 = 3,
fps30 = 4,
fps2997drop = 5,
fps30drop = 6,
fps60 = 7,
fps60drop = 8,
fpsUnknown = 99
};
class JUCE_API FrameRate
{
public:
FrameRate() = default;
FrameRate (FrameRateType type) : FrameRate (fromType (type)) {}
FrameRateType getType() const
{
switch (base)
{
case 24: return pulldown ? fps23976 : fps24;
case 25: return fps25;
case 30: return pulldown ? (drop ? fps2997drop : fps2997)
: (drop ? fps30drop : fps30);
case 60: return drop ? fps60drop : fps60;
}
return fpsUnknown;
}
int getBaseRate() const { return base; }
bool isDrop() const { return drop; }
bool isPullDown() const { return pulldown; }
double getEffectiveRate() const { return pulldown ? (double) base / 1.001 : (double) base; }
[[nodiscard]] FrameRate withBaseRate (int x) const { return with (&FrameRate::base, x); }
[[nodiscard]] FrameRate withDrop (bool x = true) const { return with (&FrameRate::drop, x); }
[[nodiscard]] FrameRate withPullDown (bool x = true) const { return with (&FrameRate::pulldown, x); }
bool operator== (const FrameRate& other) const
{
const auto tie = [] (const FrameRate& x) { return std::tie (x.base, x.drop, x.pulldown); };
return tie (*this) == tie (other);
}
bool operator!= (const FrameRate& other) const { return ! (*this == other); }
private:
static FrameRate fromType (FrameRateType type)
{
switch (type)
{
case fps23976: return FrameRate().withBaseRate (24).withPullDown();
case fps24: return FrameRate().withBaseRate (24);
case fps25: return FrameRate().withBaseRate (25);
case fps2997: return FrameRate().withBaseRate (30).withPullDown();
case fps30: return FrameRate().withBaseRate (30);
case fps2997drop: return FrameRate().withBaseRate (30).withDrop().withPullDown();
case fps30drop: return FrameRate().withBaseRate (30).withDrop();
case fps60: return FrameRate().withBaseRate (60);
case fps60drop: return FrameRate().withBaseRate (60).withDrop();
case fpsUnknown: break;
}
return {};
}
template <typename Member, typename Value>
FrameRate with (Member&& member, Value&& value) const
{
auto copy = *this;
copy.*member = std::forward<Value> (value);
return copy;
}
int base = 0;
bool drop = false, pulldown = false;
};
struct JUCE_API TimeSignature
{
int numerator = 4;
int denominator = 4;
bool operator== (const TimeSignature& other) const
{
const auto tie = [] (auto& x) { return std::tie (x.numerator, x.denominator); };
return tie (*this) == tie (other);
}
bool operator!= (const TimeSignature& other) const
{
return ! operator== (other);
}
};
struct JUCE_API LoopPoints
{
double ppqStart = 0;
double ppqEnd = 0;
bool operator== (const LoopPoints& other) const
{
const auto tie = [] (auto& x) { return std::tie (x.ppqStart, x.ppqEnd); };
return tie (*this) == tie (other);
}
bool operator!= (const LoopPoints& other) const
{
return ! operator== (other);
}
};
//==============================================================================
struct JUCE_API CurrentPositionInfo
{
double bpm = 120.0;
int timeSigNumerator = 4;
int timeSigDenominator = 4;
int64 timeInSamples = 0;
double timeInSeconds = 0;
double editOriginTime = 0;
double ppqPosition = 0;
double ppqPositionOfLastBarStart = 0;
FrameRate frameRate = FrameRateType::fps23976;
bool isPlaying = false;
bool isRecording = false;
double ppqLoopStart = 0;
double ppqLoopEnd = 0;
bool isLooping = false;
//==============================================================================
bool operator== (const CurrentPositionInfo& other) const noexcept
{
const auto tie = [] (const CurrentPositionInfo& i)
{
return std::tie (i.timeInSamples,
i.ppqPosition,
i.editOriginTime,
i.ppqPositionOfLastBarStart,
i.frameRate,
i.isPlaying,
i.isRecording,
i.bpm,
i.timeSigNumerator,
i.timeSigDenominator,
i.ppqLoopStart,
i.ppqLoopEnd,
i.isLooping);
};
return tie (*this) == tie (other);
}
bool operator!= (const CurrentPositionInfo& other) const noexcept
{
return ! operator== (other);
}
void resetToDefault()
{
*this = CurrentPositionInfo{};
}
};
//==============================================================================
class PositionInfo
{
public:
Optional<int64_t> getTimeInSamples() const { return getOptional (flagTimeSamples, timeInSamples); }
void setTimeInSamples (Optional<int64_t> timeInSamplesIn) { setOptional (flagTimeSamples, timeInSamples, timeInSamplesIn); }
Optional<double> getTimeInSeconds() const { return getOptional (flagTimeSeconds, timeInSeconds); }
void setTimeInSeconds (Optional<double> timeInSecondsIn) { setOptional (flagTimeSeconds, timeInSeconds, timeInSecondsIn); }
Optional<double> getBpm() const { return getOptional (flagTempo, tempoBpm); }
void setBpm (Optional<double> bpmIn) { setOptional (flagTempo, tempoBpm, bpmIn); }
Optional<TimeSignature> getTimeSignature() const { return getOptional (flagTimeSignature, timeSignature); }
void setTimeSignature (Optional<TimeSignature> timeSignatureIn) { setOptional (flagTimeSignature, timeSignature, timeSignatureIn); }
Optional<LoopPoints> getLoopPoints() const { return getOptional (flagLoopPoints, loopPoints); }
void setLoopPoints (Optional<LoopPoints> loopPointsIn) { setOptional (flagLoopPoints, loopPoints, loopPointsIn); }
Optional<int64_t> getBarCount() const { return getOptional (flagBarCount, barCount); }
void setBarCount (Optional<int64_t> barCountIn) { setOptional (flagBarCount, barCount, barCountIn); }
Optional<double> getPpqPositionOfLastBarStart() const { return getOptional (flagLastBarStartPpq, lastBarStartPpq); }
void setPpqPositionOfLastBarStart (Optional<double> positionIn) { setOptional (flagLastBarStartPpq, lastBarStartPpq, positionIn); }
Optional<FrameRate> getFrameRate() const { return getOptional (flagFrameRate, frame); }
void setFrameRate (Optional<FrameRate> frameRateIn) { setOptional (flagFrameRate, frame, frameRateIn); }
Optional<double> getPpqPosition() const { return getOptional (flagPpqPosition, positionPpq); }
void setPpqPosition (Optional<double> ppqPositionIn) { setOptional (flagPpqPosition, positionPpq, ppqPositionIn); }
Optional<double> getEditOriginTime() const { return getOptional (flagOriginTime, originTime); }
void setEditOriginTime (Optional<double> editOriginTimeIn) { setOptional (flagOriginTime, originTime, editOriginTimeIn); }
Optional<uint64_t> getHostTimeNs() const { return getOptional (flagHostTimeNs, hostTimeNs); }
void setHostTimeNs (Optional<uint64_t> hostTimeNsIn) { setOptional (flagHostTimeNs, hostTimeNs, hostTimeNsIn); }
bool getIsPlaying() const { return getFlag (flagIsPlaying); }
void setIsPlaying (bool isPlayingIn) { setFlag (flagIsPlaying, isPlayingIn); }
bool getIsRecording() const { return getFlag (flagIsRecording); }
void setIsRecording (bool isRecordingIn) { setFlag (flagIsRecording, isRecordingIn); }
bool getIsLooping() const { return getFlag (flagIsLooping); }
void setIsLooping (bool isLoopingIn) { setFlag (flagIsLooping, isLoopingIn); }
bool operator== (const PositionInfo& other) const noexcept
{
const auto tie = [] (const PositionInfo& i)
{
return std::make_tuple (i.getTimeInSamples(),
i.getTimeInSeconds(),
i.getPpqPosition(),
i.getEditOriginTime(),
i.getPpqPositionOfLastBarStart(),
i.getFrameRate(),
i.getBarCount(),
i.getTimeSignature(),
i.getBpm(),
i.getLoopPoints(),
i.getHostTimeNs(),
i.getIsPlaying(),
i.getIsRecording(),
i.getIsLooping());
};
return tie (*this) == tie (other);
}
bool operator!= (const PositionInfo& other) const noexcept
{
return ! operator== (other);
}
private:
bool getFlag (int64_t flagToCheck) const
{
return (flagToCheck & flags) != 0;
}
void setFlag (int64_t flagToCheck, bool value)
{
flags = (value ? flags | flagToCheck : flags & ~flagToCheck);
}
template <typename Value>
Optional<Value> getOptional (int64_t flagToCheck, Value value) const
{
return getFlag (flagToCheck) ? makeOptional (std::move (value)) : nullopt;
}
template <typename Value>
void setOptional (int64_t flagToCheck, Value& value, Optional<Value> opt)
{
if (opt.hasValue())
value = *opt;
setFlag (flagToCheck, opt.hasValue());
}
enum
{
flagTimeSignature = 1 << 0,
flagLoopPoints = 1 << 1,
flagFrameRate = 1 << 2,
flagTimeSeconds = 1 << 3,
flagLastBarStartPpq = 1 << 4,
flagPpqPosition = 1 << 5,
flagOriginTime = 1 << 6,
flagTempo = 1 << 7,
flagTimeSamples = 1 << 8,
flagBarCount = 1 << 9,
flagHostTimeNs = 1 << 10,
flagIsPlaying = 1 << 11,
flagIsRecording = 1 << 12,
flagIsLooping = 1 << 13
};
TimeSignature timeSignature;
LoopPoints loopPoints;
FrameRate frame = FrameRateType::fps23976;
double timeInSeconds = 0.0;
double lastBarStartPpq = 0.0;
double positionPpq = 0.0;
double originTime = 0.0;
double tempoBpm = 0.0;
int64_t timeInSamples = 0;
int64_t barCount = 0;
uint64_t hostTimeNs = 0;
int64_t flags = 0;
};
//==============================================================================
[[deprecated ("Use getPosition instead. Not all hosts are able to provide all time position information; getPosition differentiates clearly between set and unset fields.")]]
bool getCurrentPosition (CurrentPositionInfo& result)
{
if (const auto pos = getPosition())
{
result.resetToDefault();
if (const auto sig = pos->getTimeSignature())
{
result.timeSigNumerator = sig->numerator;
result.timeSigDenominator = sig->denominator;
}
if (const auto loop = pos->getLoopPoints())
{
result.ppqLoopStart = loop->ppqStart;
result.ppqLoopEnd = loop->ppqEnd;
}
if (const auto frame = pos->getFrameRate())
result.frameRate = *frame;
if (const auto timeInSeconds = pos->getTimeInSeconds())
result.timeInSeconds = *timeInSeconds;
if (const auto lastBarStartPpq = pos->getPpqPositionOfLastBarStart())
result.ppqPositionOfLastBarStart = *lastBarStartPpq;
if (const auto ppqPosition = pos->getPpqPosition())
result.ppqPosition = *ppqPosition;
if (const auto originTime = pos->getEditOriginTime())
result.editOriginTime = *originTime;
if (const auto bpm = pos->getBpm())
result.bpm = *bpm;
if (const auto timeInSamples = pos->getTimeInSamples())
result.timeInSamples = *timeInSamples;
result.isPlaying = pos->getIsPlaying();
result.isRecording = pos->getIsRecording();
result.isLooping = pos->getIsLooping();
return true;
}
return false;
}
virtual Optional<PositionInfo> getPosition() const = 0;
virtual bool canControlTransport();
virtual void transportPlay (bool shouldStartPlaying);
virtual void transportRecord (bool shouldStartRecording);
virtual void transportRewind();
};
} // namespace juce
juce::AudioPlayHead::FrameRate
Definition juce_AudioPlayHead.h:65
juce::AudioPlayHead::FrameRate::FrameRate
FrameRate(FrameRateType type)
Definition juce_AudioPlayHead.h:71
juce::AudioPlayHead::FrameRate::isPullDown
bool isPullDown() const
Definition juce_AudioPlayHead.h:99
juce::AudioPlayHead::FrameRate::withPullDown
FrameRate withPullDown(bool x=true) const
Definition juce_AudioPlayHead.h:111
juce::AudioPlayHead::FrameRate::withDrop
FrameRate withDrop(bool x=true) const
Definition juce_AudioPlayHead.h:108
juce::AudioPlayHead::FrameRate::getType
FrameRateType getType() const
Definition juce_AudioPlayHead.h:78
juce::AudioPlayHead::FrameRate::withBaseRate
FrameRate withBaseRate(int x) const
Definition juce_AudioPlayHead.h:105
juce::AudioPlayHead::FrameRate::getBaseRate
int getBaseRate() const
Definition juce_AudioPlayHead.h:93
juce::AudioPlayHead::FrameRate::isDrop
bool isDrop() const
Definition juce_AudioPlayHead.h:96
juce::AudioPlayHead::FrameRate::getEffectiveRate
double getEffectiveRate() const
Definition juce_AudioPlayHead.h:102
juce::AudioPlayHead::FrameRate::FrameRate
FrameRate()=default
juce::AudioPlayHead::PositionInfo
Definition juce_AudioPlayHead.h:318
juce::AudioPlayHead::PositionInfo::setBpm
void setBpm(Optional< double > bpmIn)
Definition juce_AudioPlayHead.h:336
juce::AudioPlayHead::PositionInfo::setIsRecording
void setIsRecording(bool isRecordingIn)
Definition juce_AudioPlayHead.h:408
juce::AudioPlayHead::PositionInfo::setTimeInSamples
void setTimeInSamples(Optional< int64_t > timeInSamplesIn)
Definition juce_AudioPlayHead.h:324
juce::AudioPlayHead::PositionInfo::getIsRecording
bool getIsRecording() const
Definition juce_AudioPlayHead.h:405
juce::AudioPlayHead::PositionInfo::getBpm
Optional< double > getBpm() const
Definition juce_AudioPlayHead.h:333
juce::AudioPlayHead::PositionInfo::getTimeInSamples
Optional< int64_t > getTimeInSamples() const
Definition juce_AudioPlayHead.h:321
juce::AudioPlayHead::PositionInfo::setHostTimeNs
void setHostTimeNs(Optional< uint64_t > hostTimeNsIn)
Definition juce_AudioPlayHead.h:393
juce::AudioPlayHead::PositionInfo::setLoopPoints
void setLoopPoints(Optional< LoopPoints > loopPointsIn)
Definition juce_AudioPlayHead.h:348
juce::AudioPlayHead::PositionInfo::getPpqPositionOfLastBarStart
Optional< double > getPpqPositionOfLastBarStart() const
Definition juce_AudioPlayHead.h:366
juce::AudioPlayHead::PositionInfo::setPpqPositionOfLastBarStart
void setPpqPositionOfLastBarStart(Optional< double > positionIn)
Definition juce_AudioPlayHead.h:369
juce::AudioPlayHead::PositionInfo::setBarCount
void setBarCount(Optional< int64_t > barCountIn)
Definition juce_AudioPlayHead.h:357
juce::AudioPlayHead::PositionInfo::setPpqPosition
void setPpqPosition(Optional< double > ppqPositionIn)
Definition juce_AudioPlayHead.h:381
juce::AudioPlayHead::PositionInfo::setTimeInSeconds
void setTimeInSeconds(Optional< double > timeInSecondsIn)
Definition juce_AudioPlayHead.h:330
juce::AudioPlayHead::PositionInfo::getHostTimeNs
Optional< uint64_t > getHostTimeNs() const
Definition juce_AudioPlayHead.h:390
juce::AudioPlayHead::PositionInfo::getPpqPosition
Optional< double > getPpqPosition() const
Definition juce_AudioPlayHead.h:378
juce::AudioPlayHead::PositionInfo::setIsLooping
void setIsLooping(bool isLoopingIn)
Definition juce_AudioPlayHead.h:414
juce::AudioPlayHead::PositionInfo::getTimeSignature
Optional< TimeSignature > getTimeSignature() const
Definition juce_AudioPlayHead.h:339
juce::AudioPlayHead::PositionInfo::getLoopPoints
Optional< LoopPoints > getLoopPoints() const
Definition juce_AudioPlayHead.h:345
juce::AudioPlayHead::PositionInfo::getTimeInSeconds
Optional< double > getTimeInSeconds() const
Definition juce_AudioPlayHead.h:327
juce::AudioPlayHead::PositionInfo::getIsPlaying
bool getIsPlaying() const
Definition juce_AudioPlayHead.h:396
juce::AudioPlayHead::PositionInfo::setTimeSignature
void setTimeSignature(Optional< TimeSignature > timeSignatureIn)
Definition juce_AudioPlayHead.h:342
juce::AudioPlayHead::PositionInfo::setFrameRate
void setFrameRate(Optional< FrameRate > frameRateIn)
Definition juce_AudioPlayHead.h:375
juce::AudioPlayHead::PositionInfo::setEditOriginTime
void setEditOriginTime(Optional< double > editOriginTimeIn)
Definition juce_AudioPlayHead.h:387
juce::AudioPlayHead::PositionInfo::setIsPlaying
void setIsPlaying(bool isPlayingIn)
Definition juce_AudioPlayHead.h:399
juce::AudioPlayHead::PositionInfo::getEditOriginTime
Optional< double > getEditOriginTime() const
Definition juce_AudioPlayHead.h:384
juce::AudioPlayHead::PositionInfo::getBarCount
Optional< int64_t > getBarCount() const
Definition juce_AudioPlayHead.h:354
juce::AudioPlayHead::PositionInfo::getFrameRate
Optional< FrameRate > getFrameRate() const
Definition juce_AudioPlayHead.h:372
juce::AudioPlayHead::PositionInfo::getIsLooping
bool getIsLooping() const
Definition juce_AudioPlayHead.h:411
juce::AudioPlayHead::transportPlay
virtual void transportPlay(bool shouldStartPlaying)
Definition juce_AudioPlayHead.cpp:27
juce::AudioPlayHead::canControlTransport
virtual bool canControlTransport()
Definition juce_AudioPlayHead.cpp:26
juce::AudioPlayHead::getPosition
virtual Optional< PositionInfo > getPosition() const =0
juce::AudioPlayHead::transportRewind
virtual void transportRewind()
Definition juce_AudioPlayHead.cpp:29
juce::AudioPlayHead::FrameRateType
FrameRateType
Definition juce_AudioPlayHead.h:50
juce::AudioPlayHead::getCurrentPosition
bool getCurrentPosition(CurrentPositionInfo &result)
Definition juce_AudioPlayHead.h:516
juce::AudioPlayHead::transportRecord
virtual void transportRecord(bool shouldStartRecording)
Definition juce_AudioPlayHead.cpp:28
juce::Optional
Definition juce_Optional.h:57
juce::AudioPlayHead::CurrentPositionInfo
Definition juce_AudioPlayHead.h:211
juce::AudioPlayHead::CurrentPositionInfo::timeSigDenominator
int timeSigDenominator
Definition juce_AudioPlayHead.h:219
juce::AudioPlayHead::CurrentPositionInfo::ppqPositionOfLastBarStart
double ppqPositionOfLastBarStart
Definition juce_AudioPlayHead.h:240
juce::AudioPlayHead::CurrentPositionInfo::ppqLoopEnd
double ppqLoopEnd
Definition juce_AudioPlayHead.h:264
juce::AudioPlayHead::CurrentPositionInfo::timeSigNumerator
int timeSigNumerator
Definition juce_AudioPlayHead.h:216
juce::AudioPlayHead::CurrentPositionInfo::ppqLoopStart
double ppqLoopStart
Definition juce_AudioPlayHead.h:258
juce::AudioPlayHead::CurrentPositionInfo::isPlaying
bool isPlaying
Definition juce_AudioPlayHead.h:246
juce::AudioPlayHead::CurrentPositionInfo::ppqPosition
double ppqPosition
Definition juce_AudioPlayHead.h:230
juce::AudioPlayHead::CurrentPositionInfo::frameRate
FrameRate frameRate
Definition juce_AudioPlayHead.h:243
juce::AudioPlayHead::CurrentPositionInfo::bpm
double bpm
Definition juce_AudioPlayHead.h:213
juce::AudioPlayHead::CurrentPositionInfo::isLooping
bool isLooping
Definition juce_AudioPlayHead.h:267
juce::AudioPlayHead::CurrentPositionInfo::timeInSamples
int64 timeInSamples
Definition juce_AudioPlayHead.h:222
juce::AudioPlayHead::CurrentPositionInfo::editOriginTime
double editOriginTime
Definition juce_AudioPlayHead.h:227
juce::AudioPlayHead::CurrentPositionInfo::timeInSeconds
double timeInSeconds
Definition juce_AudioPlayHead.h:224
juce::AudioPlayHead::CurrentPositionInfo::isRecording
bool isRecording
Definition juce_AudioPlayHead.h:252
juce::AudioPlayHead::LoopPoints
Definition juce_AudioPlayHead.h:184
juce::AudioPlayHead::LoopPoints::ppqEnd
double ppqEnd
Definition juce_AudioPlayHead.h:189
juce::AudioPlayHead::LoopPoints::ppqStart
double ppqStart
Definition juce_AudioPlayHead.h:186
juce::AudioPlayHead::TimeSignature
Definition juce_AudioPlayHead.h:160
juce::AudioPlayHead::TimeSignature::numerator
int numerator
Definition juce_AudioPlayHead.h:162
juce::AudioPlayHead::TimeSignature::denominator
int denominator
Definition juce_AudioPlayHead.h:165