I was looking at a large collection of Import/Export user suggestions the other day and it dawned on me that we finally have MIDI import in Mathematica 9, which is rather interesting since MIDI supports karaoke text events, which can be run in Mathematica with scheduled tasks.
So I wrote a simple code for a Karaoke machine function, which plays a given MIDI karaoke file and highlights the text that needs to be sung at the same time.
$DisplayEvents = {
{0.0, {Red, FontSize -> 18}},
{0.5, {Orange, FontSize -> 18}},
{1.0, {Gray, FontSize -> 18}}};
TextEvents[file_] := Module[{events},
events = Cases[Import[file, "Metadata"], {time_, "TextEvent" -> text_}, 2];
events = Select[events, Not[StringMatchQ[#[[2, 2]], "\\@*"]] &];
Map[{#[[1]], StringReplace[#[[2, 2]], {"\\" -> "\n\n", "/" -> "\n"}]} &,
events]];
ScheduleTextEvents[events_] := Module[{},
Outer[With[{e = #1, d = #2},
RunScheduledTask[
Unevaluated[displayEvent[e] = Last[d]], {Max[0.0, First[e] + First[d]]}]] &,
events, $DisplayEvents, 1]]
Cleanup[] := { Cell[MakeBoxes@Row[{
Button["Stop Music", Sound`StopMIDI[]; RemoveScheduledTask /@ ScheduledTasks[]],
Button["Close Notebook", NotebookClose[EvaluationNotebook[]]]}]]}
KaraokeMachine[file_] := Module[{lyrics, song, events},
song = Import[file];
events = TextEvents[file];
Map[(displayEvent[#] = {White, FontSize -> 18}) &, events];
CreateDocument[ ExpressionCell[
With[{t = events},
Dynamic[Row[With[{x = #}, Style[x[[2]], displayEvent[x]]] & /@ t]]],
"Output", ShowCellBracket -> False], Background -> Black,
WindowElements -> {"VerticalScrollBar"},
Deployed -> True, DockedCells->Cleanup[]];
EmitSound[song];
ScheduleTextEvents[events];
]
Which can then be called by giving it a karaoke file (for example this song by Frank Sinatra):
KaraokeMachine[ "myway.kar"]
My question mostly revolves about ideas on how I can improve on this (code improvements as well as suggestions on how to implement features, like fading in/out text).
(Note: Mathematica uses the Jlink/Java MIDI implementation on MacOSX, which means that you may run into this issue on newer versions of MacOSX. I haven't been able to find a solution for this yet).
NotebookClose
on the document you create seems to be an essential step. You could also add a close button. My main problem is the player window blocking all access to the other mma windows. $\endgroup$InstallJava[ CommandLine -> "/Library/Internet\\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"]
in Mathematica to force it to use 1.7, and the problems you mentioned went away. BTW do you know how to check the Java version from within Mma?SystemInformation
says "info unavailable". $\endgroup$InstallJava[CommandLine->"/usr/bin/java"]
which seems to fix the Mac sound issues. $\endgroup$Import[file, "MIDI"]
andImport[file, {"MIDI", "Metadata"}]
. If I have time in the week and no superb answers appear it sounds fun to give it a shot $\endgroup$