Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the real track length to scale waveforms #11162

Merged
merged 11 commits into from
Feb 20, 2023
Merged
2 changes: 1 addition & 1 deletion src/track/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ QString Track::getURL() const {
return m_record.getUrl();
}

ConstWaveformPointer Track::getWaveform() const {
const ConstWaveformPointer& Track::getWaveform() const {
return m_waveform;
}

Expand Down
2 changes: 1 addition & 1 deletion src/track/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Track : public QObject {
/// any metadata in file tags. Otherwise just the title (even if it is empty).
QString getTitleInfo() const;

ConstWaveformPointer getWaveform() const;
const ConstWaveformPointer& getWaveform() const;
void setWaveform(ConstWaveformPointer pWaveform);

ConstWaveformPointer getWaveformSummary() const;
Expand Down
50 changes: 26 additions & 24 deletions src/waveform/renderers/glslwaveformrenderersignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ bool GLSLWaveformRendererSignal::loadShaders() {
}

bool GLSLWaveformRendererSignal::loadTexture() {
TrackPointer trackInfo = m_waveformRenderer->getTrackInfo();
ConstWaveformPointer waveform;
int dataSize = 0;
const WaveformData* data = nullptr;

if (trackInfo) {
waveform = trackInfo->getWaveform();
if (waveform) {
dataSize = waveform->getDataSize();
if (dataSize > 1) {
data = waveform->data();
}
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform) {
dataSize = pWaveform->getDataSize();
if (dataSize > 1) {
data = pWaveform->data();
}
}

Expand All @@ -114,11 +110,11 @@ bool GLSLWaveformRendererSignal::loadTexture() {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

if (waveform != nullptr && data != nullptr) {
if (pWaveform != nullptr && data != nullptr) {
// Waveform ensures that getTextureSize is a multiple of
// getTextureStride so there is no rounding here.
int textureWidth = waveform->getTextureStride();
int textureHeight = waveform->getTextureSize() / waveform->getTextureStride();
int textureWidth = pWaveform->getTextureStride();
int textureHeight = pWaveform->getTextureSize() / pWaveform->getTextureStride();

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data);
Expand Down Expand Up @@ -222,7 +218,7 @@ void GLSLWaveformRendererSignal::onSetTrack() {

slotWaveformUpdated();

TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
const TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
return;
}
Expand Down Expand Up @@ -256,34 +252,39 @@ void GLSLWaveformRendererSignal::slotWaveformUpdated() {
}

void GLSLWaveformRendererSignal::draw(QPainter* painter, QPaintEvent* /*event*/) {
TrackPointer trackInfo = m_waveformRenderer->getTrackInfo();
if (!trackInfo) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return;
}

ConstWaveformPointer waveform = trackInfo->getWaveform();
if (waveform.isNull()) {
const double audioVisualRatio = pWaveform->getAudioVisualRatio();
if (audioVisualRatio <= 0) {
return;
}

int dataSize = waveform->getDataSize();
int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return;
}

const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return;
}

maybeInitializeGL();

// save the GL state set for QPainter
painter->beginNativePainting();

// NOTE(vRince): completion can change during loadTexture
// do not remove currenCompletion temp variable !
const int currentCompletion = waveform->getCompletion();
const int currentCompletion = pWaveform->getCompletion();
if (m_textureRenderedWaveformCompletion < currentCompletion) {
loadTexture();
m_textureRenderedWaveformCompletion = currentCompletion;
Expand All @@ -294,9 +295,10 @@ void GLSLWaveformRendererSignal::draw(QPainter* painter, QPaintEvent* /*event*/)
getGains(&allGain, &lowGain, &midGain, &highGain);

const auto firstVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getFirstDisplayedPosition() * dataSize / 2.0);
m_waveformRenderer->getFirstDisplayedPosition() * trackSamples /
audioVisualRatio / 2.0);
const auto lastVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getLastDisplayedPosition() * dataSize / 2.0);
m_waveformRenderer->getLastDisplayedPosition() * trackSamples / audioVisualRatio / 2.0);

// const int firstIndex = int(firstVisualIndex+0.5);
// firstVisualIndex = firstIndex - firstIndex%2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a check above, that the integer datasize can't be <=1. Therefore dataSize / 2.0 can't be smaller than 1.0. I wonder if a similar check for trackSamples / audioVisualRatio / 2.0 is needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trackSamples and audioVisualRatio are also checked individual.

Expand Down Expand Up @@ -329,8 +331,8 @@ void GLSLWaveformRendererSignal::draw(QPainter* painter, QPaintEvent* /*event*/)
m_frameShaderProgram->setUniformValue("framebufferSize", QVector2D(
m_framebuffer->width(), m_framebuffer->height()));
m_frameShaderProgram->setUniformValue("waveformLength", dataSize);
m_frameShaderProgram->setUniformValue("textureSize", waveform->getTextureSize());
m_frameShaderProgram->setUniformValue("textureStride", waveform->getTextureStride());
m_frameShaderProgram->setUniformValue("textureSize", pWaveform->getTextureSize());
m_frameShaderProgram->setUniformValue("textureStride", pWaveform->getTextureStride());

m_frameShaderProgram->setUniformValue("firstVisualIndex", firstVisualIndex);
m_frameShaderProgram->setUniformValue("lastVisualIndex", lastVisualIndex);
Expand Down
23 changes: 15 additions & 8 deletions src/waveform/renderers/glvsynctestrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,35 @@ void GLVSyncTestRenderer::draw(QPainter* painter, QPaintEvent* /*event*/) {

timer.start();

TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return;
}

ConstWaveformPointer waveform = pTrack->getWaveform();
if (waveform.isNull()) {
const double audioVisualRatio = pWaveform->getAudioVisualRatio();
if (audioVisualRatio <= 0) {
return;
}

const int dataSize = waveform->getDataSize();
const int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return;
}

double firstVisualIndex = m_waveformRenderer->getFirstDisplayedPosition() * dataSize;
double lastVisualIndex = m_waveformRenderer->getLastDisplayedPosition() * dataSize;
const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return;
}

double firstVisualIndex = m_waveformRenderer->getFirstDisplayedPosition() *
trackSamples / audioVisualRatio;
double lastVisualIndex = m_waveformRenderer->getLastDisplayedPosition() *
trackSamples / audioVisualRatio;

const int firstIndex = int(firstVisualIndex + 0.5);
firstVisualIndex = firstIndex - firstIndex % 2;
Expand Down
21 changes: 13 additions & 8 deletions src/waveform/renderers/glwaveformrendererfilteredsignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,35 @@ void GLWaveformRendererFilteredSignal::onSetup(const QDomNode& /*node*/) {
void GLWaveformRendererFilteredSignal::draw(QPainter* painter, QPaintEvent* /*event*/) {
maybeInitializeGL();

TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return;
}

ConstWaveformPointer waveform = pTrack->getWaveform();
if (waveform.isNull()) {
const double audioVisualRatio = pWaveform->getAudioVisualRatio();
if (audioVisualRatio <= 0) {
return;
}

const int dataSize = waveform->getDataSize();
const int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return;
}

const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return;
}

auto firstVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getFirstDisplayedPosition() * dataSize);
m_waveformRenderer->getFirstDisplayedPosition() * trackSamples / audioVisualRatio);
auto lastVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getLastDisplayedPosition() * dataSize);
m_waveformRenderer->getLastDisplayedPosition() * trackSamples / audioVisualRatio);
const auto lineWidth = static_cast<GLfloat>(
1.0 / m_waveformRenderer->getVisualSamplePerPixel() + 1);

