10
$\begingroup$

In Mathematica, there is a new function DynamicImage that can enlarge or drag picture dynamically.

So, is there a function DynamicAudio that acts like DynamicImage? In Adobe Audition, there is a line indicating which part is playing.

enter image description here

Audio file:

a = AudioChannelMix[#, "Mono"] &@ExampleData[{"Audio","ChurchBell"},"Audio"];

So I define a function can plot Audio dynamicly.

DynamicAudioPlot[a_?AudioQ, opts : OptionsPattern[]] := 
 Module[{mngr = Audio`Internals`GetAudioManager[Audio`AudioInformation[a, "AudioID"]], yrange, p = AudioPlot[a, opts]},
 yrange = Options[p, PlotRange][[1, 2, 2]]; 
 AppendTo[p[[2]], Epilog -> 
  Line[{{Dynamic@Audio`AudioDump`getGUIInfo[mngr, "AudioPosition"],yrange[[1]]}, 
        {Dynamic@Audio`AudioDump`getGUIInfo[mngr, "AudioPosition"],yrange[[2]]}}]];
 Audio`Internals`Execute[mngr, "Play"]; p]

DynamicAudioPlot[a, PlotTheme -> "Minimal", AspectRatio -> 1/5]

enter image description here

Or plot the DynamicSpectrogram of audio object

DynamicSpectrogram[a_Audio, opts : OptionsPattern[]] := Module[{mngr = 
    Audio`Internals`GetAudioManager[Audio`AudioInformation[a, "AudioID"]], yrange, 
    p = Spectrogram[a, opts]}, 
    yrange = AbsoluteOptions[p, PlotRange][[1, 2, 2]];
    AppendTo[p, Epilog -> Line[{{Dynamic@Audio`AudioDump`getGUIInfo[mngr, "AudioPosition"], yrange[[1]]}, {Dynamic@Audio`AudioDump`getGUIInfo[mngr, "AudioPosition"], yrange[[2]]}}]];
    Audio`Internals`Execute[mngr, "Play"]; p]
DynamicSpectrogram[a]

enter image description here

Or plot the attribute of audio dynamically.

DynamicAudioAttributePlot[a_?AudioQ, prop_String, opts : OptionsPattern[]] := Module[{
 mngr = Audio`Internals`GetAudioManager[Audio`AudioInformation[a, "AudioID"]], yrange, 
 p = ListLinePlot[AudioLocalMeasurements[a, prop], opts]},
 yrange = Options[p, PlotRange][[1, 2, 2]]; 
 AppendTo[p[[2]], 
 Epilog -> Line[{{Dynamic@Audio`AudioDump`getGUIInfo[mngr, "AudioPosition"], yrange[[1]]}, 
                 {Dynamic@Audio`AudioDump`getGUIInfo[mngr, "AudioPosition"], yrange[[2]]}}]];
 Audio`Internals`Execute[mngr, "Play"]; p]

DynamicAudioAttributePlot[a, "MFCC", PlotTheme -> "Detailed"]

enter image description here

But it has problem that the plot of dynamic Audio not work separately. So how to fix it? enter image description here

Ps: I find crucial reason of the problem is AudioAudioInformation`.

a = AudioChannelMix[#, "Mono"] &@ExampleData[{"Audio", "ChurchBell"}, "Audio"]

Audio`AudioInformation[a, "AudioID"] == Audio`AudioInformation[a, "AudioID"](*True*)
Audio`AudioInformation[...dialog box..., "AudioID"] ==Audio`AudioInformation[...dialog box..., "AudioID"](*Flase*)

enter image description here

You can see when directly drag the ...dialog box... to that place.Audio object become independent.

enter image description here

$\endgroup$
6
$\begingroup$

Here is my modest attempt:

dynamicAudio[aud_Audio, opts___] :=
       With[{len = QuantityMagnitude[UnitConvert[Duration[aud], "Seconds"]]},
            DynamicModule[{pts = {{0., 0.}, {len, 0.}}}, 
                          Panel[Column[{LocatorPane[Dynamic[pts,
                                (pts = Transpose[{Sort[#[[All, 1]], LessEqual],
                                                  {0., 0.}}];) &], 
                          AudioPlot[aud, Epilog -> {Opacity[1/2, LightRed], 
                                         Rectangle[Dynamic[Scaled[{0, -1}, pts[[1]]]], 
                                                   Dynamic[Scaled[{0, 1}, pts[[2]]]]]},
                                    opts], Appearance -> None], 
                          Item[Button["\[FilledRightTriangle]", 
                               EmitSound[Sound`AudioToSound[AudioTrim[aud,
                                         pts[[All, 1]]]]], 
                                      ImageSize -> Medium, Method -> "Queued"], 
                               Alignment -> Right]}]], SaveDefinitions -> True]]

An example:

ps = ExampleData[{"Sound", "PianoScale"}, "Audio"];
dynamicAudio[ps, Background -> Black, FrameTicks -> {{None, All}, {None, All}},
             PlotStyle -> Green]

dynamicAudio example

$\endgroup$
  • $\begingroup$ Excause me,I edit my question in order to make it more realistic,so I cancel my acceptance. $\endgroup$ – partida Dec 4 '16 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.