TEMPO:在动画 GIF/PDF/SVG 中播放声音

TEMPO:在动画 GIF/PDF/SVG 中播放声音

练习钢琴的时候,速度(iOS 上的一款应用程序,CHplay)是一款经常用来按时打节拍的节拍器。

我可以为 TEMPO 制作动画 GIF/PDF/SVG/frame(在beamer),但不知道如何播放声音(例如嘟嘟 8,mp3或者wav这里)每次节拍。

有人能帮忙吗(提供完整的代码和清晰的解释)?谢谢!

在此处输入图片描述

以下代码是我的 MWE。(这些是动画方面的很好的例子)

% TEMPO for piano tempo.tex
% for animated GIF, run on command line (require ImageMagick installed)
% magick -density 200 -delay 100 tempo.pdf tempo.gif
% -delay 100 >>> each frame is 100/100 = 1 second 
\documentclass[tikz]{standalone}
\begin{document}
\foreach \n in {1,...,4}{
\pagecolor{white}
\begin{tikzpicture}
\path (0,-1) rectangle (5,1);
\shade[inner color=green,outer color=green!10] 
(\n,0) circle(.5);
\foreach \i in {1,...,4}
\fill[cyan] (\i,0) circle(.3);
\end{tikzpicture}
}
\end{document}

经过微小的改动,我们可以得到动画 PDF(在 Acrobat Reader 中打开)。

% animated PDF standalone tempopdf.tex
% \multiframe{4}{n=1+1} >>> starting frame is with n=1, 
% then plus 1 each frame, so we have 4 frames
% [loop,controls]{1} >>> number 1 means 1 second each frame
\documentclass{standalone}
\usepackage{tikz}
\usepackage{animate}
\begin{document}
\begin{animateinline}[loop,controls]{1}
\multiframe{4}{n=1+1}
{
\begin{tikzpicture}
\path (0,-1) rectangle (5,1);
\shade[inner color=green,outer color=green!10] 
(\n,0) circle(.6);
\foreach \i in {1,...,4}
\fill[cyan] (\i,0) circle(.3);
\end{tikzpicture}
}
\end{animateinline}
\end{document}

投影机中的动画 PDF 框架(使用 Acrobat Reader 打开)

% animated PDF frame in beamer (open with Acrobat Reader)
\documentclass{beamer}
\usepackage{tikz}
\usepackage{animate}
\usepackage{multimedia}
\begin{document}
\begin{frame}{TEMPO for piano}
\begin{center}
\begin{animateinline}[loop,controls]{1}
\multiframe{4}{n=1+1}
{
\begin{tikzpicture}
\path (0,-1) rectangle (5,1);
\shade[inner color=green,outer color=green!10] (\n,0) circle(.6);
\foreach \i in {1,...,4} \fill[cyan] (\i,0) circle(.3);
\end{tikzpicture}
}
\end{animateinline}
\end{center}
\end{frame} 
\end{document}

答案1

点击在下面的图片上查看滴答节拍器(SVG 版本)的运行情况:

https://agrahn.gitlab.io/svg/tempoForWeb-2.1.svg

这个animate基于的示例有两个版本。

PDF版本需要 Acrobat Reader,因此仅限于台式电脑的 Windows 和 OSX 平台。(不支持移动设备。)PDF 版本用于media9播放嵌入的声音文件click.mp3,取自animate已在手册中提供滴答节拍器示例的包。

独立SVG版本可在大多数现代 Web 浏览器(Firefox、Chrome、Opera 等)上运行,甚至可在移动设备上运行。在这里,声音使用 HTML5 标签嵌入为 Base64 编码的 Blob <audio>。(在命令行中click.mp3使用“ ”进行编码base64 click.mp3。)物理嵌入的声音文件使 SVG 自成一体。

不管哪种情况,声音的播放都是由 JavaScript 在动画运行过程中每帧变化时启动的。 的“时间轴”功能animate用于将 JS 代码与每一帧关联起来(时间轴输入frames.txt)。

速度标记是在第二个animateinline环境中实现的。它提供了 39 种典型的节拍器速度,可以使用 +/- 按钮逐步调整。这些按钮使用animateJavaScript 接口来增加和减少速度标记帧数。另一个时间线文件tempo.txt将每个标记帧与一个 JS 代码片段关联起来,该代码片段会相应地缩放节拍器速度,同样使用 的 JS 接口animate

PDFpdflatex版本,使用、lualatexlatex+dvips+ps2pdf或进行编译xelatex

\documentclass[varwidth,border=12pt]{standalone}

\usepackage{tikz}
\usepackage{animate}
\usepackage{media9}
\usepackage{fontawesome5}

