Creating macro/embedding audio

41 views
Skip to first unread message

Tierney Coren

unread,
Nov 6, 2015, 2:51:17 PM11/6/15
to TiddlyWiki
Hello.

I would like to create a macro that allows me to embed an audio file into a tiddler. The example below is the best approximation I can give to show what I want to do.

\define podcast(start: '00:00:00' end:'00:05:00' audio:'podcast.com/someepisode.mp3')
<audio src="http://"$audio$"#t="$start$","$end" controls="controls"></audio>
\end

You can probably tell that I'm used to working with strings from how I did the concatenation of the audio, start, and end variables. I know how I have them here is wrong, but I have no idea how to do them correctly.

It would be super awesome if I could actually just pull the audio, start, and stop variables from fields in a tiddler - that would greatly increase the semantics of my use of fields on tiddlers.

Thanks,
Tierney C.

Tobias Beer

unread,
Nov 7, 2015, 3:25:10 AM11/7/15
to tiddl...@googlegroups.com
Hi Tierney,

Yes, you did make a few errors in this...  ;-)
\define podcast(start: '00:00:00' end:'00:05:00' audio:'podcast.com/someepisode.mp3')
<audio src="http://"$audio$"#t="$start$","$end" controls="controls"></audio>
\end
  1. you forgot to separate your parameters via comma
  2. the closing $ for end is missing in the src attribute
  3. you broke the src attribute by trying to use quotes within quotes, which you don't want or need
I'd also put the audio file first as it is the main element being specified by the macro and possibly the only parameter to be never left out. In other words, you may not need or want to specify any time frame at all. I don't see a need for a default for any of those parameters. Keep it simple!

So that already yields a much simpler:

\define audio(file, start, end)
<audio src="$file$#t=$start$,$end$" controls="controls"/>
\end


To make start or end optional, you could do...

\define audio(file, start, end)
<$reveal type="match" text="$end$" default="">
<audio src="$file$#t=$start$" controls="controls"/>
</$reveal>
<$reveal type="nomatch" text="$end$" default="">
<audio src="$file$#t=$start$,$end$" controls="controls"/>
</$reveal>
\end


As for urls, I would not assume anything.
After all, you might just wish to point to a file on a relative path.
To simplify handling of urls, you can however make use of some form of a base url, e.g.:

\define audio-embed(base, file, start, end)
<$reveal type="match" text="$end$" default="">
<audio src="$base$$file$#t=$start$" controls="controls"/>
</$reveal>
<$reveal type="nomatch" text="$end$" default="">
<audio src="$base$$file$#t=$start$,$end$" controls="controls"/>
</$reveal>
\end

\define 
audio(file, start, end)
<$macrocall $name="
audio-embed" base=<<audio-base>> file="$file$" start="$start$" end="$end$"/>
\end

example:
<$vars audio-base="http://www.hf.uio.no/csmn/english/services/knowledge/podcast/jerry-fodor/">
<<
audio "Fodor-What-Frege-got-wrong.mp3" 10 20>>
</$vars>

You can declare this "audio-base" variable in a global macro to define it once throughout the wiki, instead of per tiddler. You could also declare for the scope of the current tiddler as a macro definition, rather than using the SetWidget or the VarsWidget e.g.:


You can probably tell that I'm used to working with strings from how I did the concatenation of the audio, start, and end variables. I know how I have them here is wrong, but I have no idea how to do them correctly.

Don't be afraid to experiment. But you got to experiment! Meaning: test your code! So start simply, test, modify, test, modify, test, etc... testing is the most crucial bit. Without it, you never know and won't get there. Don't be afraid to do it wrong but you've got to check. Trial and error, we all do.

It would be super awesome if I could actually just pull the audio, start, and stop variables from fields in a tiddler - that would greatly increase the semantics of my use of fields on tiddlers.
 
That works out of the box using the MacroCallWidget and then TextReferences or Variables:

<$macrocall $name="audiofile={{!!podcast}}/>

Best wishes,

— tb
Reply all
Reply to author
Forward
0 new messages