Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

Fix queued audio during frame scrubbing

Rodneydd247feddd29
diff --git a/toonz/sources/include/toonz/txsheet.h b/toonz/sources/include/toonz/txsheet.h
index 0458b07a8..5a7b8e2e5 100644
--- a/toonz/sources/include/toonz/txsheet.h
+++ b/toonz/sources/include/toonz/txsheet.h
@@ -511,7 +511,8 @@ in TXsheetImp.
/*! Utility Function */
TSoundTrack *makeSound(int fromFrame, int toFrame, int frameRate = -1);
#endif
- void scrub(int frame, bool isPreview = false);
+ // Replace the current audition. endFrame is inclusive; -1 means one frame.
+ void scrub(int frame, bool isPreview = false, int endFrame = -1);
void stopScrub();
void play(const TSoundTrackP &soundtrack, int s0, int s1, bool loop);
diff --git a/toonz/sources/toonz/viewerpane.cpp b/toonz/sources/toonz/viewerpane.cpp
index 502555c4c..d9048ba8b 100644
--- a/toonz/sources/toonz/viewerpane.cpp
+++ b/toonz/sources/toonz/viewerpane.cpp
@@ -149,6 +149,10 @@ BaseViewerPanel::BaseViewerPanel(QWidget *parent, Qt::WindowFlags flags)
m_flipConsole->setFrameHandle(TApp::instance()->getCurrentFrame());
bool ret = true;
+ ret = ret && connect(m_flipConsole, &FlipConsole::sliderReleased, this, []() {
+ if (!TApp::instance()->getCurrentFrame()->isPlaying())
+ TApp::instance()->getCurrentXsheet()->getXsheet()->stopScrub();
+ });
// When zoom changed, only if the viewer is active, change window titl
ret = ret && connect(m_sceneViewer, SIGNAL(onZoomChanged()),
SLOT(changeWindowTitle()));
diff --git a/toonz/sources/toonzlib/CMakeLists.txt b/toonz/sources/toonzlib/CMakeLists.txt
index b70cfa8c8..dc8feabc7 100644
--- a/toonz/sources/toonzlib/CMakeLists.txt
+++ b/toonz/sources/toonzlib/CMakeLists.txt
@@ -34,6 +34,7 @@ set(MOC_HEADERS
set(HEADERS
${MOC_HEADERS}
+ audioscrubplayer.h
autoadjust.h
autopos.h
cleanupcommon.h
diff --git a/toonz/sources/toonzlib/audioscrubplayer.h b/toonz/sources/toonzlib/audioscrubplayer.h
new file mode 100644
index 000000000..64f6ce226
--- /dev/null
+++ b/toonz/sources/toonzlib/audioscrubplayer.h
@@ -0,0 +1,82 @@
+#pragma once
+
+#include "tsound.h"
+#include "tsop.h"
+
+#include <QAudioDeviceInfo>
+#include <QAudioOutput>
+#include <QBuffer>
+#include <limits>
+#include <memory>
+
+// A GUI-thread-only, finite audition stream. Reset before replacing its data:
+// QAudioOutput must never retain a stream backed by a modified/deleted buffer.
+// Continuous playback deliberately does not use this player.
+class AudioScrubPlayer final {
+ QBuffer m_buffer;
+ QAudioDeviceInfo m_device;
+ std::unique_ptr<QAudioOutput> m_output;
+
+public:
+ ~AudioScrubPlayer() { stop(); }
+
+ void stop() {
+ if (m_output) m_output->reset();
+ m_buffer.close();
+ m_buffer.setData(QByteArray());
+ }
+
+ void play(const TSoundTrackP &source) {
+ stop();
+ if (!source || source->getSampleCount() <= 0) return;
+
+ const QAudioDeviceInfo device = QAudioDeviceInfo::defaultOutputDevice();
+ if (device.isNull())
+ throw TSoundDeviceException(TSoundDeviceException::NoDevice,
+ "No audio output device for scrubbing");
+
+ // Use a conservative audition format. This changes only the temporary
+ // snippet, never the scene's stored audio or export format.
+ QAudioFormat format;
+ format.setCodec("audio/pcm");
+ format.setSampleRate(source->getSampleRate());
+ format.setChannelCount(source->getChannelCount());
+ format.setSampleSize(16);
+ format.setSampleType(QAudioFormat::SignedInt);
+ format.setByteOrder(QAudioFormat::LittleEndian);
+ if (!device.isFormatSupported(format))
+ format = device.nearestFormat(format);
+ if (!format.isValid() || !device.isFormatSupported(format) ||
+ format.codec() != "audio/pcm" || format.sampleSize() != 16 ||
+ format.sampleType() != QAudioFormat::SignedInt ||
+ format.byteOrder() != QAudioFormat::LittleEndian ||
+ format.channelCount() < 1 || format.channelCount() > 2)
+ throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat,
+ "Audio scrubbing requires 16-bit PCM output");
+
+ const TSoundTrackFormat target(format.sampleRate(), 16,
+ format.channelCount(), TSound::INT);
+ TSoundTrackP snippet = TSop::convert(source, target);
+ if (!snippet || !snippet->getRawData()) return;
+ const qint64 bytes =
+ qint64(snippet->getSampleCount()) * snippet->getSampleSize();
+ if (bytes <= 0 || bytes > std::numeric_limits<int>::max()) return;
+
+ if (!m_output || m_device != device || m_output->format() != format) {
+ m_output.reset(new QAudioOutput(device, format));
+ m_device = device;
+ // Preserve the existing TXsheet playback gain on each platform.
+#ifndef _WIN32
+ m_output->setVolume(0.5);
+#endif
+ // Qt may enlarge this request. Replacement/reset still prevents a
+ // backlog, independently of the actual hardware buffer size.
+ m_output->setBufferSize(format.bytesForDuration(20000));
+ }
+ m_buffer.setData(QByteArray(
+ reinterpret_cast<const char *>(snippet->getRawData()), int(bytes)));
+ m_buffer.open(QIODevice::ReadOnly);
+ m_output->start(&m_buffer);
+ if (m_output->error() != QAudio::NoError) stop();
+ }
+};
diff --git a/toonz/sources/toonzlib/tframehandle.cpp b/toonz/sources/toonzlib/tframehandle.cpp
index c69be8ebb..b66cf17e4 100644
--- a/toonz/sources/toonzlib/tframehandle.cpp
+++ b/toonz/sources/toonzlib/tframehandle.cpp
@@ -242,12 +242,13 @@ void TFrameHandle::timerEvent(QTimerEvent *event) {
assert(isScrubbing());
qint64 elapsedTime = m_clock.elapsed();
- int frame = m_scrubRange.first + elapsedTime * m_fps / 1000;
- int lastFrame = m_scrubRange.second;
- if (frame >= lastFrame) {
+ int frame = m_scrubRange.first + elapsedTime * m_fps / 1000;
+ int lastFrame = m_scrubRange.second;
+ // Finish the last frame's audition before stopping the range.
+ if (frame > lastFrame) {
if (m_frame != lastFrame) setFrame(lastFrame);
stopScrubbing();
- } else {
+ } else if (m_frame != frame) {
setFrame(frame);
}
}
@@ -344,8 +345,8 @@ bool TFrameHandle::scrub(int r0, int r1, double framePerSecond) {
if (m_audioColumn)
m_audioColumn->scrub(r0, r1);
else if (m_xsheet) {
- int i;
- for (i = r0; i <= r1; i++) m_xsheet->scrub(i, true);
+ // A deliberate range audition is one stream, not competing frame requests.
+ m_xsheet->scrub(r0, true, r1);
}
if (onlyOneFrame) return false;
@@ -358,10 +359,13 @@ bool TFrameHandle::scrub(int r0, int r1, double framePerSecond) {
void TFrameHandle::stopScrubbing() {
if (!isScrubbing()) return;
if (m_timerId > 0) killTimer(m_timerId);
- m_timerId = 0;
- m_scrubRange = std::make_pair(0, -1);
+ m_timerId = 0;
+ m_scrubRange = std::make_pair(0, -1);
if (m_audioColumn) m_audioColumn = 0;
- if (m_xsheet) m_xsheet = 0;
- m_fps = 0;
+ if (m_xsheet) {
+ m_xsheet->stopScrub();
+ m_xsheet = 0;
+ }
+ m_fps = 0;
emit scrubStopped();
}
diff --git a/toonz/sources/toonzlib/txsheet.cpp b/toonz/sources/toonzlib/txsheet.cpp
index 704b98918..5696cc8d4 100644
--- a/toonz/sources/toonzlib/txsheet.cpp
+++ b/toonz/sources/toonzlib/txsheet.cpp
@@ -37,6 +37,7 @@
#include "toonz/txsheet.h"
#include "toonz/preferences.h"
+#include "audioscrubplayer.h"
// STD includes
#include <set>
@@ -106,6 +107,7 @@ struct TXsheet::TXsheetImp {
int m_viewColumn;
TSoundTrackP m_mixedSound;
+ std::unique_ptr<AudioScrubPlayer> m_scrubPlayer;
ColumnFan m_columnFans[Orientations::COUNT];
ToonzScene *m_scene;
@@ -1493,39 +1495,55 @@ TSoundTrack *TXsheet::makeSound(SoundProperties *properties) {
//-----------------------------------------------------------------------------
-void TXsheet::scrub(int frame, bool isPreview) {
+void TXsheet::scrub(int frame, bool isPreview, int endFrame) {
+ // Cancel the previous audition even if this position has no usable audio.
+ if (m_imp->m_scrubPlayer) m_imp->m_scrubPlayer->stop();
try {
double fps =
getScene()->getProperties()->getOutputProperties()->getFrameRate();
+ if (frame < 0 || !std::isfinite(fps) || fps <= 0) return;
+ if (endFrame < 0) endFrame = frame;
+ if (endFrame < frame) return;
TXsheet::SoundProperties *prop = new TXsheet::SoundProperties();
prop->m_isPreview = isPreview;
TSoundTrack *st = makeSound(prop); // Absorbs prop's ownership
- if (!st) return;
-
- double samplePerFrame = st->getSampleRate() / fps;
-
- double s0 = frame * samplePerFrame, s1 = s0 + samplePerFrame;
- play(st, static_cast<int>(s0), static_cast<int>(s1), false);
+ if (!st || st->getSampleCount() <= 0 || st->getSampleRate() <= 0) return;
+
+ const double samplePerFrame = double(st->getSampleRate()) / fps;
+ if (!std::isfinite(samplePerFrame) || samplePerFrame <= 0) return;
+
+ // extract() has an inclusive end; adjacent frames must not share a sample.
+ const double begin = std::floor(double(frame) * samplePerFrame);
+ const double end =
+ std::min(double(st->getSampleCount()),
+ std::floor((double(endFrame) + 1) * samplePerFrame));
+ // Validate floating-point boundaries before converting to sample indices.
+ if (!std::isfinite(begin) || !std::isfinite(end) || begin < 0 ||
+ begin >= end)
+ return;
+ TSoundTrackP snippet = st->extract(int(begin), int(end) - 1);
+ if (!m_imp->m_scrubPlayer)
+ m_imp->m_scrubPlayer = std::make_unique<AudioScrubPlayer>();
+ m_imp->m_scrubPlayer->play(snippet);
} catch (TSoundDeviceException &e) {
- if (e.getType() == TSoundDeviceException::NoDevice) {
- std::cout << ::to_string(e.getMessage()) << '\n';
- } else {
- throw TSoundDeviceException(e.getType(), e.getMessage());
- }
+ // A failed audition must not escape a frame-navigation event handler.
+ std::cout << ::to_string(e.getMessage()) << '\n';
}
}
//-----------------------------------------------------------------------------
void TXsheet::stopScrub() {
+ if (m_imp->m_scrubPlayer) m_imp->m_scrubPlayer->stop();
if (m_player) m_player->stop();
}
//-----------------------------------------------------------------------------
void TXsheet::play(const TSoundTrackP &soundtrack, int s0, int s1, bool loop) {
+ if (m_imp->m_scrubPlayer) m_imp->m_scrubPlayer->stop();
if (!TSoundOutputDevice::installed()) return;
if (!m_player) m_player = std::make_unique<TSoundOutputDevice>();