# How can I emit a sound only during a mouseover?

Emitting a constant sound or note (no interruptions) for a fixed duration is easy, but a dynamic duration, not so easy.

-
Is this related? I mean the core method given by @SimonWoods. The locator can be easily transformed to MouseOver. mathematica.stackexchange.com/questions/5304/… – Vitaliy Kaurov Aug 20 '12 at 20:17
Yes, thanks @vitalykaurov. I think it is related but it still does not generate a perfectly constant sound (free of quick pauses) for a dynamic amount of time. – M.R. Aug 20 '12 at 20:31

Is this satisfactory?

EventHandler[
Framed@"Play flute",
{"MouseEntered" :> EmitSound[Sound@SoundNote["C", 10*^10, "Flute"]],
"MouseExited" :> EmitSound[Sound@SoundNote[SoundVolume -> 0]] }]

-
we answered a couple of seconds apart... although your continuous notion is much larger than mine... – P. Fonseca Aug 20 '12 at 18:24
@P.Fonseca, hehe, that's right – Rojo Aug 20 '12 at 18:38
I don't get any sound from this on v7; before I try to debug, any idea why? – Mr.Wizard Aug 23 '12 at 11:26
@Mr.Wizard, no. If EmitSound run separately works, I would test those events with a Print – Rojo Aug 23 '12 at 12:33
"MouseEntered" and "MouseExited" are apparently not recognized in version 7. – Mr.Wizard Aug 23 '12 at 12:39

Do you mean something like this:

Mouseover[off,Dynamic[Refresh[EmitSound[Play[Sin[700 t + 25 t Sin[350 t]], {t, 0, 1}]],
UpdateInterval -> 1]; on]]


UPDATE - re-edited

For a "continuous" sound

Mouseover[
Dynamic[EmitSound[Sound[SoundNote["C", 1, "Violin"], SoundVolume -> 0]]; off,
UpdateInterval -> 1],
Dynamic[EmitSound[Sound[SoundNote["C", 500, "Violin"]]]; on,
UpdateInterval -> 1]]


UPDATE 2

Based on @Rojo EventHandler code, here's an idea for organizing different sound effects on the same notebook:

with the help of NotebookDynamicExpression:

SetOptions[EvaluationNotebook[], NotebookDynamicExpression :> Dynamic[
Which[
inst == "quiet",
EmitSound[Sound[SoundNote[], SoundVolume -> 0]],
True,
EmitSound[Sound[SoundNote[note, 500, inst]]]
],
TrackedSymbols :> {inst}]
]


and here is a small interface:

notes = {"C", "D", "E", "F", "G", "A", "B"}; instruments = {"Violin",
"Flute", "Piano", "Guitar"};
Grid[Map[EventHandler[
Framed[#[[2]]], {"MouseEntered" :> {inst = #[[1]];
note = #[[2]];}, "MouseExited" :> {inst = "quiet"}}] &,
Table[{i, n}, {i, instruments}, {n, notes}], {2}],
ItemSize -> {2, 2}]


(sorry for the formatting, I still have troubles with this...)

-
This is nice but is not constant, the breaks inbetween are what I'm trying to avoid. – M.R. Aug 20 '12 at 18:07
@Mike I'm still looking for a solution for that... – P. Fonseca Aug 20 '12 at 18:08
@rojo Nice, but I still hear momentary pauses. And did you know UpdateInterval -> 1 is an option to Dynamic, you don't need the Refresh call – M.R. Aug 20 '12 at 20:20
@Mike, that thing you wrote was for me or for Fonseca? I don't here any pauses in mine. Beware if you have Fonseca's output showing too when you try mine, because it updates every second to "shut the sound up", so it would shut mine – Rojo Aug 21 '12 at 3:39
@Rojo Quite right! Your solution has no pauses, so I will accept it, thanks. – M.R. Aug 21 '12 at 5:25