Expand Down
21 changes: 13 additions & 8 deletions src/waveform/renderers/glwaveformrendererrgb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,35 @@ void GLWaveformRendererRGB::onSetup(const QDomNode& /* node */) {
void GLWaveformRendererRGB::draw(QPainter* painter, QPaintEvent* /*event*/) {
maybeInitializeGL();

TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return;
}

ConstWaveformPointer waveform = pTrack->getWaveform();
if (waveform.isNull()) {
const double audioVisualRatio = pWaveform->getAudioVisualRatio();
if (audioVisualRatio <= 0) {
return;
}

const int dataSize = waveform->getDataSize();
const int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return;
}

const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return;
}

auto firstVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getFirstDisplayedPosition() * dataSize);
m_waveformRenderer->getFirstDisplayedPosition() * trackSamples / audioVisualRatio);
auto lastVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getLastDisplayedPosition() * dataSize);
m_waveformRenderer->getLastDisplayedPosition() * trackSamples / audioVisualRatio);
const auto lineWidth = static_cast<GLfloat>(
(1.0 / m_waveformRenderer->getVisualSamplePerPixel()) + 1.5);

Expand Down
21 changes: 13 additions & 8 deletions src/waveform/renderers/glwaveformrenderersimplesignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,35 @@ inline void setPoint(QPointF& point, qreal x, qreal y) {
void GLWaveformRendererSimpleSignal::draw(QPainter* painter, QPaintEvent* /*event*/) {
maybeInitializeGL();

TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return;
}

ConstWaveformPointer waveform = pTrack->getWaveform();
if (waveform.isNull()) {
const double audioVisualRatio = pWaveform->getAudioVisualRatio();
if (audioVisualRatio <= 0) {
return;
}

const int dataSize = waveform->getDataSize();
const int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return;
}

const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return;
}

