TEMPO:在動畫 GIF/PDF/SVG 中播放聲音

TEMPO:在動畫 GIF/PDF/SVG 中播放聲音

練琴的時候,速度(iOS 上的應用程序,CHplay)是一個節拍器,通常用於按時節拍。

我可以為 TEMPO 製作動畫 GIF/PDF/SVG/frame(在 中beamer),但不知道如何播放聲音(例如 beep 8,mp3wav這裡)在每個節拍。

任何人都可以幫忙(提供完整的程式碼和清晰的解釋)嗎?謝謝你!

在此輸入影像描述

以下程式碼是我的 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軟體包,該軟體包已在其手冊中提供了滴答作響的節拍器範例。

獨立式靜止無功發生器版本可以在大多數現代網頁瀏覽器(Firefox、Chrome、Opera ...)中運行,甚至可以在行動裝置上運行。此處,聲音使用 HTML5<audio>標籤嵌入為 Base64 編碼的 Blob。 (在命令列上click.mp3以「」編碼base64 click.mp3。)物理嵌入的聲音檔案使 SVG 獨立。

無論哪種情況,JavaScript 都會在執行動畫的每個畫面變更時開始播放聲音。的“時間軸”特性animate用於將JS代碼與每一幀(時間軸輸入frames.txt)關聯起來。

速度標記是在第二animateinline環境中實現的。它提供 39 種典型的節拍器速度,可以使用 +/- 按鈕進行步進。這些按鈕利用animateJavaScript 介面來增加並減少速度標記影格編號。另一個時間軸檔案 ,tempo.txt將每個標記幀與一個 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}

靜止無功發生器版本,編譯用

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/ApWjVEvj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XLACAAAAMS3hWjt8TEJN45REvHKFnEP/JByCg3m/xHQ0ikRYXL/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(在網頁中)與聲音。但靜止無功發生器優越得多,因為它是向量的,因此可以自由擴展。

答案2

您也可以在其他地方用聲音製作動畫並將其添加到 Beamer 檔案中。 Beamer 似乎有一個多媒體包。 movie15 似乎也很有希望。

看看這個關於多媒體/聲音的帖子如何在 PDF 中包含音訊文件,也許這是一個有效的起點。

相關內容