% tempo markers
\def\tempos{{40,42,44,46,48,50,52,54,56,58,60,63,66,69,72,76,80,84,88,92,96,100,104,108,112,116,120,126,132,138,144,152,160,168,176,184,192,200,208}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% timeline files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% metronome
\usepackage{filecontents}
\begin{filecontents*}{frames.txt}
::0: annotRM['click'].callAS('play');
::1: annotRM['click'].callAS('play');
::2: annotRM['click'].callAS('play');
::3: annotRM['click'].callAS('play');
\end{filecontents*}
% tempo
\newwrite\TimeLineFile
\immediate\openout\TimeLineFile=tempo.txt
\foreach \i in {0,1,...,38} {
    %JavaScript for scaling the metronome's default speed (60 frames per min)
    \pgfmathparse{\tempos[\i]/60}
    \immediate\write\TimeLineFile{::\i:anim.metronome.speed=\pgfmathresult;}
}
\immediate\closeout\TimeLineFile
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setlength{\textwidth}{4.4cm}

\begin{document}
\begin{center}
%the sound player  
\makebox[0pt][r]{\includemedia[
  width=1ex,height=1ex,
  label=click,
  addresource=click.mp3,
  activate=pageopen,transparent,noplaybutton,
  flashvars={source=click.mp3&hideBar=true}
]{}{APlayer.swf}}%
\makebox[4cm][l]{The \LaTeX{} Metronome}\\[1ex]

%%%%%%%%%%%
% metronome
%%%%%%%%%%%
\begin{animateinline}[label=metronome,nomouse,loop,timeline=frames.txt]{1}
  \multiframe{4}{n=1+1}{
    \begin{tikzpicture}
      \path (0.3,-0.5) rectangle (4.7,0.5);
      \shade[inner color=green,outer color=green!10] (\n,0) circle(.6);
      \foreach \i in {1,...,4} \fill[cyan] (\i,0) circle(.3);
    \end{tikzpicture}
  }
\end{animateinline}

%%%%%%%%%%%
% controls
%%%%%%%%%%%
\makebox[4cm]{%
\mediabutton[
  jsaction={
    if(anim.metronome.isPlaying) anim.metronome.stopFirst(); 
    else anim.metronome.playFwd();
  }
]{\faPowerOff}\hfill
% tempo
\mediabutton[
  jsaction={ try {--anim.tempo.frameNum;} catch (e) {} }
]{\faMinusCircle}\,%
\begin{animateinline}[label=tempo,step,nomouse,poster=10,timeline=tempo.txt]{1}
  \multiframe{39}{i=0+1}{
    \pgfmathparse{\tempos[\i]}
    \makebox[\widthof{000}][c]{\strut\pgfmathresult}
  }
\end{animateinline}\,%
\mediabutton[
  jsaction={ try {++anim.tempo.frameNum;} catch (e) {} }
]{\faPlusCircle}}
\end{center}
\end{document}

SVG版本,要编译

latex tempoForWeb.tex
latex tempoForWeb.tex
dvisvgm --font-format=woff --exact --zoom=-1 tempoForWeb.dvi

SVG 可以在 Web 浏览器中独立查看,也可以通过 HTML 标签嵌入到网页中<object>

\documentclass[dvisvgm,varwidth,preview]{standalone}

\usepackage{tikz}
\usepackage{animate}
\usepackage{fontawesome5}

% tempo markers
\def\tempos{{40,42,44,46,48,50,52,54,56,58,60,63,66,69,72,76,80,84,88,92,96,100,104,108,112,116,120,126,132,138,144,152,160,168,176,184,192,200,208}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% timeline files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% metronome
\usepackage{filecontents}
\begin{filecontents*}{frames.txt}
::0:$('click').play();
::1:$('click').play();
::2:$('click').play();
::3:$('click').play();
\end{filecontents*}
% tempo
\newwrite\TimeLineFile
\immediate\openout\TimeLineFile=tempo.txt
\foreach \i in {0,1,...,38} {
    %JavaScript for scaling the metronome's default speed (60 frames per min)
    \pgfmathparse{\tempos[\i]/60}
    \immediate\write\TimeLineFile{::\i:anim.metronome.speed=\pgfmathresult;}
}
\immediate\closeout\TimeLineFile
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for producing push buttons
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ExplSyntaxOn
\def\controlButton#1#2{% #1:button face, #2 JavaScript
  \sbox{0}{#1}
  \makebox[0pt][l]{\usebox{0}}
  \pbs_pdfannot:nnnn{\wd0}{\ht0}{\dp0}{cursor='pointer'~onmousedown='#2'}
  \phantom{\usebox{0}}
}
\ExplSyntaxOff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setlength{\textwidth}{4.4cm}

\begin{document}
% embed sound
\special{dvisvgm:rawdef
  <audio xmlns="http://www.w3.org/1999/xhtml" id='click'>
    <source
    src="data:audio/mpeg;base64,
    SUQzBAAAAAAAOlRJVDIAAAAHAAADY2xpY2sAVERSTAAAAAYAAAMyMDA4AFRTU0UAAAAPAAADTGF2
    ZjUzLjMyLjEwMAD/+5DAAAAAAAAAAAAAAAAAAAAAAABYaW5nAAAABwAAAAUAAAnKAFVVVVVVVVVV
    VVVVVVVVVVVVVVV/f39/f39/f39/f39/f39/f39/f6qqqqqqqqqqqqqqqqqqqqqqqqqq1dXV1dXV
    1dXV1dXV1dXV1dXV1dX//////////////////////////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/+5DEAAAUjaVe1JYACx40rvMw8ABACACTCHXF
    YJgmG27XRo51BcA4AABBEMFixYsPDMzXv3mb3WLFhgYGCzl5gYOde973WOdq9evfxevXv3mZ2jCx
    Y5TtbWLHJve970669e/eb0pO3vf6L16+973v8zlKdNKXvd+9/ml74wsWLHKfOUpSk3pSjCw4MFjl
    7zM0pSl7zOUpSl73vNKUp83ve/4wvXv/lKUWL198ABkAd/AMPD/8f+uAYfHWXJSmU242sLUss7ay
    Un2Hl9HqQQKKuhNUEMwoZKsdD97mXj7TiWXCLRlUog4Z7JeI5bH4qoJcV9rXKLO5cNbKcqreQWh2
    qTo02rrDIw2anjA/Z77xC1/iM9iscSZ+yba2B/ApWjVEvjV4Wrb1M+v/2eA24Y2FypG0+mtJjT3F
    NQYWGa0WePW9tz2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L/4C4gLXhhh+
    QWTCO//xUSbC4EAE4AKKDSw0b//BtINyCEgDaQZBAwawDCnARIgsJ///E2ifQsmGOFlFYOiEzAAG
    gECQ6T///w8oNyQ/wCxUBAEUQBwcAgSF9AbZCEQdMAgOLNFk/////iEorotoWJBgYMFAOBha6LMB
    ADC10ZAL0hsotgzSUhxMQU1FMy45OS4zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
    qqqqqqqqqqqq" type="audio/mpeg"/>
%    <source src="click.mp3" type="audio/mpeg"/> % alternatively: separate mp3
  </audio>
  <script type="text/javascript">
  <![CDATA[
    function $(id) {return document.getElementById(id);};
  ]]>
  </script>
}
\begin{center}
\makebox[4cm][l]{The \LaTeX{} Metronome}\\[1ex]

%%%%%%%%%%%
% metronome
%%%%%%%%%%%
\begin{animateinline}[label=metronome,nomouse,loop,timeline=frames.txt]{1}
  \multiframe{4}{n=1+1}{
    \begin{tikzpicture}
      \path (0.3,-0.5) rectangle (4.7,0.5);
      \shade[inner color=green,outer color=green!10] (\n,0) circle(.6);
      \foreach \i in {1,...,4} \fill[cyan] (\i,0) circle(.3);
    \end{tikzpicture}
  }
\end{animateinline}

%%%%%%%%%%%
% controls
%%%%%%%%%%%
\makebox[4cm]{%
\controlButton{\faPowerOff}{%
  if(anim.metronome.isPlaying) anim.metronome.stopFirst(); 
  else anim.metronome.playFwd();
}\hfill
% tempo
\controlButton{\faMinusCircle}{%
  try {--anim.tempo.frameNum;} catch (e) {}
}\,%
\begin{animateinline}[label=tempo,step,nomouse,poster=10,timeline=tempo.txt]{1}
  \multiframe{39}{i=0+1}{
    \pgfmathparse{\tempos[\i]}
    \makebox[\widthof{000}][c]{\strut\pgfmathresult}
  }
\end{animateinline}\,%
\controlButton{\faPlusCircle}{%
  try {++anim.tempo.frameNum;} catch (e) {}
}}
\end{center}
\end{document}

独立动画动态图片是愚蠢的,据我所知,似乎没有一种简单的方法可以将嵌入的 Gif(在网页中)与声音同步。但是SVG更加优越,因为它是矢量的,因此可以自由扩展。

答案2

您也可以在其他地方制作带声音的动画,并将其添加到 Beamer 文件中。Beamer 似乎有一个多媒体包。movie15 似乎也很有前途。

看看这个关于多媒体/声音的帖子如何在 PDF 中添加音频文件,也许这是一个有效的起点。

相关内容