auto firstVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getFirstDisplayedPosition() * dataSize);
m_waveformRenderer->getFirstDisplayedPosition() * trackSamples / audioVisualRatio);
auto lastVisualIndex = static_cast<GLfloat>(
m_waveformRenderer->getLastDisplayedPosition() * dataSize);
m_waveformRenderer->getLastDisplayedPosition() * trackSamples / audioVisualRatio);
const auto lineWidth = static_cast<GLfloat>(
1.0 / m_waveformRenderer->getVisualSamplePerPixel() + 1);

Expand Down
13 changes: 4 additions & 9 deletions src/waveform/renderers/qtvsynctestrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,17 @@ void QtVSyncTestRenderer::draw(QPainter* pPainter, QPaintEvent* /*event*/) {

timer.start();

TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return;
}

ConstWaveformPointer waveform = pTrack->getWaveform();
if (waveform.isNull()) {
return;
}

const int dataSize = waveform->getDataSize();
const int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return;
}
Expand Down
25 changes: 17 additions & 8 deletions src/waveform/renderers/qtwaveformrendererfilteredsignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,37 @@ inline void setPoint(QPointF& point, qreal x, qreal y) {
int QtWaveformRendererFilteredSignal::buildPolygon() {
// We have to check the track is present because it might have been unloaded
// between the call to draw and the call to buildPolygon
TrackPointer pTrack = m_waveformRenderer->getTrackInfo();
if (!pTrack) {
ConstWaveformPointer pWaveform = m_waveformRenderer->getWaveform();
if (pWaveform.isNull()) {
return 0;
}

ConstWaveformPointer waveform = pTrack->getWaveform();
if (waveform.isNull()) {
const double audioVisualRatio = pWaveform->getAudioVisualRatio();
if (audioVisualRatio <= 0) {
return 0;
}

const int dataSize = waveform->getDataSize();
const int dataSize = pWaveform->getDataSize();
if (dataSize <= 1) {
return 0;
}

const WaveformData* data = waveform->data();
const WaveformData* data = pWaveform->data();
if (data == nullptr) {
return 0;
}

const double firstVisualIndex = m_waveformRenderer->getFirstDisplayedPosition() * dataSize;
const double lastVisualIndex = m_waveformRenderer->getLastDisplayedPosition() * dataSize;
const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return 0;
}

const double firstVisualIndex =
m_waveformRenderer->getFirstDisplayedPosition() * trackSamples /
audioVisualRatio;
const double lastVisualIndex =
m_waveformRenderer->getLastDisplayedPosition() * trackSamples /
audioVisualRatio;

m_polygon[0].clear();
m_polygon[1].clear();
Expand Down
Loading